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; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.TYPE_MOBILE_MMS; |
| 27 | import static android.net.ConnectivityManager.TYPE_MOBILE_SUPL; |
| 28 | import static android.net.ConnectivityManager.TYPE_MOBILE_DUN; |
| 29 | import static android.net.ConnectivityManager.TYPE_MOBILE_FOTA; |
| 30 | import static android.net.ConnectivityManager.TYPE_MOBILE_IMS; |
| 31 | import static android.net.ConnectivityManager.TYPE_MOBILE_CBS; |
| 32 | import static android.net.ConnectivityManager.TYPE_MOBILE_IA; |
| 33 | import static android.net.ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 34 | import static android.net.ConnectivityManager.TYPE_NONE; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 35 | import static android.net.ConnectivityManager.TYPE_WIFI; |
| 36 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 37 | import static android.net.ConnectivityManager.TYPE_PROXY; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 38 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 39 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
| 40 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 42 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 43 | import android.app.AlarmManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 44 | import android.app.AppOpsManager; |
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; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 55 | import android.content.pm.ApplicationInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.content.pm.PackageManager; |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 57 | import android.content.pm.PackageManager.NameNotFoundException; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 58 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 59 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 60 | import android.database.ContentObserver; |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 61 | import android.net.CaptivePortalTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.net.ConnectivityManager; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 63 | import android.net.DummyDataStateTracker; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 65 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 66 | import android.net.INetworkPolicyListener; |
| 67 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 68 | import android.net.INetworkStatsService; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 69 | import android.net.LinkAddress; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 70 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 71 | import android.net.LinkProperties.CompareResult; |
Jeff Sharkey | e4984be | 2013-09-10 21:03:27 -0700 | [diff] [blame] | 72 | import android.net.LinkQualityInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 73 | import android.net.MobileDataStateTracker; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 74 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 75 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 76 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 77 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 79 | import android.net.NetworkInfo.DetailedState; |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 80 | import android.net.NetworkFactory; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 81 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 82 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 83 | import android.net.NetworkState; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.NetworkStateTracker; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 85 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 86 | import android.net.Proxy; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 87 | import android.net.ProxyDataTracker; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 88 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 89 | import android.net.RouteInfo; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 90 | import android.net.SamplingDataTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 91 | import android.net.Uri; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 92 | import android.net.wimax.WimaxManagerConstants; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 93 | import android.os.AsyncTask; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | import android.os.Binder; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 95 | import android.os.Build; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 96 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 98 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 99 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 100 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.os.Looper; |
| 102 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 103 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 104 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 105 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 106 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 107 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | import android.os.ServiceManager; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 109 | import android.os.SystemClock; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 111 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 113 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 114 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 115 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 116 | import android.text.TextUtils; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 117 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 118 | import android.util.SparseArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 119 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 120 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 122 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 123 | import com.android.internal.annotations.GuardedBy; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 124 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 125 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 126 | import com.android.internal.net.VpnProfile; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 127 | import com.android.internal.telephony.DctConstants; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 128 | import com.android.internal.telephony.Phone; |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 129 | import com.android.internal.telephony.PhoneConstants; |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 130 | import com.android.internal.telephony.TelephonyIntents; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 131 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 132 | import com.android.internal.util.IndentingPrintWriter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 133 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 134 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 135 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 136 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 137 | import com.android.server.connectivity.NetworkAgentInfo; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 138 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 139 | import com.android.server.connectivity.PacManager; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 140 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 141 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 142 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 143 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 144 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 145 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 146 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 147 | import dalvik.system.DexClassLoader; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 148 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 149 | import org.xmlpull.v1.XmlPullParser; |
| 150 | import org.xmlpull.v1.XmlPullParserException; |
| 151 | |
| 152 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 154 | import java.io.FileNotFoundException; |
| 155 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 156 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | import java.io.PrintWriter; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 158 | import java.lang.reflect.Constructor; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 159 | import java.net.HttpURLConnection; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 160 | import java.net.Inet4Address; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 161 | import java.net.Inet6Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 162 | import java.net.InetAddress; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 163 | import java.net.URL; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 164 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 165 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 166 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 167 | import java.util.Collection; |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 168 | import java.util.GregorianCalendar; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 169 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 170 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 171 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 172 | import java.util.Map; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 173 | import java.util.Random; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 174 | import java.util.concurrent.atomic.AtomicBoolean; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 175 | import java.util.concurrent.atomic.AtomicInteger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 177 | import javax.net.ssl.HostnameVerifier; |
| 178 | import javax.net.ssl.HttpsURLConnection; |
| 179 | import javax.net.ssl.SSLSession; |
| 180 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 181 | import static android.net.ConnectivityManager.INVALID_NET_ID; |
| 182 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | /** |
| 184 | * @hide |
| 185 | */ |
| 186 | public class ConnectivityService extends IConnectivityManager.Stub { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 187 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | |
Robert Greenwalt | ba175a5 | 2010-10-05 19:12:26 -0700 | [diff] [blame] | 189 | private static final boolean DBG = true; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 190 | private static final boolean VDBG = true; // STOPSHIP |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 192 | // network sampling debugging |
| 193 | private static final boolean SAMPLE_DBG = false; |
| 194 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 195 | private static final boolean LOGD_RULES = false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 196 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 197 | // TODO: create better separation between radio types and network types |
| 198 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 199 | // how long to wait before switching back to a radio's default network |
| 200 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 201 | // system property that can override the above value |
| 202 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 203 | "android.telephony.apn-restore"; |
| 204 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 205 | // Default value if FAIL_FAST_TIME_MS is not set |
| 206 | private static final int DEFAULT_FAIL_FAST_TIME_MS = 1 * 60 * 1000; |
| 207 | // system property that can override DEFAULT_FAIL_FAST_TIME_MS |
| 208 | private static final String FAIL_FAST_TIME_MS = |
| 209 | "persist.radio.fail_fast_time_ms"; |
| 210 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 211 | private static final String ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED = |
| 212 | "android.net.ConnectivityService.action.PKT_CNT_SAMPLE_INTERVAL_ELAPSED"; |
| 213 | |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 214 | private static final int SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE = 0; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 215 | |
| 216 | private PendingIntent mSampleIntervalElapsedIntent; |
| 217 | |
| 218 | // Set network sampling interval at 12 minutes, this way, even if the timers get |
| 219 | // aggregated, it will fire at around 15 minutes, which should allow us to |
| 220 | // aggregate this timer with other timers (specially the socket keep alive timers) |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 221 | 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] | 222 | |
| 223 | // start network sampling a minute after booting ... |
Robert Greenwalt | 3110605 | 2014-05-16 17:19:24 -0700 | [diff] [blame] | 224 | 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] | 225 | |
| 226 | AlarmManager mAlarmManager; |
| 227 | |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 228 | // used in recursive route setting to add gateways for the host for which |
| 229 | // a host route was requested. |
| 230 | private static final int MAX_HOSTROUTE_CYCLE_COUNT = 10; |
| 231 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 232 | private Tethering mTethering; |
| 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>(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 238 | private VpnCallback mVpnCallback = new VpnCallback(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 239 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 240 | private boolean mLockdownEnabled; |
| 241 | private LockdownVpnTracker mLockdownTracker; |
| 242 | |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 243 | private Nat464Xlat mClat; |
| 244 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 245 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 246 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 247 | /** Currently active network rules by UID. */ |
| 248 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 249 | /** Set of ifaces that are costly. */ |
| 250 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 251 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | /** |
| 253 | * Sometimes we want to refer to the individual network state |
| 254 | * trackers separately, and sometimes we just want to treat them |
| 255 | * abstractly. |
| 256 | */ |
| 257 | private NetworkStateTracker mNetTrackers[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 258 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 259 | /* Handles captive portal check on a network */ |
| 260 | private CaptivePortalTracker mCaptivePortalTracker; |
| 261 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 262 | /** |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 263 | * The link properties that define the current links |
| 264 | */ |
| 265 | private LinkProperties mCurrentLinkProperties[]; |
| 266 | |
| 267 | /** |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 268 | * A per Net list of the PID's that requested access to the net |
| 269 | * used both as a refcount and for per-PID DNS selection |
| 270 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 271 | private List<Integer> mNetRequestersPids[]; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 272 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 273 | // priority order of the nettrackers |
| 274 | // (excluding dynamically set mNetworkPreference) |
| 275 | // TODO - move mNetworkTypePreference into this |
| 276 | private int[] mPriorityList; |
| 277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | private Context mContext; |
| 279 | private int mNetworkPreference; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 280 | private int mActiveDefaultNetwork = -1; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 281 | // 0 is full bad, 100 is full good |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 282 | private int mDefaultInetCondition = 0; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 283 | private int mDefaultInetConditionPublished = 0; |
| 284 | private boolean mInetConditionChangeInFlight = false; |
| 285 | private int mDefaultConnectionSequence = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 286 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 287 | private Object mDnsLock = new Object(); |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 288 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | |
| 290 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 291 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 293 | private INetworkManagementService mNetd; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 294 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 295 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 296 | private static final int ENABLED = 1; |
| 297 | private static final int DISABLED = 0; |
| 298 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 299 | private static final boolean ADD = true; |
| 300 | private static final boolean REMOVE = false; |
| 301 | |
| 302 | private static final boolean TO_DEFAULT_TABLE = true; |
| 303 | private static final boolean TO_SECONDARY_TABLE = false; |
| 304 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 305 | private static final boolean EXEMPT = true; |
| 306 | private static final boolean UNEXEMPT = false; |
| 307 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 308 | /** |
| 309 | * used internally as a delayed event to make us switch back to the |
| 310 | * default network |
| 311 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 312 | private static final int EVENT_RESTORE_DEFAULT_NETWORK = 1; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 313 | |
| 314 | /** |
| 315 | * used internally to change our mobile data enabled flag |
| 316 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 317 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 318 | |
| 319 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 320 | * used internally to synchronize inet condition reports |
| 321 | * arg1 = networkType |
| 322 | * arg2 = condition (0 bad, 100 good) |
| 323 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 324 | private static final int EVENT_INET_CONDITION_CHANGE = 4; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 325 | |
| 326 | /** |
| 327 | * used internally to mark the end of inet condition hold periods |
| 328 | * arg1 = networkType |
| 329 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 330 | private static final int EVENT_INET_CONDITION_HOLD_END = 5; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 331 | |
| 332 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 333 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 334 | * from one net to another. Clear happens when we get a new |
| 335 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 336 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 337 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 338 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 339 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 340 | /** |
| 341 | * used internally to reload global proxy settings |
| 342 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 343 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 344 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 345 | /** |
| 346 | * used internally to set external dependency met/unmet |
| 347 | * arg1 = ENABLED (met) or DISABLED (unmet) |
| 348 | * arg2 = NetworkType |
| 349 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 350 | private static final int EVENT_SET_DEPENDENCY_MET = 10; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 351 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 352 | /** |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 353 | * used internally to send a sticky broadcast delayed. |
| 354 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 355 | private static final int EVENT_SEND_STICKY_BROADCAST_INTENT = 11; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 356 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Used internally to |
| 359 | * {@link NetworkStateTracker#setPolicyDataEnable(boolean)}. |
| 360 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 361 | private static final int EVENT_SET_POLICY_DATA_ENABLE = 12; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 362 | |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 363 | private static final int EVENT_VPN_STATE_CHANGED = 13; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 364 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 365 | /** |
| 366 | * Used internally to disable fail fast of mobile data |
| 367 | */ |
Chad Brubaker | b98703e | 2013-07-23 17:44:41 -0700 | [diff] [blame] | 368 | private static final int EVENT_ENABLE_FAIL_FAST_MOBILE_DATA = 14; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 369 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 370 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 371 | * used internally to indicate that data sampling interval is up |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 372 | */ |
| 373 | private static final int EVENT_SAMPLE_INTERVAL_ELAPSED = 15; |
| 374 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 375 | /** |
| 376 | * PAC manager has received new port. |
| 377 | */ |
| 378 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 379 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 380 | /** |
| 381 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 382 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 383 | */ |
| 384 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 385 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 386 | /** |
| 387 | * used internally when registering NetworkAgents |
| 388 | * obj = Messenger |
| 389 | */ |
| 390 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 391 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 392 | /** |
| 393 | * used to add a network request |
| 394 | * includes a NetworkRequestInfo |
| 395 | */ |
| 396 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 397 | |
| 398 | /** |
| 399 | * indicates a timeout period is over - check if we had a network yet or not |
| 400 | * and if not, call the timeout calback (but leave the request live until they |
| 401 | * cancel it. |
| 402 | * includes a NetworkRequestInfo |
| 403 | */ |
| 404 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 405 | |
| 406 | /** |
| 407 | * used to add a network listener - no request |
| 408 | * includes a NetworkRequestInfo |
| 409 | */ |
| 410 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 411 | |
| 412 | /** |
| 413 | * 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] | 414 | * arg1 = UID of caller |
| 415 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 416 | */ |
| 417 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 418 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 419 | /** |
| 420 | * used internally when registering NetworkFactories |
| 421 | * obj = Messenger |
| 422 | */ |
| 423 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 424 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 425 | /** |
| 426 | * used internally to expire a wakelock when transitioning |
| 427 | * from one net to another. Expire happens when we fail to find |
| 428 | * a new network (typically after 1 minute) - |
| 429 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 430 | * a replacement network. |
| 431 | */ |
| 432 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 433 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 434 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 435 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 436 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 437 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 438 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 439 | |
| 440 | // list of DeathRecipients used to make sure features are turned off when |
| 441 | // a process dies |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 442 | private List<FeatureUser> mFeatureUsers; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 443 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 444 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 445 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 446 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 447 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 448 | private String mNetTransitionWakeLockCausedBy = ""; |
| 449 | private int mNetTransitionWakeLockSerialNumber; |
| 450 | private int mNetTransitionWakeLockTimeout; |
| 451 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 452 | private InetAddress mDefaultDns; |
| 453 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 454 | // Lock for protecting access to mAddedRoutes and mExemptAddresses |
| 455 | private final Object mRoutesLock = new Object(); |
| 456 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 457 | // this collection is used to refcount the added routes - if there are none left |
| 458 | // it's time to remove the route from the route table |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 459 | @GuardedBy("mRoutesLock") |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 460 | private Collection<RouteInfo> mAddedRoutes = new ArrayList<RouteInfo>(); |
| 461 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 462 | // this collection corresponds to the entries of mAddedRoutes that have routing exemptions |
| 463 | // used to handle cleanup of exempt rules |
| 464 | @GuardedBy("mRoutesLock") |
| 465 | private Collection<LinkAddress> mExemptAddresses = new ArrayList<LinkAddress>(); |
| 466 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 467 | // used in DBG mode to track inet condition reports |
| 468 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 469 | private ArrayList mInetLog; |
| 470 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 471 | // track the current default http proxy - tell the world if we get a new one (real change) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 472 | private ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 473 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 474 | private boolean mDefaultProxyDisabled = false; |
| 475 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 476 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 477 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 478 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 479 | private PacManager mPacManager = null; |
| 480 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 481 | private SettingsObserver mSettingsObserver; |
| 482 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 483 | private AppOpsManager mAppOpsManager; |
| 484 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 485 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 486 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 487 | |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 488 | private static class RadioAttributes { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 489 | public int mSimultaneity; |
| 490 | public int mType; |
| 491 | public RadioAttributes(String init) { |
| 492 | String fragments[] = init.split(","); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 493 | mType = Integer.parseInt(fragments[0]); |
| 494 | mSimultaneity = Integer.parseInt(fragments[1]); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | RadioAttributes[] mRadioAttributes; |
| 498 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 499 | // the set of network types that can only be enabled by system/sig apps |
| 500 | List mProtectedNetworks; |
| 501 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 502 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 503 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 504 | private AtomicInteger mEnableFailFastMobileDataTag = new AtomicInteger(0); |
| 505 | |
| 506 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 507 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 508 | // sequence number for Networks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 509 | private final static int MIN_NET_ID = 10; // some reserved marks |
| 510 | private final static int MAX_NET_ID = 65535; |
| 511 | private int mNextNetId = MIN_NET_ID; |
| 512 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 513 | // sequence number of NetworkRequests |
| 514 | private int mNextNetworkRequestId = 1; |
| 515 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 516 | private static final int UID_UNUSED = -1; |
| 517 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 518 | /** |
| 519 | * Implements support for the legacy "one network per network type" model. |
| 520 | * |
| 521 | * We used to have a static array of NetworkStateTrackers, one for each |
| 522 | * network type, but that doesn't work any more now that we can have, |
| 523 | * for example, more that one wifi network. This class stores all the |
| 524 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 525 | * API will only see the first one. |
| 526 | * |
| 527 | * It serves two main purposes: |
| 528 | * |
| 529 | * 1. Provide information about "the network for a given type" (since this |
| 530 | * API only supports one). |
| 531 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 532 | * the first network for a given type changes, or if the default network |
| 533 | * changes. |
| 534 | */ |
| 535 | private class LegacyTypeTracker { |
| 536 | /** |
| 537 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 538 | * Each list holds references to all NetworkAgentInfos that are used to |
| 539 | * satisfy requests for that network type. |
| 540 | * |
| 541 | * This array is built out at startup such that an unsupported network |
| 542 | * doesn't get an ArrayList instance, making this a tristate: |
| 543 | * unsupported, supported but not active and active. |
| 544 | * |
| 545 | * The actual lists are populated when we scan the network types that |
| 546 | * are supported on this device. |
| 547 | */ |
| 548 | private ArrayList<NetworkAgentInfo> mTypeLists[]; |
| 549 | |
| 550 | public LegacyTypeTracker() { |
| 551 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 552 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 553 | } |
| 554 | |
| 555 | public void addSupportedType(int type) { |
| 556 | if (mTypeLists[type] != null) { |
| 557 | throw new IllegalStateException( |
| 558 | "legacy list for type " + type + "already initialized"); |
| 559 | } |
| 560 | mTypeLists[type] = new ArrayList<NetworkAgentInfo>(); |
| 561 | } |
| 562 | |
| 563 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
| 564 | return mNetworkForRequestId.get(mDefaultRequest.requestId) == nai; |
| 565 | } |
| 566 | |
| 567 | public boolean isTypeSupported(int type) { |
| 568 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 569 | } |
| 570 | |
| 571 | public NetworkAgentInfo getNetworkForType(int type) { |
| 572 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 573 | return mTypeLists[type].get(0); |
| 574 | } else { |
| 575 | return null; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | public void add(int type, NetworkAgentInfo nai) { |
| 580 | if (!isTypeSupported(type)) { |
| 581 | return; // Invalid network type. |
| 582 | } |
| 583 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 584 | |
| 585 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 586 | if (list.contains(nai)) { |
| 587 | loge("Attempting to register duplicate agent for type " + type + ": " + nai); |
| 588 | return; |
| 589 | } |
| 590 | |
| 591 | if (list.isEmpty() || isDefaultNetwork(nai)) { |
| 592 | if (VDBG) log("Sending connected broadcast for type " + type + |
| 593 | "isDefaultNetwork=" + isDefaultNetwork(nai)); |
| 594 | sendLegacyNetworkBroadcast(nai, true, type); |
| 595 | } |
| 596 | list.add(nai); |
| 597 | } |
| 598 | |
| 599 | public void remove(NetworkAgentInfo nai) { |
| 600 | if (VDBG) log("Removing agent " + nai); |
| 601 | for (int type = 0; type < mTypeLists.length; type++) { |
| 602 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 603 | if (list == null || list.isEmpty()) { |
| 604 | continue; |
| 605 | } |
| 606 | |
| 607 | boolean wasFirstNetwork = false; |
| 608 | if (list.get(0).equals(nai)) { |
| 609 | // This network was the first in the list. Send broadcast. |
| 610 | wasFirstNetwork = true; |
| 611 | } |
| 612 | list.remove(nai); |
| 613 | |
| 614 | if (wasFirstNetwork || isDefaultNetwork(nai)) { |
| 615 | if (VDBG) log("Sending disconnected broadcast for type " + type + |
| 616 | "isDefaultNetwork=" + isDefaultNetwork(nai)); |
| 617 | sendLegacyNetworkBroadcast(nai, false, type); |
| 618 | } |
| 619 | |
| 620 | if (!list.isEmpty() && wasFirstNetwork) { |
| 621 | if (VDBG) log("Other network available for type " + type + |
| 622 | ", sending connected broadcast"); |
| 623 | sendLegacyNetworkBroadcast(list.get(0), false, type); |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 629 | |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 630 | public ConnectivityService(Context context, INetworkManagementService netd, |
| 631 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 632 | // Currently, omitting a NetworkFactory will create one internally |
| 633 | // TODO: create here when we have cleaner WiMAX support |
| 634 | this(context, netd, statsService, policyManager, null); |
| 635 | } |
| 636 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 637 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 638 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 639 | NetworkFactory netFactory) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 640 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 641 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 642 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 643 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 644 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 645 | mDefaultRequest = new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 646 | NetworkRequestInfo nri = new NetworkRequestInfo(null, mDefaultRequest, new Binder(), |
| 647 | NetworkRequestInfo.REQUEST); |
| 648 | mNetworkRequests.put(mDefaultRequest, nri); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 649 | |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 650 | HandlerThread handlerThread = new HandlerThread("ConnectivityServiceThread"); |
| 651 | handlerThread.start(); |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 652 | mHandler = new InternalHandler(handlerThread.getLooper()); |
| 653 | mTrackerHandler = new NetworkStateTrackerHandler(handlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 654 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 655 | if (netFactory == null) { |
| 656 | netFactory = new DefaultNetworkFactory(context, mTrackerHandler); |
| 657 | } |
| 658 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 659 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 660 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 661 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 662 | Settings.Secure.ANDROID_ID); |
| 663 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 664 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 665 | SystemProperties.set("net.hostname", name); |
| 666 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 669 | // read our default dns server ip |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 670 | String dns = Settings.Global.getString(context.getContentResolver(), |
| 671 | Settings.Global.DEFAULT_DNS_SERVER); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 672 | if (dns == null || dns.length() == 0) { |
| 673 | dns = context.getResources().getString( |
| 674 | com.android.internal.R.string.config_default_dns_server); |
| 675 | } |
| 676 | try { |
Robert Greenwalt | e590373 | 2011-02-22 16:00:42 -0800 | [diff] [blame] | 677 | mDefaultDns = NetworkUtils.numericToInetAddress(dns); |
| 678 | } catch (IllegalArgumentException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 679 | loge("Error setting defaultDns using " + dns); |
Robert Greenwalt | e90aa5e | 2010-09-01 11:34:05 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 682 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 683 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 684 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 685 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 686 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 687 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 688 | try { |
| 689 | mPolicyManager.registerListener(mPolicyListener); |
| 690 | } catch (RemoteException e) { |
| 691 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 692 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 696 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 697 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 698 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 699 | com.android.internal.R.integer.config_networkTransitionTimeout); |
| 700 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 701 | mNetTrackers = new NetworkStateTracker[ |
| 702 | ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 703 | mCurrentLinkProperties = new LinkProperties[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 704 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 705 | mRadioAttributes = new RadioAttributes[ConnectivityManager.MAX_RADIO_TYPE+1]; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 706 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 707 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 708 | // Load device network attributes from resources |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 709 | String[] raStrings = context.getResources().getStringArray( |
| 710 | com.android.internal.R.array.radioAttributes); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 711 | for (String raString : raStrings) { |
| 712 | RadioAttributes r = new RadioAttributes(raString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 713 | if (VDBG) log("raString=" + raString + " r=" + r); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 714 | if (r.mType > ConnectivityManager.MAX_RADIO_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 715 | loge("Error in radioAttributes - ignoring attempt to define type " + r.mType); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 716 | continue; |
| 717 | } |
| 718 | if (mRadioAttributes[r.mType] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 719 | loge("Error in radioAttributes - ignoring attempt to redefine type " + |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 720 | r.mType); |
| 721 | continue; |
| 722 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 723 | mRadioAttributes[r.mType] = r; |
| 724 | } |
| 725 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 726 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 727 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 728 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 729 | String[] naStrings = context.getResources().getStringArray( |
| 730 | com.android.internal.R.array.networkAttributes); |
| 731 | for (String naString : naStrings) { |
| 732 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 733 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 734 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 735 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 736 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 737 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 738 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 739 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 740 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 741 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 742 | n.type); |
| 743 | continue; |
| 744 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 745 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 746 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 747 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 748 | continue; |
| 749 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 750 | if (mRadioAttributes[n.radio] == null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 751 | loge("Error in networkAttributes - ignoring attempt to use undefined " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 752 | "radio " + n.radio + " in network type " + n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 753 | continue; |
| 754 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 755 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 756 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 757 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 758 | mNetworksDefined++; |
| 759 | } catch(Exception e) { |
| 760 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 761 | } |
| 762 | } |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 763 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 764 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 765 | mProtectedNetworks = new ArrayList<Integer>(); |
| 766 | int[] protectedNetworks = context.getResources().getIntArray( |
| 767 | com.android.internal.R.array.config_protectedNetworks); |
| 768 | for (int p : protectedNetworks) { |
| 769 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 770 | mProtectedNetworks.add(p); |
| 771 | } else { |
| 772 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 773 | } |
| 774 | } |
| 775 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 776 | // high priority first |
| 777 | mPriorityList = new int[mNetworksDefined]; |
| 778 | { |
| 779 | int insertionPoint = mNetworksDefined-1; |
| 780 | int currentLowest = 0; |
| 781 | int nextLowest = 0; |
| 782 | while (insertionPoint > -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 783 | for (NetworkConfig na : mNetConfigs) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 784 | if (na == null) continue; |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 785 | if (na.priority < currentLowest) continue; |
| 786 | if (na.priority > currentLowest) { |
| 787 | if (na.priority < nextLowest || nextLowest == 0) { |
| 788 | nextLowest = na.priority; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 789 | } |
| 790 | continue; |
| 791 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 792 | mPriorityList[insertionPoint--] = na.type; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 793 | } |
| 794 | currentLowest = nextLowest; |
| 795 | nextLowest = 0; |
| 796 | } |
| 797 | } |
| 798 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 799 | mNetRequestersPids = |
| 800 | (List<Integer> [])new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 801 | for (int i : mPriorityList) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 802 | mNetRequestersPids[i] = new ArrayList<Integer>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 803 | } |
| 804 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 805 | mFeatureUsers = new ArrayList<FeatureUser>(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 806 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 807 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 808 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 809 | |
| 810 | // Create and start trackers for hard-coded networks |
| 811 | for (int targetNetworkType : mPriorityList) { |
| 812 | final NetworkConfig config = mNetConfigs[targetNetworkType]; |
| 813 | final NetworkStateTracker tracker; |
| 814 | try { |
| 815 | tracker = netFactory.createTracker(targetNetworkType, config); |
| 816 | mNetTrackers[targetNetworkType] = tracker; |
| 817 | } catch (IllegalArgumentException e) { |
| 818 | Slog.e(TAG, "Problem creating " + getNetworkTypeName(targetNetworkType) |
| 819 | + " tracker: " + e); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 820 | continue; |
| 821 | } |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 822 | |
| 823 | tracker.startMonitoring(context, mTrackerHandler); |
| 824 | if (config.isDefault()) { |
| 825 | tracker.reconnect(); |
Robert Greenwalt | 6537b02 | 2011-11-10 16:55:20 -0800 | [diff] [blame] | 826 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 827 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 828 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 829 | mTethering = new Tethering(mContext, mNetd, statsService, this, mHandler.getLooper()); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 830 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 831 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 832 | IntentFilter intentFilter = new IntentFilter(); |
| 833 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 834 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
| 835 | mContext.registerReceiverAsUser( |
| 836 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 837 | mClat = new Nat464Xlat(mContext, mNetd, this, mTrackerHandler); |
| 838 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 839 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 840 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 841 | mNetd.registerObserver(mDataActivityObserver); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 842 | mNetd.registerObserver(mClat); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 843 | } catch (RemoteException e) { |
| 844 | loge("Error registering observer :" + e); |
| 845 | } |
| 846 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 847 | if (DBG) { |
| 848 | mInetLog = new ArrayList(); |
| 849 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 850 | |
| 851 | mSettingsObserver = new SettingsObserver(mHandler, EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 852 | mSettingsObserver.observe(mContext); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 853 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 854 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 855 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 856 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 857 | // start network sampling .. |
| 858 | Intent intent = new Intent(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED, null); |
| 859 | mSampleIntervalElapsedIntent = PendingIntent.getBroadcast(mContext, |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 860 | SAMPLE_INTERVAL_ELAPSED_REQUEST_CODE, intent, 0); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 861 | |
| 862 | mAlarmManager = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); |
| 863 | setAlarm(DEFAULT_START_SAMPLING_INTERVAL_IN_SECONDS * 1000, mSampleIntervalElapsedIntent); |
| 864 | |
| 865 | IntentFilter filter = new IntentFilter(); |
| 866 | filter.addAction(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED); |
| 867 | mContext.registerReceiver( |
| 868 | new BroadcastReceiver() { |
| 869 | @Override |
| 870 | public void onReceive(Context context, Intent intent) { |
| 871 | String action = intent.getAction(); |
| 872 | if (action.equals(ACTION_PKT_CNT_SAMPLE_INTERVAL_ELAPSED)) { |
| 873 | mHandler.sendMessage(mHandler.obtainMessage |
| 874 | (EVENT_SAMPLE_INTERVAL_ELAPSED)); |
| 875 | } |
| 876 | } |
| 877 | }, |
| 878 | new IntentFilter(filter)); |
| 879 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 880 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 881 | |
| 882 | filter = new IntentFilter(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 883 | filter.addAction(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 884 | mContext.registerReceiver(mProvisioningReceiver, filter); |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 885 | |
| 886 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 888 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 889 | private synchronized int nextNetworkRequestId() { |
| 890 | return mNextNetworkRequestId++; |
| 891 | } |
| 892 | |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 893 | private synchronized int nextNetId() { |
| 894 | int netId = mNextNetId; |
| 895 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 896 | return netId; |
| 897 | } |
| 898 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 899 | /** |
| 900 | * Factory that creates {@link NetworkStateTracker} instances using given |
| 901 | * {@link NetworkConfig}. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 902 | * |
| 903 | * TODO - this is obsolete and will be deleted. It's replaced by the |
| 904 | * registerNetworkFactory call and protocol. |
| 905 | * @Deprecated in favor of registerNetworkFactory dynamic bindings |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 906 | */ |
| 907 | public interface NetworkFactory { |
| 908 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config); |
| 909 | } |
| 910 | |
| 911 | private static class DefaultNetworkFactory implements NetworkFactory { |
| 912 | private final Context mContext; |
| 913 | private final Handler mTrackerHandler; |
| 914 | |
| 915 | public DefaultNetworkFactory(Context context, Handler trackerHandler) { |
| 916 | mContext = context; |
| 917 | mTrackerHandler = trackerHandler; |
| 918 | } |
| 919 | |
| 920 | @Override |
| 921 | public NetworkStateTracker createTracker(int targetNetworkType, NetworkConfig config) { |
| 922 | switch (config.radio) { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 923 | case TYPE_DUMMY: |
| 924 | return new DummyDataStateTracker(targetNetworkType, config.name); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 925 | case TYPE_WIMAX: |
| 926 | return makeWimaxStateTracker(mContext, mTrackerHandler); |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 927 | case TYPE_PROXY: |
| 928 | return new ProxyDataTracker(); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 929 | default: |
| 930 | throw new IllegalArgumentException( |
| 931 | "Trying to create a NetworkStateTracker for an unknown radio type: " |
| 932 | + config.radio); |
| 933 | } |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | /** |
| 938 | * Loads external WiMAX library and registers as system service, returning a |
| 939 | * {@link NetworkStateTracker} for WiMAX. Caller is still responsible for |
| 940 | * invoking {@link NetworkStateTracker#startMonitoring(Context, Handler)}. |
| 941 | */ |
| 942 | private static NetworkStateTracker makeWimaxStateTracker( |
| 943 | Context context, Handler trackerHandler) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 944 | // Initialize Wimax |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 945 | DexClassLoader wimaxClassLoader; |
| 946 | Class wimaxStateTrackerClass = null; |
| 947 | Class wimaxServiceClass = null; |
| 948 | Class wimaxManagerClass; |
| 949 | String wimaxJarLocation; |
| 950 | String wimaxLibLocation; |
| 951 | String wimaxManagerClassName; |
| 952 | String wimaxServiceClassName; |
| 953 | String wimaxStateTrackerClassName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 955 | NetworkStateTracker wimaxStateTracker = null; |
| 956 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 957 | boolean isWimaxEnabled = context.getResources().getBoolean( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 958 | com.android.internal.R.bool.config_wimaxEnabled); |
| 959 | |
| 960 | if (isWimaxEnabled) { |
| 961 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 962 | wimaxJarLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 963 | com.android.internal.R.string.config_wimaxServiceJarLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 964 | wimaxLibLocation = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 965 | com.android.internal.R.string.config_wimaxNativeLibLocation); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 966 | wimaxManagerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 967 | com.android.internal.R.string.config_wimaxManagerClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 968 | wimaxServiceClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 969 | com.android.internal.R.string.config_wimaxServiceClassname); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 970 | wimaxStateTrackerClassName = context.getResources().getString( |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 971 | com.android.internal.R.string.config_wimaxStateTrackerClassname); |
| 972 | |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 973 | if (DBG) log("wimaxJarLocation: " + wimaxJarLocation); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 974 | wimaxClassLoader = new DexClassLoader(wimaxJarLocation, |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 975 | new ContextWrapper(context).getCacheDir().getAbsolutePath(), |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 976 | wimaxLibLocation, ClassLoader.getSystemClassLoader()); |
| 977 | |
| 978 | try { |
| 979 | wimaxManagerClass = wimaxClassLoader.loadClass(wimaxManagerClassName); |
| 980 | wimaxStateTrackerClass = wimaxClassLoader.loadClass(wimaxStateTrackerClassName); |
| 981 | wimaxServiceClass = wimaxClassLoader.loadClass(wimaxServiceClassName); |
| 982 | } catch (ClassNotFoundException ex) { |
| 983 | loge("Exception finding Wimax classes: " + ex.toString()); |
| 984 | return null; |
| 985 | } |
| 986 | } catch(Resources.NotFoundException ex) { |
| 987 | loge("Wimax Resources does not exist!!! "); |
| 988 | return null; |
| 989 | } |
| 990 | |
| 991 | try { |
Dianne Hackborn | 7ff3011 | 2012-11-08 11:12:09 -0800 | [diff] [blame] | 992 | if (DBG) log("Starting Wimax Service... "); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 993 | |
| 994 | Constructor wmxStTrkrConst = wimaxStateTrackerClass.getConstructor |
| 995 | (new Class[] {Context.class, Handler.class}); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 996 | wimaxStateTracker = (NetworkStateTracker) wmxStTrkrConst.newInstance( |
| 997 | context, trackerHandler); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 998 | |
| 999 | Constructor wmxSrvConst = wimaxServiceClass.getDeclaredConstructor |
| 1000 | (new Class[] {Context.class, wimaxStateTrackerClass}); |
| 1001 | wmxSrvConst.setAccessible(true); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 1002 | IBinder svcInvoker = (IBinder)wmxSrvConst.newInstance(context, wimaxStateTracker); |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 1003 | wmxSrvConst.setAccessible(false); |
| 1004 | |
| 1005 | ServiceManager.addService(WimaxManagerConstants.WIMAX_SERVICE, svcInvoker); |
| 1006 | |
| 1007 | } catch(Exception ex) { |
| 1008 | loge("Exception creating Wimax classes: " + ex.toString()); |
| 1009 | return null; |
| 1010 | } |
| 1011 | } else { |
| 1012 | loge("Wimax is not enabled or not added to the network attributes!!! "); |
| 1013 | return null; |
| 1014 | } |
| 1015 | |
| 1016 | return wimaxStateTracker; |
| 1017 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1018 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1019 | private int getConnectivityChangeDelay() { |
| 1020 | final ContentResolver cr = mContext.getContentResolver(); |
| 1021 | |
| 1022 | /** Check system properties for the default value then use secure settings value, if any. */ |
| 1023 | int defaultDelay = SystemProperties.getInt( |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 1024 | "conn." + Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
| 1025 | ConnectivityManager.CONNECTIVITY_CHANGE_DELAY_DEFAULT); |
| 1026 | return Settings.Global.getInt(cr, Settings.Global.CONNECTIVITY_CHANGE_DELAY, |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1027 | defaultDelay); |
| 1028 | } |
| 1029 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1030 | private boolean teardown(NetworkStateTracker netTracker) { |
| 1031 | if (netTracker.teardown()) { |
| 1032 | netTracker.setTeardownRequested(true); |
| 1033 | return true; |
| 1034 | } else { |
| 1035 | return false; |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1040 | * Check if UID should be blocked from using the network represented by the |
| 1041 | * given {@link NetworkStateTracker}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1042 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1043 | private boolean isNetworkBlocked(int networkType, int uid) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1044 | final boolean networkCostly; |
| 1045 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1046 | |
| 1047 | LinkProperties lp = getLinkPropertiesForType(networkType); |
| 1048 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1049 | synchronized (mRulesLock) { |
| 1050 | networkCostly = mMeteredIfaces.contains(iface); |
| 1051 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1052 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1053 | |
| 1054 | if (networkCostly && (uidRules & RULE_REJECT_METERED) != 0) { |
| 1055 | return true; |
| 1056 | } |
| 1057 | |
| 1058 | // no restrictive rules; network is visible |
| 1059 | return false; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1060 | } |
| 1061 | |
| 1062 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1063 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 1064 | * {@link DetailedState#BLOCKED} based on |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1065 | * {@link #isNetworkBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1066 | */ |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1067 | private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) { |
| 1068 | NetworkInfo info = getNetworkInfoForType(networkType); |
| 1069 | if (isNetworkBlocked(networkType, uid)) { |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1070 | // network is blocked; clone and override state |
| 1071 | info = new NetworkInfo(info); |
| 1072 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1073 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1074 | if (mLockdownTracker != null) { |
| 1075 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 1076 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1077 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1078 | } |
| 1079 | |
| 1080 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 1082 | * It is assumed that at most one network is active at a time. If more |
| 1083 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1084 | * @return the info for the active network, or {@code null} if none is |
| 1085 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1087 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1088 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1089 | enforceAccessPermission(); |
| 1090 | final int uid = Binder.getCallingUid(); |
| 1091 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | } |
| 1093 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1094 | // only called when the default request is satisfied |
| 1095 | private void updateActiveDefaultNetwork(NetworkAgentInfo nai) { |
| 1096 | if (nai != null) { |
| 1097 | mActiveDefaultNetwork = nai.networkInfo.getType(); |
| 1098 | } else { |
| 1099 | mActiveDefaultNetwork = TYPE_NONE; |
| 1100 | } |
| 1101 | } |
| 1102 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1103 | /** |
| 1104 | * Find the first Provisioning network. |
| 1105 | * |
| 1106 | * @return NetworkInfo or null if none. |
| 1107 | */ |
| 1108 | private NetworkInfo getProvisioningNetworkInfo() { |
| 1109 | enforceAccessPermission(); |
| 1110 | |
| 1111 | // Find the first Provisioning Network |
| 1112 | NetworkInfo provNi = null; |
| 1113 | for (NetworkInfo ni : getAllNetworkInfo()) { |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 1114 | if (ni.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1115 | provNi = ni; |
| 1116 | break; |
| 1117 | } |
| 1118 | } |
| 1119 | if (DBG) log("getProvisioningNetworkInfo: X provNi=" + provNi); |
| 1120 | return provNi; |
| 1121 | } |
| 1122 | |
| 1123 | /** |
| 1124 | * Find the first Provisioning network or the ActiveDefaultNetwork |
| 1125 | * if there is no Provisioning network |
| 1126 | * |
| 1127 | * @return NetworkInfo or null if none. |
| 1128 | */ |
| 1129 | @Override |
| 1130 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 1131 | enforceAccessPermission(); |
| 1132 | |
| 1133 | NetworkInfo provNi = getProvisioningNetworkInfo(); |
| 1134 | if (provNi == null) { |
| 1135 | final int uid = Binder.getCallingUid(); |
| 1136 | provNi = getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1137 | } |
| 1138 | if (DBG) log("getProvisioningOrActiveNetworkInfo: X provNi=" + provNi); |
| 1139 | return provNi; |
| 1140 | } |
| 1141 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1142 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1143 | enforceAccessPermission(); |
| 1144 | if (isNetworkTypeValid(mActiveDefaultNetwork)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1145 | return getNetworkInfoForType(mActiveDefaultNetwork); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1146 | } |
| 1147 | return null; |
| 1148 | } |
| 1149 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1150 | @Override |
| 1151 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1152 | enforceConnectivityInternalPermission(); |
| 1153 | return getNetworkInfo(mActiveDefaultNetwork, uid); |
| 1154 | } |
| 1155 | |
| 1156 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1157 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1158 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1159 | final int uid = Binder.getCallingUid(); |
| 1160 | return getNetworkInfo(networkType, uid); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } |
| 1162 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1163 | private NetworkInfo getNetworkInfo(int networkType, int uid) { |
| 1164 | NetworkInfo info = null; |
| 1165 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1166 | if (getNetworkInfoForType(networkType) != null) { |
| 1167 | info = getFilteredNetworkInfo(networkType, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1168 | } |
| 1169 | } |
| 1170 | return info; |
| 1171 | } |
| 1172 | |
| 1173 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1174 | public NetworkInfo[] getAllNetworkInfo() { |
| 1175 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1176 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1177 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1178 | synchronized (mRulesLock) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1179 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1180 | networkType++) { |
| 1181 | if (getNetworkInfoForType(networkType) != null) { |
| 1182 | result.add(getFilteredNetworkInfo(networkType, uid)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1183 | } |
| 1184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1186 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1187 | } |
| 1188 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1189 | @Override |
| 1190 | public boolean isNetworkSupported(int networkType) { |
| 1191 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1192 | return (isNetworkTypeValid(networkType) && (getNetworkInfoForType(networkType) != null)); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1193 | } |
| 1194 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1195 | /** |
| 1196 | * Return LinkProperties for the active (i.e., connected) default |
| 1197 | * network interface. It is assumed that at most one default network |
| 1198 | * is active at a time. If more than one is active, it is indeterminate |
| 1199 | * which will be returned. |
| 1200 | * @return the ip properties for the active network, or {@code null} if |
| 1201 | * none is active |
| 1202 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1203 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1204 | public LinkProperties getActiveLinkProperties() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1205 | return getLinkPropertiesForType(mActiveDefaultNetwork); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1206 | } |
| 1207 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1208 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1209 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1210 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1211 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1212 | return getLinkPropertiesForTypeInternal(networkType); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1213 | } |
| 1214 | return null; |
| 1215 | } |
| 1216 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1217 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1218 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1219 | public LinkProperties getLinkProperties(Network network) { |
| 1220 | enforceAccessPermission(); |
| 1221 | NetworkAgentInfo nai = mNetworkForNetId.get(network.netId); |
| 1222 | if (nai != null) return new LinkProperties(nai.linkProperties); |
| 1223 | return null; |
| 1224 | } |
| 1225 | |
| 1226 | @Override |
| 1227 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1228 | enforceAccessPermission(); |
| 1229 | NetworkAgentInfo nai = mNetworkForNetId.get(network.netId); |
| 1230 | if (nai != null) return new NetworkCapabilities(nai.networkCapabilities); |
| 1231 | return null; |
| 1232 | } |
| 1233 | |
| 1234 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1235 | public NetworkState[] getAllNetworkState() { |
| 1236 | enforceAccessPermission(); |
| 1237 | final int uid = Binder.getCallingUid(); |
| 1238 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1239 | synchronized (mRulesLock) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1240 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1241 | networkType++) { |
| 1242 | if (getNetworkInfoForType(networkType) != null) { |
| 1243 | final NetworkInfo info = getFilteredNetworkInfo(networkType, uid); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1244 | final LinkProperties lp = getLinkPropertiesForTypeInternal(networkType); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1245 | final NetworkCapabilities netcap = getNetworkCapabilitiesForType(networkType); |
| 1246 | result.add(new NetworkState(info, lp, netcap)); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | return result.toArray(new NetworkState[result.size()]); |
| 1251 | } |
| 1252 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1253 | private NetworkState getNetworkStateUnchecked(int networkType) { |
| 1254 | if (isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1255 | NetworkInfo info = getNetworkInfoForType(networkType); |
| 1256 | if (info != null) { |
| 1257 | return new NetworkState(info, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1258 | getLinkPropertiesForTypeInternal(networkType), |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1259 | getNetworkCapabilitiesForType(networkType)); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1260 | } |
| 1261 | } |
| 1262 | return null; |
| 1263 | } |
| 1264 | |
| 1265 | @Override |
| 1266 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1267 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1268 | |
| 1269 | final long token = Binder.clearCallingIdentity(); |
| 1270 | try { |
| 1271 | final NetworkState state = getNetworkStateUnchecked(mActiveDefaultNetwork); |
| 1272 | if (state != null) { |
| 1273 | try { |
| 1274 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1275 | } catch (RemoteException e) { |
| 1276 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1277 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1278 | return null; |
| 1279 | } finally { |
| 1280 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1281 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1284 | @Override |
| 1285 | public boolean isActiveNetworkMetered() { |
| 1286 | enforceAccessPermission(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1287 | final long token = Binder.clearCallingIdentity(); |
| 1288 | try { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1289 | return isNetworkMeteredUnchecked(mActiveDefaultNetwork); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1290 | } finally { |
| 1291 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1292 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1293 | } |
| 1294 | |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1295 | private boolean isNetworkMeteredUnchecked(int networkType) { |
| 1296 | final NetworkState state = getNetworkStateUnchecked(networkType); |
| 1297 | if (state != null) { |
| 1298 | try { |
| 1299 | return mPolicyManager.isNetworkMetered(state); |
| 1300 | } catch (RemoteException e) { |
| 1301 | } |
| 1302 | } |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1306 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1307 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1308 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1309 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1310 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1311 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1312 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1313 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1314 | /** |
| 1315 | * Used to notice when the calling process dies so we can self-expire |
| 1316 | * |
| 1317 | * Also used to know if the process has cleaned up after itself when |
| 1318 | * our auto-expire timer goes off. The timer has a link to an object. |
| 1319 | * |
| 1320 | */ |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1321 | private class FeatureUser implements IBinder.DeathRecipient { |
| 1322 | int mNetworkType; |
| 1323 | String mFeature; |
| 1324 | IBinder mBinder; |
| 1325 | int mPid; |
| 1326 | int mUid; |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1327 | long mCreateTime; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1328 | |
| 1329 | FeatureUser(int type, String feature, IBinder binder) { |
| 1330 | super(); |
| 1331 | mNetworkType = type; |
| 1332 | mFeature = feature; |
| 1333 | mBinder = binder; |
| 1334 | mPid = getCallingPid(); |
| 1335 | mUid = getCallingUid(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1336 | mCreateTime = System.currentTimeMillis(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1337 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1338 | try { |
| 1339 | mBinder.linkToDeath(this, 0); |
| 1340 | } catch (RemoteException e) { |
| 1341 | binderDied(); |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | void unlinkDeathRecipient() { |
| 1346 | mBinder.unlinkToDeath(this, 0); |
| 1347 | } |
| 1348 | |
| 1349 | public void binderDied() { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 1350 | log("ConnectivityService FeatureUser binderDied(" + |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1351 | mNetworkType + ", " + mFeature + ", " + mBinder + "), created " + |
| 1352 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1353 | stopUsingNetworkFeature(this, false); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1354 | } |
| 1355 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1356 | public void expire() { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1357 | if (VDBG) { |
| 1358 | log("ConnectivityService FeatureUser expire(" + |
| 1359 | mNetworkType + ", " + mFeature + ", " + mBinder +"), created " + |
| 1360 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"); |
| 1361 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1362 | stopUsingNetworkFeature(this, false); |
| 1363 | } |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1364 | |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1365 | public boolean isSameUser(FeatureUser u) { |
| 1366 | if (u == null) return false; |
| 1367 | |
| 1368 | return isSameUser(u.mPid, u.mUid, u.mNetworkType, u.mFeature); |
| 1369 | } |
| 1370 | |
| 1371 | public boolean isSameUser(int pid, int uid, int networkType, String feature) { |
| 1372 | if ((mPid == pid) && (mUid == uid) && (mNetworkType == networkType) && |
| 1373 | TextUtils.equals(mFeature, feature)) { |
| 1374 | return true; |
| 1375 | } |
| 1376 | return false; |
| 1377 | } |
| 1378 | |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1379 | public String toString() { |
| 1380 | return "FeatureUser("+mNetworkType+","+mFeature+","+mPid+","+mUid+"), created " + |
| 1381 | (System.currentTimeMillis() - mCreateTime) + " mSec ago"; |
| 1382 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1383 | } |
| 1384 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1385 | // javadoc from interface |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1386 | public int startUsingNetworkFeature(int networkType, String feature, |
| 1387 | IBinder binder) { |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1388 | long startTime = 0; |
| 1389 | if (DBG) { |
| 1390 | startTime = SystemClock.elapsedRealtime(); |
| 1391 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1392 | if (VDBG) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1393 | log("startUsingNetworkFeature for net " + networkType + ": " + feature + ", uid=" |
| 1394 | + Binder.getCallingUid()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1395 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1396 | enforceChangePermission(); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1397 | try { |
| 1398 | if (!ConnectivityManager.isNetworkTypeValid(networkType) || |
| 1399 | mNetConfigs[networkType] == null) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1400 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1401 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1402 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1403 | FeatureUser f = new FeatureUser(networkType, feature, binder); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1404 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1405 | // TODO - move this into individual networktrackers |
| 1406 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1407 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1408 | if (mLockdownEnabled) { |
| 1409 | // Since carrier APNs usually aren't available from VPN |
| 1410 | // endpoint, mark them as unavailable. |
| 1411 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
| 1412 | } |
| 1413 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1414 | if (mProtectedNetworks.contains(usedNetworkType)) { |
| 1415 | enforceConnectivityInternalPermission(); |
| 1416 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1417 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1418 | // if UID is restricted, don't allow them to bring up metered APNs |
| 1419 | final boolean networkMetered = isNetworkMeteredUnchecked(usedNetworkType); |
| 1420 | final int uidRules; |
| 1421 | synchronized (mRulesLock) { |
| 1422 | uidRules = mUidRules.get(Binder.getCallingUid(), RULE_ALLOW_ALL); |
| 1423 | } |
| 1424 | if (networkMetered && (uidRules & RULE_REJECT_METERED) != 0) { |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1425 | return PhoneConstants.APN_REQUEST_FAILED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1426 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1427 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1428 | NetworkStateTracker network = mNetTrackers[usedNetworkType]; |
| 1429 | if (network != null) { |
| 1430 | Integer currentPid = new Integer(getCallingPid()); |
| 1431 | if (usedNetworkType != networkType) { |
| 1432 | NetworkInfo ni = network.getNetworkInfo(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1433 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1434 | if (ni.isAvailable() == false) { |
| 1435 | if (!TextUtils.equals(feature,Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 1436 | if (DBG) log("special network not available ni=" + ni.getTypeName()); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1437 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1438 | } else { |
| 1439 | // else make the attempt anyway - probably giving REQUEST_STARTED below |
| 1440 | if (DBG) { |
| 1441 | log("special network not available, but try anyway ni=" + |
| 1442 | ni.getTypeName()); |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | } |
| 1446 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1447 | int restoreTimer = getRestoreDefaultNetworkDelay(usedNetworkType); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1448 | |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1449 | synchronized(this) { |
| 1450 | boolean addToList = true; |
| 1451 | if (restoreTimer < 0) { |
| 1452 | // In case there is no timer is specified for the feature, |
| 1453 | // make sure we don't add duplicate entry with the same request. |
| 1454 | for (FeatureUser u : mFeatureUsers) { |
| 1455 | if (u.isSameUser(f)) { |
| 1456 | // Duplicate user is found. Do not add. |
| 1457 | addToList = false; |
| 1458 | break; |
| 1459 | } |
| 1460 | } |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1461 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1462 | |
| 1463 | if (addToList) mFeatureUsers.add(f); |
| 1464 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1465 | // this gets used for per-pid dns when connected |
| 1466 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1467 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1468 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1469 | |
| 1470 | if (restoreTimer >= 0) { |
| 1471 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 1472 | EVENT_RESTORE_DEFAULT_NETWORK, f), restoreTimer); |
| 1473 | } |
| 1474 | |
| 1475 | if ((ni.isConnectedOrConnecting() == true) && |
| 1476 | !network.isTeardownRequested()) { |
| 1477 | if (ni.isConnected() == true) { |
| 1478 | final long token = Binder.clearCallingIdentity(); |
| 1479 | try { |
| 1480 | // add the pid-specific dns |
| 1481 | handleDnsConfigurationChange(usedNetworkType); |
| 1482 | if (VDBG) log("special network already active"); |
| 1483 | } finally { |
| 1484 | Binder.restoreCallingIdentity(token); |
| 1485 | } |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1486 | return PhoneConstants.APN_ALREADY_ACTIVE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1487 | } |
| 1488 | if (VDBG) log("special network already connecting"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1489 | return PhoneConstants.APN_REQUEST_STARTED; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | // check if the radio in play can make another contact |
| 1493 | // assume if cannot for now |
| 1494 | |
| 1495 | if (DBG) { |
| 1496 | log("startUsingNetworkFeature reconnecting to " + networkType + ": " + |
| 1497 | feature); |
| 1498 | } |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1499 | if (network.reconnect()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1500 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_STARTED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1501 | return PhoneConstants.APN_REQUEST_STARTED; |
| 1502 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1503 | if (DBG) log("startUsingNetworkFeature X: return APN_REQUEST_FAILED"); |
Mikael Hedegren | 342a7cb | 2012-12-14 15:52:52 +0100 | [diff] [blame] | 1504 | return PhoneConstants.APN_REQUEST_FAILED; |
| 1505 | } |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1506 | } else { |
| 1507 | // need to remember this unsupported request so we respond appropriately on stop |
| 1508 | synchronized(this) { |
| 1509 | mFeatureUsers.add(f); |
| 1510 | if (!mNetRequestersPids[usedNetworkType].contains(currentPid)) { |
| 1511 | // this gets used for per-pid dns when connected |
| 1512 | mNetRequestersPids[usedNetworkType].add(currentPid); |
| 1513 | } |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1514 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1515 | if (DBG) log("startUsingNetworkFeature X: return -1 unsupported feature."); |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1516 | return -1; |
Robert Greenwalt | 0be1e98 | 2010-12-15 13:26:33 -0800 | [diff] [blame] | 1517 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1518 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1519 | if (DBG) log("startUsingNetworkFeature X: return APN_TYPE_NOT_AVAILABLE"); |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 1520 | return PhoneConstants.APN_TYPE_NOT_AVAILABLE; |
Robert Greenwalt | 5a0c320 | 2012-05-22 16:07:46 -0700 | [diff] [blame] | 1521 | } finally { |
| 1522 | if (DBG) { |
| 1523 | final long execTime = SystemClock.elapsedRealtime() - startTime; |
| 1524 | if (execTime > 250) { |
| 1525 | loge("startUsingNetworkFeature took too long: " + execTime + "ms"); |
| 1526 | } else { |
| 1527 | if (VDBG) log("startUsingNetworkFeature took " + execTime + "ms"); |
| 1528 | } |
| 1529 | } |
| 1530 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | } |
| 1532 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1533 | // javadoc from interface |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1534 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1535 | enforceChangePermission(); |
| 1536 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1537 | int pid = getCallingPid(); |
| 1538 | int uid = getCallingUid(); |
| 1539 | |
| 1540 | FeatureUser u = null; |
| 1541 | boolean found = false; |
| 1542 | |
| 1543 | synchronized(this) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1544 | for (FeatureUser x : mFeatureUsers) { |
| 1545 | if (x.isSameUser(pid, uid, networkType, feature)) { |
| 1546 | u = x; |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1547 | found = true; |
| 1548 | break; |
| 1549 | } |
| 1550 | } |
| 1551 | } |
| 1552 | if (found && u != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1553 | if (VDBG) log("stopUsingNetworkFeature: X"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1554 | // stop regardless of how many other time this proc had called start |
| 1555 | return stopUsingNetworkFeature(u, true); |
| 1556 | } else { |
| 1557 | // none found! |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1558 | if (VDBG) log("stopUsingNetworkFeature: X not a live request, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1559 | return 1; |
| 1560 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1561 | } |
| 1562 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1563 | private int stopUsingNetworkFeature(FeatureUser u, boolean ignoreDups) { |
| 1564 | int networkType = u.mNetworkType; |
| 1565 | String feature = u.mFeature; |
| 1566 | int pid = u.mPid; |
| 1567 | int uid = u.mUid; |
| 1568 | |
| 1569 | NetworkStateTracker tracker = null; |
| 1570 | boolean callTeardown = false; // used to carry our decision outside of sync block |
| 1571 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1572 | if (VDBG) { |
| 1573 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1574 | } |
Robert Greenwalt | b8f1634 | 2009-10-06 17:52:40 -0700 | [diff] [blame] | 1575 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1576 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1577 | if (DBG) { |
| 1578 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1579 | ", net is invalid"); |
| 1580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1581 | return -1; |
| 1582 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1583 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1584 | // need to link the mFeatureUsers list with the mNetRequestersPids state in this |
| 1585 | // sync block |
| 1586 | synchronized(this) { |
| 1587 | // check if this process still has an outstanding start request |
| 1588 | if (!mFeatureUsers.contains(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1589 | if (VDBG) { |
| 1590 | log("stopUsingNetworkFeature: this process has no outstanding requests" + |
| 1591 | ", ignoring"); |
| 1592 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1593 | return 1; |
| 1594 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1595 | u.unlinkDeathRecipient(); |
| 1596 | mFeatureUsers.remove(mFeatureUsers.indexOf(u)); |
| 1597 | // If we care about duplicate requests, check for that here. |
| 1598 | // |
| 1599 | // This is done to support the extension of a request - the app |
| 1600 | // can request we start the network feature again and renew the |
| 1601 | // auto-shutoff delay. Normal "stop" calls from the app though |
| 1602 | // do not pay attention to duplicate requests - in effect the |
| 1603 | // API does not refcount and a single stop will counter multiple starts. |
| 1604 | if (ignoreDups == false) { |
Kazuhiro Ondo | 3bed4c9 | 2011-07-19 11:23:37 -0500 | [diff] [blame] | 1605 | for (FeatureUser x : mFeatureUsers) { |
| 1606 | if (x.isSameUser(u)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1607 | if (VDBG) log("stopUsingNetworkFeature: dup is found, ignoring"); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1608 | return 1; |
| 1609 | } |
| 1610 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1611 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1612 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 1613 | // TODO - move to individual network trackers |
| 1614 | int usedNetworkType = convertFeatureToNetworkType(networkType, feature); |
| 1615 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1616 | tracker = mNetTrackers[usedNetworkType]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1617 | if (tracker == null) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1618 | if (DBG) { |
| 1619 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1620 | " no known tracker for used net type " + usedNetworkType); |
| 1621 | } |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 1622 | return -1; |
| 1623 | } |
| 1624 | if (usedNetworkType != networkType) { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1625 | Integer currentPid = new Integer(pid); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1626 | mNetRequestersPids[usedNetworkType].remove(currentPid); |
Robert Greenwalt | 9055ae3 | 2013-02-12 17:18:25 -0800 | [diff] [blame] | 1627 | |
| 1628 | final long token = Binder.clearCallingIdentity(); |
| 1629 | try { |
| 1630 | reassessPidDns(pid, true); |
| 1631 | } finally { |
| 1632 | Binder.restoreCallingIdentity(token); |
| 1633 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1634 | flushVmDnsCache(); |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1635 | if (mNetRequestersPids[usedNetworkType].size() != 0) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1636 | if (VDBG) { |
| 1637 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1638 | " others still using it"); |
| 1639 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1640 | return 1; |
| 1641 | } |
| 1642 | callTeardown = true; |
Robert Greenwalt | 19b9ab4 | 2011-01-10 11:58:31 -0800 | [diff] [blame] | 1643 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1644 | if (DBG) { |
| 1645 | log("stopUsingNetworkFeature: net " + networkType + ": " + feature + |
| 1646 | " not a known feature - dropping"); |
| 1647 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1648 | } |
| 1649 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1650 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1651 | if (callTeardown) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 1652 | if (DBG) { |
| 1653 | log("stopUsingNetworkFeature: teardown net " + networkType + ": " + feature); |
| 1654 | } |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1655 | tracker.teardown(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1656 | return 1; |
| 1657 | } else { |
Robert Greenwalt | 02648a4 | 2010-05-18 10:52:51 -0700 | [diff] [blame] | 1658 | return -1; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1659 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /** |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1663 | * Check if the address falls into any of currently running VPN's route's. |
| 1664 | */ |
| 1665 | private boolean isAddressUnderVpn(InetAddress address) { |
| 1666 | synchronized (mVpns) { |
| 1667 | synchronized (mRoutesLock) { |
| 1668 | int uid = UserHandle.getCallingUserId(); |
| 1669 | Vpn vpn = mVpns.get(uid); |
| 1670 | if (vpn == null) { |
| 1671 | return false; |
| 1672 | } |
| 1673 | |
| 1674 | // Check if an exemption exists for this address. |
| 1675 | for (LinkAddress destination : mExemptAddresses) { |
| 1676 | if (!NetworkUtils.addressTypeMatches(address, destination.getAddress())) { |
| 1677 | continue; |
| 1678 | } |
| 1679 | |
Lorenzo Colitti | 7dc78cf | 2014-06-09 22:58:46 +0900 | [diff] [blame] | 1680 | int prefix = destination.getPrefixLength(); |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1681 | InetAddress addrMasked = NetworkUtils.getNetworkPart(address, prefix); |
| 1682 | InetAddress destMasked = NetworkUtils.getNetworkPart(destination.getAddress(), |
| 1683 | prefix); |
| 1684 | |
| 1685 | if (addrMasked.equals(destMasked)) { |
| 1686 | return false; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | // Finally check if the address is covered by the VPN. |
| 1691 | return vpn.isAddressCovered(address); |
| 1692 | } |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | /** |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1697 | * @deprecated use requestRouteToHostAddress instead |
| 1698 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1699 | * Ensure that a network route exists to deliver traffic to the specified |
| 1700 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1701 | * @param networkType the type of the network over which traffic to the |
| 1702 | * specified host is to be routed |
| 1703 | * @param hostAddress the IP address of the host to which the route is |
| 1704 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1705 | * @return {@code true} on success, {@code false} on failure |
| 1706 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1707 | public boolean requestRouteToHost(int networkType, int hostAddress, String packageName) { |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1708 | InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress); |
| 1709 | |
| 1710 | if (inetAddress == null) { |
| 1711 | return false; |
| 1712 | } |
| 1713 | |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1714 | return requestRouteToHostAddress(networkType, inetAddress.getAddress(), packageName); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
| 1717 | /** |
| 1718 | * Ensure that a network route exists to deliver traffic to the specified |
| 1719 | * host via the specified network interface. |
| 1720 | * @param networkType the type of the network over which traffic to the |
| 1721 | * specified host is to be routed |
| 1722 | * @param hostAddress the IP address of the host to which the route is |
| 1723 | * desired |
| 1724 | * @return {@code true} on success, {@code false} on failure |
| 1725 | */ |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 1726 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress, |
| 1727 | String packageName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1729 | if (mProtectedNetworks.contains(networkType)) { |
| 1730 | enforceConnectivityInternalPermission(); |
| 1731 | } |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1732 | boolean exempt; |
| 1733 | InetAddress addr; |
| 1734 | try { |
| 1735 | addr = InetAddress.getByAddress(hostAddress); |
| 1736 | } catch (UnknownHostException e) { |
| 1737 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1738 | return false; |
| 1739 | } |
| 1740 | // System apps may request routes bypassing the VPN to keep other networks working. |
| 1741 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 1742 | exempt = true; |
| 1743 | } else { |
| 1744 | mAppOpsManager.checkPackage(Binder.getCallingUid(), packageName); |
| 1745 | try { |
| 1746 | ApplicationInfo info = mContext.getPackageManager().getApplicationInfo(packageName, |
| 1747 | 0); |
| 1748 | exempt = (info.flags & ApplicationInfo.FLAG_SYSTEM) != 0; |
| 1749 | } catch (NameNotFoundException e) { |
| 1750 | throw new IllegalArgumentException("Failed to find calling package details", e); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // Non-exempt routeToHost's can only be added if the host is not covered by the VPN. |
| 1755 | // This can be either because the VPN's routes do not cover the destination or a |
| 1756 | // system application added an exemption that covers this destination. |
| 1757 | if (!exempt && isAddressUnderVpn(addr)) { |
| 1758 | return false; |
| 1759 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1760 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1761 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1762 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | return false; |
| 1764 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1765 | |
| 1766 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1767 | if (nai == null) { |
| 1768 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1769 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1770 | } else { |
| 1771 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1772 | } |
| 1773 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1774 | } |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1775 | |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1776 | DetailedState netState = nai.networkInfo.getDetailedState(); |
| 1777 | |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1778 | if ((netState != DetailedState.CONNECTED && |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1779 | netState != DetailedState.CAPTIVE_PORTAL_CHECK)) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1780 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1781 | log("requestRouteToHostAddress on down network " |
| 1782 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1783 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1784 | } |
| 1785 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1786 | } |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1787 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1788 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1789 | try { |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1790 | LinkProperties lp = nai.linkProperties; |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1791 | boolean ok = modifyRouteToAddress(lp, addr, ADD, TO_DEFAULT_TABLE, exempt, |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1792 | nai.network.netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1793 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1794 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1795 | } finally { |
| 1796 | Binder.restoreCallingIdentity(token); |
| 1797 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1800 | private boolean addRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1801 | boolean exempt, int netId) { |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1802 | return modifyRoute(p, r, 0, ADD, toDefaultTable, exempt, netId, false, UID_UNUSED); |
Robert Greenwalt | f43396c | 2011-05-06 17:10:53 -0700 | [diff] [blame] | 1803 | } |
| 1804 | |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 1805 | private boolean removeRoute(LinkProperties p, RouteInfo r, boolean toDefaultTable, int netId) { |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1806 | return modifyRoute(p, r, 0, REMOVE, toDefaultTable, UNEXEMPT, netId, false, UID_UNUSED); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1807 | } |
| 1808 | |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1809 | private boolean modifyRouteToAddress(LinkProperties lp, InetAddress addr, boolean doAdd, |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1810 | boolean toDefaultTable, boolean exempt, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1811 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1812 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1813 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1814 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1815 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1816 | if (bestRoute.getGateway().equals(addr)) { |
| 1817 | // 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] | 1818 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1819 | } else { |
| 1820 | // if we will connect to this through another route, add a direct route |
| 1821 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1822 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1823 | } |
| 1824 | } |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1825 | return modifyRoute(lp, bestRoute, 0, doAdd, toDefaultTable, exempt, netId, true, uid); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1826 | } |
| 1827 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1828 | /* |
| 1829 | * TODO: Clean all this stuff up. Once we have UID-based routing, stuff will break due to |
| 1830 | * incorrect tracking of mAddedRoutes, so a cleanup becomes necessary and urgent. But at |
| 1831 | * the same time, there'll be no more need to track mAddedRoutes or mExemptAddresses, |
| 1832 | * or even have the concept of an exempt address, or do things like "selectBestRoute", or |
| 1833 | * determine "default" vs "secondary" table, etc., so the cleanup becomes possible. |
| 1834 | */ |
Lorenzo Colitti | 2adea7f | 2013-03-12 07:39:59 +0900 | [diff] [blame] | 1835 | private boolean modifyRoute(LinkProperties lp, RouteInfo r, int cycleCount, boolean doAdd, |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1836 | boolean toDefaultTable, boolean exempt, int netId, boolean legacy, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1837 | if ((lp == null) || (r == null)) { |
| 1838 | if (DBG) log("modifyRoute got unexpected null: " + lp + ", " + r); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1839 | return false; |
| 1840 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1841 | |
| 1842 | if (cycleCount > MAX_HOSTROUTE_CYCLE_COUNT) { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1843 | loge("Error modifying route - too much recursion"); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1847 | String ifaceName = r.getInterface(); |
| 1848 | if(ifaceName == null) { |
| 1849 | loge("Error modifying route - no interface name"); |
| 1850 | return false; |
| 1851 | } |
Robert Greenwalt | ca441ee | 2013-04-11 13:48:16 -0700 | [diff] [blame] | 1852 | if (r.hasGateway()) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1853 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), r.getGateway()); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1854 | if (bestRoute != null) { |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1855 | if (bestRoute.getGateway().equals(r.getGateway())) { |
| 1856 | // 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] | 1857 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), ifaceName); |
Robert Greenwalt | 476f552 | 2011-07-15 09:45:08 -0700 | [diff] [blame] | 1858 | } else { |
| 1859 | // if we will connect to our gateway through another route, add a direct |
| 1860 | // route to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1861 | bestRoute = RouteInfo.makeHostRoute(r.getGateway(), |
| 1862 | bestRoute.getGateway(), |
| 1863 | ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1864 | } |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1865 | modifyRoute(lp, bestRoute, cycleCount+1, doAdd, toDefaultTable, exempt, netId, |
| 1866 | legacy, uid); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1867 | } |
| 1868 | } |
| 1869 | if (doAdd) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1870 | if (VDBG) log("Adding " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1871 | try { |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1872 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1873 | synchronized (mRoutesLock) { |
| 1874 | // only track default table - only one apps can effect |
| 1875 | mAddedRoutes.add(r); |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1876 | if (legacy) { |
| 1877 | mNetd.addLegacyRouteForNetId(netId, r, uid); |
| 1878 | } else { |
| 1879 | mNetd.addRoute(netId, r); |
| 1880 | } |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1881 | if (exempt) { |
Sreeram Ramachandran | cc91c7b | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 1882 | LinkAddress dest = r.getDestinationLinkAddress(); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1883 | if (!mExemptAddresses.contains(dest)) { |
| 1884 | mNetd.setHostExemption(dest); |
| 1885 | mExemptAddresses.add(dest); |
| 1886 | } |
| 1887 | } |
| 1888 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1889 | } else { |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1890 | if (legacy) { |
| 1891 | mNetd.addLegacyRouteForNetId(netId, r, uid); |
| 1892 | } else { |
| 1893 | mNetd.addRoute(netId, r); |
| 1894 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1895 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1896 | } catch (Exception e) { |
| 1897 | // never crash - catch them all |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1898 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1899 | return false; |
| 1900 | } |
| 1901 | } else { |
| 1902 | // if we remove this one and there are no more like it, then refcount==0 and |
| 1903 | // we can remove it from the table |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1904 | if (toDefaultTable) { |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1905 | synchronized (mRoutesLock) { |
| 1906 | mAddedRoutes.remove(r); |
| 1907 | if (mAddedRoutes.contains(r) == false) { |
| 1908 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 1909 | try { |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1910 | if (legacy) { |
| 1911 | mNetd.removeLegacyRouteForNetId(netId, r, uid); |
| 1912 | } else { |
| 1913 | mNetd.removeRoute(netId, r); |
| 1914 | } |
Sreeram Ramachandran | cc91c7b | 2014-06-03 18:41:43 -0700 | [diff] [blame] | 1915 | LinkAddress dest = r.getDestinationLinkAddress(); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 1916 | if (mExemptAddresses.contains(dest)) { |
| 1917 | mNetd.clearHostExemption(dest); |
| 1918 | mExemptAddresses.remove(dest); |
| 1919 | } |
| 1920 | } catch (Exception e) { |
| 1921 | // never crash - catch them all |
| 1922 | if (VDBG) loge("Exception trying to remove a route: " + e); |
| 1923 | return false; |
| 1924 | } |
| 1925 | } else { |
| 1926 | if (VDBG) log("not removing " + r + " as it's still in use"); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1927 | } |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 1928 | } |
| 1929 | } else { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1930 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1931 | try { |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1932 | if (legacy) { |
| 1933 | mNetd.removeLegacyRouteForNetId(netId, r, uid); |
| 1934 | } else { |
| 1935 | mNetd.removeRoute(netId, r); |
| 1936 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1937 | } catch (Exception e) { |
| 1938 | // never crash - catch them all |
Robert Greenwalt | 2473a4ba | 2012-04-23 18:00:37 -0700 | [diff] [blame] | 1939 | if (VDBG) loge("Exception trying to remove a route: " + e); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1940 | return false; |
| 1941 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1942 | } |
| 1943 | } |
| 1944 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1945 | } |
| 1946 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1947 | public void setDataDependency(int networkType, boolean met) { |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1948 | enforceConnectivityInternalPermission(); |
| 1949 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1950 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_DEPENDENCY_MET, |
| 1951 | (met ? ENABLED : DISABLED), networkType)); |
| 1952 | } |
| 1953 | |
| 1954 | private void handleSetDependencyMet(int networkType, boolean met) { |
| 1955 | if (mNetTrackers[networkType] != null) { |
| 1956 | if (DBG) { |
| 1957 | log("handleSetDependencyMet(" + networkType + ", " + met + ")"); |
| 1958 | } |
| 1959 | mNetTrackers[networkType].setDependencyMet(met); |
| 1960 | } |
| 1961 | } |
| 1962 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1963 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1964 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1965 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1966 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1967 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1968 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1969 | } |
| 1970 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1971 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1972 | // skip update when we've already applied rules |
| 1973 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1974 | if (oldRules == uidRules) return; |
| 1975 | |
| 1976 | mUidRules.put(uid, uidRules); |
| 1977 | } |
| 1978 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1979 | // TODO: notify UID when it has requested targeted updates |
| 1980 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1981 | |
| 1982 | @Override |
| 1983 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1984 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1985 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1986 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | synchronized (mRulesLock) { |
| 1990 | mMeteredIfaces.clear(); |
| 1991 | for (String iface : meteredIfaces) { |
| 1992 | mMeteredIfaces.add(iface); |
| 1993 | } |
| 1994 | } |
| 1995 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1996 | |
| 1997 | @Override |
| 1998 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1999 | // caller is NPMS, since we only register with them |
| 2000 | if (LOGD_RULES) { |
| 2001 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 2002 | } |
| 2003 | |
| 2004 | // kick off connectivity change broadcast for active network, since |
| 2005 | // global background policy change is radical. |
| 2006 | final int networkType = mActiveDefaultNetwork; |
| 2007 | if (isNetworkTypeValid(networkType)) { |
| 2008 | final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 2009 | if (tracker != null) { |
| 2010 | final NetworkInfo info = tracker.getNetworkInfo(); |
| 2011 | if (info != null && info.isConnected()) { |
| 2012 | sendConnectedBroadcast(info); |
| 2013 | } |
| 2014 | } |
| 2015 | } |
| 2016 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 2017 | }; |
| 2018 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 2019 | @Override |
| 2020 | public void setPolicyDataEnable(int networkType, boolean enabled) { |
| 2021 | // only someone like NPMS should only be calling us |
| 2022 | mContext.enforceCallingOrSelfPermission(MANAGE_NETWORK_POLICY, TAG); |
| 2023 | |
| 2024 | mHandler.sendMessage(mHandler.obtainMessage( |
| 2025 | EVENT_SET_POLICY_DATA_ENABLE, networkType, (enabled ? ENABLED : DISABLED))); |
| 2026 | } |
| 2027 | |
| 2028 | private void handleSetPolicyDataEnable(int networkType, boolean enabled) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2029 | // TODO - handle this passing to factories |
| 2030 | // if (isNetworkTypeValid(networkType)) { |
| 2031 | // final NetworkStateTracker tracker = mNetTrackers[networkType]; |
| 2032 | // if (tracker != null) { |
| 2033 | // tracker.setPolicyDataEnable(enabled); |
| 2034 | // } |
| 2035 | // } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2036 | } |
| 2037 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2038 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2039 | mContext.enforceCallingOrSelfPermission( |
| 2040 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 2041 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2042 | } |
| 2043 | |
| 2044 | private void enforceChangePermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2045 | mContext.enforceCallingOrSelfPermission( |
| 2046 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 2047 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | } |
| 2049 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2050 | // TODO Make this a special check when it goes public |
| 2051 | private void enforceTetherChangePermission() { |
| 2052 | mContext.enforceCallingOrSelfPermission( |
| 2053 | android.Manifest.permission.CHANGE_NETWORK_STATE, |
| 2054 | "ConnectivityService"); |
| 2055 | } |
| 2056 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2057 | private void enforceTetherAccessPermission() { |
| 2058 | mContext.enforceCallingOrSelfPermission( |
| 2059 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 2060 | "ConnectivityService"); |
| 2061 | } |
| 2062 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2063 | private void enforceConnectivityInternalPermission() { |
| 2064 | mContext.enforceCallingOrSelfPermission( |
| 2065 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 2066 | "ConnectivityService"); |
| 2067 | } |
| 2068 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 2069 | private void enforceMarkNetworkSocketPermission() { |
| 2070 | //Media server special case |
| 2071 | if (Binder.getCallingUid() == Process.MEDIA_UID) { |
| 2072 | return; |
| 2073 | } |
| 2074 | mContext.enforceCallingOrSelfPermission( |
| 2075 | android.Manifest.permission.MARK_NETWORK_SOCKET, |
| 2076 | "ConnectivityService"); |
| 2077 | } |
| 2078 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | /** |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2080 | * Handle a {@code DISCONNECTED} event. If this pertains to the non-active |
| 2081 | * network, we ignore it. If it is for the active network, we send out a |
| 2082 | * broadcast. But first, we check whether it might be possible to connect |
| 2083 | * to a different network. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2084 | * @param info the {@code NetworkInfo} for the network |
| 2085 | */ |
| 2086 | private void handleDisconnect(NetworkInfo info) { |
| 2087 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2088 | int prevNetType = info.getType(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2089 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2090 | mNetTrackers[prevNetType].setTeardownRequested(false); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2091 | int thisNetId = mNetTrackers[prevNetType].getNetwork().netId; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2092 | |
| 2093 | // Remove idletimer previously setup in {@code handleConnect} |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2094 | // Already in place in new function. This is dead code. |
| 2095 | // if (mNetConfigs[prevNetType].isDefault()) { |
| 2096 | // removeDataActivityTracking(prevNetType); |
| 2097 | // } |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2098 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2099 | /* |
| 2100 | * If the disconnected network is not the active one, then don't report |
| 2101 | * this as a loss of connectivity. What probably happened is that we're |
| 2102 | * getting the disconnect for a network that we explicitly disabled |
| 2103 | * in accordance with network preference policies. |
| 2104 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2105 | if (!mNetConfigs[prevNetType].isDefault()) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2106 | List<Integer> pids = mNetRequestersPids[prevNetType]; |
| 2107 | for (Integer pid : pids) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2108 | // will remove them because the net's no longer connected |
| 2109 | // need to do this now as only now do we know the pids and |
| 2110 | // can properly null things that are no longer referenced. |
| 2111 | reassessPidDns(pid.intValue(), false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2112 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2113 | } |
| 2114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2115 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2116 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2117 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | if (info.isFailover()) { |
| 2119 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2120 | info.setFailover(false); |
| 2121 | } |
| 2122 | if (info.getReason() != null) { |
| 2123 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2124 | } |
| 2125 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2126 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2127 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2128 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2129 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2130 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2131 | tryFailover(prevNetType); |
| 2132 | if (mActiveDefaultNetwork != -1) { |
| 2133 | NetworkInfo switchTo = mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(); |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2134 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, switchTo); |
| 2135 | } else { |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2136 | mDefaultInetConditionPublished = 0; // we're not connected anymore |
Robert Greenwalt | cc4b401 | 2010-01-25 17:54:29 -0800 | [diff] [blame] | 2137 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 2138 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2139 | } |
Robert Greenwalt | 029be81 | 2010-09-20 18:01:43 -0700 | [diff] [blame] | 2140 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2141 | |
| 2142 | // Reset interface if no other connections are using the same interface |
| 2143 | boolean doReset = true; |
| 2144 | LinkProperties linkProperties = mNetTrackers[prevNetType].getLinkProperties(); |
| 2145 | if (linkProperties != null) { |
| 2146 | String oldIface = linkProperties.getInterfaceName(); |
| 2147 | if (TextUtils.isEmpty(oldIface) == false) { |
| 2148 | for (NetworkStateTracker networkStateTracker : mNetTrackers) { |
| 2149 | if (networkStateTracker == null) continue; |
| 2150 | NetworkInfo networkInfo = networkStateTracker.getNetworkInfo(); |
| 2151 | if (networkInfo.isConnected() && networkInfo.getType() != prevNetType) { |
| 2152 | LinkProperties l = networkStateTracker.getLinkProperties(); |
| 2153 | if (l == null) continue; |
| 2154 | if (oldIface.equals(l.getInterfaceName())) { |
| 2155 | doReset = false; |
| 2156 | break; |
| 2157 | } |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | } |
| 2162 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2163 | // do this before we broadcast the change |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2164 | // Already done in new function. This is dead code. |
| 2165 | // handleConnectivityChange(prevNetType, doReset); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2166 | |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2167 | final Intent immediateIntent = new Intent(intent); |
| 2168 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 2169 | sendStickyBroadcast(immediateIntent); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2170 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2171 | /* |
| 2172 | * If the failover network is already connected, then immediately send |
| 2173 | * out a followup broadcast indicating successful failover |
| 2174 | */ |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2175 | if (mActiveDefaultNetwork != -1) { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2176 | sendConnectedBroadcastDelayed(mNetTrackers[mActiveDefaultNetwork].getNetworkInfo(), |
| 2177 | getConnectivityChangeDelay()); |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2178 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2179 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2180 | // mNetd.removeNetwork(thisNetId); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2181 | } catch (Exception e) { |
| 2182 | loge("Exception removing network: " + e); |
| 2183 | } finally { |
| 2184 | mNetTrackers[prevNetType].setNetId(INVALID_NET_ID); |
| 2185 | } |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2186 | } |
| 2187 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2188 | private void tryFailover(int prevNetType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2189 | /* |
Robert Greenwalt | bff9018 | 2011-01-06 15:41:07 -0800 | [diff] [blame] | 2190 | * If this is a default network, check if other defaults are available. |
| 2191 | * Try to reconnect on all available and let them hash it out when |
| 2192 | * more than one connects. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2193 | */ |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2194 | if (mNetConfigs[prevNetType].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2195 | if (mActiveDefaultNetwork == prevNetType) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2196 | if (DBG) { |
| 2197 | log("tryFailover: set mActiveDefaultNetwork=-1, prevNetType=" + prevNetType); |
| 2198 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2199 | mActiveDefaultNetwork = -1; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2200 | try { |
| 2201 | mNetd.clearDefaultNetId(); |
| 2202 | } catch (Exception e) { |
| 2203 | loge("Exception clearing default network :" + e); |
| 2204 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2205 | } |
| 2206 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2207 | // don't signal a reconnect for anything lower or equal priority than our |
| 2208 | // current connected default |
| 2209 | // TODO - don't filter by priority now - nice optimization but risky |
| 2210 | // int currentPriority = -1; |
| 2211 | // if (mActiveDefaultNetwork != -1) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2212 | // currentPriority = mNetConfigs[mActiveDefaultNetwork].mPriority; |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2213 | // } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2214 | |
Robert Greenwalt | da03c4e | 2010-01-20 19:29:41 -0800 | [diff] [blame] | 2215 | for (int checkType=0; checkType <= ConnectivityManager.MAX_NETWORK_TYPE; checkType++) { |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 2216 | if (checkType == prevNetType) continue; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2217 | if (mNetConfigs[checkType] == null) continue; |
| 2218 | if (!mNetConfigs[checkType].isDefault()) continue; |
Robert Greenwalt | 424781e | 2011-11-11 09:56:03 -0800 | [diff] [blame] | 2219 | if (mNetTrackers[checkType] == null) continue; |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2220 | |
| 2221 | // Enabling the isAvailable() optimization caused mobile to not get |
| 2222 | // selected if it was in the middle of error handling. Specifically |
| 2223 | // a moble connection that took 30 seconds to complete the DEACTIVATE_DATA_CALL |
| 2224 | // would not be available and we wouldn't get connected to anything. |
| 2225 | // So removing the isAvailable() optimization below for now. TODO: This |
| 2226 | // optimization should work and we need to investigate why it doesn't work. |
| 2227 | // This could be related to how DEACTIVATE_DATA_CALL is reporting its |
| 2228 | // complete before it is really complete. |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2229 | |
Wink Saville | 9f7a0b2 | 2011-01-26 15:43:49 -0800 | [diff] [blame] | 2230 | // if (!mNetTrackers[checkType].isAvailable()) continue; |
| 2231 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2232 | // if (currentPriority >= mNetConfigs[checkType].mPriority) continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2233 | |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2234 | NetworkStateTracker checkTracker = mNetTrackers[checkType]; |
| 2235 | NetworkInfo checkInfo = checkTracker.getNetworkInfo(); |
| 2236 | if (!checkInfo.isConnectedOrConnecting() || checkTracker.isTeardownRequested()) { |
| 2237 | checkInfo.setFailover(true); |
| 2238 | checkTracker.reconnect(); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2239 | } |
Robert Greenwalt | f21ef7d | 2011-01-11 13:56:33 -0800 | [diff] [blame] | 2240 | if (DBG) log("Attempting to switch to " + checkInfo.getTypeName()); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2242 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | } |
| 2244 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2245 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2246 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2247 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2248 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2251 | private void sendConnectedBroadcastDelayed(NetworkInfo info, int delayMs) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2252 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION_IMMEDIATE); |
| 2253 | sendGeneralBroadcastDelayed(info, CONNECTIVITY_ACTION, delayMs); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2256 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 2257 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 2258 | } |
| 2259 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2260 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2261 | if (mLockdownTracker != null) { |
| 2262 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 2263 | } |
| 2264 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2265 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2266 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2267 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2268 | if (info.isFailover()) { |
| 2269 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2270 | info.setFailover(false); |
| 2271 | } |
| 2272 | if (info.getReason() != null) { |
| 2273 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2274 | } |
| 2275 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2276 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2277 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2278 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2279 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2280 | return intent; |
| 2281 | } |
| 2282 | |
| 2283 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 2284 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 2285 | } |
| 2286 | |
| 2287 | private void sendGeneralBroadcastDelayed(NetworkInfo info, String bcastType, int delayMs) { |
| 2288 | sendStickyBroadcastDelayed(makeGeneralIntent(info, bcastType), delayMs); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | } |
| 2290 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2291 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2292 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 2293 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 2294 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2295 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2296 | final long ident = Binder.clearCallingIdentity(); |
| 2297 | try { |
| 2298 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 2299 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2300 | } finally { |
| 2301 | Binder.restoreCallingIdentity(ident); |
| 2302 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2303 | } |
| 2304 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2305 | private void sendStickyBroadcast(Intent intent) { |
| 2306 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2307 | if (!mSystemReady) { |
| 2308 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2309 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2310 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2311 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2312 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2313 | } |
| 2314 | |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2315 | final long ident = Binder.clearCallingIdentity(); |
| 2316 | try { |
| 2317 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 2318 | } finally { |
| 2319 | Binder.restoreCallingIdentity(ident); |
| 2320 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2321 | } |
| 2322 | } |
| 2323 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2324 | private void sendStickyBroadcastDelayed(Intent intent, int delayMs) { |
| 2325 | if (delayMs <= 0) { |
| 2326 | sendStickyBroadcast(intent); |
| 2327 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 2328 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2329 | log("sendStickyBroadcastDelayed: delayMs=" + delayMs + ", action=" |
| 2330 | + intent.getAction()); |
| 2331 | } |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2332 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 2333 | EVENT_SEND_STICKY_BROADCAST_INTENT, intent), delayMs); |
| 2334 | } |
| 2335 | } |
| 2336 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2337 | void systemReady() { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2338 | mCaptivePortalTracker = CaptivePortalTracker.makeCaptivePortalTracker(mContext, this); |
| 2339 | loadGlobalProxy(); |
| 2340 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2341 | synchronized(this) { |
| 2342 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2343 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2344 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2345 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2346 | } |
| 2347 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2348 | // load the global proxy at startup |
| 2349 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2350 | |
| 2351 | // Try bringing up tracker, but if KeyStore isn't ready yet, wait |
| 2352 | // for user to unlock device. |
| 2353 | if (!updateLockdownVpn()) { |
| 2354 | final IntentFilter filter = new IntentFilter(Intent.ACTION_USER_PRESENT); |
| 2355 | mContext.registerReceiver(mUserPresentReceiver, filter); |
| 2356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2357 | } |
| 2358 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2359 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 2360 | @Override |
| 2361 | public void onReceive(Context context, Intent intent) { |
| 2362 | // Try creating lockdown tracker, since user present usually means |
| 2363 | // unlocked keystore. |
| 2364 | if (updateLockdownVpn()) { |
| 2365 | mContext.unregisterReceiver(this); |
| 2366 | } |
| 2367 | } |
| 2368 | }; |
| 2369 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2370 | private boolean isNewNetTypePreferredOverCurrentNetType(int type) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2371 | if (((type != mNetworkPreference) |
| 2372 | && (mNetConfigs[mActiveDefaultNetwork].priority > mNetConfigs[type].priority)) |
| 2373 | || (mNetworkPreference == mActiveDefaultNetwork)) { |
| 2374 | return false; |
| 2375 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2376 | return true; |
| 2377 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2378 | |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2379 | private void handleConnect(NetworkInfo info) { |
| 2380 | final int newNetType = info.getType(); |
| 2381 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2382 | // snapshot isFailover, because sendConnectedBroadcast() resets it |
| 2383 | boolean isFailover = info.isFailover(); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2384 | final NetworkStateTracker thisNet = mNetTrackers[newNetType]; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2385 | final String thisIface = thisNet.getLinkProperties().getInterfaceName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2386 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2387 | if (VDBG) { |
| 2388 | log("handleConnect: E newNetType=" + newNetType + " thisIface=" + thisIface |
| 2389 | + " isFailover" + isFailover); |
| 2390 | } |
| 2391 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2392 | // if this is a default net and other default is running |
| 2393 | // kill the one not preferred |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2394 | if (mNetConfigs[newNetType].isDefault()) { |
| 2395 | if (mActiveDefaultNetwork != -1 && mActiveDefaultNetwork != newNetType) { |
| 2396 | if (isNewNetTypePreferredOverCurrentNetType(newNetType)) { |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2397 | String teardownPolicy = SystemProperties.get("net.teardownPolicy"); |
| 2398 | if (TextUtils.equals(teardownPolicy, "keep") == false) { |
| 2399 | // tear down the other |
| 2400 | NetworkStateTracker otherNet = |
| 2401 | mNetTrackers[mActiveDefaultNetwork]; |
| 2402 | if (DBG) { |
| 2403 | log("Policy requires " + otherNet.getNetworkInfo().getTypeName() + |
| 2404 | " teardown"); |
| 2405 | } |
| 2406 | if (!teardown(otherNet)) { |
| 2407 | loge("Network declined teardown request"); |
| 2408 | teardown(thisNet); |
| 2409 | return; |
| 2410 | } |
| 2411 | } else { |
| 2412 | //TODO - remove |
| 2413 | loge("network teardown skipped due to net.teardownPolicy setting"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2414 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2415 | } else { |
| 2416 | // don't accept this one |
| 2417 | if (VDBG) { |
| 2418 | log("Not broadcasting CONNECT_ACTION " + |
| 2419 | "to torn down network " + info.getTypeName()); |
| 2420 | } |
| 2421 | teardown(thisNet); |
| 2422 | return; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2423 | } |
| 2424 | } |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2425 | int thisNetId = nextNetId(); |
| 2426 | thisNet.setNetId(thisNetId); |
| 2427 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2428 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2429 | } catch (Exception e) { |
| 2430 | loge("Exception creating network :" + e); |
| 2431 | teardown(thisNet); |
| 2432 | return; |
| 2433 | } |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2434 | // Already in place in new function. This is dead code. |
| 2435 | // setupDataActivityTracking(newNetType); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2436 | synchronized (ConnectivityService.this) { |
| 2437 | // have a new default network, release the transition wakelock in a second |
| 2438 | // if it's held. The second pause is to allow apps to reconnect over the |
| 2439 | // new network |
| 2440 | if (mNetTransitionWakeLock.isHeld()) { |
| 2441 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 2442 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2443 | mNetTransitionWakeLockSerialNumber, 0), |
| 2444 | 1000); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2445 | } |
| 2446 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2447 | mActiveDefaultNetwork = newNetType; |
Sreeram Ramachandran | 79a1058 | 2014-04-15 16:35:19 -0700 | [diff] [blame] | 2448 | try { |
| 2449 | mNetd.setDefaultNetId(thisNetId); |
| 2450 | } catch (Exception e) { |
| 2451 | loge("Exception setting default network :" + e); |
| 2452 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2453 | // this will cause us to come up initially as unconnected and switching |
| 2454 | // to connected after our normal pause unless somebody reports us as reall |
| 2455 | // disconnected |
| 2456 | mDefaultInetConditionPublished = 0; |
| 2457 | mDefaultConnectionSequence++; |
| 2458 | mInetConditionChangeInFlight = false; |
| 2459 | // Don't do this - if we never sign in stay, grey |
| 2460 | //reportNetworkCondition(mActiveDefaultNetwork, 100); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 2461 | updateNetworkSettings(thisNet); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2462 | } else { |
| 2463 | int thisNetId = nextNetId(); |
| 2464 | thisNet.setNetId(thisNetId); |
| 2465 | try { |
Robert Greenwalt | 9876e63 | 2014-05-13 06:32:53 -0700 | [diff] [blame] | 2466 | // mNetd.createNetwork(thisNetId, thisIface); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 2467 | } catch (Exception e) { |
| 2468 | loge("Exception creating network :" + e); |
| 2469 | teardown(thisNet); |
| 2470 | return; |
| 2471 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2472 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2473 | thisNet.setTeardownRequested(false); |
Lorenzo Colitti | 7ef9257 | 2014-05-14 01:42:29 -0700 | [diff] [blame] | 2474 | // Already in place in new function. This is dead code. |
| 2475 | // updateMtuSizeSettings(thisNet); |
| 2476 | // handleConnectivityChange(newNetType, false); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2477 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2478 | |
| 2479 | // notify battery stats service about this network |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 2480 | if (thisIface != null) { |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2481 | try { |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2482 | BatteryStatsService.getService().noteNetworkInterfaceType(thisIface, newNetType); |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 2483 | } catch (RemoteException e) { |
| 2484 | // ignored; service lives in system_server |
| 2485 | } |
| 2486 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2487 | } |
| 2488 | |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2489 | /** @hide */ |
| 2490 | @Override |
| 2491 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 2492 | enforceConnectivityInternalPermission(); |
| 2493 | if (DBG) log("captivePortalCheckCompleted: ni=" + info + " captive=" + isCaptivePortal); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2494 | // mNetTrackers[info.getType()].captivePortalCheckCompleted(isCaptivePortal); |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2497 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2498 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2499 | * |
| 2500 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2501 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2502 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2503 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2504 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2505 | |
| 2506 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2507 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2508 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2509 | if (networkAgent.networkCapabilities.hasTransport( |
| 2510 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2511 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2512 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Dianne Hackborn | e13c4c0 | 2014-02-11 17:18:35 -0800 | [diff] [blame] | 2513 | 5); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2514 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2515 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 2516 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2517 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2518 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2519 | 0); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2520 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2521 | } else { |
| 2522 | // do not track any other networks |
| 2523 | timeout = 0; |
| 2524 | } |
| 2525 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2526 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2527 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2528 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2529 | } catch (Exception e) { |
| 2530 | // You shall not crash! |
| 2531 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2532 | } |
| 2533 | } |
| 2534 | } |
| 2535 | |
| 2536 | /** |
| 2537 | * Remove data activity tracking when network disconnects. |
| 2538 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2539 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2540 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 2541 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2542 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2543 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 2544 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2545 | try { |
| 2546 | // the call fails silently if no idletimer setup for this interface |
| 2547 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2548 | } catch (Exception e) { |
| 2549 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2550 | } |
| 2551 | } |
| 2552 | } |
| 2553 | |
| 2554 | /** |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2555 | * After a change in the connectivity state of a network. We're mainly |
| 2556 | * concerned with making sure that the list of DNS servers is set up |
| 2557 | * according to which networks are connected, and ensuring that the |
| 2558 | * right routing table entries exist. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2559 | * |
| 2560 | * TODO - delete when we're sure all this functionallity is captured. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2561 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2562 | private void handleConnectivityChange(int netType, LinkProperties curLp, boolean doReset) { |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2563 | int resetMask = doReset ? NetworkUtils.RESET_ALL_ADDRESSES : 0; |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2564 | boolean exempt = ConnectivityManager.isNetworkTypeExempt(netType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2565 | if (VDBG) { |
| 2566 | log("handleConnectivityChange: netType=" + netType + " doReset=" + doReset |
| 2567 | + " resetMask=" + resetMask); |
| 2568 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2569 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2570 | /* |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2571 | * If a non-default network is enabled, add the host routes that |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2572 | * will allow it's DNS servers to be accessed. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2573 | */ |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2574 | handleDnsConfigurationChange(netType); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2575 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2576 | LinkProperties newLp = null; |
| 2577 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2578 | if (mNetTrackers[netType].getNetworkInfo().isConnected()) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2579 | newLp = mNetTrackers[netType].getLinkProperties(); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2580 | if (VDBG) { |
| 2581 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2582 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2583 | "\n curLp=" + curLp + |
| 2584 | "\n newLp=" + newLp); |
| 2585 | } |
| 2586 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2587 | if (curLp != null) { |
| 2588 | if (curLp.isIdenticalInterfaceName(newLp)) { |
| 2589 | CompareResult<LinkAddress> car = curLp.compareAddresses(newLp); |
| 2590 | if ((car.removed.size() != 0) || (car.added.size() != 0)) { |
| 2591 | for (LinkAddress linkAddr : car.removed) { |
| 2592 | if (linkAddr.getAddress() instanceof Inet4Address) { |
| 2593 | resetMask |= NetworkUtils.RESET_IPV4_ADDRESSES; |
| 2594 | } |
| 2595 | if (linkAddr.getAddress() instanceof Inet6Address) { |
| 2596 | resetMask |= NetworkUtils.RESET_IPV6_ADDRESSES; |
| 2597 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2598 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2599 | if (DBG) { |
| 2600 | log("handleConnectivityChange: addresses changed" + |
| 2601 | " linkProperty[" + netType + "]:" + " resetMask=" + resetMask + |
| 2602 | "\n car=" + car); |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2603 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2604 | } else { |
Robert Greenwalt | d01f842 | 2014-01-23 16:03:04 -0800 | [diff] [blame] | 2605 | if (VDBG) { |
| 2606 | log("handleConnectivityChange: addresses are the same reset per" + |
| 2607 | " doReset linkProperty[" + netType + "]:" + |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2608 | " resetMask=" + resetMask); |
| 2609 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2610 | } |
| 2611 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2612 | resetMask = NetworkUtils.RESET_ALL_ADDRESSES; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2613 | if (DBG) { |
| 2614 | log("handleConnectivityChange: interface not not equivalent reset both" + |
| 2615 | " linkProperty[" + netType + "]:" + |
| 2616 | " resetMask=" + resetMask); |
| 2617 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2618 | } |
Wink Saville | e822225 | 2011-07-13 13:44:13 -0700 | [diff] [blame] | 2619 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2620 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2621 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2622 | } |
| 2623 | } else { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2624 | if (VDBG) { |
| 2625 | log("handleConnectivityChange: changed linkProperty[" + netType + "]:" + |
| 2626 | " doReset=" + doReset + " resetMask=" + resetMask + |
| 2627 | "\n curLp=" + curLp + |
| 2628 | "\n newLp= null"); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2629 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2630 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2631 | mCurrentLinkProperties[netType] = newLp; |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2632 | boolean resetDns = updateRoutes(newLp, curLp, mNetConfigs[netType].isDefault(), exempt, |
| 2633 | mNetTrackers[netType].getNetwork().netId); |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2634 | |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2635 | if (resetMask != 0 || resetDns) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2636 | if (VDBG) log("handleConnectivityChange: resetting"); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2637 | if (curLp != null) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2638 | if (VDBG) log("handleConnectivityChange: resetting curLp=" + curLp); |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2639 | for (String iface : curLp.getAllInterfaceNames()) { |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2640 | if (TextUtils.isEmpty(iface) == false) { |
| 2641 | if (resetMask != 0) { |
| 2642 | if (DBG) log("resetConnections(" + iface + ", " + resetMask + ")"); |
| 2643 | NetworkUtils.resetConnections(iface, resetMask); |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2644 | |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2645 | // Tell VPN the interface is down. It is a temporary |
| 2646 | // but effective fix to make VPN aware of the change. |
| 2647 | if ((resetMask & NetworkUtils.RESET_IPV4_ADDRESSES) != 0) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 2648 | synchronized(mVpns) { |
| 2649 | for (int i = 0; i < mVpns.size(); i++) { |
| 2650 | mVpns.valueAt(i).interfaceStatusChanged(iface, false); |
| 2651 | } |
| 2652 | } |
Lorenzo Colitti | 4aa9bcf | 2013-03-20 19:22:58 +0900 | [diff] [blame] | 2653 | } |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 2654 | } |
Robert Greenwalt | 80eb63b | 2013-05-23 18:33:06 -0700 | [diff] [blame] | 2655 | } else { |
| 2656 | loge("Can't reset connection for type "+netType); |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2657 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2658 | } |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2659 | if (resetDns) { |
| 2660 | flushVmDnsCache(); |
| 2661 | if (VDBG) log("resetting DNS cache for type " + netType); |
| 2662 | try { |
| 2663 | mNetd.flushNetworkDnsCache(mNetTrackers[netType].getNetwork().netId); |
| 2664 | } catch (Exception e) { |
| 2665 | // never crash - catch them all |
| 2666 | if (DBG) loge("Exception resetting dns cache: " + e); |
| 2667 | } |
| 2668 | } |
Robert Greenwalt | ec896c6 | 2011-06-15 12:22:07 -0700 | [diff] [blame] | 2669 | } |
| 2670 | } |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2671 | |
| 2672 | // TODO: Temporary notifying upstread change to Tethering. |
| 2673 | // @see bug/4455071 |
| 2674 | /** Notify TetheringService if interface name has been changed. */ |
| 2675 | if (TextUtils.equals(mNetTrackers[netType].getNetworkInfo().getReason(), |
Wink Saville | a639b31 | 2012-07-10 12:37:54 -0700 | [diff] [blame] | 2676 | PhoneConstants.REASON_LINK_PROPERTIES_CHANGED)) { |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 2677 | if (isTetheringSupported()) { |
| 2678 | mTethering.handleTetherIfaceChange(); |
| 2679 | } |
| 2680 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2681 | } |
| 2682 | |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2683 | /** |
| 2684 | * Add and remove routes using the old properties (null if not previously connected), |
| 2685 | * new properties (null if becoming disconnected). May even be double null, which |
| 2686 | * is a noop. |
| 2687 | * Uses isLinkDefault to determine if default routes should be set or conversely if |
| 2688 | * host routes should be set to the dns servers |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2689 | * returns a boolean indicating the routes changed |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2690 | */ |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2691 | private boolean updateRoutes(LinkProperties newLp, LinkProperties curLp, |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2692 | boolean isLinkDefault, boolean exempt, int netId) { |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2693 | Collection<RouteInfo> routesToAdd = null; |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2694 | CompareResult<InetAddress> dnsDiff = new CompareResult<InetAddress>(); |
| 2695 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2696 | if (curLp != null) { |
| 2697 | // check for the delta between the current set and the new |
Lorenzo Colitti | d1e0fae | 2013-07-31 23:23:21 +0900 | [diff] [blame] | 2698 | routeDiff = curLp.compareAllRoutes(newLp); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2699 | dnsDiff = curLp.compareDnses(newLp); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2700 | } else if (newLp != null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 2701 | routeDiff.added = newLp.getAllRoutes(); |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 2702 | dnsDiff.added = newLp.getDnsServers(); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2703 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2704 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2705 | boolean routesChanged = (routeDiff.removed.size() != 0 || routeDiff.added.size() != 0); |
| 2706 | |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2707 | for (RouteInfo r : routeDiff.removed) { |
| 2708 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2709 | if (VDBG) log("updateRoutes: default remove route r=" + r); |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2710 | removeRoute(curLp, r, TO_DEFAULT_TABLE, netId); |
Robert Greenwalt | 3b28e9a | 2011-11-02 14:37:19 -0700 | [diff] [blame] | 2711 | } |
| 2712 | if (isLinkDefault == false) { |
| 2713 | // remove from a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2714 | removeRoute(curLp, r, TO_SECONDARY_TABLE, netId); |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 2715 | } |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 2716 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2717 | |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2718 | for (RouteInfo r : routeDiff.added) { |
| 2719 | if (isLinkDefault || ! r.isDefaultRoute()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2720 | addRoute(newLp, r, TO_DEFAULT_TABLE, exempt, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2721 | } else { |
| 2722 | // add to a secondary route table |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2723 | addRoute(newLp, r, TO_SECONDARY_TABLE, UNEXEMPT, netId); |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2724 | |
| 2725 | // many radios add a default route even when we don't want one. |
| 2726 | // remove the default route unless somebody else has asked for it |
| 2727 | String ifaceName = newLp.getInterfaceName(); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2728 | synchronized (mRoutesLock) { |
| 2729 | if (!TextUtils.isEmpty(ifaceName) && !mAddedRoutes.contains(r)) { |
| 2730 | if (VDBG) log("Removing " + r + " for interface " + ifaceName); |
| 2731 | try { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 2732 | mNetd.removeRoute(netId, r); |
Chad Brubaker | f336d72 | 2013-07-15 16:34:04 -0700 | [diff] [blame] | 2733 | } catch (Exception e) { |
| 2734 | // never crash - catch them all |
| 2735 | if (DBG) loge("Exception trying to remove a route: " + e); |
| 2736 | } |
Robert Greenwalt | 979ee3c | 2013-04-05 16:49:32 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | } |
| 2740 | } |
| 2741 | |
Robert Greenwalt | f125a09 | 2011-08-15 12:31:55 -0700 | [diff] [blame] | 2742 | return routesChanged; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2743 | } |
| 2744 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2745 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2746 | * Reads the network specific MTU size from reources. |
| 2747 | * and set it on it's iface. |
| 2748 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2749 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 2750 | final String iface = newLp.getInterfaceName(); |
| 2751 | final int mtu = newLp.getMtu(); |
| 2752 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 2753 | if (VDBG) log("identical MTU - not setting"); |
| 2754 | return; |
| 2755 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2756 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2757 | if (mtu < 68 || mtu > 10000) { |
| 2758 | loge("Unexpected mtu value: " + mtu + ", " + iface); |
| 2759 | return; |
| 2760 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2761 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2762 | try { |
| 2763 | if (VDBG) log("Setting MTU size: " + iface + ", " + mtu); |
| 2764 | mNetd.setMtu(iface, mtu); |
| 2765 | } catch (Exception e) { |
| 2766 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2767 | } |
| 2768 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2769 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2770 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2771 | * Reads the network specific TCP buffer sizes from SystemProperties |
| 2772 | * net.tcp.buffersize.[default|wifi|umts|edge|gprs] and set them for system |
| 2773 | * wide use |
| 2774 | */ |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2775 | private void updateNetworkSettings(NetworkStateTracker nt) { |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2776 | String key = nt.getTcpBufferSizesPropName(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2777 | String bufferSizes = key == null ? null : SystemProperties.get(key); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2778 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 2779 | if (TextUtils.isEmpty(bufferSizes)) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2780 | if (VDBG) log(key + " not found in system properties. Using defaults"); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2781 | |
| 2782 | // Setting to default values so we won't be stuck to previous values |
| 2783 | key = "net.tcp.buffersize.default"; |
| 2784 | bufferSizes = SystemProperties.get(key); |
| 2785 | } |
| 2786 | |
| 2787 | // Set values in kernel |
| 2788 | if (bufferSizes.length() != 0) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2789 | if (VDBG) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2790 | log("Setting TCP values: [" + bufferSizes |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2791 | + "] which comes from [" + key + "]"); |
| 2792 | } |
| 2793 | setBufferSize(bufferSizes); |
| 2794 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2795 | |
| 2796 | final String defaultRwndKey = "net.tcp.default_init_rwnd"; |
| 2797 | int defaultRwndValue = SystemProperties.getInt(defaultRwndKey, 0); |
| 2798 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
| 2799 | Settings.Global.TCP_DEFAULT_INIT_RWND, defaultRwndValue); |
| 2800 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2801 | if (rwndValue != 0) { |
| 2802 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 2803 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2806 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2807 | * Writes TCP buffer sizes to /sys/kernel/ipv4/tcp_[r/w]mem_[min/def/max] |
| 2808 | * which maps to /proc/sys/net/ipv4/tcp_rmem and tcpwmem |
| 2809 | * |
| 2810 | * @param bufferSizes in the format of "readMin, readInitial, readMax, |
| 2811 | * writeMin, writeInitial, writeMax" |
| 2812 | */ |
| 2813 | private void setBufferSize(String bufferSizes) { |
| 2814 | try { |
| 2815 | String[] values = bufferSizes.split(","); |
| 2816 | |
| 2817 | if (values.length == 6) { |
| 2818 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 2819 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 2820 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 2821 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 2822 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 2823 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 2824 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2825 | } else { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2826 | loge("Invalid buffersize string: " + bufferSizes); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2827 | } |
| 2828 | } catch (IOException e) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 2829 | loge("Can't set tcp buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } |
| 2832 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2833 | /** |
| 2834 | * Adjust the per-process dns entries (net.dns<x>.<pid>) based |
| 2835 | * on the highest priority active net which this process requested. |
| 2836 | * If there aren't any, clear it out |
| 2837 | */ |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2838 | private void reassessPidDns(int pid, boolean doBump) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2839 | { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2840 | if (VDBG) log("reassessPidDns for pid " + pid); |
| 2841 | Integer myPid = new Integer(pid); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2842 | for(int i : mPriorityList) { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2843 | if (mNetConfigs[i].isDefault()) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2844 | continue; |
| 2845 | } |
| 2846 | NetworkStateTracker nt = mNetTrackers[i]; |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2847 | if (nt.getNetworkInfo().isConnected() && |
| 2848 | !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2849 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 2850 | if (p == null) continue; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2851 | if (mNetRequestersPids[i].contains(myPid)) { |
| 2852 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2853 | // TODO: Reimplement this via local variable in bionic. |
| 2854 | // mNetd.setDnsNetworkForPid(nt.getNetwork().netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2855 | } catch (Exception e) { |
| 2856 | Slog.e(TAG, "exception reasseses pid dns: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2857 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2858 | return; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2859 | } |
| 2860 | } |
| 2861 | } |
| 2862 | // nothing found - delete |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2863 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2864 | // TODO: Reimplement this via local variable in bionic. |
| 2865 | // mNetd.clearDnsNetworkForPid(pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2866 | } catch (Exception e) { |
| 2867 | Slog.e(TAG, "exception clear interface from pid: " + e); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2868 | } |
| 2869 | } |
| 2870 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2871 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 2872 | /* |
| 2873 | * Tell the VMs to toss their DNS caches |
| 2874 | */ |
| 2875 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 2876 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 2877 | /* |
| 2878 | * Connectivity events can happen before boot has completed ... |
| 2879 | */ |
| 2880 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2881 | final long ident = Binder.clearCallingIdentity(); |
| 2882 | try { |
| 2883 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 2884 | } finally { |
| 2885 | Binder.restoreCallingIdentity(ident); |
| 2886 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2889 | // Caller must grab mDnsLock. |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2890 | private void updateDnsLocked(String network, int netId, |
| 2891 | Collection<InetAddress> dnses, String domains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2892 | int last = 0; |
| 2893 | if (dnses.size() == 0 && mDefaultDns != null) { |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2894 | dnses = new ArrayList(); |
| 2895 | dnses.add(mDefaultDns); |
| 2896 | if (DBG) { |
| 2897 | loge("no dns provided for " + network + " - using " + mDefaultDns.getHostAddress()); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2898 | } |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2899 | } |
| 2900 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2901 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2902 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), domains); |
Chad Brubaker | 20a7e88 | 2013-07-23 17:13:36 -0700 | [diff] [blame] | 2903 | |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 2904 | for (InetAddress dns : dnses) { |
| 2905 | ++last; |
| 2906 | String key = "net.dns" + last; |
| 2907 | String value = dns.getHostAddress(); |
| 2908 | SystemProperties.set(key, value); |
| 2909 | } |
| 2910 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 2911 | String key = "net.dns" + i; |
| 2912 | SystemProperties.set(key, ""); |
| 2913 | } |
| 2914 | mNumDnsEntries = last; |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2915 | } catch (Exception e) { |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 2916 | loge("exception setting default dns interface: " + e); |
Robert Greenwalt | 63837f4 | 2013-01-19 00:34:07 +0000 | [diff] [blame] | 2917 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2918 | } |
| 2919 | |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2920 | private void handleDnsConfigurationChange(int netType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2921 | // add default net's dns entries |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2922 | NetworkStateTracker nt = mNetTrackers[netType]; |
| 2923 | if (nt != null && nt.getNetworkInfo().isConnected() && !nt.isTeardownRequested()) { |
Robert Greenwalt | 37e65eb | 2010-08-30 10:56:47 -0700 | [diff] [blame] | 2924 | LinkProperties p = nt.getLinkProperties(); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2925 | if (p == null) return; |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 2926 | Collection<InetAddress> dnses = p.getDnsServers(); |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2927 | int netId = nt.getNetwork().netId; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2928 | if (mNetConfigs[netType].isDefault()) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 2929 | String network = nt.getNetworkInfo().getTypeName(); |
| 2930 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2931 | updateDnsLocked(network, netId, dnses, p.getDomains()); |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2932 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2933 | } else { |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2934 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2935 | mNetd.setDnsServersForNetwork(netId, |
Robert Greenwalt | 8058f62 | 2012-11-09 10:52:27 -0800 | [diff] [blame] | 2936 | NetworkUtils.makeStrings(dnses), p.getDomains()); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2937 | } catch (Exception e) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 2938 | if (DBG) loge("exception setting dns servers: " + e); |
Robert Greenwalt | a10b7fd | 2011-07-25 16:06:25 -0700 | [diff] [blame] | 2939 | } |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 2940 | // set per-pid dns for attached secondary nets |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2941 | List<Integer> pids = mNetRequestersPids[netType]; |
| 2942 | for (Integer pid : pids) { |
| 2943 | try { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 2944 | // TODO: Reimplement this via local variable in bionic. |
| 2945 | // mNetd.setDnsNetworkForPid(netId, pid); |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2946 | } catch (Exception e) { |
| 2947 | Slog.e(TAG, "exception setting interface for pid: " + e); |
| 2948 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2949 | } |
| 2950 | } |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 2951 | flushVmDnsCache(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2952 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2953 | } |
| 2954 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2955 | @Override |
| 2956 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2957 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 2958 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2959 | if(restoreDefaultNetworkDelayStr != null && |
| 2960 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2961 | try { |
| 2962 | return Integer.valueOf(restoreDefaultNetworkDelayStr); |
| 2963 | } catch (NumberFormatException e) { |
| 2964 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2965 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2966 | // if the system property isn't set, use the value for the apn type |
| 2967 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2968 | |
| 2969 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2970 | (mNetConfigs[networkType] != null)) { |
| 2971 | ret = mNetConfigs[networkType].restoreTime; |
| 2972 | } |
| 2973 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2977 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 2978 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2979 | if (mContext.checkCallingOrSelfPermission( |
| 2980 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2981 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2982 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 2983 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 2984 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2985 | return; |
| 2986 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2987 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2988 | pw.println("NetworkFactories for:"); |
| 2989 | pw.increaseIndent(); |
| 2990 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 2991 | pw.println(nfi.name); |
| 2992 | } |
| 2993 | pw.decreaseIndent(); |
| 2994 | pw.println(); |
| 2995 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2996 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 2997 | pw.print("Active default network: "); |
| 2998 | if (defaultNai == null) { |
| 2999 | pw.println("none"); |
| 3000 | } else { |
| 3001 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3002 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 3003 | pw.println(); |
| 3004 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3005 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3006 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3007 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 3008 | pw.println(nai.toString()); |
| 3009 | pw.increaseIndent(); |
| 3010 | pw.println("Requests:"); |
| 3011 | pw.increaseIndent(); |
| 3012 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3013 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3014 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3015 | pw.decreaseIndent(); |
| 3016 | pw.println("Lingered:"); |
| 3017 | pw.increaseIndent(); |
| 3018 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 3019 | pw.decreaseIndent(); |
| 3020 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3021 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3022 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3023 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3024 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3025 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3026 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 3027 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 3028 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 3029 | } |
| 3030 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3031 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3032 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3033 | synchronized (this) { |
| 3034 | pw.println("NetworkTranstionWakeLock is currently " + |
| 3035 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held."); |
| 3036 | pw.println("It was last requested for "+mNetTransitionWakeLockCausedBy); |
| 3037 | } |
| 3038 | pw.println(); |
| 3039 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3040 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3041 | |
| 3042 | if (mInetLog != null) { |
| 3043 | pw.println(); |
| 3044 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3045 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3046 | for(int i = 0; i < mInetLog.size(); i++) { |
| 3047 | pw.println(mInetLog.get(i)); |
| 3048 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 3049 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3050 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3051 | } |
| 3052 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 3053 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3054 | private class NetworkStateTrackerHandler extends Handler { |
| 3055 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 3056 | super(looper); |
| 3057 | } |
| 3058 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3059 | @Override |
| 3060 | public void handleMessage(Message msg) { |
| 3061 | NetworkInfo info; |
| 3062 | switch (msg.what) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3063 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3064 | handleAsyncChannelHalfConnect(msg); |
| 3065 | break; |
| 3066 | } |
| 3067 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 3068 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3069 | if (nai != null) nai.asyncChannel.disconnect(); |
| 3070 | break; |
| 3071 | } |
| 3072 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 3073 | handleAsyncChannelDisconnected(msg); |
| 3074 | break; |
| 3075 | } |
| 3076 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
| 3077 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3078 | if (nai == null) { |
| 3079 | loge("EVENT_NETWORK_CAPABILITIES_CHANGED from unknown NetworkAgent"); |
| 3080 | } else { |
| 3081 | updateCapabilities(nai, (NetworkCapabilities)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3082 | } |
| 3083 | break; |
| 3084 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3085 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
| 3086 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3087 | if (nai == null) { |
| 3088 | loge("NetworkAgent not found for EVENT_NETWORK_PROPERTIES_CHANGED"); |
| 3089 | } else { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3090 | if (VDBG) log("Update of Linkproperties for " + nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3091 | LinkProperties oldLp = nai.linkProperties; |
| 3092 | nai.linkProperties = (LinkProperties)msg.obj; |
| 3093 | updateLinkProperties(nai, oldLp); |
| 3094 | } |
| 3095 | break; |
| 3096 | } |
| 3097 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
| 3098 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3099 | if (nai == null) { |
| 3100 | loge("EVENT_NETWORK_INFO_CHANGED from unknown NetworkAgent"); |
| 3101 | break; |
| 3102 | } |
| 3103 | info = (NetworkInfo) msg.obj; |
| 3104 | updateNetworkInfo(nai, info); |
| 3105 | break; |
| 3106 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3107 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
| 3108 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3109 | if (nai == null) { |
| 3110 | loge("EVENT_NETWORK_SCORE_CHANGED from unknown NetworkAgent"); |
| 3111 | break; |
| 3112 | } |
| 3113 | Integer score = (Integer) msg.obj; |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 3114 | if (score != null) updateNetworkScore(nai, score.intValue()); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3115 | break; |
| 3116 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3117 | case NetworkMonitor.EVENT_NETWORK_VALIDATED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3118 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
| 3119 | handleConnectionValidated(nai); |
| 3120 | break; |
| 3121 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3122 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3123 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
| 3124 | handleLingerComplete(nai); |
| 3125 | break; |
| 3126 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3127 | case NetworkStateTracker.EVENT_STATE_CHANGED: { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3128 | info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3129 | NetworkInfo.State state = info.getState(); |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3130 | |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3131 | if (VDBG || (state == NetworkInfo.State.CONNECTED) || |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 3132 | (state == NetworkInfo.State.DISCONNECTED) || |
| 3133 | (state == NetworkInfo.State.SUSPENDED)) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3134 | log("ConnectivityChange for " + |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3135 | info.getTypeName() + ": " + |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3136 | state + "/" + info.getDetailedState()); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3137 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3138 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3139 | // Since mobile has the notion of a network/apn that can be used for |
| 3140 | // provisioning we need to check every time we're connected as |
| 3141 | // CaptiveProtalTracker won't detected it because DCT doesn't report it |
| 3142 | // as connected as ACTION_ANY_DATA_CONNECTION_STATE_CHANGED instead its |
| 3143 | // reported as ACTION_DATA_CONNECTION_CONNECTED_TO_PROVISIONING_APN. Which |
| 3144 | // is received by MDST and sent here as EVENT_STATE_CHANGED. |
| 3145 | if (ConnectivityManager.isNetworkTypeMobile(info.getType()) |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3146 | && (0 != Settings.Global.getInt(mContext.getContentResolver(), |
| 3147 | Settings.Global.DEVICE_PROVISIONED, 0)) |
Wink Saville | 127beff | 2013-09-14 09:04:53 -0700 | [diff] [blame] | 3148 | && (((state == NetworkInfo.State.CONNECTED) |
| 3149 | && (info.getType() == ConnectivityManager.TYPE_MOBILE)) |
| 3150 | || info.isConnectedToProvisioningNetwork())) { |
| 3151 | log("ConnectivityChange checkMobileProvisioning for" |
| 3152 | + " TYPE_MOBILE or ProvisioningNetwork"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3153 | checkMobileProvisioning(CheckMp.MAX_TIMEOUT_MS); |
Wink Saville | fde1ac9 | 2013-07-16 17:16:37 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
Jeff Sharkey | 2528b50 | 2012-11-09 15:57:02 -0800 | [diff] [blame] | 3156 | EventLogTags.writeConnectivityStateChanged( |
| 3157 | info.getType(), info.getSubtype(), info.getDetailedState().ordinal()); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 3158 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3159 | if (info.isConnectedToProvisioningNetwork()) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3160 | /** |
| 3161 | * TODO: Create ConnectivityManager.TYPE_MOBILE_PROVISIONING |
| 3162 | * for now its an in between network, its a network that |
| 3163 | * is actually a default network but we don't want it to be |
| 3164 | * announced as such to keep background applications from |
| 3165 | * trying to use it. It turns out that some still try so we |
| 3166 | * take the additional step of clearing any default routes |
| 3167 | * to the link that may have incorrectly setup by the lower |
| 3168 | * levels. |
| 3169 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3170 | LinkProperties lp = getLinkPropertiesForTypeInternal(info.getType()); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3171 | if (DBG) { |
| 3172 | log("EVENT_STATE_CHANGED: connected to provisioning network, lp=" + lp); |
| 3173 | } |
| 3174 | |
| 3175 | // Clear any default routes setup by the radio so |
| 3176 | // any activity by applications trying to use this |
| 3177 | // connection will fail until the provisioning network |
| 3178 | // is enabled. |
| 3179 | for (RouteInfo r : lp.getRoutes()) { |
Sreeram Ramachandran | b2829fa | 2014-04-15 19:07:12 -0700 | [diff] [blame] | 3180 | removeRoute(lp, r, TO_DEFAULT_TABLE, |
| 3181 | mNetTrackers[info.getType()].getNetwork().netId); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3182 | } |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3183 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3184 | } else if (state == NetworkInfo.State.SUSPENDED) { |
Robert Greenwalt | 511288a | 2009-12-07 11:33:18 -0800 | [diff] [blame] | 3185 | } else if (state == NetworkInfo.State.CONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3186 | // handleConnect(info); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3187 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3188 | if (mLockdownTracker != null) { |
| 3189 | mLockdownTracker.onNetworkInfoChanged(info); |
| 3190 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3191 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3192 | } |
| 3193 | case NetworkStateTracker.EVENT_CONFIGURATION_CHANGED: { |
Robert Greenwalt | c76b8fa | 2010-07-23 15:46:26 -0700 | [diff] [blame] | 3194 | info = (NetworkInfo) msg.obj; |
Kazuhiro Ondo | 1ccb10e | 2011-06-22 21:10:34 -0500 | [diff] [blame] | 3195 | // TODO: Temporary allowing network configuration |
| 3196 | // change not resetting sockets. |
| 3197 | // @see bug/4455071 |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3198 | handleConnectivityChange(info.getType(), mCurrentLinkProperties[info.getType()], |
| 3199 | false); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3200 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3201 | } |
| 3202 | case NetworkStateTracker.EVENT_NETWORK_SUBTYPE_CHANGED: { |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3203 | info = (NetworkInfo) msg.obj; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3204 | int type = info.getType(); |
Robert Greenwalt | d49f64c | 2014-02-28 16:44:00 -0800 | [diff] [blame] | 3205 | if (mNetConfigs[type].isDefault()) updateNetworkSettings(mNetTrackers[type]); |
Robert Greenwalt | d14e176 | 2012-08-20 11:15:39 -0700 | [diff] [blame] | 3206 | break; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3207 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | } |
| 3211 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3212 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 3213 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3214 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3215 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 3216 | if (VDBG) log("NetworkFactory connected"); |
| 3217 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3218 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3219 | if (nri.isRequest == false) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3220 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3221 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3222 | (nai != null ? nai.currentScore : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3223 | } |
| 3224 | } else { |
| 3225 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3226 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3227 | } |
| 3228 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 3229 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 3230 | if (VDBG) log("NetworkAgent connected"); |
| 3231 | // A network agent has requested a connection. Establish the connection. |
| 3232 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 3233 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 3234 | } else { |
| 3235 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3236 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3237 | if (nai != null) { |
| 3238 | mNetworkForNetId.remove(nai.network.netId); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3239 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3240 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | private void handleAsyncChannelDisconnected(Message msg) { |
| 3245 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 3246 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3247 | if (DBG) { |
| 3248 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 3249 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3250 | // A network agent has disconnected. |
| 3251 | // Tell netd to clean up the configuration for this network |
| 3252 | // (routing rules, DNS, etc). |
| 3253 | try { |
| 3254 | mNetd.removeNetwork(nai.network.netId); |
| 3255 | } catch (Exception e) { |
| 3256 | loge("Exception removing network: " + e); |
| 3257 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3258 | // TODO - if we move the logic to the network agent (have them disconnect |
| 3259 | // because they lost all their requests or because their score isn't good) |
| 3260 | // then they would disconnect organically, report their new state and then |
| 3261 | // disconnect the channel. |
| 3262 | if (nai.networkInfo.isConnected()) { |
| 3263 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 3264 | null, null); |
| 3265 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3266 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3267 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 3268 | mNetworkAgentInfos.remove(msg.replyTo); |
| 3269 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3270 | mLegacyTypeTracker.remove(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3271 | mNetworkForNetId.remove(nai.network.netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3272 | // Since we've lost the network, go through all the requests that |
| 3273 | // it was satisfying and see if any other factory can satisfy them. |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3274 | final ArrayList<NetworkAgentInfo> toActivate = new ArrayList<NetworkAgentInfo>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3275 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 3276 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 3277 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3278 | if (VDBG) { |
| 3279 | log(" checking request " + request + ", currentNetwork = " + |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3280 | (currentNetwork != null ? currentNetwork.name() : "null")); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3281 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3282 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 3283 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3284 | sendUpdatedScoreToFactories(request, 0); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3285 | NetworkAgentInfo alternative = null; |
| 3286 | for (Map.Entry entry : mNetworkAgentInfos.entrySet()) { |
| 3287 | NetworkAgentInfo existing = (NetworkAgentInfo)entry.getValue(); |
| 3288 | if (existing.networkInfo.isConnected() && |
| 3289 | request.networkCapabilities.satisfiedByNetworkCapabilities( |
| 3290 | existing.networkCapabilities) && |
| 3291 | (alternative == null || |
| 3292 | alternative.currentScore < existing.currentScore)) { |
| 3293 | alternative = existing; |
| 3294 | } |
| 3295 | } |
| 3296 | if (alternative != null && !toActivate.contains(alternative)) { |
| 3297 | toActivate.add(alternative); |
| 3298 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3299 | } |
| 3300 | } |
| 3301 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 3302 | removeDataActivityTracking(nai); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3303 | mActiveDefaultNetwork = ConnectivityManager.TYPE_NONE; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3304 | requestNetworkTransitionWakelock(nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3305 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 3306 | for (NetworkAgentInfo networkToActivate : toActivate) { |
| 3307 | networkToActivate.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 3308 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3309 | } |
| 3310 | } |
| 3311 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3312 | private void handleRegisterNetworkRequest(Message msg) { |
| 3313 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 3314 | final NetworkCapabilities newCap = nri.request.networkCapabilities; |
| 3315 | int score = 0; |
| 3316 | |
| 3317 | // Check for the best currently alive network that satisfies this request |
| 3318 | NetworkAgentInfo bestNetwork = null; |
| 3319 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
| 3320 | if (VDBG) log("handleRegisterNetworkRequest checking " + network.name()); |
| 3321 | if (newCap.satisfiedByNetworkCapabilities(network.networkCapabilities)) { |
| 3322 | if (VDBG) log("apparently satisfied. currentScore=" + network.currentScore); |
| 3323 | if ((bestNetwork == null) || bestNetwork.currentScore < network.currentScore) { |
| 3324 | bestNetwork = network; |
| 3325 | } |
| 3326 | } |
| 3327 | } |
| 3328 | if (bestNetwork != null) { |
| 3329 | if (VDBG) log("using " + bestNetwork.name()); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3330 | bestNetwork.addRequest(nri.request); |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 3331 | mNetworkForRequestId.put(nri.request.requestId, bestNetwork); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 3332 | int legacyType = nri.request.legacyType; |
| 3333 | if (legacyType != TYPE_NONE) { |
| 3334 | mLegacyTypeTracker.add(legacyType, bestNetwork); |
| 3335 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3336 | notifyNetworkCallback(bestNetwork, nri); |
| 3337 | score = bestNetwork.currentScore; |
| 3338 | } |
| 3339 | mNetworkRequests.put(nri.request, nri); |
| 3340 | if (msg.what == EVENT_REGISTER_NETWORK_REQUEST) { |
| 3341 | if (DBG) log("sending new NetworkRequest to factories"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3342 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3343 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3344 | 0, nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3345 | } |
| 3346 | } |
| 3347 | } |
| 3348 | |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3349 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
| 3350 | NetworkRequestInfo nri = mNetworkRequests.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3351 | if (nri != null) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3352 | if (nri.mUid != callingUid) { |
| 3353 | if (DBG) log("Attempt to release unowned NetworkRequest " + request); |
| 3354 | return; |
| 3355 | } |
| 3356 | if (DBG) log("releasing NetworkRequest " + request); |
| 3357 | mNetworkRequests.remove(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3358 | // tell the network currently servicing this that it's no longer interested |
| 3359 | NetworkAgentInfo affectedNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 3360 | if (affectedNetwork != null) { |
Robert Greenwalt | 4064af8 | 2014-05-16 08:54:07 -0700 | [diff] [blame] | 3361 | mNetworkForRequestId.remove(nri.request.requestId); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3362 | affectedNetwork.networkRequests.remove(nri.request.requestId); |
| 3363 | if (VDBG) { |
| 3364 | log(" Removing from current network " + affectedNetwork.name() + ", leaving " + |
| 3365 | affectedNetwork.networkRequests.size() + " requests."); |
| 3366 | } |
| 3367 | } |
| 3368 | |
| 3369 | if (nri.isRequest) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3370 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 3371 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 3372 | nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | if (affectedNetwork != null) { |
| 3376 | // check if this network still has live requests - otherwise, tear down |
| 3377 | // TODO - probably push this to the NF/NA |
| 3378 | boolean keep = false; |
| 3379 | for (int i = 0; i < affectedNetwork.networkRequests.size(); i++) { |
| 3380 | NetworkRequest r = affectedNetwork.networkRequests.valueAt(i); |
| 3381 | if (mNetworkRequests.get(r).isRequest) { |
| 3382 | keep = true; |
| 3383 | break; |
| 3384 | } |
| 3385 | } |
| 3386 | if (keep == false) { |
| 3387 | if (DBG) log("no live requests for " + affectedNetwork.name() + |
| 3388 | "; disconnecting"); |
| 3389 | affectedNetwork.asyncChannel.disconnect(); |
| 3390 | } |
| 3391 | } |
| 3392 | } |
| 3393 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 3394 | } |
| 3395 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3396 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3397 | private class InternalHandler extends Handler { |
| 3398 | public InternalHandler(Looper looper) { |
| 3399 | super(looper); |
| 3400 | } |
| 3401 | |
| 3402 | @Override |
| 3403 | public void handleMessage(Message msg) { |
| 3404 | NetworkInfo info; |
| 3405 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3406 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3407 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3408 | String causedBy = null; |
| 3409 | synchronized (ConnectivityService.this) { |
| 3410 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 3411 | mNetTransitionWakeLock.isHeld()) { |
| 3412 | mNetTransitionWakeLock.release(); |
| 3413 | causedBy = mNetTransitionWakeLockCausedBy; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3414 | } else { |
| 3415 | break; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3416 | } |
| 3417 | } |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3418 | if (msg.what == EVENT_EXPIRE_NET_TRANSITION_WAKELOCK) { |
| 3419 | log("Failed to find a new network - expiring NetTransition Wakelock"); |
| 3420 | } else { |
| 3421 | log("NetTransition Wakelock (" + (causedBy == null ? "unknown" : causedBy) + |
| 3422 | " cleared because we found a replacement network"); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3423 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3424 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3425 | } |
| 3426 | case EVENT_RESTORE_DEFAULT_NETWORK: { |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 3427 | FeatureUser u = (FeatureUser)msg.obj; |
| 3428 | u.expire(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3429 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3430 | } |
| 3431 | case EVENT_INET_CONDITION_CHANGE: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3432 | int netType = msg.arg1; |
| 3433 | int condition = msg.arg2; |
| 3434 | handleInetConditionChange(netType, condition); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3435 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3436 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3437 | case EVENT_INET_CONDITION_HOLD_END: { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3438 | int netType = msg.arg1; |
| 3439 | int sequence = msg.arg2; |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3440 | handleInetConditionHoldEnd(netType, sequence); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3441 | break; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3442 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3443 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3444 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3445 | break; |
| 3446 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3447 | case EVENT_SET_DEPENDENCY_MET: { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3448 | boolean met = (msg.arg1 == ENABLED); |
| 3449 | handleSetDependencyMet(msg.arg2, met); |
| 3450 | break; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3451 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3452 | case EVENT_SEND_STICKY_BROADCAST_INTENT: { |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3453 | Intent intent = (Intent)msg.obj; |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 3454 | sendStickyBroadcast(intent); |
| 3455 | break; |
| 3456 | } |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3457 | case EVENT_SET_POLICY_DATA_ENABLE: { |
| 3458 | final int networkType = msg.arg1; |
| 3459 | final boolean enabled = msg.arg2 == ENABLED; |
| 3460 | handleSetPolicyDataEnable(networkType, enabled); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3461 | break; |
| 3462 | } |
| 3463 | case EVENT_VPN_STATE_CHANGED: { |
| 3464 | if (mLockdownTracker != null) { |
| 3465 | mLockdownTracker.onVpnStateChanged((NetworkInfo) msg.obj); |
| 3466 | } |
| 3467 | break; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 3468 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3469 | case EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: { |
| 3470 | int tag = mEnableFailFastMobileDataTag.get(); |
| 3471 | if (msg.arg1 == tag) { |
| 3472 | MobileDataStateTracker mobileDst = |
| 3473 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 3474 | if (mobileDst != null) { |
| 3475 | mobileDst.setEnableFailFastMobileData(msg.arg2); |
| 3476 | } |
| 3477 | } else { |
| 3478 | log("EVENT_ENABLE_FAIL_FAST_MOBILE_DATA: stale arg1:" + msg.arg1 |
| 3479 | + " != tag:" + tag); |
| 3480 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3481 | break; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3482 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3483 | case EVENT_SAMPLE_INTERVAL_ELAPSED: { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3484 | handleNetworkSamplingTimeout(); |
| 3485 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3486 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3487 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3488 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3489 | break; |
| 3490 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3491 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3492 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 3493 | break; |
| 3494 | } |
| 3495 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 3496 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3497 | break; |
| 3498 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3499 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 3500 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 3501 | break; |
| 3502 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3503 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 3504 | case EVENT_REGISTER_NETWORK_LISTENER: { |
| 3505 | handleRegisterNetworkRequest(msg); |
| 3506 | break; |
| 3507 | } |
| 3508 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3509 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3510 | break; |
| 3511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3512 | } |
| 3513 | } |
| 3514 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3515 | |
| 3516 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3517 | public int tether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3518 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3519 | |
| 3520 | if (isTetheringSupported()) { |
| 3521 | return mTethering.tether(iface); |
| 3522 | } else { |
| 3523 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3524 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3528 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3529 | enforceTetherChangePermission(); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3530 | |
| 3531 | if (isTetheringSupported()) { |
| 3532 | return mTethering.untether(iface); |
| 3533 | } else { |
| 3534 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3535 | } |
| 3536 | } |
| 3537 | |
| 3538 | // javadoc from interface |
| 3539 | public int getLastTetherError(String iface) { |
| 3540 | enforceTetherAccessPermission(); |
| 3541 | |
| 3542 | if (isTetheringSupported()) { |
| 3543 | return mTethering.getLastTetherError(iface); |
| 3544 | } else { |
| 3545 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3546 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | // TODO - proper iface API for selection by property, inspection, etc |
| 3550 | public String[] getTetherableUsbRegexs() { |
| 3551 | enforceTetherAccessPermission(); |
| 3552 | if (isTetheringSupported()) { |
| 3553 | return mTethering.getTetherableUsbRegexs(); |
| 3554 | } else { |
| 3555 | return new String[0]; |
| 3556 | } |
| 3557 | } |
| 3558 | |
| 3559 | public String[] getTetherableWifiRegexs() { |
| 3560 | enforceTetherAccessPermission(); |
| 3561 | if (isTetheringSupported()) { |
| 3562 | return mTethering.getTetherableWifiRegexs(); |
| 3563 | } else { |
| 3564 | return new String[0]; |
| 3565 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3566 | } |
| 3567 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3568 | public String[] getTetherableBluetoothRegexs() { |
| 3569 | enforceTetherAccessPermission(); |
| 3570 | if (isTetheringSupported()) { |
| 3571 | return mTethering.getTetherableBluetoothRegexs(); |
| 3572 | } else { |
| 3573 | return new String[0]; |
| 3574 | } |
| 3575 | } |
| 3576 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3577 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3578 | enforceTetherChangePermission(); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3579 | if (isTetheringSupported()) { |
| 3580 | return mTethering.setUsbTethering(enable); |
| 3581 | } else { |
| 3582 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3583 | } |
| 3584 | } |
| 3585 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3586 | // TODO - move iface listing, queries, etc to new module |
| 3587 | // javadoc from interface |
| 3588 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3589 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3590 | return mTethering.getTetherableIfaces(); |
| 3591 | } |
| 3592 | |
| 3593 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3594 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3595 | return mTethering.getTetheredIfaces(); |
| 3596 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3597 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3598 | public String[] getTetheringErroredIfaces() { |
| 3599 | enforceTetherAccessPermission(); |
| 3600 | return mTethering.getErroredIfaces(); |
| 3601 | } |
| 3602 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3603 | // if ro.tether.denied = true we default to no tethering |
| 3604 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3605 | // had previously been turned off. |
| 3606 | public boolean isTetheringSupported() { |
| 3607 | enforceTetherAccessPermission(); |
| 3608 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 3609 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
| 3610 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0); |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 3611 | return tetherEnabledInSettings && ((mTethering.getTetherableUsbRegexs().length != 0 || |
| 3612 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 3613 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 3614 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3615 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3616 | |
| 3617 | // An API NetworkStateTrackers can call when they lose their network. |
| 3618 | // This will automatically be cleared after X seconds or a network becomes CONNECTED, |
| 3619 | // whichever happens first. The timer is started by the first caller and not |
| 3620 | // restarted by subsequent callers. |
| 3621 | public void requestNetworkTransitionWakelock(String forWhom) { |
| 3622 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3623 | int serialNum = 0; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3624 | synchronized (this) { |
| 3625 | if (mNetTransitionWakeLock.isHeld()) return; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3626 | serialNum = ++mNetTransitionWakeLockSerialNumber; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3627 | mNetTransitionWakeLock.acquire(); |
| 3628 | mNetTransitionWakeLockCausedBy = forWhom; |
| 3629 | } |
| 3630 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3631 | EVENT_EXPIRE_NET_TRANSITION_WAKELOCK, serialNum, 0), |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3632 | mNetTransitionWakeLockTimeout); |
| 3633 | return; |
| 3634 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3635 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3636 | // 100 percent is full good, 0 is full bad. |
| 3637 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3638 | if (VDBG) log("reportNetworkCondition(" + networkType + ", " + percentage + ")"); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3639 | mContext.enforceCallingOrSelfPermission( |
| 3640 | android.Manifest.permission.STATUS_BAR, |
| 3641 | "ConnectivityService"); |
| 3642 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 3643 | if (DBG) { |
| 3644 | int pid = getCallingPid(); |
| 3645 | int uid = getCallingUid(); |
| 3646 | String s = pid + "(" + uid + ") reports inet is " + |
| 3647 | (percentage > 50 ? "connected" : "disconnected") + " (" + percentage + ") on " + |
| 3648 | "network Type " + networkType + " at " + GregorianCalendar.getInstance().getTime(); |
| 3649 | mInetLog.add(s); |
| 3650 | while(mInetLog.size() > INET_CONDITION_LOG_MAX_SIZE) { |
| 3651 | mInetLog.remove(0); |
| 3652 | } |
| 3653 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3654 | mHandler.sendMessage(mHandler.obtainMessage( |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3655 | EVENT_INET_CONDITION_CHANGE, networkType, percentage)); |
| 3656 | } |
| 3657 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3658 | public void reportBadNetwork(Network network) { |
| 3659 | //TODO |
| 3660 | } |
| 3661 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3662 | private void handleInetConditionChange(int netType, int condition) { |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3663 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3664 | if (DBG) log("handleInetConditionChange: no active default network - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3665 | return; |
| 3666 | } |
| 3667 | if (mActiveDefaultNetwork != netType) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3668 | if (DBG) log("handleInetConditionChange: net=" + netType + |
| 3669 | " != default=" + mActiveDefaultNetwork + " - ignore"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3670 | return; |
| 3671 | } |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3672 | if (VDBG) { |
| 3673 | log("handleInetConditionChange: net=" + |
| 3674 | netType + ", condition=" + condition + |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3675 | ",mActiveDefaultNetwork=" + mActiveDefaultNetwork); |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3676 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3677 | mDefaultInetCondition = condition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3678 | int delay; |
| 3679 | if (mInetConditionChangeInFlight == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3680 | if (VDBG) log("handleInetConditionChange: starting a change hold"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3681 | // setup a new hold to debounce this |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3682 | if (mDefaultInetCondition > 50) { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3683 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3684 | Settings.Global.INET_CONDITION_DEBOUNCE_UP_DELAY, 500); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3685 | } else { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3686 | delay = Settings.Global.getInt(mContext.getContentResolver(), |
| 3687 | Settings.Global.INET_CONDITION_DEBOUNCE_DOWN_DELAY, 3000); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3688 | } |
| 3689 | mInetConditionChangeInFlight = true; |
| 3690 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_INET_CONDITION_HOLD_END, |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3691 | mActiveDefaultNetwork, mDefaultConnectionSequence), delay); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3692 | } else { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3693 | // we've set the new condition, when this hold ends that will get picked up |
| 3694 | if (VDBG) log("handleInetConditionChange: currently in hold - not setting new end evt"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3695 | } |
| 3696 | } |
| 3697 | |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3698 | private void handleInetConditionHoldEnd(int netType, int sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3699 | if (DBG) { |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3700 | log("handleInetConditionHoldEnd: net=" + netType + |
| 3701 | ", condition=" + mDefaultInetCondition + |
| 3702 | ", published condition=" + mDefaultInetConditionPublished); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3703 | } |
| 3704 | mInetConditionChangeInFlight = false; |
| 3705 | |
| 3706 | if (mActiveDefaultNetwork == -1) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3707 | if (DBG) log("handleInetConditionHoldEnd: no active default network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3708 | return; |
| 3709 | } |
| 3710 | if (mDefaultConnectionSequence != sequence) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3711 | if (DBG) log("handleInetConditionHoldEnd: event hold for obsolete network - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3712 | return; |
| 3713 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3714 | // TODO: Figure out why this optimization sometimes causes a |
| 3715 | // change in mDefaultInetCondition to be missed and the |
| 3716 | // UI to not be updated. |
| 3717 | //if (mDefaultInetConditionPublished == mDefaultInetCondition) { |
| 3718 | // if (DBG) log("no change in condition - aborting"); |
| 3719 | // return; |
| 3720 | //} |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 3721 | NetworkInfo networkInfo = getNetworkInfoForType(mActiveDefaultNetwork); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3722 | if (networkInfo.isConnected() == false) { |
Wink Saville | c9acde9 | 2011-09-21 11:05:43 -0700 | [diff] [blame] | 3723 | if (DBG) log("handleInetConditionHoldEnd: default network not connected - ignoring"); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3724 | return; |
| 3725 | } |
Wink Saville | 5b7573e | 2013-01-31 00:30:13 +0000 | [diff] [blame] | 3726 | mDefaultInetConditionPublished = mDefaultInetCondition; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3727 | sendInetConditionBroadcast(networkInfo); |
| 3728 | return; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3729 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3730 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3731 | public ProxyInfo getProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3732 | // this information is already available as a world read/writable jvm property |
| 3733 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3734 | // of proxy info to all the JVMs. |
| 3735 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3736 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3737 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3738 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 3739 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3740 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3741 | } |
| 3742 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3743 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3744 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3745 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3746 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3747 | if (proxyProperties == mGlobalProxy) return; |
| 3748 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 3749 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 3750 | |
| 3751 | String host = ""; |
| 3752 | int port = 0; |
| 3753 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3754 | String pacFileUrl = ""; |
| 3755 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3756 | (proxyProperties.getPacFileUrl() != null))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3757 | if (!proxyProperties.isValid()) { |
| 3758 | if (DBG) |
| 3759 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3760 | return; |
| 3761 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3762 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3763 | host = mGlobalProxy.getHost(); |
| 3764 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3765 | exclList = mGlobalProxy.getExclusionListAsString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3766 | if (proxyProperties.getPacFileUrl() != null) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3767 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3768 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3769 | } else { |
| 3770 | mGlobalProxy = null; |
| 3771 | } |
| 3772 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3773 | final long token = Binder.clearCallingIdentity(); |
| 3774 | try { |
| 3775 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 3776 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 3777 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 3778 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3779 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3780 | } finally { |
| 3781 | Binder.restoreCallingIdentity(token); |
| 3782 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3783 | } |
| 3784 | |
| 3785 | if (mGlobalProxy == null) { |
| 3786 | proxyProperties = mDefaultProxy; |
| 3787 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3788 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3789 | } |
| 3790 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3791 | private void loadGlobalProxy() { |
| 3792 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3793 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 3794 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 3795 | String exclList = Settings.Global.getString(res, |
| 3796 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3797 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 3798 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3799 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3800 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3801 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3802 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3803 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3804 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3805 | if (!proxyProperties.isValid()) { |
| 3806 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 3807 | return; |
| 3808 | } |
| 3809 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3810 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 3811 | mGlobalProxy = proxyProperties; |
| 3812 | } |
| 3813 | } |
| 3814 | } |
| 3815 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3816 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 3817 | // this information is already available as a world read/writable jvm property |
| 3818 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 3819 | // of proxy info to all the JVMs. |
| 3820 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3821 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3822 | return mGlobalProxy; |
| 3823 | } |
| 3824 | } |
| 3825 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3826 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3827 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3828 | && (proxy.getPacFileUrl() == null)) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3829 | proxy = null; |
| 3830 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3831 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3832 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3833 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 3834 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3835 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 3836 | return; |
| 3837 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 3838 | |
| 3839 | // This call could be coming from the PacManager, containing the port of the local |
| 3840 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 3841 | // global (to get the correct local port), and send a broadcast. |
| 3842 | // TODO: Switch PacManager to have its own message to send back rather than |
| 3843 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
| 3844 | if ((mGlobalProxy != null) && (proxy != null) && (proxy.getPacFileUrl() != null) |
| 3845 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 3846 | mGlobalProxy = proxy; |
| 3847 | sendProxyBroadcast(mGlobalProxy); |
| 3848 | return; |
| 3849 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3850 | mDefaultProxy = proxy; |
| 3851 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3852 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3853 | if (!mDefaultProxyDisabled) { |
| 3854 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3855 | } |
| 3856 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3857 | } |
| 3858 | |
| 3859 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3860 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 3861 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3862 | if (!TextUtils.isEmpty(proxy)) { |
| 3863 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3864 | if (data.length == 0) { |
| 3865 | return; |
| 3866 | } |
| 3867 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3868 | String proxyHost = data[0]; |
| 3869 | int proxyPort = 8080; |
| 3870 | if (data.length > 1) { |
| 3871 | try { |
| 3872 | proxyPort = Integer.parseInt(data[1]); |
| 3873 | } catch (NumberFormatException e) { |
| 3874 | return; |
| 3875 | } |
| 3876 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3877 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3878 | setGlobalProxy(p); |
| 3879 | } |
| 3880 | } |
| 3881 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3882 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 3883 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 3884 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3885 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3886 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3887 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3888 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3889 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3890 | final long ident = Binder.clearCallingIdentity(); |
| 3891 | try { |
| 3892 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3893 | } finally { |
| 3894 | Binder.restoreCallingIdentity(ident); |
| 3895 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3896 | } |
| 3897 | |
| 3898 | private static class SettingsObserver extends ContentObserver { |
| 3899 | private int mWhat; |
| 3900 | private Handler mHandler; |
| 3901 | SettingsObserver(Handler handler, int what) { |
| 3902 | super(handler); |
| 3903 | mHandler = handler; |
| 3904 | mWhat = what; |
| 3905 | } |
| 3906 | |
| 3907 | void observe(Context context) { |
| 3908 | ContentResolver resolver = context.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3909 | resolver.registerContentObserver(Settings.Global.getUriFor( |
| 3910 | Settings.Global.HTTP_PROXY), false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3911 | } |
| 3912 | |
| 3913 | @Override |
| 3914 | public void onChange(boolean selfChange) { |
| 3915 | mHandler.obtainMessage(mWhat).sendToTarget(); |
| 3916 | } |
| 3917 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3918 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3919 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3920 | Slog.d(TAG, s); |
| 3921 | } |
| 3922 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3923 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3924 | Slog.e(TAG, s); |
| 3925 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3926 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3927 | int convertFeatureToNetworkType(int networkType, String feature) { |
| 3928 | int usedNetworkType = networkType; |
| 3929 | |
| 3930 | if(networkType == ConnectivityManager.TYPE_MOBILE) { |
| 3931 | if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_MMS)) { |
| 3932 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_MMS; |
| 3933 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_SUPL)) { |
| 3934 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_SUPL; |
| 3935 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN) || |
| 3936 | TextUtils.equals(feature, Phone.FEATURE_ENABLE_DUN_ALWAYS)) { |
| 3937 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_DUN; |
| 3938 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_HIPRI)) { |
| 3939 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_HIPRI; |
| 3940 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_FOTA)) { |
| 3941 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_FOTA; |
| 3942 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_IMS)) { |
| 3943 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_IMS; |
| 3944 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_CBS)) { |
| 3945 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_CBS; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 3946 | } else if (TextUtils.equals(feature, Phone.FEATURE_ENABLE_EMERGENCY)) { |
| 3947 | usedNetworkType = ConnectivityManager.TYPE_MOBILE_EMERGENCY; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3948 | } else { |
| 3949 | Slog.e(TAG, "Can't match any mobile netTracker!"); |
| 3950 | } |
| 3951 | } else if (networkType == ConnectivityManager.TYPE_WIFI) { |
| 3952 | if (TextUtils.equals(feature, "p2p")) { |
| 3953 | usedNetworkType = ConnectivityManager.TYPE_WIFI_P2P; |
| 3954 | } else { |
| 3955 | Slog.e(TAG, "Can't match any wifi netTracker!"); |
| 3956 | } |
| 3957 | } else { |
| 3958 | Slog.e(TAG, "Unexpected network type"); |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3959 | } |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 3960 | return usedNetworkType; |
Wink Saville | 2b8bcfe | 2011-02-24 17:58:51 -0800 | [diff] [blame] | 3961 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3962 | |
| 3963 | private static <T> T checkNotNull(T value, String message) { |
| 3964 | if (value == null) { |
| 3965 | throw new NullPointerException(message); |
| 3966 | } |
| 3967 | return value; |
| 3968 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3969 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3970 | /** |
| 3971 | * Protect a socket from VPN routing rules. This method is used by |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3972 | * VpnBuilder and not available in ConnectivityManager. Permissions |
| 3973 | * are checked in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3974 | * @hide |
| 3975 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3976 | @Override |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3977 | public boolean protectVpn(ParcelFileDescriptor socket) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3978 | throwIfLockdownEnabled(); |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3979 | try { |
| 3980 | int type = mActiveDefaultNetwork; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3981 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3982 | if (ConnectivityManager.isNetworkTypeValid(type) && mNetTrackers[type] != null) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3983 | synchronized(mVpns) { |
Chad Brubaker | bcf12b3 | 2014-02-11 14:18:56 -0800 | [diff] [blame] | 3984 | mVpns.get(user).protect(socket); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3985 | } |
Chia-chi Yeh | 5779c9c | 2011-07-14 16:19:19 -0700 | [diff] [blame] | 3986 | return true; |
| 3987 | } |
| 3988 | } catch (Exception e) { |
| 3989 | // ignore |
| 3990 | } finally { |
| 3991 | try { |
| 3992 | socket.close(); |
| 3993 | } catch (Exception e) { |
| 3994 | // ignore |
| 3995 | } |
| 3996 | } |
| 3997 | return false; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3998 | } |
| 3999 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4000 | /** |
| 4001 | * Prepare for a VPN application. This method is used by VpnDialogs |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4002 | * and not available in ConnectivityManager. Permissions are checked |
| 4003 | * in Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4004 | * @hide |
| 4005 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4006 | @Override |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 4007 | public boolean prepareVpn(String oldPackage, String newPackage) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4008 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4009 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4010 | synchronized(mVpns) { |
| 4011 | return mVpns.get(user).prepare(oldPackage, newPackage); |
| 4012 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4013 | } |
| 4014 | |
Chad Brubaker | d475c70 | 2013-07-11 13:29:30 -0700 | [diff] [blame] | 4015 | @Override |
| 4016 | public void markSocketAsUser(ParcelFileDescriptor socket, int uid) { |
| 4017 | enforceMarkNetworkSocketPermission(); |
| 4018 | final long token = Binder.clearCallingIdentity(); |
| 4019 | try { |
| 4020 | int mark = mNetd.getMarkForUid(uid); |
| 4021 | // Clear the mark on the socket if no mark is needed to prevent socket reuse issues |
| 4022 | if (mark == -1) { |
| 4023 | mark = 0; |
| 4024 | } |
| 4025 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 4026 | } catch (RemoteException e) { |
| 4027 | } finally { |
| 4028 | Binder.restoreCallingIdentity(token); |
| 4029 | } |
| 4030 | } |
| 4031 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4032 | /** |
| 4033 | * Configure a TUN interface and return its file descriptor. Parameters |
| 4034 | * 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] | 4035 | * and not available in ConnectivityManager. Permissions are checked in |
| 4036 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4037 | * @hide |
| 4038 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4039 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4040 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4041 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4042 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4043 | synchronized(mVpns) { |
| 4044 | return mVpns.get(user).establish(config); |
| 4045 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4046 | } |
| 4047 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4048 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4049 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 4050 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4051 | */ |
| 4052 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4053 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4054 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4055 | final LinkProperties egress = getActiveLinkProperties(); |
| 4056 | if (egress == null) { |
| 4057 | throw new IllegalStateException("Missing active network connection"); |
| 4058 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4059 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4060 | synchronized(mVpns) { |
| 4061 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 4062 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4063 | } |
| 4064 | |
| 4065 | /** |
| 4066 | * Return the information of the ongoing legacy VPN. This method is used |
| 4067 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 4068 | * are checked in Vpn class. |
| 4069 | * @hide |
| 4070 | */ |
| 4071 | @Override |
| 4072 | public LegacyVpnInfo getLegacyVpnInfo() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4073 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4074 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4075 | synchronized(mVpns) { |
| 4076 | return mVpns.get(user).getLegacyVpnInfo(); |
| 4077 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4080 | /** |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4081 | * Returns the information of the ongoing VPN. This method is used by VpnDialogs and |
| 4082 | * not available in ConnectivityManager. |
| 4083 | * Permissions are checked in Vpn class. |
| 4084 | * @hide |
| 4085 | */ |
| 4086 | @Override |
| 4087 | public VpnConfig getVpnConfig() { |
| 4088 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4089 | synchronized(mVpns) { |
| 4090 | return mVpns.get(user).getVpnConfig(); |
| 4091 | } |
| 4092 | } |
| 4093 | |
| 4094 | /** |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4095 | * Callback for VPN subsystem. Currently VPN is not adapted to the service |
| 4096 | * through NetworkStateTracker since it works differently. For example, it |
| 4097 | * needs to override DNS servers but never takes the default routes. It |
| 4098 | * relies on another data network, and it could keep existing connections |
| 4099 | * alive after reconnecting, switching between networks, or even resuming |
| 4100 | * from deep sleep. Calls from applications should be done synchronously |
| 4101 | * to avoid race conditions. As these are all hidden APIs, refactoring can |
| 4102 | * be done whenever a better abstraction is developed. |
| 4103 | */ |
| 4104 | public class VpnCallback { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4105 | private VpnCallback() { |
| 4106 | } |
| 4107 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 4108 | public void onStateChanged(NetworkInfo info) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4109 | mHandler.obtainMessage(EVENT_VPN_STATE_CHANGED, info).sendToTarget(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 4110 | } |
| 4111 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4112 | public void override(String iface, List<String> dnsServers, List<String> searchDomains) { |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4113 | if (dnsServers == null) { |
| 4114 | restore(); |
| 4115 | return; |
| 4116 | } |
| 4117 | |
| 4118 | // Convert DNS servers into addresses. |
| 4119 | List<InetAddress> addresses = new ArrayList<InetAddress>(); |
| 4120 | for (String address : dnsServers) { |
| 4121 | // Double check the addresses and remove invalid ones. |
| 4122 | try { |
| 4123 | addresses.add(InetAddress.parseNumericAddress(address)); |
| 4124 | } catch (Exception e) { |
| 4125 | // ignore |
| 4126 | } |
| 4127 | } |
| 4128 | if (addresses.isEmpty()) { |
| 4129 | restore(); |
| 4130 | return; |
| 4131 | } |
| 4132 | |
| 4133 | // Concatenate search domains into a string. |
| 4134 | StringBuilder buffer = new StringBuilder(); |
| 4135 | if (searchDomains != null) { |
| 4136 | for (String domain : searchDomains) { |
| 4137 | buffer.append(domain).append(' '); |
| 4138 | } |
| 4139 | } |
| 4140 | String domains = buffer.toString().trim(); |
| 4141 | |
| 4142 | // Apply DNS changes. |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4143 | synchronized (mDnsLock) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4144 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4145 | // updateDnsLocked("VPN", netId, addresses, domains); |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4146 | } |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4147 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4148 | // Temporarily disable the default proxy (not global). |
| 4149 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4150 | mDefaultProxyDisabled = true; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4151 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4152 | sendProxyBroadcast(null); |
| 4153 | } |
| 4154 | } |
| 4155 | |
| 4156 | // TODO: support proxy per network. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4157 | } |
| 4158 | |
Chia-chi Yeh | 44bb251 | 2011-07-14 18:01:57 -0700 | [diff] [blame] | 4159 | public void restore() { |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4160 | synchronized (mProxyLock) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4161 | mDefaultProxyDisabled = false; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 4162 | if (mGlobalProxy == null && mDefaultProxy != null) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4163 | sendProxyBroadcast(mDefaultProxy); |
| 4164 | } |
| 4165 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4166 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4167 | |
| 4168 | public void protect(ParcelFileDescriptor socket) { |
| 4169 | try { |
| 4170 | final int mark = mNetd.getMarkForProtect(); |
| 4171 | NetworkUtils.markSocket(socket.getFd(), mark); |
| 4172 | } catch (RemoteException e) { |
| 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | public void setRoutes(String interfaze, List<RouteInfo> routes) { |
| 4177 | for (RouteInfo route : routes) { |
| 4178 | try { |
| 4179 | mNetd.setMarkedForwardingRoute(interfaze, route); |
| 4180 | } catch (RemoteException e) { |
| 4181 | } |
| 4182 | } |
| 4183 | } |
| 4184 | |
| 4185 | public void setMarkedForwarding(String interfaze) { |
| 4186 | try { |
| 4187 | mNetd.setMarkedForwarding(interfaze); |
| 4188 | } catch (RemoteException e) { |
| 4189 | } |
| 4190 | } |
| 4191 | |
| 4192 | public void clearMarkedForwarding(String interfaze) { |
| 4193 | try { |
| 4194 | mNetd.clearMarkedForwarding(interfaze); |
| 4195 | } catch (RemoteException e) { |
| 4196 | } |
| 4197 | } |
| 4198 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4199 | public void addUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4200 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 4201 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4202 | addUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4203 | } |
| 4204 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4205 | public void clearUserForwarding(String interfaze, int uid, boolean forwardDns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4206 | int uidStart = uid * UserHandle.PER_USER_RANGE; |
| 4207 | int uidEnd = uidStart + UserHandle.PER_USER_RANGE - 1; |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4208 | clearUidForwarding(interfaze, uidStart, uidEnd, forwardDns); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4209 | } |
| 4210 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4211 | public void addUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 4212 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4213 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4214 | // try { |
| 4215 | // mNetd.setUidRangeRoute(netId, uidStart, uidEnd, forwardDns); |
| 4216 | // } catch (RemoteException e) { |
| 4217 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4218 | |
| 4219 | } |
| 4220 | |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 4221 | public void clearUidForwarding(String interfaze, int uidStart, int uidEnd, |
| 4222 | boolean forwardDns) { |
Paul Jensen | 13e817d | 2014-04-10 14:16:37 -0400 | [diff] [blame] | 4223 | // TODO: Re-enable this when the netId of the VPN is known. |
| 4224 | // try { |
| 4225 | // mNetd.clearUidRangeRoute(interfaze, uidStart, uidEnd); |
| 4226 | // } catch (RemoteException e) { |
| 4227 | // } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4228 | |
| 4229 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4230 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4231 | |
| 4232 | @Override |
| 4233 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 4234 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4235 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 4236 | return false; |
| 4237 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4238 | |
| 4239 | // Tear down existing lockdown if profile was removed |
| 4240 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 4241 | if (mLockdownEnabled) { |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 4242 | if (!mKeyStore.isUnlocked()) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4243 | Slog.w(TAG, "KeyStore locked; unable to create LockdownTracker"); |
| 4244 | return false; |
| 4245 | } |
| 4246 | |
| 4247 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 4248 | final VpnProfile profile = VpnProfile.decode( |
| 4249 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4250 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 4251 | synchronized(mVpns) { |
| 4252 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, mVpns.get(user), |
| 4253 | profile)); |
| 4254 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4255 | } else { |
| 4256 | setLockdownTracker(null); |
| 4257 | } |
| 4258 | |
| 4259 | return true; |
| 4260 | } |
| 4261 | |
| 4262 | /** |
| 4263 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4264 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4265 | */ |
| 4266 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4267 | // Shutdown any existing tracker |
| 4268 | final LockdownVpnTracker existing = mLockdownTracker; |
| 4269 | mLockdownTracker = null; |
| 4270 | if (existing != null) { |
| 4271 | existing.shutdown(); |
| 4272 | } |
| 4273 | |
| 4274 | try { |
| 4275 | if (tracker != null) { |
| 4276 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 4277 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4278 | mLockdownTracker = tracker; |
| 4279 | mLockdownTracker.init(); |
| 4280 | } else { |
| 4281 | mNetd.setFirewallEnabled(false); |
| 4282 | } |
| 4283 | } catch (RemoteException e) { |
| 4284 | // ignored; NMS lives inside system_server |
| 4285 | } |
| 4286 | } |
| 4287 | |
| 4288 | private void throwIfLockdownEnabled() { |
| 4289 | if (mLockdownEnabled) { |
| 4290 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4291 | } |
| 4292 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4293 | |
| 4294 | public void supplyMessenger(int networkType, Messenger messenger) { |
| 4295 | enforceConnectivityInternalPermission(); |
| 4296 | |
| 4297 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
| 4298 | mNetTrackers[networkType].supplyMessenger(messenger); |
| 4299 | } |
| 4300 | } |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 4301 | |
| 4302 | public int findConnectionTypeForIface(String iface) { |
| 4303 | enforceConnectivityInternalPermission(); |
| 4304 | |
| 4305 | if (TextUtils.isEmpty(iface)) return ConnectivityManager.TYPE_NONE; |
| 4306 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 4307 | if (tracker != null) { |
| 4308 | LinkProperties lp = tracker.getLinkProperties(); |
| 4309 | if (lp != null && iface.equals(lp.getInterfaceName())) { |
| 4310 | return tracker.getNetworkInfo().getType(); |
| 4311 | } |
| 4312 | } |
| 4313 | } |
| 4314 | return ConnectivityManager.TYPE_NONE; |
| 4315 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4316 | |
| 4317 | /** |
| 4318 | * Have mobile data fail fast if enabled. |
| 4319 | * |
| 4320 | * @param enabled DctConstants.ENABLED/DISABLED |
| 4321 | */ |
| 4322 | private void setEnableFailFastMobileData(int enabled) { |
| 4323 | int tag; |
| 4324 | |
| 4325 | if (enabled == DctConstants.ENABLED) { |
| 4326 | tag = mEnableFailFastMobileDataTag.incrementAndGet(); |
| 4327 | } else { |
| 4328 | tag = mEnableFailFastMobileDataTag.get(); |
| 4329 | } |
| 4330 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_ENABLE_FAIL_FAST_MOBILE_DATA, tag, |
| 4331 | enabled)); |
| 4332 | } |
| 4333 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4334 | private boolean isMobileDataStateTrackerReady() { |
| 4335 | MobileDataStateTracker mdst = |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4336 | (MobileDataStateTracker) mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4337 | return (mdst != null) && (mdst.isReady()); |
| 4338 | } |
| 4339 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4340 | /** |
| 4341 | * The ResultReceiver resultCode for checkMobileProvisioning (CMP_RESULT_CODE) |
| 4342 | */ |
| 4343 | |
| 4344 | /** |
| 4345 | * No connection was possible to the network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4346 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4347 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4348 | private static final int CMP_RESULT_CODE_NO_CONNECTION = 0; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4349 | |
| 4350 | /** |
| 4351 | * A connection was made to the internet, all is well. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4352 | * This is NOT a warm sim. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4353 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4354 | private static final int CMP_RESULT_CODE_CONNECTABLE = 1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4355 | |
| 4356 | /** |
| 4357 | * A connection was made but no dns server was available to resolve a name to address. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4358 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4359 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4360 | private static final int CMP_RESULT_CODE_NO_DNS = 2; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4361 | |
| 4362 | /** |
| 4363 | * A connection was made but could not open a TCP connection. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4364 | * This is NOT a warm sim since provisioning network is supported. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4365 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4366 | private static final int CMP_RESULT_CODE_NO_TCP_CONNECTION = 3; |
| 4367 | |
| 4368 | /** |
| 4369 | * A connection was made but there was a redirection, we appear to be in walled garden. |
| 4370 | * This is an indication of a warm sim on a mobile network such as T-Mobile. |
| 4371 | */ |
| 4372 | private static final int CMP_RESULT_CODE_REDIRECTED = 4; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4373 | |
| 4374 | /** |
| 4375 | * The mobile network is a provisioning network. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4376 | * This is an indication of a warm sim on a mobile network such as AT&T. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4377 | */ |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4378 | private static final int CMP_RESULT_CODE_PROVISIONING_NETWORK = 5; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4379 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4380 | /** |
| 4381 | * The mobile network is provisioning |
| 4382 | */ |
| 4383 | private static final int CMP_RESULT_CODE_IS_PROVISIONING = 6; |
| 4384 | |
| 4385 | private AtomicBoolean mIsProvisioningNetwork = new AtomicBoolean(false); |
| 4386 | private AtomicBoolean mIsStartingProvisioning = new AtomicBoolean(false); |
| 4387 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4388 | private AtomicBoolean mIsCheckingMobileProvisioning = new AtomicBoolean(false); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4389 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4390 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4391 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
| 4392 | int timeOutMs = -1; |
| 4393 | if (DBG) log("checkMobileProvisioning: E suggestedTimeOutMs=" + suggestedTimeOutMs); |
| 4394 | enforceConnectivityInternalPermission(); |
Wink Saville | 68e6c64 | 2013-07-02 10:55:14 -0700 | [diff] [blame] | 4395 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4396 | final long token = Binder.clearCallingIdentity(); |
| 4397 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4398 | timeOutMs = suggestedTimeOutMs; |
| 4399 | if (suggestedTimeOutMs > CheckMp.MAX_TIMEOUT_MS) { |
| 4400 | timeOutMs = CheckMp.MAX_TIMEOUT_MS; |
| 4401 | } |
| 4402 | |
| 4403 | // Check that mobile networks are supported |
| 4404 | if (!isNetworkSupported(ConnectivityManager.TYPE_MOBILE) |
| 4405 | || !isNetworkSupported(ConnectivityManager.TYPE_MOBILE_HIPRI)) { |
| 4406 | if (DBG) log("checkMobileProvisioning: X no mobile network"); |
| 4407 | return timeOutMs; |
| 4408 | } |
| 4409 | |
| 4410 | // If we're already checking don't do it again |
| 4411 | // TODO: Add a queue of results... |
| 4412 | if (mIsCheckingMobileProvisioning.getAndSet(true)) { |
| 4413 | if (DBG) log("checkMobileProvisioning: X already checking ignore for the moment"); |
| 4414 | return timeOutMs; |
| 4415 | } |
| 4416 | |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4417 | // Start off with mobile notification off |
| 4418 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4419 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4420 | CheckMp checkMp = new CheckMp(mContext, this); |
| 4421 | CheckMp.CallBack cb = new CheckMp.CallBack() { |
| 4422 | @Override |
| 4423 | void onComplete(Integer result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4424 | if (DBG) log("CheckMp.onComplete: result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4425 | NetworkInfo ni = |
| 4426 | mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI].getNetworkInfo(); |
| 4427 | switch(result) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4428 | case CMP_RESULT_CODE_CONNECTABLE: |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4429 | case CMP_RESULT_CODE_NO_CONNECTION: |
| 4430 | case CMP_RESULT_CODE_NO_DNS: |
| 4431 | case CMP_RESULT_CODE_NO_TCP_CONNECTION: { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4432 | if (DBG) log("CheckMp.onComplete: ignore, connected or no connection"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4433 | break; |
| 4434 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4435 | case CMP_RESULT_CODE_REDIRECTED: { |
| 4436 | if (DBG) log("CheckMp.onComplete: warm sim"); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4437 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4438 | if (TextUtils.isEmpty(url)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4439 | url = getMobileRedirectedProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4440 | } |
| 4441 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4442 | if (DBG) log("CheckMp.onComplete: warm (redirected), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4443 | setProvNotificationVisible(true, |
| 4444 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4445 | url); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4446 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4447 | if (DBG) log("CheckMp.onComplete: warm (redirected), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4448 | } |
| 4449 | break; |
| 4450 | } |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4451 | case CMP_RESULT_CODE_PROVISIONING_NETWORK: { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4452 | String url = getMobileProvisioningUrl(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4453 | if (TextUtils.isEmpty(url) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4454 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), url=" + url); |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4455 | setProvNotificationVisible(true, |
| 4456 | ConnectivityManager.TYPE_MOBILE_HIPRI, ni.getExtraInfo(), |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4457 | url); |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4458 | // Mark that we've got a provisioning network and |
| 4459 | // Disable Mobile Data until user actually starts provisioning. |
| 4460 | mIsProvisioningNetwork.set(true); |
| 4461 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4462 | mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4463 | |
| 4464 | // Disable radio until user starts provisioning |
| 4465 | mdst.setRadio(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4466 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4467 | if (DBG) log("CheckMp.onComplete: warm (no dns/tcp), no url"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4468 | } |
| 4469 | break; |
| 4470 | } |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4471 | case CMP_RESULT_CODE_IS_PROVISIONING: { |
| 4472 | // FIXME: Need to know when provisioning is done. Probably we can |
| 4473 | // check the completion status if successful we're done if we |
| 4474 | // "timedout" or still connected to provisioning APN turn off data? |
| 4475 | if (DBG) log("CheckMp.onComplete: provisioning started"); |
| 4476 | mIsStartingProvisioning.set(false); |
| 4477 | break; |
| 4478 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4479 | default: { |
| 4480 | loge("CheckMp.onComplete: ignore unexpected result=" + result); |
| 4481 | break; |
| 4482 | } |
| 4483 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4484 | mIsCheckingMobileProvisioning.set(false); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4485 | } |
| 4486 | }; |
| 4487 | CheckMp.Params params = |
| 4488 | new CheckMp.Params(checkMp.getDefaultUrl(), timeOutMs, cb); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4489 | if (DBG) log("checkMobileProvisioning: params=" + params); |
Paul Jensen | b6bb6c6 | 2014-05-12 17:39:09 -0400 | [diff] [blame] | 4490 | // TODO: Reenable when calls to the now defunct |
| 4491 | // MobileDataStateTracker.isProvisioningNetwork() are removed. |
| 4492 | // This code should be moved to the Telephony code. |
| 4493 | // checkMp.execute(params); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4494 | } finally { |
| 4495 | Binder.restoreCallingIdentity(token); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4496 | if (DBG) log("checkMobileProvisioning: X"); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4497 | } |
| 4498 | return timeOutMs; |
| 4499 | } |
| 4500 | |
| 4501 | static class CheckMp extends |
| 4502 | AsyncTask<CheckMp.Params, Void, Integer> { |
| 4503 | private static final String CHECKMP_TAG = "CheckMp"; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4504 | |
| 4505 | // adb shell setprop persist.checkmp.testfailures 1 to enable testing failures |
| 4506 | private static boolean mTestingFailures; |
| 4507 | |
| 4508 | // Choosing 4 loops as half of them will use HTTPS and the other half HTTP |
| 4509 | private static final int MAX_LOOPS = 4; |
| 4510 | |
| 4511 | // Number of milli-seconds to complete all of the retires |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4512 | public static final int MAX_TIMEOUT_MS = 60000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4513 | |
| 4514 | // The socket should retry only 5 seconds, the default is longer |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4515 | private static final int SOCKET_TIMEOUT_MS = 5000; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4516 | |
| 4517 | // Sleep time for network errors |
| 4518 | private static final int NET_ERROR_SLEEP_SEC = 3; |
| 4519 | |
| 4520 | // Sleep time for network route establishment |
| 4521 | private static final int NET_ROUTE_ESTABLISHMENT_SLEEP_SEC = 3; |
| 4522 | |
| 4523 | // Short sleep time for polling :( |
| 4524 | private static final int POLLING_SLEEP_SEC = 1; |
| 4525 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4526 | private Context mContext; |
| 4527 | private ConnectivityService mCs; |
| 4528 | private TelephonyManager mTm; |
| 4529 | private Params mParams; |
| 4530 | |
| 4531 | /** |
| 4532 | * Parameters for AsyncTask.execute |
| 4533 | */ |
| 4534 | static class Params { |
| 4535 | private String mUrl; |
| 4536 | private long mTimeOutMs; |
| 4537 | private CallBack mCb; |
| 4538 | |
| 4539 | Params(String url, long timeOutMs, CallBack cb) { |
| 4540 | mUrl = url; |
| 4541 | mTimeOutMs = timeOutMs; |
| 4542 | mCb = cb; |
| 4543 | } |
| 4544 | |
| 4545 | @Override |
| 4546 | public String toString() { |
| 4547 | return "{" + " url=" + mUrl + " mTimeOutMs=" + mTimeOutMs + " mCb=" + mCb + "}"; |
| 4548 | } |
| 4549 | } |
| 4550 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4551 | // As explained to me by Brian Carlstrom and Kenny Root, Certificates can be |
| 4552 | // issued by name or ip address, for Google its by name so when we construct |
| 4553 | // this HostnameVerifier we'll pass the original Uri and use it to verify |
| 4554 | // the host. If the host name in the original uril fails we'll test the |
| 4555 | // hostname parameter just incase things change. |
| 4556 | static class CheckMpHostnameVerifier implements HostnameVerifier { |
| 4557 | Uri mOrgUri; |
| 4558 | |
| 4559 | CheckMpHostnameVerifier(Uri orgUri) { |
| 4560 | mOrgUri = orgUri; |
| 4561 | } |
| 4562 | |
| 4563 | @Override |
| 4564 | public boolean verify(String hostname, SSLSession session) { |
| 4565 | HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier(); |
| 4566 | String orgUriHost = mOrgUri.getHost(); |
| 4567 | boolean retVal = hv.verify(orgUriHost, session) || hv.verify(hostname, session); |
| 4568 | if (DBG) { |
| 4569 | log("isMobileOk: hostnameVerify retVal=" + retVal + " hostname=" + hostname |
| 4570 | + " orgUriHost=" + orgUriHost); |
| 4571 | } |
| 4572 | return retVal; |
| 4573 | } |
| 4574 | } |
| 4575 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4576 | /** |
| 4577 | * The call back object passed in Params. onComplete will be called |
| 4578 | * on the main thread. |
| 4579 | */ |
| 4580 | abstract static class CallBack { |
| 4581 | // Called on the main thread. |
| 4582 | abstract void onComplete(Integer result); |
| 4583 | } |
| 4584 | |
| 4585 | public CheckMp(Context context, ConnectivityService cs) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4586 | if (Build.IS_DEBUGGABLE) { |
| 4587 | mTestingFailures = |
| 4588 | SystemProperties.getInt("persist.checkmp.testfailures", 0) == 1; |
| 4589 | } else { |
| 4590 | mTestingFailures = false; |
| 4591 | } |
| 4592 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4593 | mContext = context; |
| 4594 | mCs = cs; |
| 4595 | |
| 4596 | // Setup access to TelephonyService we'll be using. |
| 4597 | mTm = (TelephonyManager) mContext.getSystemService( |
| 4598 | Context.TELEPHONY_SERVICE); |
| 4599 | } |
| 4600 | |
| 4601 | /** |
| 4602 | * Get the default url to use for the test. |
| 4603 | */ |
| 4604 | public String getDefaultUrl() { |
| 4605 | // See http://go/clientsdns for usage approval |
| 4606 | String server = Settings.Global.getString(mContext.getContentResolver(), |
| 4607 | Settings.Global.CAPTIVE_PORTAL_SERVER); |
| 4608 | if (server == null) { |
| 4609 | server = "clients3.google.com"; |
| 4610 | } |
| 4611 | return "http://" + server + "/generate_204"; |
| 4612 | } |
| 4613 | |
| 4614 | /** |
| 4615 | * Detect if its possible to connect to the http url. DNS based detection techniques |
| 4616 | * do not work at all hotspots. The best way to check is to perform a request to |
| 4617 | * a known address that fetches the data we expect. |
| 4618 | */ |
| 4619 | private synchronized Integer isMobileOk(Params params) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4620 | Integer result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4621 | Uri orgUri = Uri.parse(params.mUrl); |
| 4622 | Random rand = new Random(); |
| 4623 | mParams = params; |
| 4624 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4625 | if (mCs.isNetworkSupported(ConnectivityManager.TYPE_MOBILE) == false) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4626 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4627 | log("isMobileOk: X not mobile capable result=" + result); |
| 4628 | return result; |
| 4629 | } |
| 4630 | |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4631 | if (mCs.mIsStartingProvisioning.get()) { |
| 4632 | result = CMP_RESULT_CODE_IS_PROVISIONING; |
| 4633 | log("isMobileOk: X is provisioning result=" + result); |
| 4634 | return result; |
| 4635 | } |
| 4636 | |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4637 | // See if we've already determined we've got a provisioning connection, |
| 4638 | // if so we don't need to do anything active. |
| 4639 | MobileDataStateTracker mdstDefault = (MobileDataStateTracker) |
| 4640 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
| 4641 | boolean isDefaultProvisioning = mdstDefault.isProvisioningNetwork(); |
| 4642 | log("isMobileOk: isDefaultProvisioning=" + isDefaultProvisioning); |
| 4643 | |
| 4644 | MobileDataStateTracker mdstHipri = (MobileDataStateTracker) |
| 4645 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4646 | boolean isHipriProvisioning = mdstHipri.isProvisioningNetwork(); |
| 4647 | log("isMobileOk: isHipriProvisioning=" + isHipriProvisioning); |
| 4648 | |
| 4649 | if (isDefaultProvisioning || isHipriProvisioning) { |
| 4650 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
| 4651 | log("isMobileOk: X default || hipri is provisioning result=" + result); |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4652 | return result; |
| 4653 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4654 | |
Wink Saville | afe58b0 | 2013-08-13 12:41:06 -0700 | [diff] [blame] | 4655 | try { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4656 | // Continue trying to connect until time has run out |
| 4657 | long endTime = SystemClock.elapsedRealtime() + params.mTimeOutMs; |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4658 | |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4659 | if (!mCs.isMobileDataStateTrackerReady()) { |
| 4660 | // Wait for MobileDataStateTracker to be ready. |
| 4661 | if (DBG) log("isMobileOk: mdst is not ready"); |
| 4662 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4663 | if (mCs.isMobileDataStateTrackerReady()) { |
| 4664 | // Enable fail fast as we'll do retries here and use a |
| 4665 | // hipri connection so the default connection stays active. |
| 4666 | if (DBG) log("isMobileOk: mdst ready, enable fail fast of mobile data"); |
| 4667 | mCs.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 4668 | break; |
| 4669 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4670 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | dce52cd | 2013-08-16 17:17:28 -0700 | [diff] [blame] | 4671 | } |
| 4672 | } |
| 4673 | |
| 4674 | log("isMobileOk: start hipri url=" + params.mUrl); |
| 4675 | |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4676 | // First wait until we can start using hipri |
| 4677 | Binder binder = new Binder(); |
| 4678 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4679 | int ret = mCs.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4680 | Phone.FEATURE_ENABLE_HIPRI, binder); |
| 4681 | if ((ret == PhoneConstants.APN_ALREADY_ACTIVE) |
| 4682 | || (ret == PhoneConstants.APN_REQUEST_STARTED)) { |
| 4683 | log("isMobileOk: hipri started"); |
| 4684 | break; |
| 4685 | } |
| 4686 | if (VDBG) log("isMobileOk: hipri not started yet"); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4687 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4688 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 7f6ee2e | 2013-08-07 11:02:57 -0700 | [diff] [blame] | 4689 | } |
| 4690 | |
| 4691 | // Continue trying to connect until time has run out |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4692 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4693 | try { |
| 4694 | // Wait for hipri to connect. |
| 4695 | // TODO: Don't poll and handle situation where hipri fails |
| 4696 | // because default is retrying. See b/9569540 |
| 4697 | NetworkInfo.State state = mCs |
| 4698 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4699 | if (state != NetworkInfo.State.CONNECTED) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4700 | if (true/*VDBG*/) { |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4701 | log("isMobileOk: not connected ni=" + |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4702 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
Wink Saville | 1aef7fc | 2013-07-31 15:49:04 -0700 | [diff] [blame] | 4703 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4704 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4705 | result = CMP_RESULT_CODE_NO_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4706 | continue; |
| 4707 | } |
| 4708 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4709 | // Hipri has started check if this is a provisioning url |
| 4710 | MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4711 | mCs.mNetTrackers[ConnectivityManager.TYPE_MOBILE_HIPRI]; |
| 4712 | if (mdst.isProvisioningNetwork()) { |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4713 | result = CMP_RESULT_CODE_PROVISIONING_NETWORK; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4714 | if (DBG) log("isMobileOk: X isProvisioningNetwork result=" + result); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4715 | return result; |
| 4716 | } else { |
| 4717 | if (DBG) log("isMobileOk: isProvisioningNetwork is false, continue"); |
| 4718 | } |
| 4719 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4720 | // Get of the addresses associated with the url host. We need to use the |
| 4721 | // address otherwise HttpURLConnection object will use the name to get |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4722 | // the addresses and will try every address but that will bypass the |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4723 | // route to host we setup and the connection could succeed as the default |
| 4724 | // interface might be connected to the internet via wifi or other interface. |
| 4725 | InetAddress[] addresses; |
| 4726 | try { |
| 4727 | addresses = InetAddress.getAllByName(orgUri.getHost()); |
| 4728 | } catch (UnknownHostException e) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4729 | result = CMP_RESULT_CODE_NO_DNS; |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4730 | log("isMobileOk: X UnknownHostException result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4731 | return result; |
| 4732 | } |
| 4733 | log("isMobileOk: addresses=" + inetAddressesToString(addresses)); |
| 4734 | |
| 4735 | // Get the type of addresses supported by this link |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4736 | LinkProperties lp = mCs.getLinkPropertiesForTypeInternal( |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4737 | ConnectivityManager.TYPE_MOBILE_HIPRI); |
Lorenzo Colitti | 4faa027 | 2013-08-08 11:00:12 +0900 | [diff] [blame] | 4738 | boolean linkHasIpv4 = lp.hasIPv4Address(); |
Lorenzo Colitti | 76ea6c6 | 2014-06-23 22:33:43 +0900 | [diff] [blame] | 4739 | boolean linkHasIpv6 = lp.hasGlobalIPv6Address(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4740 | log("isMobileOk: linkHasIpv4=" + linkHasIpv4 |
| 4741 | + " linkHasIpv6=" + linkHasIpv6); |
| 4742 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4743 | final ArrayList<InetAddress> validAddresses = |
| 4744 | new ArrayList<InetAddress>(addresses.length); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4745 | |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4746 | for (InetAddress addr : addresses) { |
| 4747 | if (((addr instanceof Inet4Address) && linkHasIpv4) || |
| 4748 | ((addr instanceof Inet6Address) && linkHasIpv6)) { |
| 4749 | validAddresses.add(addr); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4750 | } |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | if (validAddresses.size() == 0) { |
| 4754 | return CMP_RESULT_CODE_NO_CONNECTION; |
| 4755 | } |
| 4756 | |
| 4757 | int addrTried = 0; |
| 4758 | while (true) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4759 | // Loop through at most MAX_LOOPS valid addresses or until |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4760 | // we run out of time |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4761 | if (addrTried++ >= MAX_LOOPS) { |
| 4762 | log("isMobileOk: too many loops tried - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4763 | break; |
| 4764 | } |
| 4765 | if (SystemClock.elapsedRealtime() >= endTime) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4766 | log("isMobileOk: spend too much time - giving up"); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4767 | break; |
| 4768 | } |
| 4769 | |
| 4770 | InetAddress hostAddr = validAddresses.get(rand.nextInt( |
| 4771 | validAddresses.size())); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4772 | |
| 4773 | // Make a route to host so we check the specific interface. |
| 4774 | if (mCs.requestRouteToHostAddress(ConnectivityManager.TYPE_MOBILE_HIPRI, |
Chad Brubaker | f81daa9 | 2014-02-14 13:22:34 -0800 | [diff] [blame] | 4775 | hostAddr.getAddress(), null)) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4776 | // Wait a short time to be sure the route is established ?? |
| 4777 | log("isMobileOk:" |
| 4778 | + " wait to establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4779 | sleep(NET_ROUTE_ESTABLISHMENT_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4780 | } else { |
| 4781 | log("isMobileOk:" |
| 4782 | + " could not establish route to hostAddr=" + hostAddr); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4783 | // Wait a short time before the next attempt |
| 4784 | sleep(NET_ERROR_SLEEP_SEC); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4785 | continue; |
| 4786 | } |
| 4787 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4788 | // Rewrite the url to have numeric address to use the specific route |
| 4789 | // using http for half the attempts and https for the other half. |
| 4790 | // Doing https first and http second as on a redirected walled garden |
| 4791 | // such as t-mobile uses we get a SocketTimeoutException: "SSL |
| 4792 | // handshake timed out" which we declare as |
| 4793 | // CMP_RESULT_CODE_NO_TCP_CONNECTION. We could change this, but by |
| 4794 | // having http second we will be using logic used for some time. |
| 4795 | URL newUrl; |
| 4796 | String scheme = (addrTried <= (MAX_LOOPS/2)) ? "https" : "http"; |
| 4797 | newUrl = new URL(scheme, hostAddr.getHostAddress(), |
| 4798 | orgUri.getPath()); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4799 | log("isMobileOk: newUrl=" + newUrl); |
| 4800 | |
| 4801 | HttpURLConnection urlConn = null; |
| 4802 | try { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4803 | // Open the connection set the request headers and get the response |
| 4804 | urlConn = (HttpURLConnection)newUrl.openConnection( |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4805 | java.net.Proxy.NO_PROXY); |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4806 | if (scheme.equals("https")) { |
| 4807 | ((HttpsURLConnection)urlConn).setHostnameVerifier( |
| 4808 | new CheckMpHostnameVerifier(orgUri)); |
| 4809 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4810 | urlConn.setInstanceFollowRedirects(false); |
| 4811 | urlConn.setConnectTimeout(SOCKET_TIMEOUT_MS); |
| 4812 | urlConn.setReadTimeout(SOCKET_TIMEOUT_MS); |
| 4813 | urlConn.setUseCaches(false); |
| 4814 | urlConn.setAllowUserInteraction(false); |
Lorenzo Colitti | 143a2cf | 2013-10-08 10:41:25 +0900 | [diff] [blame] | 4815 | // Set the "Connection" to "Close" as by default "Keep-Alive" |
| 4816 | // is used which is useless in this case. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4817 | urlConn.setRequestProperty("Connection", "close"); |
| 4818 | int responseCode = urlConn.getResponseCode(); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4819 | |
| 4820 | // For debug display the headers |
| 4821 | Map<String, List<String>> headers = urlConn.getHeaderFields(); |
| 4822 | log("isMobileOk: headers=" + headers); |
| 4823 | |
| 4824 | // Close the connection |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4825 | urlConn.disconnect(); |
| 4826 | urlConn = null; |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4827 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4828 | if (mTestingFailures) { |
| 4829 | // Pretend no connection, this tests using http and https |
| 4830 | result = CMP_RESULT_CODE_NO_CONNECTION; |
| 4831 | log("isMobileOk: TESTING_FAILURES, pretend no connction"); |
| 4832 | continue; |
| 4833 | } |
| 4834 | |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4835 | if (responseCode == 204) { |
| 4836 | // Return |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4837 | result = CMP_RESULT_CODE_CONNECTABLE; |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4838 | log("isMobileOk: X got expected responseCode=" + responseCode |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4839 | + " result=" + result); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4840 | return result; |
| 4841 | } else { |
| 4842 | // Retry to be sure this was redirected, we've gotten |
| 4843 | // occasions where a server returned 200 even though |
| 4844 | // the device didn't have a "warm" sim. |
| 4845 | log("isMobileOk: not expected responseCode=" + responseCode); |
Robert Greenwalt | b9acff3 | 2013-10-14 18:03:02 -0700 | [diff] [blame] | 4846 | // TODO - it would be nice in the single-address case to do |
| 4847 | // another DNS resolve here, but flushing the cache is a bit |
| 4848 | // heavy-handed. |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4849 | result = CMP_RESULT_CODE_REDIRECTED; |
| 4850 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4851 | } catch (Exception e) { |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4852 | log("isMobileOk: HttpURLConnection Exception" + e); |
Wink Saville | 4fea041 | 2013-09-06 09:53:08 -0700 | [diff] [blame] | 4853 | result = CMP_RESULT_CODE_NO_TCP_CONNECTION; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4854 | if (urlConn != null) { |
| 4855 | urlConn.disconnect(); |
| 4856 | urlConn = null; |
| 4857 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4858 | sleep(NET_ERROR_SLEEP_SEC); |
| 4859 | continue; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4860 | } |
| 4861 | } |
Wink Saville | d95ccc5 | 2013-09-13 12:40:11 -0700 | [diff] [blame] | 4862 | log("isMobileOk: X loops|timed out result=" + result); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4863 | return result; |
| 4864 | } catch (Exception e) { |
| 4865 | log("isMobileOk: Exception e=" + e); |
| 4866 | continue; |
| 4867 | } |
| 4868 | } |
| 4869 | log("isMobileOk: timed out"); |
| 4870 | } finally { |
| 4871 | log("isMobileOk: F stop hipri"); |
| 4872 | mCs.setEnableFailFastMobileData(DctConstants.DISABLED); |
| 4873 | mCs.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, |
| 4874 | Phone.FEATURE_ENABLE_HIPRI); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4875 | |
| 4876 | // Wait for hipri to disconnect. |
| 4877 | long endTime = SystemClock.elapsedRealtime() + 5000; |
| 4878 | |
| 4879 | while(SystemClock.elapsedRealtime() < endTime) { |
| 4880 | NetworkInfo.State state = mCs |
| 4881 | .getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI).getState(); |
| 4882 | if (state != NetworkInfo.State.DISCONNECTED) { |
| 4883 | if (VDBG) { |
| 4884 | log("isMobileOk: connected ni=" + |
| 4885 | mCs.getNetworkInfo(ConnectivityManager.TYPE_MOBILE_HIPRI)); |
| 4886 | } |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4887 | sleep(POLLING_SLEEP_SEC); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4888 | continue; |
| 4889 | } |
| 4890 | } |
| 4891 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4892 | log("isMobileOk: X result=" + result); |
| 4893 | } |
| 4894 | return result; |
| 4895 | } |
| 4896 | |
| 4897 | @Override |
| 4898 | protected Integer doInBackground(Params... params) { |
| 4899 | return isMobileOk(params[0]); |
| 4900 | } |
| 4901 | |
| 4902 | @Override |
| 4903 | protected void onPostExecute(Integer result) { |
| 4904 | log("onPostExecute: result=" + result); |
| 4905 | if ((mParams != null) && (mParams.mCb != null)) { |
| 4906 | mParams.mCb.onComplete(result); |
| 4907 | } |
| 4908 | } |
| 4909 | |
| 4910 | private String inetAddressesToString(InetAddress[] addresses) { |
| 4911 | StringBuffer sb = new StringBuffer(); |
| 4912 | boolean firstTime = true; |
| 4913 | for(InetAddress addr : addresses) { |
| 4914 | if (firstTime) { |
| 4915 | firstTime = false; |
| 4916 | } else { |
| 4917 | sb.append(","); |
| 4918 | } |
| 4919 | sb.append(addr); |
| 4920 | } |
| 4921 | return sb.toString(); |
| 4922 | } |
| 4923 | |
| 4924 | private void printNetworkInfo() { |
| 4925 | boolean hasIccCard = mTm.hasIccCard(); |
| 4926 | int simState = mTm.getSimState(); |
| 4927 | log("hasIccCard=" + hasIccCard |
| 4928 | + " simState=" + simState); |
| 4929 | NetworkInfo[] ni = mCs.getAllNetworkInfo(); |
| 4930 | if (ni != null) { |
| 4931 | log("ni.length=" + ni.length); |
| 4932 | for (NetworkInfo netInfo: ni) { |
| 4933 | log("netInfo=" + netInfo.toString()); |
| 4934 | } |
| 4935 | } else { |
| 4936 | log("no network info ni=null"); |
| 4937 | } |
| 4938 | } |
| 4939 | |
| 4940 | /** |
| 4941 | * Sleep for a few seconds then return. |
| 4942 | * @param seconds |
| 4943 | */ |
| 4944 | private static void sleep(int seconds) { |
Jake Hamby | 8f9b33e | 2014-01-15 13:08:03 -0800 | [diff] [blame] | 4945 | long stopTime = System.nanoTime() + (seconds * 1000000000); |
| 4946 | long sleepTime; |
| 4947 | while ((sleepTime = stopTime - System.nanoTime()) > 0) { |
| 4948 | try { |
| 4949 | Thread.sleep(sleepTime / 1000000); |
| 4950 | } catch (InterruptedException ignored) { |
| 4951 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4952 | } |
| 4953 | } |
| 4954 | |
Wink Saville | 1d3a89e | 2013-10-31 06:35:22 -0700 | [diff] [blame] | 4955 | private static void log(String s) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4956 | Slog.d(ConnectivityService.TAG, "[" + CHECKMP_TAG + "] " + s); |
| 4957 | } |
| 4958 | } |
| 4959 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4960 | // TODO: Move to ConnectivityManager and make public? |
| 4961 | private static final String CONNECTED_TO_PROVISIONING_NETWORK_ACTION = |
| 4962 | "com.android.server.connectivityservice.CONNECTED_TO_PROVISIONING_NETWORK_ACTION"; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4963 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4964 | private BroadcastReceiver mProvisioningReceiver = new BroadcastReceiver() { |
| 4965 | @Override |
| 4966 | public void onReceive(Context context, Intent intent) { |
| 4967 | if (intent.getAction().equals(CONNECTED_TO_PROVISIONING_NETWORK_ACTION)) { |
| 4968 | handleMobileProvisioningAction(intent.getStringExtra("EXTRA_URL")); |
| 4969 | } |
| 4970 | } |
| 4971 | }; |
| 4972 | |
| 4973 | private void handleMobileProvisioningAction(String url) { |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4974 | // Mark notification as not visible |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 4975 | setProvNotificationVisible(false, ConnectivityManager.TYPE_MOBILE_HIPRI, null, null); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4976 | |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4977 | // Check airplane mode |
| 4978 | boolean isAirplaneModeOn = Settings.System.getInt(mContext.getContentResolver(), |
| 4979 | Settings.Global.AIRPLANE_MODE_ON, 0) == 1; |
| 4980 | // If provisioning network and not in airplane mode handle as a special case, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4981 | // otherwise launch browser with the intent directly. |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4982 | if (mIsProvisioningNetwork.get() && !isAirplaneModeOn) { |
Robert Greenwalt | 42b746e | 2014-03-19 19:08:44 +0000 | [diff] [blame] | 4983 | if (DBG) log("handleMobileProvisioningAction: on prov network enable then launch"); |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4984 | mIsProvisioningNetwork.set(false); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4985 | // mIsStartingProvisioning.set(true); |
| 4986 | // MobileDataStateTracker mdst = (MobileDataStateTracker) |
| 4987 | // mNetTrackers[ConnectivityManager.TYPE_MOBILE]; |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4988 | // Radio was disabled on CMP_RESULT_CODE_PROVISIONING_NETWORK, enable it here |
Amit Mahajan | 0409e9f | 2014-06-10 10:47:26 -0700 | [diff] [blame] | 4989 | // mdst.setRadio(true); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4990 | // mdst.setEnableFailFastMobileData(DctConstants.ENABLED); |
| 4991 | // mdst.enableMobileProvisioning(url); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4992 | } else { |
Robert Greenwalt | 1b98e90 | 2014-03-19 12:41:54 -0700 | [diff] [blame] | 4993 | if (DBG) log("handleMobileProvisioningAction: not prov network"); |
Amit Mahajan | 20c5ce5 | 2014-06-06 16:53:34 -0700 | [diff] [blame] | 4994 | mIsProvisioningNetwork.set(false); |
Andrew Flynn | 0c3e1df | 2013-10-11 10:16:31 -0700 | [diff] [blame] | 4995 | // Check for apps that can handle provisioning first |
| 4996 | Intent provisioningIntent = new Intent(TelephonyIntents.ACTION_CARRIER_SETUP); |
| 4997 | provisioningIntent.addCategory(TelephonyIntents.CATEGORY_MCCMNC_PREFIX |
| 4998 | + mTelephonyManager.getSimOperator()); |
| 4999 | if (mContext.getPackageManager().resolveActivity(provisioningIntent, 0 /* flags */) |
| 5000 | != null) { |
| 5001 | provisioningIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 5002 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 5003 | mContext.startActivity(provisioningIntent); |
| 5004 | } else { |
| 5005 | // If no apps exist, use standard URL ACTION_VIEW method |
| 5006 | Intent newIntent = Intent.makeMainSelectorActivity(Intent.ACTION_MAIN, |
| 5007 | Intent.CATEGORY_APP_BROWSER); |
| 5008 | newIntent.setData(Uri.parse(url)); |
| 5009 | newIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 5010 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 5011 | try { |
| 5012 | mContext.startActivity(newIntent); |
| 5013 | } catch (ActivityNotFoundException e) { |
| 5014 | loge("handleMobileProvisioningAction: startActivity failed" + e); |
| 5015 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5016 | } |
| 5017 | } |
| 5018 | } |
| 5019 | |
| 5020 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
| 5021 | private volatile boolean mIsNotificationVisible = false; |
| 5022 | |
| 5023 | private void setProvNotificationVisible(boolean visible, int networkType, String extraInfo, |
| 5024 | String url) { |
| 5025 | if (DBG) { |
| 5026 | log("setProvNotificationVisible: E visible=" + visible + " networkType=" + networkType |
| 5027 | + " extraInfo=" + extraInfo + " url=" + url); |
| 5028 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5029 | |
| 5030 | Resources r = Resources.getSystem(); |
| 5031 | NotificationManager notificationManager = (NotificationManager) mContext |
| 5032 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 5033 | |
| 5034 | if (visible) { |
| 5035 | CharSequence title; |
| 5036 | CharSequence details; |
| 5037 | int icon; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5038 | Intent intent; |
| 5039 | Notification notification = new Notification(); |
| 5040 | switch (networkType) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5041 | case ConnectivityManager.TYPE_WIFI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5042 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 5043 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5044 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5045 | icon = R.drawable.stat_notify_wifi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5046 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 5047 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 5048 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 5049 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5050 | break; |
| 5051 | case ConnectivityManager.TYPE_MOBILE: |
| 5052 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5053 | title = r.getString(R.string.network_available_sign_in, 0); |
| 5054 | // TODO: Change this to pull from NetworkInfo once a printable |
| 5055 | // name has been added to it |
| 5056 | details = mTelephonyManager.getNetworkOperatorName(); |
| 5057 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5058 | intent = new Intent(CONNECTED_TO_PROVISIONING_NETWORK_ACTION); |
| 5059 | intent.putExtra("EXTRA_URL", url); |
| 5060 | intent.setFlags(0); |
| 5061 | notification.contentIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5062 | break; |
| 5063 | default: |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5064 | title = r.getString(R.string.network_available_sign_in, 0); |
| 5065 | details = r.getString(R.string.network_available_sign_in_detailed, |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5066 | extraInfo); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5067 | icon = R.drawable.stat_notify_rssi_in_range; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5068 | intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); |
| 5069 | intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | |
| 5070 | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 5071 | notification.contentIntent = PendingIntent.getActivity(mContext, 0, intent, 0); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5072 | break; |
| 5073 | } |
| 5074 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5075 | notification.when = 0; |
| 5076 | notification.icon = icon; |
| 5077 | notification.flags = Notification.FLAG_AUTO_CANCEL; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5078 | notification.tickerText = title; |
| 5079 | notification.setLatestEventInfo(mContext, title, details, notification.contentIntent); |
| 5080 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5081 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 5082 | notificationManager.notify(NOTIFICATION_ID, networkType, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5083 | } catch (NullPointerException npe) { |
| 5084 | loge("setNotificaitionVisible: visible notificationManager npe=" + npe); |
| 5085 | npe.printStackTrace(); |
| 5086 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5087 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5088 | try { |
Wink Saville | 3ec8e7f | 2013-10-03 08:34:46 -0700 | [diff] [blame] | 5089 | notificationManager.cancel(NOTIFICATION_ID, networkType); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5090 | } catch (NullPointerException npe) { |
| 5091 | loge("setNotificaitionVisible: cancel notificationManager npe=" + npe); |
| 5092 | npe.printStackTrace(); |
| 5093 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5094 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5095 | mIsNotificationVisible = visible; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5096 | } |
| 5097 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5098 | /** Location to an updatable file listing carrier provisioning urls. |
| 5099 | * An example: |
| 5100 | * |
| 5101 | * <?xml version="1.0" encoding="utf-8"?> |
| 5102 | * <provisioningUrls> |
| 5103 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
| 5104 | * <redirectedUrl mcc="310" mnc="4">http://www.google.com</redirectedUrl> |
| 5105 | * </provisioningUrls> |
| 5106 | */ |
| 5107 | private static final String PROVISIONING_URL_PATH = |
| 5108 | "/data/misc/radio/provisioning_urls.xml"; |
| 5109 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5110 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5111 | /** XML tag for root element. */ |
| 5112 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 5113 | /** XML tag for individual url */ |
| 5114 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
| 5115 | /** XML tag for redirected url */ |
| 5116 | private static final String TAG_REDIRECTED_URL = "redirectedUrl"; |
| 5117 | /** XML attribute for mcc */ |
| 5118 | private static final String ATTR_MCC = "mcc"; |
| 5119 | /** XML attribute for mnc */ |
| 5120 | private static final String ATTR_MNC = "mnc"; |
| 5121 | |
| 5122 | private static final int REDIRECTED_PROVISIONING = 1; |
| 5123 | private static final int PROVISIONING = 2; |
| 5124 | |
| 5125 | private String getProvisioningUrlBaseFromFile(int type) { |
| 5126 | FileReader fileReader = null; |
| 5127 | XmlPullParser parser = null; |
| 5128 | Configuration config = mContext.getResources().getConfiguration(); |
| 5129 | String tagType; |
| 5130 | |
| 5131 | switch (type) { |
| 5132 | case PROVISIONING: |
| 5133 | tagType = TAG_PROVISIONING_URL; |
| 5134 | break; |
| 5135 | case REDIRECTED_PROVISIONING: |
| 5136 | tagType = TAG_REDIRECTED_URL; |
| 5137 | break; |
| 5138 | default: |
| 5139 | throw new RuntimeException("getProvisioningUrlBaseFromFile: Unexpected parameter " + |
| 5140 | type); |
| 5141 | } |
| 5142 | |
| 5143 | try { |
| 5144 | fileReader = new FileReader(mProvisioningUrlFile); |
| 5145 | parser = Xml.newPullParser(); |
| 5146 | parser.setInput(fileReader); |
| 5147 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 5148 | |
| 5149 | while (true) { |
| 5150 | XmlUtils.nextElement(parser); |
| 5151 | |
| 5152 | String element = parser.getName(); |
| 5153 | if (element == null) break; |
| 5154 | |
| 5155 | if (element.equals(tagType)) { |
| 5156 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 5157 | try { |
| 5158 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 5159 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 5160 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 5161 | parser.next(); |
| 5162 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 5163 | return parser.getText(); |
| 5164 | } |
| 5165 | } |
| 5166 | } |
| 5167 | } catch (NumberFormatException e) { |
| 5168 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 5169 | } |
| 5170 | } |
| 5171 | } |
| 5172 | return null; |
| 5173 | } catch (FileNotFoundException e) { |
| 5174 | loge("Carrier Provisioning Urls file not found"); |
| 5175 | } catch (XmlPullParserException e) { |
| 5176 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 5177 | } catch (IOException e) { |
| 5178 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 5179 | } finally { |
| 5180 | if (fileReader != null) { |
| 5181 | try { |
| 5182 | fileReader.close(); |
| 5183 | } catch (IOException e) {} |
| 5184 | } |
| 5185 | } |
| 5186 | return null; |
| 5187 | } |
| 5188 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5189 | @Override |
| 5190 | public String getMobileRedirectedProvisioningUrl() { |
| 5191 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5192 | String url = getProvisioningUrlBaseFromFile(REDIRECTED_PROVISIONING); |
| 5193 | if (TextUtils.isEmpty(url)) { |
| 5194 | url = mContext.getResources().getString(R.string.mobile_redirected_provisioning_url); |
| 5195 | } |
| 5196 | return url; |
| 5197 | } |
| 5198 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5199 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5200 | public String getMobileProvisioningUrl() { |
| 5201 | enforceConnectivityInternalPermission(); |
| 5202 | String url = getProvisioningUrlBaseFromFile(PROVISIONING); |
| 5203 | if (TextUtils.isEmpty(url)) { |
| 5204 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5205 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5206 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 5207 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 5208 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5209 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5210 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5211 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 5212 | if (TextUtils.isEmpty(phoneNumber)) { |
| 5213 | phoneNumber = "0000000000"; |
| 5214 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5215 | url = String.format(url, |
| 5216 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 5217 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 5218 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5219 | } |
| 5220 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 5221 | return url; |
| 5222 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 5223 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 5224 | @Override |
| 5225 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
| 5226 | String extraInfo, String url) { |
| 5227 | enforceConnectivityInternalPermission(); |
| 5228 | setProvNotificationVisible(visible, networkType, extraInfo, url); |
| 5229 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 5230 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5231 | @Override |
| 5232 | public void setAirplaneMode(boolean enable) { |
| 5233 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5234 | final long ident = Binder.clearCallingIdentity(); |
| 5235 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 5236 | final ContentResolver cr = mContext.getContentResolver(); |
| 5237 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 5238 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 5239 | intent.putExtra("state", enable); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 5240 | mContext.sendBroadcast(intent); |
| 5241 | } finally { |
| 5242 | Binder.restoreCallingIdentity(ident); |
| 5243 | } |
| 5244 | } |
| 5245 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 5246 | private void onUserStart(int userId) { |
| 5247 | synchronized(mVpns) { |
| 5248 | Vpn userVpn = mVpns.get(userId); |
| 5249 | if (userVpn != null) { |
| 5250 | loge("Starting user already has a VPN"); |
| 5251 | return; |
| 5252 | } |
| 5253 | userVpn = new Vpn(mContext, mVpnCallback, mNetd, this, userId); |
| 5254 | mVpns.put(userId, userVpn); |
| 5255 | userVpn.startMonitoring(mContext, mTrackerHandler); |
| 5256 | } |
| 5257 | } |
| 5258 | |
| 5259 | private void onUserStop(int userId) { |
| 5260 | synchronized(mVpns) { |
| 5261 | Vpn userVpn = mVpns.get(userId); |
| 5262 | if (userVpn == null) { |
| 5263 | loge("Stopping user has no VPN"); |
| 5264 | return; |
| 5265 | } |
| 5266 | mVpns.delete(userId); |
| 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 5271 | @Override |
| 5272 | public void onReceive(Context context, Intent intent) { |
| 5273 | final String action = intent.getAction(); |
| 5274 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 5275 | if (userId == UserHandle.USER_NULL) return; |
| 5276 | |
| 5277 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 5278 | onUserStart(userId); |
| 5279 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 5280 | onUserStop(userId); |
| 5281 | } |
| 5282 | } |
| 5283 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5284 | |
| 5285 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5286 | public LinkQualityInfo getLinkQualityInfo(int networkType) { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5287 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5288 | if (isNetworkTypeValid(networkType) && mNetTrackers[networkType] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5289 | return mNetTrackers[networkType].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5290 | } else { |
| 5291 | return null; |
| 5292 | } |
| 5293 | } |
| 5294 | |
| 5295 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5296 | public LinkQualityInfo getActiveLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5297 | enforceAccessPermission(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5298 | if (isNetworkTypeValid(mActiveDefaultNetwork) && |
| 5299 | mNetTrackers[mActiveDefaultNetwork] != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5300 | return mNetTrackers[mActiveDefaultNetwork].getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5301 | } else { |
| 5302 | return null; |
| 5303 | } |
| 5304 | } |
| 5305 | |
| 5306 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5307 | public LinkQualityInfo[] getAllLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5308 | enforceAccessPermission(); |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5309 | final ArrayList<LinkQualityInfo> result = Lists.newArrayList(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5310 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5311 | if (tracker != null) { |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5312 | LinkQualityInfo li = tracker.getLinkQualityInfo(); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5313 | if (li != null) { |
| 5314 | result.add(li); |
| 5315 | } |
| 5316 | } |
| 5317 | } |
| 5318 | |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 5319 | return result.toArray(new LinkQualityInfo[result.size()]); |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 5320 | } |
| 5321 | |
| 5322 | /* Infrastructure for network sampling */ |
| 5323 | |
| 5324 | private void handleNetworkSamplingTimeout() { |
| 5325 | |
| 5326 | log("Sampling interval elapsed, updating statistics .."); |
| 5327 | |
| 5328 | // initialize list of interfaces .. |
| 5329 | Map<String, SamplingDataTracker.SamplingSnapshot> mapIfaceToSample = |
| 5330 | new HashMap<String, SamplingDataTracker.SamplingSnapshot>(); |
| 5331 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5332 | if (tracker != null) { |
| 5333 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5334 | if (ifaceName != null) { |
| 5335 | mapIfaceToSample.put(ifaceName, null); |
| 5336 | } |
| 5337 | } |
| 5338 | } |
| 5339 | |
| 5340 | // Read samples for all interfaces |
| 5341 | SamplingDataTracker.getSamplingSnapshots(mapIfaceToSample); |
| 5342 | |
| 5343 | // process samples for all networks |
| 5344 | for (NetworkStateTracker tracker : mNetTrackers) { |
| 5345 | if (tracker != null) { |
| 5346 | String ifaceName = tracker.getNetworkInterfaceName(); |
| 5347 | SamplingDataTracker.SamplingSnapshot ss = mapIfaceToSample.get(ifaceName); |
| 5348 | if (ss != null) { |
| 5349 | // end the previous sampling cycle |
| 5350 | tracker.stopSampling(ss); |
| 5351 | // start a new sampling cycle .. |
| 5352 | tracker.startSampling(ss); |
| 5353 | } |
| 5354 | } |
| 5355 | } |
| 5356 | |
| 5357 | log("Done."); |
| 5358 | |
| 5359 | int samplingIntervalInSeconds = Settings.Global.getInt(mContext.getContentResolver(), |
| 5360 | Settings.Global.CONNECTIVITY_SAMPLING_INTERVAL_IN_SECONDS, |
| 5361 | DEFAULT_SAMPLING_INTERVAL_IN_SECONDS); |
| 5362 | |
| 5363 | if (DBG) log("Setting timer for " + String.valueOf(samplingIntervalInSeconds) + "seconds"); |
| 5364 | |
| 5365 | setAlarm(samplingIntervalInSeconds * 1000, mSampleIntervalElapsedIntent); |
| 5366 | } |
| 5367 | |
| 5368 | void setAlarm(int timeoutInMilliseconds, PendingIntent intent) { |
| 5369 | long wakeupTime = SystemClock.elapsedRealtime() + timeoutInMilliseconds; |
| 5370 | mAlarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, wakeupTime, intent); |
| 5371 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5372 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5373 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 5374 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5375 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 5376 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5377 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5378 | private static class NetworkFactoryInfo { |
| 5379 | public final String name; |
| 5380 | public final Messenger messenger; |
| 5381 | public final AsyncChannel asyncChannel; |
| 5382 | |
| 5383 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 5384 | this.name = name; |
| 5385 | this.messenger = messenger; |
| 5386 | this.asyncChannel = asyncChannel; |
| 5387 | } |
| 5388 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5389 | |
| 5390 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
| 5391 | static final boolean REQUEST = true; |
| 5392 | static final boolean LISTEN = false; |
| 5393 | |
| 5394 | final NetworkRequest request; |
| 5395 | IBinder mBinder; |
| 5396 | final int mPid; |
| 5397 | final int mUid; |
| 5398 | final Messenger messenger; |
| 5399 | final boolean isRequest; |
| 5400 | |
| 5401 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, boolean isRequest) { |
| 5402 | super(); |
| 5403 | messenger = m; |
| 5404 | request = r; |
| 5405 | mBinder = binder; |
| 5406 | mPid = getCallingPid(); |
| 5407 | mUid = getCallingUid(); |
| 5408 | this.isRequest = isRequest; |
| 5409 | |
| 5410 | try { |
| 5411 | mBinder.linkToDeath(this, 0); |
| 5412 | } catch (RemoteException e) { |
| 5413 | binderDied(); |
| 5414 | } |
| 5415 | } |
| 5416 | |
| 5417 | void unlinkDeathRecipient() { |
| 5418 | mBinder.unlinkToDeath(this, 0); |
| 5419 | } |
| 5420 | |
| 5421 | public void binderDied() { |
| 5422 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 5423 | request + ", " + mBinder + ")"); |
| 5424 | releaseNetworkRequest(request); |
| 5425 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5426 | |
| 5427 | public String toString() { |
| 5428 | return (isRequest ? "Request" : "Listen") + " from uid/pid:" + mUid + "/" + |
| 5429 | mPid + " for " + request; |
| 5430 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | @Override |
| 5434 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5435 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5436 | if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED) |
| 5437 | == false) { |
| 5438 | enforceConnectivityInternalPermission(); |
| 5439 | } else { |
| 5440 | enforceChangePermission(); |
| 5441 | } |
| 5442 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5443 | if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5444 | throw new IllegalArgumentException("Bad timeout specified"); |
| 5445 | } |
| 5446 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5447 | networkCapabilities), legacyType, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5448 | if (DBG) log("requestNetwork for " + networkRequest); |
| 5449 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 5450 | NetworkRequestInfo.REQUEST); |
| 5451 | |
| 5452 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5453 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5454 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5455 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5456 | } |
| 5457 | return networkRequest; |
| 5458 | } |
| 5459 | |
| 5460 | @Override |
| 5461 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 5462 | PendingIntent operation) { |
| 5463 | // TODO |
| 5464 | return null; |
| 5465 | } |
| 5466 | |
| 5467 | @Override |
| 5468 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 5469 | Messenger messenger, IBinder binder) { |
| 5470 | enforceAccessPermission(); |
| 5471 | |
| 5472 | NetworkRequest networkRequest = new NetworkRequest(new NetworkCapabilities( |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5473 | networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5474 | if (DBG) log("listenForNetwork for " + networkRequest); |
| 5475 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
| 5476 | NetworkRequestInfo.LISTEN); |
| 5477 | |
| 5478 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 5479 | return networkRequest; |
| 5480 | } |
| 5481 | |
| 5482 | @Override |
| 5483 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 5484 | PendingIntent operation) { |
| 5485 | } |
| 5486 | |
| 5487 | @Override |
| 5488 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 5489 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), |
| 5490 | 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5491 | } |
| 5492 | |
| 5493 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5494 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5495 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5496 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 5497 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5498 | } |
| 5499 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5500 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
| 5501 | if (VDBG) log("Got NetworkFactory Messenger for " + nfi.name); |
| 5502 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 5503 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 5504 | } |
| 5505 | |
| 5506 | @Override |
| 5507 | public void unregisterNetworkFactory(Messenger messenger) { |
| 5508 | enforceConnectivityInternalPermission(); |
| 5509 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 5510 | } |
| 5511 | |
| 5512 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 5513 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 5514 | if (nfi == null) { |
| 5515 | if (VDBG) log("Failed to find Messenger in unregisterNetworkFactory"); |
| 5516 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5517 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5518 | if (VDBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5519 | } |
| 5520 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5521 | /** |
| 5522 | * NetworkAgentInfo supporting a request by requestId. |
| 5523 | * These have already been vetted (their Capabilities satisfy the request) |
| 5524 | * and the are the highest scored network available. |
| 5525 | * the are keyed off the Requests requestId. |
| 5526 | */ |
| 5527 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 5528 | new SparseArray<NetworkAgentInfo>(); |
| 5529 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5530 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 5531 | new SparseArray<NetworkAgentInfo>(); |
| 5532 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5533 | // NetworkAgentInfo keyed off its connecting messenger |
| 5534 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
| 5535 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 5536 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 5537 | |
| 5538 | private final NetworkRequest mDefaultRequest; |
| 5539 | |
| 5540 | public void registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 5541 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
| 5542 | int currentScore) { |
| 5543 | enforceConnectivityInternalPermission(); |
| 5544 | |
| 5545 | NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), nextNetId(), |
| 5546 | new NetworkInfo(networkInfo), new LinkProperties(linkProperties), |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 5547 | new NetworkCapabilities(networkCapabilities), currentScore, mContext, mTrackerHandler); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 5548 | if (VDBG) log("registerNetworkAgent " + nai); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5549 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
| 5550 | } |
| 5551 | |
| 5552 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 5553 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 5554 | mNetworkAgentInfos.put(na.messenger, na); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5555 | mNetworkForNetId.put(na.network.netId, na); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5556 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 5557 | NetworkInfo networkInfo = na.networkInfo; |
| 5558 | na.networkInfo = null; |
| 5559 | updateNetworkInfo(na, networkInfo); |
| 5560 | } |
| 5561 | |
| 5562 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 5563 | LinkProperties newLp = networkAgent.linkProperties; |
| 5564 | int netId = networkAgent.network.netId; |
| 5565 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5566 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5567 | updateMtu(newLp, oldLp); |
| 5568 | // TODO - figure out what to do for clat |
| 5569 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 5570 | // updateMtu(lp, null); |
| 5571 | // } |
| 5572 | updateRoutes(newLp, oldLp, netId); |
| 5573 | updateDnses(newLp, oldLp, netId); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5574 | updateClat(newLp, oldLp, networkAgent); |
| 5575 | } |
| 5576 | |
| 5577 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo na) { |
| 5578 | // Update 464xlat state. |
| 5579 | if (mClat.requiresClat(na)) { |
| 5580 | |
| 5581 | // If the connection was previously using clat, but is not using it now, stop the clat |
| 5582 | // daemon. Normally, this happens automatically when the connection disconnects, but if |
| 5583 | // the disconnect is not reported, or if the connection's LinkProperties changed for |
| 5584 | // some other reason (e.g., handoff changes the IP addresses on the link), it would |
| 5585 | // still be running. If it's not running, then stopping it is a no-op. |
| 5586 | if (Nat464Xlat.isRunningClat(oldLp) && !Nat464Xlat.isRunningClat(newLp)) { |
| 5587 | mClat.stopClat(); |
| 5588 | } |
| 5589 | // If the link requires clat to be running, then start the daemon now. |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 5590 | if (na.networkInfo.isConnected()) { |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5591 | mClat.startClat(na); |
| 5592 | } else { |
| 5593 | mClat.stopClat(); |
| 5594 | } |
| 5595 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5596 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5597 | |
| 5598 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5599 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 5600 | if (oldLp != null) { |
| 5601 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 5602 | } else if (newLp != null) { |
| 5603 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 5604 | } |
| 5605 | for (String iface : interfaceDiff.added) { |
| 5606 | try { |
| 5607 | mNetd.addInterfaceToNetwork(iface, netId); |
| 5608 | } catch (Exception e) { |
| 5609 | loge("Exception adding interface: " + e); |
| 5610 | } |
| 5611 | } |
| 5612 | for (String iface : interfaceDiff.removed) { |
| 5613 | try { |
| 5614 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 5615 | } catch (Exception e) { |
| 5616 | loge("Exception removing interface: " + e); |
| 5617 | } |
| 5618 | } |
| 5619 | } |
| 5620 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5621 | private void updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5622 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 5623 | if (oldLp != null) { |
| 5624 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 5625 | } else if (newLp != null) { |
| 5626 | routeDiff.added = newLp.getAllRoutes(); |
| 5627 | } |
| 5628 | |
| 5629 | // add routes before removing old in case it helps with continuous connectivity |
| 5630 | |
| 5631 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 5632 | for (RouteInfo route : routeDiff.added) { |
| 5633 | if (route.hasGateway()) continue; |
| 5634 | try { |
| 5635 | mNetd.addRoute(netId, route); |
| 5636 | } catch (Exception e) { |
| 5637 | loge("Exception in addRoute for non-gateway: " + e); |
| 5638 | } |
| 5639 | } |
| 5640 | for (RouteInfo route : routeDiff.added) { |
| 5641 | if (route.hasGateway() == false) continue; |
| 5642 | try { |
| 5643 | mNetd.addRoute(netId, route); |
| 5644 | } catch (Exception e) { |
| 5645 | loge("Exception in addRoute for gateway: " + e); |
| 5646 | } |
| 5647 | } |
| 5648 | |
| 5649 | for (RouteInfo route : routeDiff.removed) { |
| 5650 | try { |
| 5651 | mNetd.removeRoute(netId, route); |
| 5652 | } catch (Exception e) { |
| 5653 | loge("Exception in removeRoute: " + e); |
| 5654 | } |
| 5655 | } |
| 5656 | } |
| 5657 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5658 | if (oldLp == null || (newLp.isIdenticalDnses(oldLp) == false)) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 5659 | Collection<InetAddress> dnses = newLp.getDnsServers(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5660 | if (dnses.size() == 0 && mDefaultDns != null) { |
| 5661 | dnses = new ArrayList(); |
| 5662 | dnses.add(mDefaultDns); |
| 5663 | if (DBG) { |
| 5664 | loge("no dns provided for netId " + netId + ", so using defaults"); |
| 5665 | } |
| 5666 | } |
| 5667 | try { |
| 5668 | mNetd.setDnsServersForNetwork(netId, NetworkUtils.makeStrings(dnses), |
| 5669 | newLp.getDomains()); |
| 5670 | } catch (Exception e) { |
| 5671 | loge("Exception in setDnsServersForNetwork: " + e); |
| 5672 | } |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5673 | NetworkAgentInfo defaultNai = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 5674 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 5675 | setDefaultDnsSystemProperties(dnses); |
| 5676 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5677 | } |
| 5678 | } |
| 5679 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5680 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 5681 | int last = 0; |
| 5682 | for (InetAddress dns : dnses) { |
| 5683 | ++last; |
| 5684 | String key = "net.dns" + last; |
| 5685 | String value = dns.getHostAddress(); |
| 5686 | SystemProperties.set(key, value); |
| 5687 | } |
| 5688 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 5689 | String key = "net.dns" + i; |
| 5690 | SystemProperties.set(key, ""); |
| 5691 | } |
| 5692 | mNumDnsEntries = last; |
| 5693 | } |
| 5694 | |
| 5695 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5696 | private void updateCapabilities(NetworkAgentInfo networkAgent, |
| 5697 | NetworkCapabilities networkCapabilities) { |
| 5698 | // TODO - what else here? Verify still satisfies everybody? |
| 5699 | // Check if satisfies somebody new? call callbacks? |
| 5700 | networkAgent.networkCapabilities = networkCapabilities; |
| 5701 | } |
| 5702 | |
| 5703 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 5704 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5705 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 5706 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 5707 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5708 | } |
| 5709 | } |
| 5710 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5711 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5712 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5713 | if (nri.messenger == null) return; // Default request has no msgr |
| 5714 | Object o; |
| 5715 | int a1 = 0; |
| 5716 | int a2 = 0; |
| 5717 | switch (notificationType) { |
| 5718 | case ConnectivityManager.CALLBACK_LOSING: |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5719 | a1 = 30 * 1000; // TODO - read this from NetworkMonitor |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5720 | // fall through |
| 5721 | case ConnectivityManager.CALLBACK_PRECHECK: |
| 5722 | case ConnectivityManager.CALLBACK_AVAILABLE: |
| 5723 | case ConnectivityManager.CALLBACK_LOST: |
| 5724 | case ConnectivityManager.CALLBACK_CAP_CHANGED: |
| 5725 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
| 5726 | o = new NetworkRequest(nri.request); |
| 5727 | a2 = networkAgent.network.netId; |
| 5728 | break; |
| 5729 | } |
| 5730 | case ConnectivityManager.CALLBACK_UNAVAIL: |
| 5731 | case ConnectivityManager.CALLBACK_RELEASED: { |
| 5732 | o = new NetworkRequest(nri.request); |
| 5733 | break; |
| 5734 | } |
| 5735 | default: { |
| 5736 | loge("Unknown notificationType " + notificationType); |
| 5737 | return; |
| 5738 | } |
| 5739 | } |
| 5740 | Message msg = Message.obtain(); |
| 5741 | msg.arg1 = a1; |
| 5742 | msg.arg2 = a2; |
| 5743 | msg.obj = o; |
| 5744 | msg.what = notificationType; |
| 5745 | try { |
| 5746 | if (VDBG) log("sending notification " + notificationType + " for " + nri.request); |
| 5747 | nri.messenger.send(msg); |
| 5748 | } catch (RemoteException e) { |
| 5749 | // may occur naturally in the race of binder death. |
| 5750 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 5751 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5752 | } |
| 5753 | |
| 5754 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 5755 | if (oldNetwork == null) { |
| 5756 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 5757 | return; |
| 5758 | } |
| 5759 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 5760 | if (DBG) { |
| 5761 | if (oldNetwork.networkRequests.size() != 0) { |
| 5762 | loge("Dead network still had " + oldNetwork.networkRequests.size() + " requests"); |
| 5763 | } |
| 5764 | } |
| 5765 | oldNetwork.asyncChannel.disconnect(); |
| 5766 | } |
| 5767 | |
| 5768 | private void handleConnectionValidated(NetworkAgentInfo newNetwork) { |
| 5769 | if (newNetwork == null) { |
| 5770 | loge("Unknown NetworkAgentInfo in handleConnectionValidated"); |
| 5771 | return; |
| 5772 | } |
| 5773 | boolean keep = false; |
| 5774 | boolean isNewDefault = false; |
| 5775 | if (DBG) log("handleConnectionValidated for "+newNetwork.name()); |
| 5776 | // check if any NetworkRequest wants this NetworkAgent |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5777 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
| 5778 | if (VDBG) log(" new Network has: " + newNetwork.networkCapabilities); |
| 5779 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 5780 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 5781 | if (newNetwork == currentNetwork) { |
| 5782 | if (VDBG) log("Network " + newNetwork.name() + " was already satisfying" + |
| 5783 | " request " + nri.request.requestId + ". No change."); |
| 5784 | keep = true; |
| 5785 | continue; |
| 5786 | } |
| 5787 | |
| 5788 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5789 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
| 5790 | if (nri.request.networkCapabilities.satisfiedByNetworkCapabilities( |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5791 | newNetwork.networkCapabilities)) { |
| 5792 | // next check if it's better than any current network we're using for |
| 5793 | // this request |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5794 | if (VDBG) { |
| 5795 | log("currentScore = " + |
| 5796 | (currentNetwork != null ? currentNetwork.currentScore : 0) + |
| 5797 | ", newScore = " + newNetwork.currentScore); |
| 5798 | } |
| 5799 | if (currentNetwork == null || |
| 5800 | currentNetwork.currentScore < newNetwork.currentScore) { |
| 5801 | if (currentNetwork != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5802 | if (VDBG) log(" accepting network in place of " + currentNetwork.name()); |
| 5803 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 5804 | currentNetwork.networkLingered.add(nri.request); |
| 5805 | affectedNetworks.add(currentNetwork); |
| 5806 | } else { |
| 5807 | if (VDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5808 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5809 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 5810 | newNetwork.addRequest(nri.request); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5811 | int legacyType = nri.request.legacyType; |
| 5812 | if (legacyType != TYPE_NONE) { |
| 5813 | mLegacyTypeTracker.add(legacyType, newNetwork); |
| 5814 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5815 | keep = true; |
| 5816 | // TODO - this could get expensive if we have alot of requests for this |
| 5817 | // network. Think about if there is a way to reduce this. Push |
| 5818 | // netid->request mapping to each factory? |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5819 | sendUpdatedScoreToFactories(nri.request, newNetwork.currentScore); |
| 5820 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5821 | isNewDefault = true; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5822 | updateActiveDefaultNetwork(newNetwork); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5823 | if (newNetwork.linkProperties != null) { |
Robert Greenwalt | df2b878 | 2014-06-06 10:30:11 -0700 | [diff] [blame] | 5824 | setDefaultDnsSystemProperties( |
| 5825 | newNetwork.linkProperties.getDnsServers()); |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 5826 | } else { |
| 5827 | setDefaultDnsSystemProperties(new ArrayList<InetAddress>()); |
| 5828 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5829 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5830 | } |
| 5831 | } |
| 5832 | } |
| 5833 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5834 | for (NetworkAgentInfo nai : affectedNetworks) { |
| 5835 | boolean teardown = true; |
| 5836 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 5837 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 5838 | try { |
| 5839 | if (mNetworkRequests.get(nr).isRequest) { |
| 5840 | teardown = false; |
| 5841 | } |
| 5842 | } catch (Exception e) { |
| 5843 | loge("Request " + nr + " not found in mNetworkRequests."); |
| 5844 | loge(" it came from request list of " + nai.name()); |
| 5845 | } |
| 5846 | } |
| 5847 | if (teardown) { |
| 5848 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 5849 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 5850 | } else { |
| 5851 | // not going to linger, so kill the list of linger networks.. only |
| 5852 | // notify them of linger if it happens as the result of gaining another, |
| 5853 | // but if they transition and old network stays up, don't tell them of linger |
| 5854 | // or very delayed loss |
| 5855 | nai.networkLingered.clear(); |
| 5856 | if (VDBG) log("Lingered for " + nai.name() + " cleared"); |
| 5857 | } |
| 5858 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5859 | if (keep) { |
| 5860 | if (isNewDefault) { |
| 5861 | if (VDBG) log("Switching to new default network: " + newNetwork); |
| 5862 | setupDataActivityTracking(newNetwork); |
| 5863 | try { |
| 5864 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 5865 | } catch (Exception e) { |
| 5866 | loge("Exception setting default network :" + e); |
| 5867 | } |
| 5868 | if (newNetwork.equals(mNetworkForRequestId.get(mDefaultRequest.requestId))) { |
| 5869 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
| 5870 | } |
| 5871 | synchronized (ConnectivityService.this) { |
| 5872 | // have a new default network, release the transition wakelock in |
| 5873 | // a second if it's held. The second pause is to allow apps |
| 5874 | // to reconnect over the new network |
| 5875 | if (mNetTransitionWakeLock.isHeld()) { |
| 5876 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 5877 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 5878 | mNetTransitionWakeLockSerialNumber, 0), |
| 5879 | 1000); |
| 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | // this will cause us to come up initially as unconnected and switching |
| 5884 | // to connected after our normal pause unless somebody reports us as |
| 5885 | // really disconnected |
| 5886 | mDefaultInetConditionPublished = 0; |
| 5887 | mDefaultConnectionSequence++; |
| 5888 | mInetConditionChangeInFlight = false; |
| 5889 | // TODO - read the tcp buffer size config string from somewhere |
| 5890 | // updateNetworkSettings(); |
| 5891 | } |
| 5892 | // notify battery stats service about this network |
Dianne Hackborn | 2932513 | 2014-05-21 15:01:03 -0700 | [diff] [blame] | 5893 | try { |
| 5894 | BatteryStatsService.getService().noteNetworkInterfaceType( |
| 5895 | newNetwork.linkProperties.getInterfaceName(), |
| 5896 | newNetwork.networkInfo.getType()); |
| 5897 | } catch (RemoteException e) { } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5898 | notifyNetworkCallbacks(newNetwork, ConnectivityManager.CALLBACK_AVAILABLE); |
| 5899 | } else { |
| 5900 | if (DBG && newNetwork.networkRequests.size() != 0) { |
| 5901 | loge("tearing down network with live requests:"); |
| 5902 | for (int i=0; i < newNetwork.networkRequests.size(); i++) { |
| 5903 | loge(" " + newNetwork.networkRequests.valueAt(i)); |
| 5904 | } |
| 5905 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5906 | if (VDBG) log("Validated network turns out to be unwanted. Tear it down."); |
| 5907 | newNetwork.asyncChannel.disconnect(); |
| 5908 | } |
| 5909 | } |
| 5910 | |
| 5911 | |
| 5912 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 5913 | NetworkInfo.State state = newInfo.getState(); |
| 5914 | NetworkInfo oldInfo = networkAgent.networkInfo; |
| 5915 | networkAgent.networkInfo = newInfo; |
| 5916 | |
| 5917 | if (oldInfo != null && oldInfo.getState() == state) { |
| 5918 | if (VDBG) log("ignoring duplicate network state non-change"); |
| 5919 | return; |
| 5920 | } |
| 5921 | if (DBG) { |
| 5922 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 5923 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 5924 | " to " + state); |
| 5925 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5926 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5927 | if (state == NetworkInfo.State.CONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5928 | try { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 5929 | // This is likely caused by the fact that this network already |
| 5930 | // exists. An example is when a network goes from CONNECTED to |
| 5931 | // CONNECTING and back (like wifi on DHCP renew). |
| 5932 | // TODO: keep track of which networks we've created, or ask netd |
| 5933 | // to tell us whether we've already created this network or not. |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5934 | mNetd.createNetwork(networkAgent.network.netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5935 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 5936 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 5937 | + e.getMessage()); |
| 5938 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5939 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 5940 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5941 | updateLinkProperties(networkAgent, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5942 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 5943 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5944 | } else if (state == NetworkInfo.State.DISCONNECTED || |
| 5945 | state == NetworkInfo.State.SUSPENDED) { |
| 5946 | networkAgent.asyncChannel.disconnect(); |
| 5947 | } |
| 5948 | } |
| 5949 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 5950 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
| 5951 | if (DBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
| 5952 | |
| 5953 | nai.currentScore = score; |
| 5954 | |
| 5955 | // TODO - This will not do the right thing if this network is lowering |
| 5956 | // its score and has requests that can be served by other |
| 5957 | // currently-active networks, or if the network is increasing its |
| 5958 | // score and other networks have requests that can be better served |
| 5959 | // by this network. |
| 5960 | // |
| 5961 | // Really we want to see if any of our requests migrate to other |
| 5962 | // active/lingered networks and if any other requests migrate to us (depending |
| 5963 | // on increasing/decreasing currentScore. That's a bit of work and probably our |
| 5964 | // score checking/network allocation code needs to be modularized so we can understand |
| 5965 | // (see handleConnectionValided for an example). |
| 5966 | // |
| 5967 | // As a first order approx, lets just advertise the new score to factories. If |
| 5968 | // somebody can beat it they will nominate a network and our normal net replacement |
| 5969 | // code will fire. |
| 5970 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 5971 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 5972 | sendUpdatedScoreToFactories(nr, score); |
| 5973 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 5974 | } |
| 5975 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5976 | // notify only this one new request of the current state |
| 5977 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 5978 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 5979 | // TODO - read state from monitor to decide what to send. |
| 5980 | // if (nai.networkMonitor.isLingering()) { |
| 5981 | // notifyType = NetworkCallbacks.LOSING; |
| 5982 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 5983 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 5984 | // } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5985 | callCallbackForRequest(nri, nai, notifyType); |
| 5986 | } |
| 5987 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5988 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, boolean connected, int type) { |
| 5989 | if (connected) { |
| 5990 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 5991 | info.setType(type); |
| 5992 | sendConnectedBroadcastDelayed(info, getConnectivityChangeDelay()); |
| 5993 | } else { |
| 5994 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 5995 | info.setType(type); |
| 5996 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 5997 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 5998 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 5999 | if (info.isFailover()) { |
| 6000 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 6001 | nai.networkInfo.setFailover(false); |
| 6002 | } |
| 6003 | if (info.getReason() != null) { |
| 6004 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 6005 | } |
| 6006 | if (info.getExtraInfo() != null) { |
| 6007 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 6008 | } |
| 6009 | NetworkAgentInfo newDefaultAgent = null; |
| 6010 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 6011 | newDefaultAgent = mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 6012 | if (newDefaultAgent != null) { |
| 6013 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 6014 | newDefaultAgent.networkInfo); |
| 6015 | } else { |
| 6016 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 6017 | } |
| 6018 | } |
| 6019 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 6020 | mDefaultInetConditionPublished); |
| 6021 | final Intent immediateIntent = new Intent(intent); |
| 6022 | immediateIntent.setAction(CONNECTIVITY_ACTION_IMMEDIATE); |
| 6023 | sendStickyBroadcast(immediateIntent); |
| 6024 | sendStickyBroadcastDelayed(intent, getConnectivityChangeDelay()); |
| 6025 | if (newDefaultAgent != null) { |
| 6026 | sendConnectedBroadcastDelayed(newDefaultAgent.networkInfo, |
| 6027 | getConnectivityChangeDelay()); |
| 6028 | } |
| 6029 | } |
| 6030 | } |
| 6031 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6032 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
| 6033 | if (VDBG) log("notifyType " + notifyType + " for " + networkAgent.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6034 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6035 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 6036 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6037 | if (VDBG) log(" sending notification for " + nr); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6038 | callCallbackForRequest(nri, networkAgent, notifyType); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6039 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6040 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6041 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6042 | private LinkProperties getLinkPropertiesForTypeInternal(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6043 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 6044 | return (nai != null) ? |
| 6045 | new LinkProperties(nai.linkProperties) : |
| 6046 | new LinkProperties(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | private NetworkInfo getNetworkInfoForType(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6050 | if (!mLegacyTypeTracker.isTypeSupported(networkType)) |
| 6051 | return null; |
| 6052 | |
| 6053 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 6054 | if (nai != null) { |
| 6055 | NetworkInfo result = new NetworkInfo(nai.networkInfo); |
| 6056 | result.setType(networkType); |
| 6057 | return result; |
| 6058 | } else { |
| 6059 | return new NetworkInfo(networkType, 0, "Unknown", ""); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6060 | } |
| 6061 | } |
| 6062 | |
| 6063 | private NetworkCapabilities getNetworkCapabilitiesForType(int networkType) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6064 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 6065 | return (nai != null) ? |
| 6066 | new NetworkCapabilities(nai.networkCapabilities) : |
| 6067 | new NetworkCapabilities(); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6068 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6069 | } |