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 | |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.NETID_UNSET; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_NONE; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_VPN; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 24 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 25 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 26 | import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 27 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 28 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 29 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
| 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 31 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 32 | import static android.net.NetworkPolicyManager.RULE_ALLOW_METERED; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 33 | import static android.net.NetworkPolicyManager.MASK_METERED_NETWORKS; |
| 34 | import static android.net.NetworkPolicyManager.MASK_ALL_NETWORKS; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 35 | import static android.net.NetworkPolicyManager.RULE_NONE; |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 36 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 37 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 38 | import static android.net.NetworkPolicyManager.RULE_TEMPORARY_ALLOW_METERED; |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 39 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 40 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 41 | import android.annotation.Nullable; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 42 | import android.app.BroadcastOptions; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 43 | import android.app.Notification; |
| 44 | import android.app.NotificationManager; |
| 45 | import android.app.PendingIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 46 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.content.ContentResolver; |
| 48 | import android.content.Context; |
| 49 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 50 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.content.pm.PackageManager; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 52 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 53 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 54 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 56 | import android.net.ConnectivityManager.PacketKeepalive; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 58 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 59 | import android.net.INetworkPolicyListener; |
| 60 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 61 | import android.net.INetworkStatsService; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 62 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 63 | import android.net.LinkProperties.CompareResult; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 64 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 65 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 66 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 67 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 68 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 69 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 70 | import android.net.NetworkMisc; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 71 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 72 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 73 | import android.net.NetworkState; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 74 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 75 | import android.net.Proxy; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 76 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 77 | import android.net.RouteInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 78 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 79 | import android.net.Uri; |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 80 | import android.net.metrics.DefaultNetworkEvent; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 81 | import android.net.metrics.NetworkEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | import android.os.Binder; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 83 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 84 | import android.os.Bundle; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 85 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 87 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 88 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 89 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.os.Looper; |
| 91 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 92 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 93 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 94 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 95 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 96 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 97 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 99 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 100 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 102 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 103 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 104 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 105 | import android.text.TextUtils; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 106 | import android.util.ArraySet; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 107 | import android.util.LocalLog; |
| 108 | import android.util.LocalLog.ReadOnlyLocalLog; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 109 | import android.util.Log; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 110 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 111 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 112 | import android.util.SparseArray; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 113 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 114 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 115 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 117 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 118 | import com.android.internal.annotations.GuardedBy; |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 119 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 120 | import com.android.internal.app.IBatteryStats; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 121 | import com.android.internal.net.LegacyVpnInfo; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 122 | import com.android.internal.net.NetworkStatsFactory; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 123 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 124 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 125 | import com.android.internal.net.VpnProfile; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 126 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 127 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 128 | import com.android.internal.util.MessageUtils; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 129 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 130 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 131 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 132 | import com.android.server.connectivity.KeepaliveTracker; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 133 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 134 | import com.android.server.connectivity.NetworkAgentInfo; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 135 | import com.android.server.connectivity.NetworkDiagnostics; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 136 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 137 | import com.android.server.connectivity.PacManager; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 138 | import com.android.server.connectivity.PermissionMonitor; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 139 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 140 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 141 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 142 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 143 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 144 | import com.google.android.collect.Lists; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 145 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 146 | import org.xmlpull.v1.XmlPullParser; |
| 147 | import org.xmlpull.v1.XmlPullParserException; |
| 148 | |
| 149 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 151 | import java.io.FileNotFoundException; |
| 152 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 153 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | import java.io.PrintWriter; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 155 | import java.net.Inet4Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 156 | import java.net.InetAddress; |
| 157 | import java.net.UnknownHostException; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 158 | import java.util.ArrayDeque; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 159 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 160 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 161 | import java.util.Collection; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 162 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 163 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 164 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 165 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 166 | import java.util.Objects; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 167 | import java.util.SortedSet; |
| 168 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * @hide |
| 172 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 173 | public class ConnectivityService extends IConnectivityManager.Stub |
| 174 | implements PendingIntent.OnFinished { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 175 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 177 | private static final boolean DBG = true; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 178 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 180 | private static final boolean LOGD_RULES = false; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 181 | private static final boolean LOGD_BLOCKED_NETWORKINFO = true; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 182 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 183 | // TODO: create better separation between radio types and network types |
| 184 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 185 | // how long to wait before switching back to a radio's default network |
| 186 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 187 | // system property that can override the above value |
| 188 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 189 | "android.telephony.apn-restore"; |
| 190 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 191 | // How long to wait before putting up a "This network doesn't have an Internet connection, |
| 192 | // connect anyway?" dialog after the user selects a network that doesn't validate. |
| 193 | private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000; |
| 194 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 195 | // How long to delay to removal of a pending intent based request. |
| 196 | // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS |
| 197 | private final int mReleasePendingIntentDelayMs; |
| 198 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 199 | private Tethering mTethering; |
| 200 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 201 | private final PermissionMonitor mPermissionMonitor; |
| 202 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 203 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 204 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 205 | @GuardedBy("mVpns") |
| 206 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 207 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 208 | private boolean mLockdownEnabled; |
| 209 | private LockdownVpnTracker mLockdownTracker; |
| 210 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 211 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 212 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 213 | /** Currently active network rules by UID. */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 214 | @GuardedBy("mRulesLock") |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 215 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 216 | /** Set of ifaces that are costly. */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 217 | @GuardedBy("mRulesLock") |
| 218 | private ArraySet<String> mMeteredIfaces = new ArraySet<>(); |
| 219 | /** Flag indicating if background data is restricted. */ |
| 220 | @GuardedBy("mRulesLock") |
| 221 | private boolean mRestrictBackground; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 222 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 223 | final private Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | private int mNetworkPreference; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 225 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 226 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 228 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
| 230 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 231 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 233 | private INetworkManagementService mNetd; |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 234 | private INetworkStatsService mStatsService; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 235 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 236 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 237 | private String mCurrentTcpBufferSizes; |
| 238 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 239 | private static final int ENABLED = 1; |
| 240 | private static final int DISABLED = 0; |
| 241 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 242 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
| 243 | new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class }); |
| 244 | |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 245 | private enum ReapUnvalidatedNetworks { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 246 | // Tear down networks that have no chance (e.g. even if validated) of becoming |
| 247 | // the highest scoring network satisfying a NetworkRequest. This should be passed when |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 248 | // all networks have been rematched against all NetworkRequests. |
| 249 | REAP, |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 250 | // Don't reap networks. This should be passed when some networks have not yet been |
| 251 | // rematched against all NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 252 | DONT_REAP |
| 253 | }; |
| 254 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 255 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 256 | * used internally to change our mobile data enabled flag |
| 257 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 258 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 259 | |
| 260 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 261 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 262 | * from one net to another. Clear happens when we get a new |
| 263 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 264 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 265 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 266 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 267 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 268 | /** |
| 269 | * used internally to reload global proxy settings |
| 270 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 271 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 272 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 273 | /** |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 274 | * PAC manager has received new port. |
| 275 | */ |
| 276 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 277 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 278 | /** |
| 279 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 280 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 281 | */ |
| 282 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 283 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 284 | /** |
| 285 | * used internally when registering NetworkAgents |
| 286 | * obj = Messenger |
| 287 | */ |
| 288 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 289 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 290 | /** |
| 291 | * used to add a network request |
| 292 | * includes a NetworkRequestInfo |
| 293 | */ |
| 294 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 295 | |
| 296 | /** |
| 297 | * indicates a timeout period is over - check if we had a network yet or not |
| 298 | * and if not, call the timeout calback (but leave the request live until they |
| 299 | * cancel it. |
| 300 | * includes a NetworkRequestInfo |
| 301 | */ |
| 302 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 303 | |
| 304 | /** |
| 305 | * used to add a network listener - no request |
| 306 | * includes a NetworkRequestInfo |
| 307 | */ |
| 308 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 309 | |
| 310 | /** |
| 311 | * 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] | 312 | * arg1 = UID of caller |
| 313 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 314 | */ |
| 315 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 316 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 317 | /** |
| 318 | * used internally when registering NetworkFactories |
| 319 | * obj = Messenger |
| 320 | */ |
| 321 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 322 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 323 | /** |
| 324 | * used internally to expire a wakelock when transitioning |
| 325 | * from one net to another. Expire happens when we fail to find |
| 326 | * a new network (typically after 1 minute) - |
| 327 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 328 | * a replacement network. |
| 329 | */ |
| 330 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 331 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 332 | /** |
| 333 | * Used internally to indicate the system is ready. |
| 334 | */ |
| 335 | private static final int EVENT_SYSTEM_READY = 25; |
| 336 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 337 | /** |
| 338 | * used to add a network request with a pending intent |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 339 | * obj = NetworkRequestInfo |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 340 | */ |
| 341 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 342 | |
| 343 | /** |
| 344 | * used to remove a pending intent and its associated network request. |
| 345 | * arg1 = UID of caller |
| 346 | * obj = PendingIntent |
| 347 | */ |
| 348 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 349 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 350 | /** |
| 351 | * used to specify whether a network should be used even if unvalidated. |
| 352 | * arg1 = whether to accept the network if it's unvalidated (1 or 0) |
| 353 | * arg2 = whether to remember this choice in the future (1 or 0) |
| 354 | * obj = network |
| 355 | */ |
| 356 | private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28; |
| 357 | |
| 358 | /** |
| 359 | * used to ask the user to confirm a connection to an unvalidated network. |
| 360 | * obj = network |
| 361 | */ |
| 362 | private static final int EVENT_PROMPT_UNVALIDATED = 29; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 363 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 364 | /** |
| 365 | * used internally to (re)configure mobile data always-on settings. |
| 366 | */ |
| 367 | private static final int EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON = 30; |
| 368 | |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 369 | /** |
| 370 | * used to add a network listener with a pending intent |
| 371 | * obj = NetworkRequestInfo |
| 372 | */ |
| 373 | private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31; |
| 374 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 375 | /** Handler thread used for both of the handlers below. */ |
| 376 | @VisibleForTesting |
| 377 | protected final HandlerThread mHandlerThread; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 378 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 379 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 380 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 381 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 382 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 383 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 384 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 385 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 386 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 387 | private String mNetTransitionWakeLockCausedBy = ""; |
| 388 | private int mNetTransitionWakeLockSerialNumber; |
| 389 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 390 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 391 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 392 | // used in DBG mode to track inet condition reports |
| 393 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 394 | private ArrayList mInetLog; |
| 395 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 396 | // track the current default http proxy - tell the world if we get a new one (real change) |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 397 | private volatile ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 398 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 399 | private boolean mDefaultProxyDisabled = false; |
| 400 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 401 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 402 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 403 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 404 | private PacManager mPacManager = null; |
| 405 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 406 | final private SettingsObserver mSettingsObserver; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 407 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 408 | private UserManager mUserManager; |
| 409 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 410 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 411 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 412 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 413 | // the set of network types that can only be enabled by system/sig apps |
| 414 | List mProtectedNetworks; |
| 415 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 416 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 417 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 418 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 419 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 420 | private KeepaliveTracker mKeepaliveTracker; |
| 421 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 422 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
| 423 | private final static int MIN_NET_ID = 100; // some reserved marks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 424 | private final static int MAX_NET_ID = 65535; |
| 425 | private int mNextNetId = MIN_NET_ID; |
| 426 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 427 | // sequence number of NetworkRequests |
| 428 | private int mNextNetworkRequestId = 1; |
| 429 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 430 | // NetworkRequest activity String log entries. |
| 431 | private static final int MAX_NETWORK_REQUEST_LOGS = 20; |
| 432 | private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS); |
| 433 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 434 | // Array of <Network,ReadOnlyLocalLogs> tracking network validation and results |
| 435 | private static final int MAX_VALIDATION_LOGS = 10; |
| 436 | private final ArrayDeque<Pair<Network,ReadOnlyLocalLog>> mValidationLogs = |
| 437 | new ArrayDeque<Pair<Network,ReadOnlyLocalLog>>(MAX_VALIDATION_LOGS); |
| 438 | |
| 439 | private void addValidationLogs(ReadOnlyLocalLog log, Network network) { |
| 440 | synchronized(mValidationLogs) { |
| 441 | while (mValidationLogs.size() >= MAX_VALIDATION_LOGS) { |
| 442 | mValidationLogs.removeLast(); |
| 443 | } |
| 444 | mValidationLogs.addFirst(new Pair(network, log)); |
| 445 | } |
| 446 | } |
| 447 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 448 | /** |
| 449 | * Implements support for the legacy "one network per network type" model. |
| 450 | * |
| 451 | * We used to have a static array of NetworkStateTrackers, one for each |
| 452 | * network type, but that doesn't work any more now that we can have, |
| 453 | * for example, more that one wifi network. This class stores all the |
| 454 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 455 | * API will only see the first one. |
| 456 | * |
| 457 | * It serves two main purposes: |
| 458 | * |
| 459 | * 1. Provide information about "the network for a given type" (since this |
| 460 | * API only supports one). |
| 461 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 462 | * the first network for a given type changes, or if the default network |
| 463 | * changes. |
| 464 | */ |
| 465 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 466 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 467 | private static final boolean DBG = true; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 468 | private static final boolean VDBG = false; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 469 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 470 | /** |
| 471 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 472 | * Each list holds references to all NetworkAgentInfos that are used to |
| 473 | * satisfy requests for that network type. |
| 474 | * |
| 475 | * This array is built out at startup such that an unsupported network |
| 476 | * doesn't get an ArrayList instance, making this a tristate: |
| 477 | * unsupported, supported but not active and active. |
| 478 | * |
| 479 | * The actual lists are populated when we scan the network types that |
| 480 | * are supported on this device. |
| 481 | */ |
| 482 | private ArrayList<NetworkAgentInfo> mTypeLists[]; |
| 483 | |
| 484 | public LegacyTypeTracker() { |
| 485 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 486 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 487 | } |
| 488 | |
| 489 | public void addSupportedType(int type) { |
| 490 | if (mTypeLists[type] != null) { |
| 491 | throw new IllegalStateException( |
| 492 | "legacy list for type " + type + "already initialized"); |
| 493 | } |
| 494 | mTypeLists[type] = new ArrayList<NetworkAgentInfo>(); |
| 495 | } |
| 496 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 497 | public boolean isTypeSupported(int type) { |
| 498 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 499 | } |
| 500 | |
| 501 | public NetworkAgentInfo getNetworkForType(int type) { |
| 502 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 503 | return mTypeLists[type].get(0); |
| 504 | } else { |
| 505 | return null; |
| 506 | } |
| 507 | } |
| 508 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 509 | private void maybeLogBroadcast(NetworkAgentInfo nai, DetailedState state, int type, |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 510 | boolean isDefaultNetwork) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 511 | if (DBG) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 512 | log("Sending " + state + |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 513 | " broadcast for type " + type + " " + nai.name() + |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 514 | " isDefaultNetwork=" + isDefaultNetwork); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 519 | public void add(int type, NetworkAgentInfo nai) { |
| 520 | if (!isTypeSupported(type)) { |
| 521 | return; // Invalid network type. |
| 522 | } |
| 523 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 524 | |
| 525 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 526 | if (list.contains(nai)) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 527 | return; |
| 528 | } |
| 529 | |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 530 | list.add(nai); |
| 531 | |
| 532 | // Send a broadcast if this is the first network of its type or if it's the default. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 533 | final boolean isDefaultNetwork = isDefaultNetwork(nai); |
| 534 | if (list.size() == 1 || isDefaultNetwork) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 535 | maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork); |
| 536 | sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 537 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 538 | } |
| 539 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 540 | /** Removes the given network from the specified legacy type list. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 541 | public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 542 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 543 | if (list == null || list.isEmpty()) { |
| 544 | return; |
| 545 | } |
| 546 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 547 | final boolean wasFirstNetwork = list.get(0).equals(nai); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 548 | |
| 549 | if (!list.remove(nai)) { |
| 550 | return; |
| 551 | } |
| 552 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 553 | final DetailedState state = DetailedState.DISCONNECTED; |
| 554 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 555 | if (wasFirstNetwork || wasDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 556 | maybeLogBroadcast(nai, state, type, wasDefault); |
| 557 | sendLegacyNetworkBroadcast(nai, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | if (!list.isEmpty() && wasFirstNetwork) { |
| 561 | if (DBG) log("Other network available for type " + type + |
| 562 | ", sending connected broadcast"); |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 563 | final NetworkAgentInfo replacement = list.get(0); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 564 | maybeLogBroadcast(replacement, state, type, isDefaultNetwork(replacement)); |
| 565 | sendLegacyNetworkBroadcast(replacement, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | /** Removes the given network from all legacy type lists. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 570 | public void remove(NetworkAgentInfo nai, boolean wasDefault) { |
| 571 | if (VDBG) log("Removing agent " + nai + " wasDefault=" + wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 572 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 573 | remove(type, nai, wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 574 | } |
| 575 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 576 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 577 | // send out another legacy broadcast - currently only used for suspend/unsuspend |
| 578 | // toggle |
| 579 | public void update(NetworkAgentInfo nai) { |
| 580 | final boolean isDefault = isDefaultNetwork(nai); |
| 581 | final DetailedState state = nai.networkInfo.getDetailedState(); |
| 582 | for (int type = 0; type < mTypeLists.length; type++) { |
| 583 | final ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 584 | final boolean contains = (list != null && list.contains(nai)); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 585 | final boolean isFirst = (list != null && list.size() > 0 && nai == list.get(0)); |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 586 | if (isFirst || (contains && isDefault)) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 587 | maybeLogBroadcast(nai, state, type, isDefault); |
| 588 | sendLegacyNetworkBroadcast(nai, state, type); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 593 | private String naiToString(NetworkAgentInfo nai) { |
| 594 | String name = (nai != null) ? nai.name() : "null"; |
| 595 | String state = (nai.networkInfo != null) ? |
| 596 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 597 | "???/???"; |
| 598 | return name + " " + state; |
| 599 | } |
| 600 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 601 | public void dump(IndentingPrintWriter pw) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 602 | pw.println("mLegacyTypeTracker:"); |
| 603 | pw.increaseIndent(); |
| 604 | pw.print("Supported types:"); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 605 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 606 | if (mTypeLists[type] != null) pw.print(" " + type); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 607 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 608 | pw.println(); |
| 609 | pw.println("Current state:"); |
| 610 | pw.increaseIndent(); |
| 611 | for (int type = 0; type < mTypeLists.length; type++) { |
| 612 | if (mTypeLists[type] == null|| mTypeLists[type].size() == 0) continue; |
| 613 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
| 614 | pw.println(type + " " + naiToString(nai)); |
| 615 | } |
| 616 | } |
| 617 | pw.decreaseIndent(); |
| 618 | pw.decreaseIndent(); |
| 619 | pw.println(); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 620 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 621 | } |
| 622 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 623 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 624 | @VisibleForTesting |
| 625 | protected HandlerThread createHandlerThread() { |
| 626 | return new HandlerThread("ConnectivityServiceThread"); |
| 627 | } |
| 628 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 629 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 630 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 631 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 632 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 633 | mDefaultRequest = createInternetRequestForTransport(-1); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 634 | NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 635 | new Binder(), NetworkRequestType.REQUEST); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 636 | mNetworkRequests.put(mDefaultRequest, defaultNRI); |
| 637 | mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 638 | |
| 639 | mDefaultMobileDataRequest = createInternetRequestForTransport( |
| 640 | NetworkCapabilities.TRANSPORT_CELLULAR); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 641 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 642 | mHandlerThread = createHandlerThread(); |
| 643 | mHandlerThread.start(); |
| 644 | mHandler = new InternalHandler(mHandlerThread.getLooper()); |
| 645 | mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 646 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 647 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 648 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 649 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 650 | Settings.Secure.ANDROID_ID); |
| 651 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 652 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 653 | SystemProperties.set("net.hostname", name); |
| 654 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 655 | } |
| 656 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 657 | mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(), |
| 658 | Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000); |
| 659 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 660 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 661 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 662 | mStatsService = checkNotNull(statsService, "missing INetworkStatsService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 663 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 664 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 665 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 666 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 667 | try { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 668 | mPolicyManager.setConnectivityListener(mPolicyListener); |
| 669 | mRestrictBackground = mPolicyManager.getRestrictBackground(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 670 | } catch (RemoteException e) { |
| 671 | // ouch, no rules updates means some processes may never get network |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 672 | loge("unable to register INetworkPolicyListener" + e); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 676 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 677 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 678 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 679 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 680 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 681 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 682 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 683 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 684 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 685 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 686 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 687 | String[] naStrings = context.getResources().getStringArray( |
| 688 | com.android.internal.R.array.networkAttributes); |
| 689 | for (String naString : naStrings) { |
| 690 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 691 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 692 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 693 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 694 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 695 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 696 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 697 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 698 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 699 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 700 | n.type); |
| 701 | continue; |
| 702 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 703 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 704 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 705 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 706 | continue; |
| 707 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 708 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 709 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 710 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 711 | mNetworksDefined++; |
| 712 | } catch(Exception e) { |
| 713 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 714 | } |
| 715 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 716 | |
| 717 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 718 | if (mNetConfigs[TYPE_VPN] == null) { |
| 719 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 720 | // don't need to add TYPE_VPN to mNetConfigs. |
| 721 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 722 | mNetworksDefined++; // used only in the log() statement below. |
| 723 | } |
| 724 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 725 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 726 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 727 | mProtectedNetworks = new ArrayList<Integer>(); |
| 728 | int[] protectedNetworks = context.getResources().getIntArray( |
| 729 | com.android.internal.R.array.config_protectedNetworks); |
| 730 | for (int p : protectedNetworks) { |
| 731 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 732 | mProtectedNetworks.add(p); |
| 733 | } else { |
| 734 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 735 | } |
| 736 | } |
| 737 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 738 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 739 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 740 | |
Jeremy Klein | fa8712b | 2016-01-26 11:10:55 -0800 | [diff] [blame] | 741 | mTethering = new Tethering(mContext, mNetd, statsService); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 742 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 743 | mPermissionMonitor = new PermissionMonitor(mContext, mNetd); |
| 744 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 745 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 746 | IntentFilter intentFilter = new IntentFilter(); |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 747 | intentFilter.addAction(Intent.ACTION_USER_STARTED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 748 | intentFilter.addAction(Intent.ACTION_USER_STOPPED); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 749 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 750 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 751 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 752 | mContext.registerReceiverAsUser( |
| 753 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 754 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 755 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 756 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 757 | mNetd.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 758 | } catch (RemoteException e) { |
| 759 | loge("Error registering observer :" + e); |
| 760 | } |
| 761 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 762 | if (DBG) { |
| 763 | mInetLog = new ArrayList(); |
| 764 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 765 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 766 | mSettingsObserver = new SettingsObserver(mContext, mHandler); |
| 767 | registerSettingsCallbacks(); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 768 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 769 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 770 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 771 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 772 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 773 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 774 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 775 | |
| 776 | mKeepaliveTracker = new KeepaliveTracker(mHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 777 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 778 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 779 | private NetworkRequest createInternetRequestForTransport(int transportType) { |
| 780 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 781 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 782 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 783 | if (transportType > -1) { |
| 784 | netCap.addTransportType(transportType); |
| 785 | } |
| 786 | return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); |
| 787 | } |
| 788 | |
| 789 | private void handleMobileDataAlwaysOn() { |
| 790 | final boolean enable = (Settings.Global.getInt( |
| 791 | mContext.getContentResolver(), Settings.Global.MOBILE_DATA_ALWAYS_ON, 0) == 1); |
| 792 | final boolean isEnabled = (mNetworkRequests.get(mDefaultMobileDataRequest) != null); |
| 793 | if (enable == isEnabled) { |
| 794 | return; // Nothing to do. |
| 795 | } |
| 796 | |
| 797 | if (enable) { |
| 798 | handleRegisterNetworkRequest(new NetworkRequestInfo( |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 799 | null, mDefaultMobileDataRequest, new Binder(), NetworkRequestType.REQUEST)); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 800 | } else { |
| 801 | handleReleaseNetworkRequest(mDefaultMobileDataRequest, Process.SYSTEM_UID); |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | private void registerSettingsCallbacks() { |
| 806 | // Watch for global HTTP proxy changes. |
| 807 | mSettingsObserver.observe( |
| 808 | Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), |
| 809 | EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 810 | |
| 811 | // Watch for whether or not to keep mobile data always on. |
| 812 | mSettingsObserver.observe( |
| 813 | Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), |
| 814 | EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON); |
| 815 | } |
| 816 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 817 | private synchronized int nextNetworkRequestId() { |
| 818 | return mNextNetworkRequestId++; |
| 819 | } |
| 820 | |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 821 | @VisibleForTesting |
| 822 | protected int reserveNetId() { |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 823 | synchronized (mNetworkForNetId) { |
| 824 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 825 | int netId = mNextNetId; |
| 826 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 827 | // Make sure NetID unused. http://b/16815182 |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 828 | if (!mNetIdInUse.get(netId)) { |
| 829 | mNetIdInUse.put(netId, true); |
| 830 | return netId; |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 831 | } |
| 832 | } |
| 833 | } |
| 834 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 835 | } |
| 836 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 837 | private NetworkState getFilteredNetworkState(int networkType, int uid, boolean ignoreBlocked) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 838 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 839 | final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 840 | final NetworkState state; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 841 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 842 | state = nai.getNetworkState(); |
| 843 | state.networkInfo.setType(networkType); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 844 | } else { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 845 | final NetworkInfo info = new NetworkInfo(networkType, 0, |
| 846 | getNetworkTypeName(networkType), ""); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 847 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 848 | info.setIsAvailable(true); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 849 | state = new NetworkState(info, new LinkProperties(), new NetworkCapabilities(), |
| 850 | null, null, null); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 851 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 852 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 853 | return state; |
| 854 | } else { |
| 855 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 856 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 857 | } |
| 858 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 859 | private NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
| 860 | if (network == null) { |
| 861 | return null; |
| 862 | } |
| 863 | synchronized (mNetworkForNetId) { |
| 864 | return mNetworkForNetId.get(network.netId); |
| 865 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 866 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 867 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 868 | private Network[] getVpnUnderlyingNetworks(int uid) { |
| 869 | if (!mLockdownEnabled) { |
| 870 | int user = UserHandle.getUserId(uid); |
| 871 | synchronized (mVpns) { |
| 872 | Vpn vpn = mVpns.get(user); |
| 873 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 874 | return vpn.getUnderlyingNetworks(); |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | return null; |
| 879 | } |
| 880 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 881 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 882 | NetworkAgentInfo nai = getDefaultNetwork(); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 883 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 884 | final Network[] networks = getVpnUnderlyingNetworks(uid); |
| 885 | if (networks != null) { |
| 886 | // getUnderlyingNetworks() returns: |
| 887 | // null => there was no VPN, or the VPN didn't specify anything, so we use the default. |
| 888 | // empty array => the VPN explicitly said "no default network". |
| 889 | // non-empty array => the VPN specified one or more default networks; we use the |
| 890 | // first one. |
| 891 | if (networks.length > 0) { |
| 892 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 893 | } else { |
| 894 | nai = null; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 898 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 899 | return nai.getNetworkState(); |
| 900 | } else { |
| 901 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 902 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | /** |
| 906 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 907 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 908 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid, |
| 909 | boolean ignoreBlocked) { |
| 910 | // Networks aren't blocked when ignoring blocked status |
| 911 | if (ignoreBlocked) return false; |
| 912 | // Networks are never blocked for system services |
| 913 | if (uid < Process.FIRST_APPLICATION_UID) return false; |
| 914 | |
| 915 | final boolean networkMetered; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 916 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 917 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 918 | synchronized (mVpns) { |
| 919 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
| 920 | if (vpn != null && vpn.isBlockingUid(uid)) { |
| 921 | return true; |
| 922 | } |
| 923 | } |
| 924 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 925 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 926 | synchronized (mRulesLock) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 927 | networkMetered = mMeteredIfaces.contains(iface); |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 928 | uidRules = mUidRules.get(uid, RULE_NONE); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 929 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 930 | |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 931 | boolean allowed = true; |
| 932 | // Check Data Saver Mode first... |
| 933 | if (networkMetered) { |
| 934 | if ((uidRules & RULE_REJECT_METERED) != 0) { |
| 935 | if (LOGD_RULES) Log.d(TAG, "uid " + uid + " is blacklisted"); |
| 936 | // Explicitly blacklisted. |
| 937 | allowed = false; |
| 938 | } else { |
| 939 | allowed = !mRestrictBackground |
| 940 | || (uidRules & RULE_ALLOW_METERED) != 0 |
| 941 | || (uidRules & RULE_TEMPORARY_ALLOW_METERED) != 0; |
| 942 | if (LOGD_RULES) Log.d(TAG, "allowed status for uid " + uid + " when" |
| 943 | + " mRestrictBackground=" + mRestrictBackground |
| 944 | + ", whitelisted=" + ((uidRules & RULE_ALLOW_METERED) != 0) |
| 945 | + ", tempWhitelist= + ((uidRules & RULE_TEMPORARY_ALLOW_METERED) != 0)" |
| 946 | + ": " + allowed); |
| 947 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 948 | } |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 949 | // ...then power restrictions. |
| 950 | if (allowed) { |
| 951 | allowed = (uidRules & RULE_REJECT_ALL) == 0; |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 952 | if (LOGD_RULES) Log.d(TAG, "allowed status for uid " + uid + " when" |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 953 | + " rule is " + uidRulesToString(uidRules) + ": " + allowed); |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 954 | } |
| 955 | return !allowed; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 956 | } |
| 957 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 958 | private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) { |
| 959 | if (ni == null || !LOGD_BLOCKED_NETWORKINFO) return; |
| 960 | boolean removed = false; |
| 961 | boolean added = false; |
| 962 | synchronized (mBlockedAppUids) { |
| 963 | if (ni.getDetailedState() == DetailedState.BLOCKED && mBlockedAppUids.add(uid)) { |
| 964 | added = true; |
| 965 | } else if (ni.isConnected() && mBlockedAppUids.remove(uid)) { |
| 966 | removed = true; |
| 967 | } |
| 968 | } |
| 969 | if (added) log("Returning blocked NetworkInfo to uid=" + uid); |
| 970 | else if (removed) log("Returning unblocked NetworkInfo to uid=" + uid); |
| 971 | } |
| 972 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 973 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 974 | * Apply any relevant filters to {@link NetworkState} for the given UID. For |
| 975 | * example, this may mark the network as {@link DetailedState#BLOCKED} based |
| 976 | * on {@link #isNetworkWithLinkPropertiesBlocked}, or |
| 977 | * {@link NetworkInfo#isMetered()} based on network policies. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 978 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 979 | private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 980 | if (state == null || state.networkInfo == null || state.linkProperties == null) return; |
| 981 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 982 | if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, ignoreBlocked)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 983 | state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 984 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 985 | if (mLockdownTracker != null) { |
| 986 | mLockdownTracker.augmentNetworkInfo(state.networkInfo); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 987 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 988 | |
| 989 | // TODO: apply metered state closer to NetworkAgentInfo |
| 990 | final long token = Binder.clearCallingIdentity(); |
| 991 | try { |
| 992 | state.networkInfo.setMetered(mPolicyManager.isNetworkMetered(state)); |
| 993 | } catch (RemoteException e) { |
| 994 | } finally { |
| 995 | Binder.restoreCallingIdentity(token); |
| 996 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 1001 | * It is assumed that at most one network is active at a time. If more |
| 1002 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1003 | * @return the info for the active network, or {@code null} if none is |
| 1004 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1005 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1006 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1007 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1008 | enforceAccessPermission(); |
| 1009 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1010 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1011 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1012 | maybeLogBlockedNetworkInfo(state.networkInfo, uid); |
| 1013 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1014 | } |
| 1015 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1016 | @Override |
| 1017 | public Network getActiveNetwork() { |
| 1018 | enforceAccessPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1019 | return getActiveNetworkForUidInternal(Binder.getCallingUid(), false); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1023 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1024 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1025 | return getActiveNetworkForUidInternal(uid, ignoreBlocked); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1026 | } |
| 1027 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1028 | private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1029 | final int user = UserHandle.getUserId(uid); |
| 1030 | int vpnNetId = NETID_UNSET; |
| 1031 | synchronized (mVpns) { |
| 1032 | final Vpn vpn = mVpns.get(user); |
| 1033 | if (vpn != null && vpn.appliesToUid(uid)) vpnNetId = vpn.getNetId(); |
| 1034 | } |
| 1035 | NetworkAgentInfo nai; |
| 1036 | if (vpnNetId != NETID_UNSET) { |
| 1037 | synchronized (mNetworkForNetId) { |
| 1038 | nai = mNetworkForNetId.get(vpnNetId); |
| 1039 | } |
| 1040 | if (nai != null) return nai.network; |
| 1041 | } |
| 1042 | nai = getDefaultNetwork(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1043 | if (nai != null |
| 1044 | && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, ignoreBlocked)) { |
| 1045 | nai = null; |
| 1046 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1047 | return nai != null ? nai.network : null; |
| 1048 | } |
| 1049 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1050 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1051 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1052 | final int uid = Binder.getCallingUid(); |
| 1053 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1054 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1055 | } |
| 1056 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1057 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1058 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1059 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1060 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1061 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1062 | return state.networkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1066 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1067 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1068 | final int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1069 | if (getVpnUnderlyingNetworks(uid) != null) { |
| 1070 | // A VPN is active, so we may need to return one of its underlying networks. This |
| 1071 | // information is not available in LegacyTypeTracker, so we have to get it from |
| 1072 | // getUnfilteredActiveNetworkState. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1073 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1074 | if (state.networkInfo != null && state.networkInfo.getType() == networkType) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1075 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1076 | return state.networkInfo; |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1077 | } |
| 1078 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1079 | final NetworkState state = getFilteredNetworkState(networkType, uid, false); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1080 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | } |
| 1082 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1083 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1084 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1085 | enforceAccessPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1086 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1087 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1088 | final NetworkState state = nai.getNetworkState(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1089 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1090 | return state.networkInfo; |
| 1091 | } else { |
| 1092 | return null; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1093 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | public NetworkInfo[] getAllNetworkInfo() { |
| 1098 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1099 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1100 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1101 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1102 | NetworkInfo info = getNetworkInfo(networkType); |
| 1103 | if (info != null) { |
| 1104 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1105 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1106 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1107 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | } |
| 1109 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1110 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1111 | public Network getNetworkForType(int networkType) { |
| 1112 | enforceAccessPermission(); |
| 1113 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1114 | NetworkState state = getFilteredNetworkState(networkType, uid, false); |
| 1115 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, false)) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1116 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1117 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1118 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
| 1121 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1122 | public Network[] getAllNetworks() { |
| 1123 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1124 | synchronized (mNetworkForNetId) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1125 | final Network[] result = new Network[mNetworkForNetId.size()]; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1126 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1127 | result[i] = mNetworkForNetId.valueAt(i).network; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1128 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1129 | return result; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1130 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1133 | @Override |
| 1134 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1135 | // The basic principle is: if an app's traffic could possibly go over a |
| 1136 | // network, without the app doing anything multinetwork-specific, |
| 1137 | // (hence, by "default"), then include that network's capabilities in |
| 1138 | // the array. |
| 1139 | // |
| 1140 | // In the normal case, app traffic only goes over the system's default |
| 1141 | // network connection, so that's the only network returned. |
| 1142 | // |
| 1143 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1144 | // over whatever underlying networks the VPN specifies, while other app |
| 1145 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1146 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1147 | // returned includes the VPN's underlying networks and the system |
| 1148 | // default. |
| 1149 | enforceAccessPermission(); |
| 1150 | |
| 1151 | HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>(); |
| 1152 | |
| 1153 | NetworkAgentInfo nai = getDefaultNetwork(); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1154 | NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1155 | if (nc != null) { |
| 1156 | result.put(nai.network, nc); |
| 1157 | } |
| 1158 | |
| 1159 | if (!mLockdownEnabled) { |
| 1160 | synchronized (mVpns) { |
| 1161 | Vpn vpn = mVpns.get(userId); |
| 1162 | if (vpn != null) { |
| 1163 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1164 | if (networks != null) { |
| 1165 | for (Network network : networks) { |
| 1166 | nai = getNetworkAgentInfoForNetwork(network); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1167 | nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1168 | if (nc != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1169 | result.put(network, nc); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1178 | out = result.values().toArray(out); |
| 1179 | return out; |
| 1180 | } |
| 1181 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1182 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1183 | public boolean isNetworkSupported(int networkType) { |
| 1184 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1185 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1188 | /** |
| 1189 | * Return LinkProperties for the active (i.e., connected) default |
| 1190 | * network interface. It is assumed that at most one default network |
| 1191 | * is active at a time. If more than one is active, it is indeterminate |
| 1192 | * which will be returned. |
| 1193 | * @return the ip properties for the active network, or {@code null} if |
| 1194 | * none is active |
| 1195 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1196 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1197 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1198 | enforceAccessPermission(); |
| 1199 | final int uid = Binder.getCallingUid(); |
| 1200 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1201 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1204 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1205 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1206 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1207 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1208 | if (nai != null) { |
| 1209 | synchronized (nai) { |
| 1210 | return new LinkProperties(nai.linkProperties); |
| 1211 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1212 | } |
| 1213 | return null; |
| 1214 | } |
| 1215 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1216 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1217 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1218 | public LinkProperties getLinkProperties(Network network) { |
| 1219 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1220 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1221 | if (nai != null) { |
| 1222 | synchronized (nai) { |
| 1223 | return new LinkProperties(nai.linkProperties); |
| 1224 | } |
| 1225 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1226 | return null; |
| 1227 | } |
| 1228 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1229 | private NetworkCapabilities getNetworkCapabilitiesInternal(NetworkAgentInfo nai) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1230 | if (nai != null) { |
| 1231 | synchronized (nai) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1232 | if (nai.networkCapabilities != null) { |
| 1233 | return new NetworkCapabilities(nai.networkCapabilities); |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1234 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1237 | return null; |
| 1238 | } |
| 1239 | |
| 1240 | @Override |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1241 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1242 | enforceAccessPermission(); |
| 1243 | return getNetworkCapabilitiesInternal(getNetworkAgentInfoForNetwork(network)); |
| 1244 | } |
| 1245 | |
| 1246 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1247 | public NetworkState[] getAllNetworkState() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1248 | // Require internal since we're handing out IMSI details |
| 1249 | enforceConnectivityInternalPermission(); |
| 1250 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1251 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1252 | for (Network network : getAllNetworks()) { |
| 1253 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1254 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1255 | result.add(nai.getNetworkState()); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1256 | } |
| 1257 | } |
| 1258 | return result.toArray(new NetworkState[result.size()]); |
| 1259 | } |
| 1260 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1261 | @Override |
| 1262 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1263 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1264 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1265 | final long token = Binder.clearCallingIdentity(); |
| 1266 | try { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1267 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1268 | if (state.networkInfo != null) { |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1269 | try { |
| 1270 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1271 | } catch (RemoteException e) { |
| 1272 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1273 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1274 | return null; |
| 1275 | } finally { |
| 1276 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1277 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1278 | } |
| 1279 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1280 | @Override |
| 1281 | public boolean isActiveNetworkMetered() { |
| 1282 | enforceAccessPermission(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1283 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1284 | final NetworkInfo info = getActiveNetworkInfo(); |
| 1285 | return (info != null) ? info.isMetered() : false; |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1286 | } |
| 1287 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1288 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1289 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1290 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1291 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1292 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1293 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1294 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1295 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1296 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1297 | * Ensure that a network route exists to deliver traffic to the specified |
| 1298 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1299 | * @param networkType the type of the network over which traffic to the |
| 1300 | * specified host is to be routed |
| 1301 | * @param hostAddress the IP address of the host to which the route is |
| 1302 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1303 | * @return {@code true} on success, {@code false} on failure |
| 1304 | */ |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1305 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1307 | if (mProtectedNetworks.contains(networkType)) { |
| 1308 | enforceConnectivityInternalPermission(); |
| 1309 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1310 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1311 | InetAddress addr; |
| 1312 | try { |
| 1313 | addr = InetAddress.getByAddress(hostAddress); |
| 1314 | } catch (UnknownHostException e) { |
| 1315 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1316 | return false; |
| 1317 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1319 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1320 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1323 | |
| 1324 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1325 | if (nai == null) { |
| 1326 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1327 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1328 | } else { |
| 1329 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1330 | } |
| 1331 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1332 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1333 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1334 | DetailedState netState; |
| 1335 | synchronized (nai) { |
| 1336 | netState = nai.networkInfo.getDetailedState(); |
| 1337 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1338 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1339 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1340 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1341 | log("requestRouteToHostAddress on down network " |
| 1342 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1343 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1344 | } |
| 1345 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1347 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1348 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1349 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1350 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1351 | LinkProperties lp; |
| 1352 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1353 | synchronized (nai) { |
| 1354 | lp = nai.linkProperties; |
| 1355 | netId = nai.network.netId; |
| 1356 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1357 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1358 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1359 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1360 | } finally { |
| 1361 | Binder.restoreCallingIdentity(token); |
| 1362 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1365 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1366 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1367 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1368 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1369 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1370 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1371 | if (bestRoute.getGateway().equals(addr)) { |
| 1372 | // 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] | 1373 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1374 | } else { |
| 1375 | // if we will connect to this through another route, add a direct route |
| 1376 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1377 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
Lorenzo Colitti | aa281e2 | 2015-09-04 13:12:42 +0900 | [diff] [blame] | 1380 | if (DBG) log("Adding legacy route " + bestRoute + |
| 1381 | " for UID/PID " + uid + "/" + Binder.getCallingPid()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1382 | try { |
| 1383 | mNetd.addLegacyRouteForNetId(netId, bestRoute, uid); |
| 1384 | } catch (Exception e) { |
| 1385 | // never crash - catch them all |
| 1386 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1387 | return false; |
| 1388 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1389 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | } |
| 1391 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1392 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1393 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1394 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1395 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1396 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1397 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1400 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1401 | // skip update when we've already applied rules |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 1402 | final int oldRules = mUidRules.get(uid, RULE_NONE); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1403 | if (oldRules == uidRules) return; |
| 1404 | |
Felipe Leme | 781ba14 | 2016-05-09 16:24:48 -0700 | [diff] [blame] | 1405 | if (uidRules == RULE_NONE) { |
| 1406 | mUidRules.delete(uid); |
| 1407 | } else { |
| 1408 | mUidRules.put(uid, uidRules); |
| 1409 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1412 | // TODO: notify UID when it has requested targeted updates |
| 1413 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1414 | |
| 1415 | @Override |
| 1416 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1417 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1418 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1419 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | synchronized (mRulesLock) { |
| 1423 | mMeteredIfaces.clear(); |
| 1424 | for (String iface : meteredIfaces) { |
| 1425 | mMeteredIfaces.add(iface); |
| 1426 | } |
| 1427 | } |
| 1428 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1429 | |
| 1430 | @Override |
| 1431 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1432 | // caller is NPMS, since we only register with them |
| 1433 | if (LOGD_RULES) { |
| 1434 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1435 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1436 | |
| 1437 | synchronized (mRulesLock) { |
| 1438 | mRestrictBackground = restrictBackground; |
| 1439 | } |
| 1440 | |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1441 | if (restrictBackground) { |
| 1442 | log("onRestrictBackgroundChanged(true): disabling tethering"); |
| 1443 | mTethering.untetherAll(); |
| 1444 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1445 | } |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 1446 | |
| 1447 | @Override |
| 1448 | public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) { |
| 1449 | if (LOGD_RULES) { |
| 1450 | // caller is NPMS, since we only register with them |
| 1451 | log("onRestrictBackgroundWhitelistChanged(uid=" + uid + ", whitelisted=" |
| 1452 | + whitelisted + ")"); |
| 1453 | } |
| 1454 | } |
Felipe Leme | 99d5d3d | 2016-05-16 13:30:57 -0700 | [diff] [blame] | 1455 | @Override |
| 1456 | public void onRestrictBackgroundBlacklistChanged(int uid, boolean blacklisted) { |
| 1457 | if (LOGD_RULES) { |
| 1458 | // caller is NPMS, since we only register with them |
| 1459 | log("onRestrictBackgroundBlacklistChanged(uid=" + uid + ", blacklisted=" |
| 1460 | + blacklisted + ")"); |
| 1461 | } |
| 1462 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1463 | }; |
| 1464 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 1465 | /** |
| 1466 | * Require that the caller is either in the same user or has appropriate permission to interact |
| 1467 | * across users. |
| 1468 | * |
| 1469 | * @param userId Target user for whatever operation the current IPC is supposed to perform. |
| 1470 | */ |
| 1471 | private void enforceCrossUserPermission(int userId) { |
| 1472 | if (userId == UserHandle.getCallingUserId()) { |
| 1473 | // Not a cross-user call. |
| 1474 | return; |
| 1475 | } |
| 1476 | mContext.enforceCallingOrSelfPermission( |
| 1477 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1478 | "ConnectivityService"); |
| 1479 | } |
| 1480 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1481 | private void enforceInternetPermission() { |
| 1482 | mContext.enforceCallingOrSelfPermission( |
| 1483 | android.Manifest.permission.INTERNET, |
| 1484 | "ConnectivityService"); |
| 1485 | } |
| 1486 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1487 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1488 | mContext.enforceCallingOrSelfPermission( |
| 1489 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1490 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | private void enforceChangePermission() { |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1494 | ConnectivityManager.enforceChangePermission(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | } |
| 1496 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1497 | private void enforceTetherAccessPermission() { |
| 1498 | mContext.enforceCallingOrSelfPermission( |
| 1499 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1500 | "ConnectivityService"); |
| 1501 | } |
| 1502 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1503 | private void enforceConnectivityInternalPermission() { |
| 1504 | mContext.enforceCallingOrSelfPermission( |
| 1505 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1506 | "ConnectivityService"); |
| 1507 | } |
| 1508 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1509 | private void enforceKeepalivePermission() { |
Lorenzo Colitti | 7914ce5 | 2015-09-08 13:21:48 +0900 | [diff] [blame] | 1510 | mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1511 | } |
| 1512 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1513 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1514 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1515 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1519 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1520 | } |
| 1521 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1522 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1523 | if (mLockdownTracker != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1524 | info = new NetworkInfo(info); |
| 1525 | mLockdownTracker.augmentNetworkInfo(info); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1528 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1529 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1530 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1531 | if (info.isFailover()) { |
| 1532 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1533 | info.setFailover(false); |
| 1534 | } |
| 1535 | if (info.getReason() != null) { |
| 1536 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1537 | } |
| 1538 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1539 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1540 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1541 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1542 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1543 | return intent; |
| 1544 | } |
| 1545 | |
| 1546 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1547 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1548 | } |
| 1549 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1550 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1551 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1552 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1553 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1554 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1555 | final long ident = Binder.clearCallingIdentity(); |
| 1556 | try { |
| 1557 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1558 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 1559 | } finally { |
| 1560 | Binder.restoreCallingIdentity(ident); |
| 1561 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1564 | private void sendStickyBroadcast(Intent intent) { |
| 1565 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1566 | if (!mSystemReady) { |
| 1567 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1568 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1569 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1570 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1571 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1572 | } |
| 1573 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1574 | Bundle options = null; |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1575 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1576 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 1577 | final NetworkInfo ni = intent.getParcelableExtra( |
| 1578 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 1579 | if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) { |
| 1580 | intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL); |
| 1581 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1582 | } else { |
| 1583 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 1584 | opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M); |
| 1585 | options = opts.toBundle(); |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 1586 | } |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1587 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 1588 | try { |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1589 | bs.noteConnectivityChanged(intent.getIntExtra( |
| 1590 | ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE), |
| 1591 | ni != null ? ni.getState().toString() : "?"); |
| 1592 | } catch (RemoteException e) { |
| 1593 | } |
| 1594 | } |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1595 | try { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1596 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL, options); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1597 | } finally { |
| 1598 | Binder.restoreCallingIdentity(ident); |
| 1599 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | void systemReady() { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1604 | loadGlobalProxy(); |
| 1605 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1606 | synchronized(this) { |
| 1607 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1608 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1609 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1610 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1611 | } |
| 1612 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1613 | // load the global proxy at startup |
| 1614 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1615 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1616 | // Try bringing up tracker, but KeyStore won't be ready yet for secondary users so wait |
| 1617 | // for user to unlock device too. |
| 1618 | updateLockdownVpn(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1619 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1620 | // Configure whether mobile data is always on. |
| 1621 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON)); |
| 1622 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1623 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 1624 | |
| 1625 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1628 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1629 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1630 | * |
| 1631 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1632 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1633 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1634 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1635 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1636 | |
| 1637 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1638 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1639 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1640 | if (networkAgent.networkCapabilities.hasTransport( |
| 1641 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1642 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1643 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Adam Lesinski | ed6160d | 2015-08-18 11:47:07 -0700 | [diff] [blame] | 1644 | 10); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1645 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1646 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 1647 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1648 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1649 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 1650 | 15); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1651 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1652 | } else { |
| 1653 | // do not track any other networks |
| 1654 | timeout = 0; |
| 1655 | } |
| 1656 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1657 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1658 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1659 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1660 | } catch (Exception e) { |
| 1661 | // You shall not crash! |
| 1662 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | } |
| 1666 | |
| 1667 | /** |
| 1668 | * Remove data activity tracking when network disconnects. |
| 1669 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1670 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1671 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 1672 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1673 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1674 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 1675 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1676 | try { |
| 1677 | // the call fails silently if no idletimer setup for this interface |
| 1678 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1679 | } catch (Exception e) { |
| 1680 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1681 | } |
| 1682 | } |
| 1683 | } |
| 1684 | |
| 1685 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1686 | * Reads the network specific MTU size from reources. |
| 1687 | * and set it on it's iface. |
| 1688 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1689 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 1690 | final String iface = newLp.getInterfaceName(); |
| 1691 | final int mtu = newLp.getMtu(); |
| 1692 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 1693 | if (VDBG) log("identical MTU - not setting"); |
| 1694 | return; |
| 1695 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1696 | |
Robert Greenwalt | 4307403 | 2014-08-15 17:53:05 -0700 | [diff] [blame] | 1697 | if (LinkProperties.isValidMtu(mtu, newLp.hasGlobalIPv6Address()) == false) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1698 | if (mtu != 0) loge("Unexpected mtu value: " + mtu + ", " + iface); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1699 | return; |
| 1700 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1701 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 1702 | // Cannot set MTU without interface name |
| 1703 | if (TextUtils.isEmpty(iface)) { |
| 1704 | loge("Setting MTU size with null iface."); |
| 1705 | return; |
| 1706 | } |
| 1707 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1708 | try { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1709 | if (VDBG) log("Setting MTU size: " + iface + ", " + mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1710 | mNetd.setMtu(iface, mtu); |
| 1711 | } catch (Exception e) { |
| 1712 | Slog.e(TAG, "exception in setMtu()" + e); |
| 1713 | } |
| 1714 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1715 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1716 | private static final String DEFAULT_TCP_BUFFER_SIZES = "4096,87380,110208,4096,16384,110208"; |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1717 | private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd"; |
| 1718 | |
| 1719 | // Overridden for testing purposes to avoid writing to SystemProperties. |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 1720 | @VisibleForTesting |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1721 | protected int getDefaultTcpRwnd() { |
| 1722 | return SystemProperties.getInt(DEFAULT_TCP_RWND_KEY, 0); |
| 1723 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1724 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1725 | private void updateTcpBufferSizes(NetworkAgentInfo nai) { |
| 1726 | if (isDefaultNetwork(nai) == false) { |
| 1727 | return; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1730 | String tcpBufferSizes = nai.linkProperties.getTcpBufferSizes(); |
| 1731 | String[] values = null; |
| 1732 | if (tcpBufferSizes != null) { |
| 1733 | values = tcpBufferSizes.split(","); |
| 1734 | } |
| 1735 | |
| 1736 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1737 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1738 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 1739 | values = tcpBufferSizes.split(","); |
| 1740 | } |
| 1741 | |
| 1742 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 1743 | |
| 1744 | try { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1745 | if (VDBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1746 | |
| 1747 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
| 1748 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 1749 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 1750 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 1751 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 1752 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 1753 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
| 1754 | mCurrentTcpBufferSizes = tcpBufferSizes; |
| 1755 | } catch (IOException e) { |
| 1756 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1757 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1758 | |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1759 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1760 | Settings.Global.TCP_DEFAULT_INIT_RWND, getDefaultTcpRwnd()); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1761 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 1762 | if (rwndValue != 0) { |
| 1763 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 1764 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1765 | } |
| 1766 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1767 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1768 | /* |
| 1769 | * Tell the VMs to toss their DNS caches |
| 1770 | */ |
| 1771 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 1772 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 1773 | /* |
| 1774 | * Connectivity events can happen before boot has completed ... |
| 1775 | */ |
| 1776 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1777 | final long ident = Binder.clearCallingIdentity(); |
| 1778 | try { |
| 1779 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1780 | } finally { |
| 1781 | Binder.restoreCallingIdentity(ident); |
| 1782 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1783 | } |
| 1784 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1785 | @Override |
| 1786 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1787 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 1788 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 1789 | if(restoreDefaultNetworkDelayStr != null && |
| 1790 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 1791 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 1792 | return Integer.parseInt(restoreDefaultNetworkDelayStr); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1793 | } catch (NumberFormatException e) { |
| 1794 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1795 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 1796 | // if the system property isn't set, use the value for the apn type |
| 1797 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 1798 | |
| 1799 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 1800 | (mNetConfigs[networkType] != null)) { |
| 1801 | ret = mNetConfigs[networkType].restoreTime; |
| 1802 | } |
| 1803 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | } |
| 1805 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1806 | private boolean argsContain(String[] args, String target) { |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1807 | for (String arg : args) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1808 | if (arg.equals(target)) return true; |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1809 | } |
| 1810 | return false; |
| 1811 | } |
| 1812 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1813 | private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { |
| 1814 | final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); |
| 1815 | final long DIAG_TIME_MS = 5000; |
| 1816 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1817 | // Start gathering diagnostic information. |
| 1818 | netDiags.add(new NetworkDiagnostics( |
| 1819 | nai.network, |
| 1820 | new LinkProperties(nai.linkProperties), // Must be a copy. |
| 1821 | DIAG_TIME_MS)); |
| 1822 | } |
| 1823 | |
| 1824 | for (NetworkDiagnostics netDiag : netDiags) { |
| 1825 | pw.println(); |
| 1826 | netDiag.waitForMeasurements(); |
| 1827 | netDiag.dump(pw); |
| 1828 | } |
| 1829 | } |
| 1830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1831 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1832 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1833 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1834 | if (mContext.checkCallingOrSelfPermission( |
| 1835 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1836 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1837 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 1838 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1839 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | return; |
| 1841 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1842 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1843 | if (argsContain(args, "--diag")) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1844 | dumpNetworkDiagnostics(pw); |
| 1845 | return; |
| 1846 | } |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1847 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1848 | pw.print("NetworkFactories for:"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1849 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1850 | pw.print(" " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1851 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1852 | pw.println(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1853 | pw.println(); |
| 1854 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 1855 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1856 | pw.print("Active default network: "); |
| 1857 | if (defaultNai == null) { |
| 1858 | pw.println("none"); |
| 1859 | } else { |
| 1860 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1861 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1862 | pw.println(); |
| 1863 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1864 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1865 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1866 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1867 | pw.println(nai.toString()); |
| 1868 | pw.increaseIndent(); |
| 1869 | pw.println("Requests:"); |
| 1870 | pw.increaseIndent(); |
| 1871 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 1872 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1873 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1874 | pw.decreaseIndent(); |
| 1875 | pw.println("Lingered:"); |
| 1876 | pw.increaseIndent(); |
| 1877 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 1878 | pw.decreaseIndent(); |
| 1879 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1880 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1881 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1882 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1883 | |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1884 | pw.println("Metered Interfaces:"); |
| 1885 | pw.increaseIndent(); |
| 1886 | for (String value : mMeteredIfaces) { |
| 1887 | pw.println(value); |
| 1888 | } |
| 1889 | pw.decreaseIndent(); |
| 1890 | pw.println(); |
| 1891 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1892 | pw.print("Restrict background: "); |
| 1893 | pw.println(mRestrictBackground); |
| 1894 | pw.println(); |
| 1895 | |
Felipe Leme | 46c4fc3 | 2016-05-04 09:21:43 -0700 | [diff] [blame] | 1896 | pw.println("Status for known UIDs:"); |
| 1897 | pw.increaseIndent(); |
| 1898 | final int size = mUidRules.size(); |
| 1899 | for (int i = 0; i < size; i++) { |
| 1900 | final int uid = mUidRules.keyAt(i); |
| 1901 | pw.print("UID="); |
| 1902 | pw.print(uid); |
| 1903 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 1904 | pw.print(" rules="); |
| 1905 | pw.print(uidRulesToString(uidRules)); |
| 1906 | pw.println(); |
| 1907 | } |
| 1908 | pw.println(); |
| 1909 | pw.decreaseIndent(); |
| 1910 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1911 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1912 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1913 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 1914 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1915 | } |
| 1916 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1917 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1918 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1919 | mLegacyTypeTracker.dump(pw); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1920 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1921 | synchronized (this) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1922 | pw.print("mNetTransitionWakeLock: currently " + |
| 1923 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held"); |
| 1924 | if (!TextUtils.isEmpty(mNetTransitionWakeLockCausedBy)) { |
| 1925 | pw.println(", last requested for " + mNetTransitionWakeLockCausedBy); |
| 1926 | } else { |
| 1927 | pw.println(", last requested never"); |
| 1928 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1929 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1930 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1931 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1932 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1933 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1934 | pw.println(); |
| 1935 | mKeepaliveTracker.dump(pw); |
| 1936 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1937 | pw.println(); |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1938 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1939 | if (mInetLog != null && mInetLog.size() > 0) { |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1940 | pw.println(); |
| 1941 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1942 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1943 | for(int i = 0; i < mInetLog.size(); i++) { |
| 1944 | pw.println(mInetLog.get(i)); |
| 1945 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1946 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1947 | } |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1948 | |
| 1949 | if (argsContain(args, "--short") == false) { |
| 1950 | pw.println(); |
| 1951 | synchronized (mValidationLogs) { |
| 1952 | pw.println("mValidationLogs (most recent first):"); |
| 1953 | for (Pair<Network,ReadOnlyLocalLog> p : mValidationLogs) { |
| 1954 | pw.println(p.first); |
| 1955 | pw.increaseIndent(); |
| 1956 | p.second.dump(fd, pw, args); |
| 1957 | pw.decreaseIndent(); |
| 1958 | } |
| 1959 | } |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 1960 | |
| 1961 | pw.println(); |
| 1962 | pw.println("mNetworkRequestInfoLogs (most recent first):"); |
| 1963 | pw.increaseIndent(); |
| 1964 | mNetworkRequestInfoLogs.reverseDump(fd, pw, args); |
| 1965 | pw.decreaseIndent(); |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1966 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1967 | } |
| 1968 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1969 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1970 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1971 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1972 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 1973 | if (officialNai != null || VDBG) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1974 | final String msg = sMagicDecoderRing.get(what, Integer.toString(what)); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1975 | loge(msg + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1976 | " - " + nai); |
| 1977 | } |
| 1978 | return false; |
| 1979 | } |
| 1980 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1981 | private boolean isRequest(NetworkRequest request) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 1982 | return mNetworkRequests.get(request).isRequest(); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1983 | } |
| 1984 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1985 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1986 | private class NetworkStateTrackerHandler extends Handler { |
| 1987 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 1988 | super(looper); |
| 1989 | } |
| 1990 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1991 | private boolean maybeHandleAsyncChannelMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1992 | switch (msg.what) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1993 | default: |
| 1994 | return false; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1995 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1996 | handleAsyncChannelHalfConnect(msg); |
| 1997 | break; |
| 1998 | } |
| 1999 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 2000 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2001 | if (nai != null) nai.asyncChannel.disconnect(); |
| 2002 | break; |
| 2003 | } |
| 2004 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 2005 | handleAsyncChannelDisconnected(msg); |
| 2006 | break; |
| 2007 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2008 | } |
| 2009 | return true; |
| 2010 | } |
| 2011 | |
| 2012 | private void maybeHandleNetworkAgentMessage(Message msg) { |
| 2013 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2014 | if (nai == null) { |
| 2015 | if (VDBG) { |
| 2016 | final String what = sMagicDecoderRing.get(msg.what, Integer.toString(msg.what)); |
| 2017 | log(String.format("%s from unknown NetworkAgent", what)); |
| 2018 | } |
| 2019 | return; |
| 2020 | } |
| 2021 | |
| 2022 | switch (msg.what) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2023 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2024 | final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj; |
| 2025 | if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL) || |
| 2026 | networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) { |
| 2027 | Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2028 | } |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2029 | if (nai.everConnected && !nai.networkCapabilities.equalImmutableCapabilities( |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2030 | networkCapabilities)) { |
| 2031 | Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " |
| 2032 | + nai.networkCapabilities + " -> " + networkCapabilities); |
| 2033 | } |
| 2034 | updateCapabilities(nai, networkCapabilities); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2035 | break; |
| 2036 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2037 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2038 | if (VDBG) { |
| 2039 | log("Update of LinkProperties for " + nai.name() + |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2040 | "; created=" + nai.created + |
| 2041 | "; everConnected=" + nai.everConnected); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2042 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2043 | LinkProperties oldLp = nai.linkProperties; |
| 2044 | synchronized (nai) { |
| 2045 | nai.linkProperties = (LinkProperties)msg.obj; |
| 2046 | } |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2047 | if (nai.everConnected) updateLinkProperties(nai, oldLp); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2048 | break; |
| 2049 | } |
| 2050 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2051 | NetworkInfo info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2052 | updateNetworkInfo(nai, info); |
| 2053 | break; |
| 2054 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2055 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2056 | Integer score = (Integer) msg.obj; |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 2057 | if (score != null) updateNetworkScore(nai, score.intValue()); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2058 | break; |
| 2059 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2060 | case NetworkAgent.EVENT_UID_RANGES_ADDED: { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2061 | try { |
| 2062 | mNetd.addVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 2063 | } catch (Exception e) { |
| 2064 | // Never crash! |
| 2065 | loge("Exception in addVpnUidRanges: " + e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2066 | } |
| 2067 | break; |
| 2068 | } |
| 2069 | case NetworkAgent.EVENT_UID_RANGES_REMOVED: { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2070 | try { |
| 2071 | mNetd.removeVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 2072 | } catch (Exception e) { |
| 2073 | // Never crash! |
| 2074 | loge("Exception in removeVpnUidRanges: " + e); |
| 2075 | } |
| 2076 | break; |
| 2077 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2078 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2079 | if (nai.everConnected && !nai.networkMisc.explicitlySelected) { |
| 2080 | loge("ERROR: already-connected network explicitly selected."); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2081 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2082 | nai.networkMisc.explicitlySelected = true; |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2083 | nai.networkMisc.acceptUnvalidated = (boolean) msg.obj; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2084 | break; |
| 2085 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2086 | case NetworkAgent.EVENT_PACKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2087 | mKeepaliveTracker.handleEventPacketKeepalive(nai, msg); |
| 2088 | break; |
| 2089 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2090 | } |
| 2091 | } |
| 2092 | |
| 2093 | private boolean maybeHandleNetworkMonitorMessage(Message msg) { |
| 2094 | switch (msg.what) { |
| 2095 | default: |
| 2096 | return false; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2097 | case NetworkMonitor.EVENT_NETWORK_TESTED: { |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2098 | final NetworkAgentInfo nai; |
| 2099 | synchronized (mNetworkForNetId) { |
| 2100 | nai = mNetworkForNetId.get(msg.arg2); |
| 2101 | } |
| 2102 | if (nai != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 2103 | final boolean valid = |
| 2104 | (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID); |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2105 | if (DBG) log(nai.name() + " validation " + (valid ? "passed" : "failed") + |
| 2106 | (msg.obj == null ? "" : " with redirect to " + (String)msg.obj)); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2107 | if (valid != nai.lastValidated) { |
| 2108 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2109 | nai.lastValidated = valid; |
| 2110 | nai.everValidated |= valid; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2111 | updateCapabilities(nai, nai.networkCapabilities); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2112 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2113 | if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2114 | } |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 2115 | updateInetCondition(nai); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2116 | // Let the NetworkAgent know the state of its network |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2117 | Bundle redirectUrlBundle = new Bundle(); |
| 2118 | redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, (String)msg.obj); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2119 | nai.asyncChannel.sendMessage( |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2120 | NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2121 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2122 | 0, redirectUrlBundle); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2123 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2124 | break; |
| 2125 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2126 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2127 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2128 | if (isLiveNetworkAgent(nai, msg.what)) { |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2129 | handleLingerComplete(nai); |
| 2130 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2131 | break; |
| 2132 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2133 | case NetworkMonitor.EVENT_PROVISIONING_NOTIFICATION: { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2134 | final int netId = msg.arg2; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2135 | final boolean visible = (msg.arg1 != 0); |
| 2136 | final NetworkAgentInfo nai; |
| 2137 | synchronized (mNetworkForNetId) { |
| 2138 | nai = mNetworkForNetId.get(netId); |
| 2139 | } |
| 2140 | // If captive portal status has changed, update capabilities. |
| 2141 | if (nai != null && (visible != nai.lastCaptivePortalDetected)) { |
| 2142 | nai.lastCaptivePortalDetected = visible; |
| 2143 | nai.everCaptivePortalDetected |= visible; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2144 | updateCapabilities(nai, nai.networkCapabilities); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2145 | } |
| 2146 | if (!visible) { |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2147 | setProvNotificationVisibleIntent(false, netId, null, 0, null, null, false); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2148 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2149 | if (nai == null) { |
| 2150 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2151 | break; |
| 2152 | } |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2153 | setProvNotificationVisibleIntent(true, netId, NotificationType.SIGN_IN, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2154 | nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(), |
| 2155 | (PendingIntent)msg.obj, nai.networkMisc.explicitlySelected); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2156 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2157 | break; |
| 2158 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2159 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2160 | return true; |
| 2161 | } |
| 2162 | |
| 2163 | @Override |
| 2164 | public void handleMessage(Message msg) { |
| 2165 | if (!maybeHandleAsyncChannelMessage(msg) && !maybeHandleNetworkMonitorMessage(msg)) { |
| 2166 | maybeHandleNetworkAgentMessage(msg); |
| 2167 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2168 | } |
| 2169 | } |
| 2170 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2171 | private void linger(NetworkAgentInfo nai) { |
| 2172 | nai.lingering = true; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 2173 | NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_LINGER); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2174 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 2175 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 2176 | } |
| 2177 | |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2178 | // Cancel any lingering so the linger timeout doesn't teardown a network. |
| 2179 | // This should be called when a network begins satisfying a NetworkRequest. |
| 2180 | // Note: depending on what state the NetworkMonitor is in (e.g., |
| 2181 | // if it's awaiting captive portal login, or if validation failed), this |
| 2182 | // may trigger a re-evaluation of the network. |
| 2183 | private void unlinger(NetworkAgentInfo nai) { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2184 | nai.networkLingered.clear(); |
| 2185 | if (!nai.lingering) return; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2186 | nai.lingering = false; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 2187 | NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_UNLINGER); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2188 | if (VDBG) log("Canceling linger of " + nai.name()); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2189 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 2190 | } |
| 2191 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2192 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2193 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2194 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2195 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2196 | if (VDBG) log("NetworkFactory connected"); |
| 2197 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2198 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2199 | if (!nri.isRequest()) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2200 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2201 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2202 | (nai != null ? nai.getCurrentScore() : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2203 | } |
| 2204 | } else { |
| 2205 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2206 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2207 | } |
| 2208 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2209 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2210 | if (VDBG) log("NetworkAgent connected"); |
| 2211 | // A network agent has requested a connection. Establish the connection. |
| 2212 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2213 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2214 | } else { |
| 2215 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2216 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2217 | if (nai != null) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2218 | final boolean wasDefault = isDefaultNetwork(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2219 | synchronized (mNetworkForNetId) { |
| 2220 | mNetworkForNetId.remove(nai.network.netId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2221 | mNetIdInUse.delete(nai.network.netId); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2222 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2223 | // Just in case. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2224 | mLegacyTypeTracker.remove(nai, wasDefault); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2225 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2226 | } |
| 2227 | } |
| 2228 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2229 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2230 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2231 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2232 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2233 | if (DBG) { |
| 2234 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 2235 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2236 | // A network agent has disconnected. |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2237 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2238 | // because they lost all their requests or because their score isn't good) |
| 2239 | // then they would disconnect organically, report their new state and then |
| 2240 | // disconnect the channel. |
| 2241 | if (nai.networkInfo.isConnected()) { |
| 2242 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2243 | null, null); |
| 2244 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2245 | final boolean wasDefault = isDefaultNetwork(nai); |
| 2246 | if (wasDefault) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2247 | mDefaultInetConditionPublished = 0; |
| 2248 | } |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 2249 | notifyIfacesChangedForNetworkStats(); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 2250 | // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied |
| 2251 | // by other networks that are already connected. Perhaps that can be done by |
| 2252 | // sending all CALLBACK_LOST messages (for requests, not listens) at the end |
| 2253 | // of rematchAllNetworksAndRequests |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2254 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2255 | mKeepaliveTracker.handleStopAllKeepalives(nai, |
| 2256 | ConnectivityManager.PacketKeepalive.ERROR_INVALID_NETWORK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2257 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 2258 | mNetworkAgentInfos.remove(msg.replyTo); |
| 2259 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2260 | synchronized (mNetworkForNetId) { |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2261 | // Remove the NetworkAgent, but don't mark the netId as |
| 2262 | // available until we've told netd to delete it below. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2263 | mNetworkForNetId.remove(nai.network.netId); |
| 2264 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2265 | // Remove all previously satisfied requests. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2266 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2267 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 2268 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
| 2269 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 2270 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2271 | sendUpdatedScoreToFactories(request, 0); |
| 2272 | } |
| 2273 | } |
| 2274 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 2275 | removeDataActivityTracking(nai); |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 2276 | notifyLockdownVpn(nai); |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2277 | requestNetworkTransitionWakelock(nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2278 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2279 | mLegacyTypeTracker.remove(nai, wasDefault); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2280 | rematchAllNetworksAndRequests(null, 0); |
Pierre Imai | 04e22225 | 2016-04-12 18:02:58 +0900 | [diff] [blame] | 2281 | if (wasDefault && getDefaultNetwork() == null) { |
| 2282 | // Log that we lost the default network and there is no replacement. |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 2283 | logDefaultNetworkEvent(null, nai); |
Pierre Imai | 04e22225 | 2016-04-12 18:02:58 +0900 | [diff] [blame] | 2284 | } |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2285 | if (nai.created) { |
| 2286 | // Tell netd to clean up the configuration for this network |
| 2287 | // (routing rules, DNS, etc). |
| 2288 | // This may be slow as it requires a lot of netd shelling out to ip and |
| 2289 | // ip[6]tables to flush routes and remove the incoming packet mark rule, so do it |
| 2290 | // after we've rematched networks with requests which should make a potential |
| 2291 | // fallback network the default or requested a new network from the |
| 2292 | // NetworkFactories, so network traffic isn't interrupted for an unnecessarily |
| 2293 | // long time. |
| 2294 | try { |
| 2295 | mNetd.removeNetwork(nai.network.netId); |
| 2296 | } catch (Exception e) { |
| 2297 | loge("Exception removing network: " + e); |
| 2298 | } |
| 2299 | } |
| 2300 | synchronized (mNetworkForNetId) { |
| 2301 | mNetIdInUse.delete(nai.network.netId); |
| 2302 | } |
| 2303 | } else { |
| 2304 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(msg.replyTo); |
| 2305 | if (DBG && nfi != null) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2306 | } |
| 2307 | } |
| 2308 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2309 | // If this method proves to be too slow then we can maintain a separate |
| 2310 | // pendingIntent => NetworkRequestInfo map. |
| 2311 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 2312 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 2313 | Intent intent = pendingIntent.getIntent(); |
| 2314 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 2315 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 2316 | if (existingPendingIntent != null && |
| 2317 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 2318 | return entry.getValue(); |
| 2319 | } |
| 2320 | } |
| 2321 | return null; |
| 2322 | } |
| 2323 | |
| 2324 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 2325 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 2326 | |
| 2327 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 2328 | if (existingRequest != null) { // remove the existing request. |
| 2329 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 2330 | + nri.request + " because their intents matched."); |
| 2331 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid()); |
| 2332 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2333 | handleRegisterNetworkRequest(nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2334 | } |
| 2335 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2336 | private void handleRegisterNetworkRequest(NetworkRequestInfo nri) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2337 | mNetworkRequests.put(nri.request, nri); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2338 | mNetworkRequestInfoLogs.log("REGISTER " + nri); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2339 | if (!nri.isRequest()) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2340 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 2341 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 2342 | network.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 2343 | updateSignalStrengthThresholds(network, "REGISTER", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2344 | } |
| 2345 | } |
| 2346 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2347 | rematchAllNetworksAndRequests(null, 0); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2348 | if (nri.isRequest() && mNetworkForRequestId.get(nri.request.requestId) == null) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2349 | sendUpdatedScoreToFactories(nri.request, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2350 | } |
| 2351 | } |
| 2352 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2353 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 2354 | int callingUid) { |
| 2355 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 2356 | if (nri != null) { |
| 2357 | handleReleaseNetworkRequest(nri.request, callingUid); |
| 2358 | } |
| 2359 | } |
| 2360 | |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2361 | // Is nai unneeded by all NetworkRequests (and should be disconnected)? |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2362 | // This is whether it is satisfying any NetworkRequests or were it to become validated, |
| 2363 | // would it have a chance of satisfying any NetworkRequests. |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2364 | private boolean unneeded(NetworkAgentInfo nai) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2365 | if (!nai.everConnected || nai.isVPN() || nai.lingering) return false; |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2366 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 2367 | // If this Network is already the highest scoring Network for a request, or if |
| 2368 | // there is hope for it to become one if it validated, then it is needed. |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2369 | if (nri.isRequest() && nai.satisfies(nri.request) && |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2370 | (nai.networkRequests.get(nri.request.requestId) != null || |
| 2371 | // Note that this catches two important cases: |
| 2372 | // 1. Unvalidated cellular will not be reaped when unvalidated WiFi |
| 2373 | // is currently satisfying the request. This is desirable when |
| 2374 | // cellular ends up validating but WiFi does not. |
| 2375 | // 2. Unvalidated WiFi will not be reaped when validated cellular |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 2376 | // is currently satisfying the request. This is desirable when |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2377 | // WiFi ends up validating and out scoring cellular. |
| 2378 | mNetworkForRequestId.get(nri.request.requestId).getCurrentScore() < |
| 2379 | nai.getCurrentScoreAsValidated())) { |
| 2380 | return false; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2381 | } |
| 2382 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2383 | return true; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2384 | } |
| 2385 | |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2386 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
| 2387 | NetworkRequestInfo nri = mNetworkRequests.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2388 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 2389 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2390 | if (DBG) log("Attempt to release unowned NetworkRequest " + request); |
| 2391 | return; |
| 2392 | } |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2393 | if (VDBG || (DBG && nri.isRequest())) log("releasing NetworkRequest " + request); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 2394 | nri.unlinkDeathRecipient(); |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2395 | mNetworkRequests.remove(request); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 2396 | synchronized (mUidToNetworkRequestCount) { |
| 2397 | int requests = mUidToNetworkRequestCount.get(nri.mUid, 0); |
| 2398 | if (requests < 1) { |
| 2399 | Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + |
| 2400 | nri.mUid); |
| 2401 | } else if (requests == 1) { |
| 2402 | mUidToNetworkRequestCount.removeAt( |
| 2403 | mUidToNetworkRequestCount.indexOfKey(nri.mUid)); |
| 2404 | } else { |
| 2405 | mUidToNetworkRequestCount.put(nri.mUid, requests - 1); |
| 2406 | } |
| 2407 | } |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2408 | mNetworkRequestInfoLogs.log("RELEASE " + nri); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2409 | if (nri.isRequest()) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2410 | // Find all networks that are satisfying this request and remove the request |
| 2411 | // from their request lists. |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2412 | // TODO - it's my understanding that for a request there is only a single |
| 2413 | // network satisfying it, so this loop is wasteful |
| 2414 | boolean wasKept = false; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2415 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2416 | if (nai.networkRequests.get(nri.request.requestId) != null) { |
| 2417 | nai.networkRequests.remove(nri.request.requestId); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2418 | if (VDBG) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2419 | log(" Removing from current network " + nai.name() + |
| 2420 | ", leaving " + nai.networkRequests.size() + |
| 2421 | " requests."); |
| 2422 | } |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2423 | if (unneeded(nai)) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2424 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2425 | teardownUnneededNetwork(nai); |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2426 | } else { |
| 2427 | // suspect there should only be one pass through here |
| 2428 | // but if any were kept do the check below |
| 2429 | wasKept |= true; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2430 | } |
| 2431 | } |
| 2432 | } |
| 2433 | |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2434 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
| 2435 | if (nai != null) { |
| 2436 | mNetworkForRequestId.remove(nri.request.requestId); |
| 2437 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2438 | // Maintain the illusion. When this request arrived, we might have pretended |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2439 | // that a network connected to serve it, even though the network was already |
| 2440 | // connected. Now that this request has gone away, we might have to pretend |
| 2441 | // that the network disconnected. LegacyTypeTracker will generate that |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2442 | // phantom disconnect for this type. |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2443 | if (nri.request.legacyType != TYPE_NONE && nai != null) { |
| 2444 | boolean doRemove = true; |
| 2445 | if (wasKept) { |
| 2446 | // check if any of the remaining requests for this network are for the |
| 2447 | // same legacy type - if so, don't remove the nai |
| 2448 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2449 | NetworkRequest otherRequest = nai.networkRequests.valueAt(i); |
| 2450 | if (otherRequest.legacyType == nri.request.legacyType && |
| 2451 | isRequest(otherRequest)) { |
| 2452 | if (DBG) log(" still have other legacy request - leaving"); |
| 2453 | doRemove = false; |
| 2454 | } |
| 2455 | } |
| 2456 | } |
| 2457 | |
| 2458 | if (doRemove) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2459 | mLegacyTypeTracker.remove(nri.request.legacyType, nai, false); |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2460 | } |
| 2461 | } |
| 2462 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2463 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2464 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 2465 | nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2466 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2467 | } else { |
| 2468 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 2469 | // if this listen request applies and remove it. |
| 2470 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2471 | nai.networkRequests.remove(nri.request.requestId); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 2472 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 2473 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 2474 | updateSignalStrengthThresholds(nai, "RELEASE", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2475 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 2479 | } |
| 2480 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2481 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2482 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2483 | enforceConnectivityInternalPermission(); |
| 2484 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED, |
| 2485 | accept ? 1 : 0, always ? 1: 0, network)); |
| 2486 | } |
| 2487 | |
| 2488 | private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2489 | if (DBG) log("handleSetAcceptUnvalidated network=" + network + |
| 2490 | " accept=" + accept + " always=" + always); |
| 2491 | |
| 2492 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2493 | if (nai == null) { |
| 2494 | // Nothing to do. |
| 2495 | return; |
| 2496 | } |
| 2497 | |
| 2498 | if (nai.everValidated) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2499 | // The network validated while the dialog box was up. Take no action. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2500 | return; |
| 2501 | } |
| 2502 | |
| 2503 | if (!nai.networkMisc.explicitlySelected) { |
| 2504 | Slog.wtf(TAG, "BUG: setAcceptUnvalidated non non-explicitly selected network"); |
| 2505 | } |
| 2506 | |
| 2507 | if (accept != nai.networkMisc.acceptUnvalidated) { |
| 2508 | int oldScore = nai.getCurrentScore(); |
| 2509 | nai.networkMisc.acceptUnvalidated = accept; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2510 | rematchAllNetworksAndRequests(nai, oldScore); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2511 | sendUpdatedScoreToFactories(nai); |
| 2512 | } |
| 2513 | |
| 2514 | if (always) { |
| 2515 | nai.asyncChannel.sendMessage( |
| 2516 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, accept ? 1 : 0); |
| 2517 | } |
| 2518 | |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2519 | if (!accept) { |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 2520 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 2521 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 2522 | // Teardown the nework. |
| 2523 | teardownUnneededNetwork(nai); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2524 | } |
| 2525 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2529 | if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2530 | mHandler.sendMessageDelayed( |
| 2531 | mHandler.obtainMessage(EVENT_PROMPT_UNVALIDATED, nai.network), |
| 2532 | PROMPT_UNVALIDATED_DELAY_MS); |
| 2533 | } |
| 2534 | |
| 2535 | private void handlePromptUnvalidated(Network network) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2536 | if (VDBG) log("handlePromptUnvalidated " + network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2537 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2538 | |
| 2539 | // Only prompt if the network is unvalidated and was explicitly selected by the user, and if |
| 2540 | // we haven't already been told to switch to it regardless of whether it validated or not. |
Lorenzo Colitti | d49159f | 2015-05-14 23:15:10 +0900 | [diff] [blame] | 2541 | // Also don't prompt on captive portals because we're already prompting the user to sign in. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2542 | if (nai == null || nai.everValidated || nai.everCaptivePortalDetected || |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2543 | !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated) { |
| 2544 | return; |
| 2545 | } |
| 2546 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2547 | Intent intent = new Intent(ConnectivityManager.ACTION_PROMPT_UNVALIDATED); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2548 | intent.setData(Uri.fromParts("netId", Integer.toString(network.netId), null)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2549 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2550 | intent.setClassName("com.android.settings", |
| 2551 | "com.android.settings.wifi.WifiNoInternetDialog"); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2552 | |
| 2553 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 2554 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 2555 | setProvNotificationVisibleIntent(true, nai.network.netId, NotificationType.NO_INTERNET, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2556 | nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(), pendingIntent, true); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2557 | } |
| 2558 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2559 | private class InternalHandler extends Handler { |
| 2560 | public InternalHandler(Looper looper) { |
| 2561 | super(looper); |
| 2562 | } |
| 2563 | |
| 2564 | @Override |
| 2565 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2566 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2567 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2568 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2569 | String causedBy = null; |
| 2570 | synchronized (ConnectivityService.this) { |
| 2571 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 2572 | mNetTransitionWakeLock.isHeld()) { |
| 2573 | mNetTransitionWakeLock.release(); |
| 2574 | causedBy = mNetTransitionWakeLockCausedBy; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2575 | } else { |
| 2576 | break; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2577 | } |
| 2578 | } |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2579 | if (VDBG) { |
| 2580 | if (msg.what == EVENT_EXPIRE_NET_TRANSITION_WAKELOCK) { |
| 2581 | log("Failed to find a new network - expiring NetTransition Wakelock"); |
| 2582 | } else { |
| 2583 | log("NetTransition Wakelock (" + |
| 2584 | (causedBy == null ? "unknown" : causedBy) + |
| 2585 | " cleared because we found a replacement network"); |
| 2586 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2587 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 2588 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2589 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2590 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2591 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2592 | break; |
| 2593 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2594 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2595 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2596 | break; |
| 2597 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2598 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2599 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 2600 | break; |
| 2601 | } |
| 2602 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 2603 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2604 | break; |
| 2605 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2606 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 2607 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 2608 | break; |
| 2609 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2610 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 2611 | case EVENT_REGISTER_NETWORK_LISTENER: { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2612 | handleRegisterNetworkRequest((NetworkRequestInfo) msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2613 | break; |
| 2614 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2615 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: |
| 2616 | case EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT: { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2617 | handleRegisterNetworkRequestWithIntent(msg); |
| 2618 | break; |
| 2619 | } |
| 2620 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 2621 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 2622 | break; |
| 2623 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2624 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2625 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2626 | break; |
| 2627 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2628 | case EVENT_SET_ACCEPT_UNVALIDATED: { |
| 2629 | handleSetAcceptUnvalidated((Network) msg.obj, msg.arg1 != 0, msg.arg2 != 0); |
| 2630 | break; |
| 2631 | } |
| 2632 | case EVENT_PROMPT_UNVALIDATED: { |
| 2633 | handlePromptUnvalidated((Network) msg.obj); |
| 2634 | break; |
| 2635 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2636 | case EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON: { |
| 2637 | handleMobileDataAlwaysOn(); |
| 2638 | break; |
| 2639 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2640 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
| 2641 | case NetworkAgent.CMD_START_PACKET_KEEPALIVE: { |
| 2642 | mKeepaliveTracker.handleStartKeepalive(msg); |
| 2643 | break; |
| 2644 | } |
| 2645 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
| 2646 | case NetworkAgent.CMD_STOP_PACKET_KEEPALIVE: { |
| 2647 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); |
| 2648 | int slot = msg.arg1; |
| 2649 | int reason = msg.arg2; |
| 2650 | mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); |
| 2651 | break; |
| 2652 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2653 | case EVENT_SYSTEM_READY: { |
| 2654 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2655 | nai.networkMonitor.systemReady = true; |
| 2656 | } |
| 2657 | break; |
| 2658 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2659 | } |
| 2660 | } |
| 2661 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2662 | |
| 2663 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2664 | public int tether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2665 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2666 | if (isTetheringSupported()) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2667 | final int status = mTethering.tether(iface); |
| 2668 | if (status == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 2669 | try { |
| 2670 | mPolicyManager.onTetheringChanged(iface, true); |
| 2671 | } catch (RemoteException e) { |
| 2672 | } |
| 2673 | } |
| 2674 | return status; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2675 | } else { |
| 2676 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2677 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2681 | public int untether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2682 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2683 | |
| 2684 | if (isTetheringSupported()) { |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 2685 | final int status = mTethering.untether(iface); |
| 2686 | if (status == ConnectivityManager.TETHER_ERROR_NO_ERROR) { |
| 2687 | try { |
| 2688 | mPolicyManager.onTetheringChanged(iface, false); |
| 2689 | } catch (RemoteException e) { |
| 2690 | } |
| 2691 | } |
| 2692 | return status; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2693 | } else { |
| 2694 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2695 | } |
| 2696 | } |
| 2697 | |
| 2698 | // javadoc from interface |
| 2699 | public int getLastTetherError(String iface) { |
| 2700 | enforceTetherAccessPermission(); |
| 2701 | |
| 2702 | if (isTetheringSupported()) { |
| 2703 | return mTethering.getLastTetherError(iface); |
| 2704 | } else { |
| 2705 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2706 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2707 | } |
| 2708 | |
| 2709 | // TODO - proper iface API for selection by property, inspection, etc |
| 2710 | public String[] getTetherableUsbRegexs() { |
| 2711 | enforceTetherAccessPermission(); |
| 2712 | if (isTetheringSupported()) { |
| 2713 | return mTethering.getTetherableUsbRegexs(); |
| 2714 | } else { |
| 2715 | return new String[0]; |
| 2716 | } |
| 2717 | } |
| 2718 | |
| 2719 | public String[] getTetherableWifiRegexs() { |
| 2720 | enforceTetherAccessPermission(); |
| 2721 | if (isTetheringSupported()) { |
| 2722 | return mTethering.getTetherableWifiRegexs(); |
| 2723 | } else { |
| 2724 | return new String[0]; |
| 2725 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2728 | public String[] getTetherableBluetoothRegexs() { |
| 2729 | enforceTetherAccessPermission(); |
| 2730 | if (isTetheringSupported()) { |
| 2731 | return mTethering.getTetherableBluetoothRegexs(); |
| 2732 | } else { |
| 2733 | return new String[0]; |
| 2734 | } |
| 2735 | } |
| 2736 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2737 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2738 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2739 | if (isTetheringSupported()) { |
| 2740 | return mTethering.setUsbTethering(enable); |
| 2741 | } else { |
| 2742 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2743 | } |
| 2744 | } |
| 2745 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2746 | // TODO - move iface listing, queries, etc to new module |
| 2747 | // javadoc from interface |
| 2748 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2749 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2750 | return mTethering.getTetherableIfaces(); |
| 2751 | } |
| 2752 | |
| 2753 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2754 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2755 | return mTethering.getTetheredIfaces(); |
| 2756 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2757 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2758 | public String[] getTetheringErroredIfaces() { |
| 2759 | enforceTetherAccessPermission(); |
| 2760 | return mTethering.getErroredIfaces(); |
| 2761 | } |
| 2762 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2763 | public String[] getTetheredDhcpRanges() { |
| 2764 | enforceConnectivityInternalPermission(); |
| 2765 | return mTethering.getTetheredDhcpRanges(); |
| 2766 | } |
| 2767 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2768 | // if ro.tether.denied = true we default to no tethering |
| 2769 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 2770 | // had previously been turned off. |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2771 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2772 | public boolean isTetheringSupported() { |
| 2773 | enforceTetherAccessPermission(); |
| 2774 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2775 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 2776 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0) |
| 2777 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2778 | return tetherEnabledInSettings && mUserManager.isAdminUser() && |
| 2779 | ((mTethering.getTetherableUsbRegexs().length != 0 || |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 2780 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 2781 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 2782 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2783 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2784 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2785 | @Override |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2786 | public void startTethering(int type, ResultReceiver receiver, |
| 2787 | boolean showProvisioningUi) { |
| 2788 | ConnectivityManager.enforceTetherChangePermission(mContext); |
| 2789 | if (!isTetheringSupported()) { |
| 2790 | receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null); |
| 2791 | return; |
| 2792 | } |
| 2793 | mTethering.startTethering(type, receiver, showProvisioningUi); |
| 2794 | } |
| 2795 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2796 | @Override |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2797 | public void stopTethering(int type) { |
| 2798 | ConnectivityManager.enforceTetherChangePermission(mContext); |
| 2799 | mTethering.stopTethering(type); |
| 2800 | } |
| 2801 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 2802 | // Called when we lose the default network and have no replacement yet. |
| 2803 | // This will automatically be cleared after X seconds or a new default network |
| 2804 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 2805 | // first caller and not restarted by subsequent callers. |
| 2806 | private void requestNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2807 | int serialNum = 0; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2808 | synchronized (this) { |
| 2809 | if (mNetTransitionWakeLock.isHeld()) return; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2810 | serialNum = ++mNetTransitionWakeLockSerialNumber; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2811 | mNetTransitionWakeLock.acquire(); |
| 2812 | mNetTransitionWakeLockCausedBy = forWhom; |
| 2813 | } |
| 2814 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2815 | EVENT_EXPIRE_NET_TRANSITION_WAKELOCK, serialNum, 0), |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2816 | mNetTransitionWakeLockTimeout); |
| 2817 | return; |
| 2818 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 2819 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2820 | // 100 percent is full good, 0 is full bad. |
| 2821 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2822 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Lorenzo Colitti | 0831f66 | 2015-02-11 07:39:20 +0900 | [diff] [blame] | 2823 | if (nai == null) return; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2824 | reportNetworkConnectivity(nai.network, percentage > 50); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2825 | } |
| 2826 | |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2827 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2828 | enforceAccessPermission(); |
| 2829 | enforceInternetPermission(); |
| 2830 | |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2831 | NetworkAgentInfo nai; |
| 2832 | if (network == null) { |
| 2833 | nai = getDefaultNetwork(); |
| 2834 | } else { |
| 2835 | nai = getNetworkAgentInfoForNetwork(network); |
| 2836 | } |
Paul Jensen | 4e8050e | 2015-06-25 10:28:34 -0400 | [diff] [blame] | 2837 | if (nai == null || nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTING || |
| 2838 | nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTED) { |
| 2839 | return; |
| 2840 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2841 | // Revalidate if the app report does not match our current validated state. |
| 2842 | if (hasConnectivity == nai.lastValidated) return; |
| 2843 | final int uid = Binder.getCallingUid(); |
| 2844 | if (DBG) { |
| 2845 | log("reportNetworkConnectivity(" + nai.network.netId + ", " + hasConnectivity + |
| 2846 | ") by " + uid); |
| 2847 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2848 | synchronized (nai) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2849 | // Validating a network that has not yet connected could result in a call to |
| 2850 | // rematchNetworkAndRequests() which is not meant to work on such networks. |
| 2851 | if (!nai.everConnected) return; |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2852 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 2853 | if (isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, false)) return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2854 | |
| 2855 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid); |
| 2856 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2857 | } |
| 2858 | |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2859 | private ProxyInfo getDefaultProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2860 | // this information is already available as a world read/writable jvm property |
| 2861 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2862 | // of proxy info to all the JVMs. |
| 2863 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2864 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2865 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2866 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 2867 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2868 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2869 | } |
| 2870 | |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2871 | public ProxyInfo getProxyForNetwork(Network network) { |
| 2872 | if (network == null) return getDefaultProxy(); |
| 2873 | final ProxyInfo globalProxy = getGlobalProxy(); |
| 2874 | if (globalProxy != null) return globalProxy; |
| 2875 | if (!NetworkUtils.queryUserAccess(Binder.getCallingUid(), network.netId)) return null; |
| 2876 | // Don't call getLinkProperties() as it requires ACCESS_NETWORK_STATE permission, which |
| 2877 | // caller may not have. |
| 2878 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2879 | if (nai == null) return null; |
| 2880 | synchronized (nai) { |
| 2881 | final ProxyInfo proxyInfo = nai.linkProperties.getHttpProxy(); |
| 2882 | if (proxyInfo == null) return null; |
| 2883 | return new ProxyInfo(proxyInfo); |
| 2884 | } |
| 2885 | } |
| 2886 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2887 | // Convert empty ProxyInfo's to null as null-checks are used to determine if proxies are present |
| 2888 | // (e.g. if mGlobalProxy==null fall back to network-specific proxy, if network-specific |
| 2889 | // proxy is null then there is no proxy in place). |
| 2890 | private ProxyInfo canonicalizeProxyInfo(ProxyInfo proxy) { |
| 2891 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
| 2892 | && (proxy.getPacFileUrl() == null || Uri.EMPTY.equals(proxy.getPacFileUrl()))) { |
| 2893 | proxy = null; |
| 2894 | } |
| 2895 | return proxy; |
| 2896 | } |
| 2897 | |
| 2898 | // ProxyInfo equality function with a couple modifications over ProxyInfo.equals() to make it |
| 2899 | // better for determining if a new proxy broadcast is necessary: |
| 2900 | // 1. Canonicalize empty ProxyInfos to null so an empty proxy compares equal to null so as to |
| 2901 | // avoid unnecessary broadcasts. |
| 2902 | // 2. Make sure all parts of the ProxyInfo's compare true, including the host when a PAC URL |
| 2903 | // is in place. This is important so legacy PAC resolver (see com.android.proxyhandler) |
| 2904 | // changes aren't missed. The legacy PAC resolver pretends to be a simple HTTP proxy but |
| 2905 | // actually uses the PAC to resolve; this results in ProxyInfo's with PAC URL, host and port |
| 2906 | // all set. |
| 2907 | private boolean proxyInfoEqual(ProxyInfo a, ProxyInfo b) { |
| 2908 | a = canonicalizeProxyInfo(a); |
| 2909 | b = canonicalizeProxyInfo(b); |
| 2910 | // ProxyInfo.equals() doesn't check hosts when PAC URLs are present, but we need to check |
| 2911 | // hosts even when PAC URLs are present to account for the legacy PAC resolver. |
| 2912 | return Objects.equals(a, b) && (a == null || Objects.equals(a.getHost(), b.getHost())); |
| 2913 | } |
| 2914 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2915 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2916 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2917 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2918 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2919 | if (proxyProperties == mGlobalProxy) return; |
| 2920 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 2921 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 2922 | |
| 2923 | String host = ""; |
| 2924 | int port = 0; |
| 2925 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2926 | String pacFileUrl = ""; |
| 2927 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2928 | !Uri.EMPTY.equals(proxyProperties.getPacFileUrl()))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2929 | if (!proxyProperties.isValid()) { |
| 2930 | if (DBG) |
| 2931 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2932 | return; |
| 2933 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2934 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2935 | host = mGlobalProxy.getHost(); |
| 2936 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2937 | exclList = mGlobalProxy.getExclusionListAsString(); |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2938 | if (!Uri.EMPTY.equals(proxyProperties.getPacFileUrl())) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2939 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2940 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2941 | } else { |
| 2942 | mGlobalProxy = null; |
| 2943 | } |
| 2944 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2945 | final long token = Binder.clearCallingIdentity(); |
| 2946 | try { |
| 2947 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 2948 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 2949 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 2950 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2951 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2952 | } finally { |
| 2953 | Binder.restoreCallingIdentity(token); |
| 2954 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2955 | |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 2956 | if (mGlobalProxy == null) { |
| 2957 | proxyProperties = mDefaultProxy; |
| 2958 | } |
| 2959 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2960 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2961 | } |
| 2962 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2963 | private void loadGlobalProxy() { |
| 2964 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2965 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 2966 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 2967 | String exclList = Settings.Global.getString(res, |
| 2968 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2969 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 2970 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2971 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2972 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2973 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2974 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2975 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2976 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2977 | if (!proxyProperties.isValid()) { |
| 2978 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2979 | return; |
| 2980 | } |
| 2981 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2982 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2983 | mGlobalProxy = proxyProperties; |
| 2984 | } |
| 2985 | } |
| 2986 | } |
| 2987 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2988 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2989 | // this information is already available as a world read/writable jvm property |
| 2990 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2991 | // of proxy info to all the JVMs. |
| 2992 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2993 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2994 | return mGlobalProxy; |
| 2995 | } |
| 2996 | } |
| 2997 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2998 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2999 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 3000 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3001 | proxy = null; |
| 3002 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3003 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3004 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3005 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 3006 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 3007 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 3008 | return; |
| 3009 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 3010 | |
| 3011 | // This call could be coming from the PacManager, containing the port of the local |
| 3012 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 3013 | // global (to get the correct local port), and send a broadcast. |
| 3014 | // TODO: Switch PacManager to have its own message to send back rather than |
| 3015 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 3016 | if ((mGlobalProxy != null) && (proxy != null) |
| 3017 | && (!Uri.EMPTY.equals(proxy.getPacFileUrl())) |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 3018 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 3019 | mGlobalProxy = proxy; |
| 3020 | sendProxyBroadcast(mGlobalProxy); |
| 3021 | return; |
| 3022 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3023 | mDefaultProxy = proxy; |
| 3024 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 3025 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3026 | if (!mDefaultProxyDisabled) { |
| 3027 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3028 | } |
| 3029 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3030 | } |
| 3031 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3032 | // If the proxy has changed from oldLp to newLp, resend proxy broadcast with default proxy. |
| 3033 | // This method gets called when any network changes proxy, but the broadcast only ever contains |
| 3034 | // the default proxy (even if it hasn't changed). |
| 3035 | // TODO: Deprecate the broadcast extras as they aren't necessarily applicable in a multi-network |
| 3036 | // world where an app might be bound to a non-default network. |
| 3037 | private void updateProxy(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { |
| 3038 | ProxyInfo newProxyInfo = newLp == null ? null : newLp.getHttpProxy(); |
| 3039 | ProxyInfo oldProxyInfo = oldLp == null ? null : oldLp.getHttpProxy(); |
| 3040 | |
| 3041 | if (!proxyInfoEqual(newProxyInfo, oldProxyInfo)) { |
| 3042 | sendProxyBroadcast(getDefaultProxy()); |
| 3043 | } |
| 3044 | } |
| 3045 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3046 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 3047 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 3048 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3049 | if (!TextUtils.isEmpty(proxy)) { |
| 3050 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 3051 | if (data.length == 0) { |
| 3052 | return; |
| 3053 | } |
| 3054 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3055 | String proxyHost = data[0]; |
| 3056 | int proxyPort = 8080; |
| 3057 | if (data.length > 1) { |
| 3058 | try { |
| 3059 | proxyPort = Integer.parseInt(data[1]); |
| 3060 | } catch (NumberFormatException e) { |
| 3061 | return; |
| 3062 | } |
| 3063 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3064 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3065 | setGlobalProxy(p); |
| 3066 | } |
| 3067 | } |
| 3068 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3069 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 3070 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 3071 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 3072 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3073 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3074 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3075 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3076 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3077 | final long ident = Binder.clearCallingIdentity(); |
| 3078 | try { |
| 3079 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3080 | } finally { |
| 3081 | Binder.restoreCallingIdentity(ident); |
| 3082 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | private static class SettingsObserver extends ContentObserver { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3086 | final private HashMap<Uri, Integer> mUriEventMap; |
| 3087 | final private Context mContext; |
| 3088 | final private Handler mHandler; |
| 3089 | |
| 3090 | SettingsObserver(Context context, Handler handler) { |
| 3091 | super(null); |
| 3092 | mUriEventMap = new HashMap<Uri, Integer>(); |
| 3093 | mContext = context; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3094 | mHandler = handler; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3095 | } |
| 3096 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3097 | void observe(Uri uri, int what) { |
| 3098 | mUriEventMap.put(uri, what); |
| 3099 | final ContentResolver resolver = mContext.getContentResolver(); |
| 3100 | resolver.registerContentObserver(uri, false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | @Override |
| 3104 | public void onChange(boolean selfChange) { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3105 | Slog.wtf(TAG, "Should never be reached."); |
| 3106 | } |
| 3107 | |
| 3108 | @Override |
| 3109 | public void onChange(boolean selfChange, Uri uri) { |
| 3110 | final Integer what = mUriEventMap.get(uri); |
| 3111 | if (what != null) { |
| 3112 | mHandler.obtainMessage(what.intValue()).sendToTarget(); |
| 3113 | } else { |
| 3114 | loge("No matching event to send for URI=" + uri); |
| 3115 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3116 | } |
| 3117 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3118 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3119 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3120 | Slog.d(TAG, s); |
| 3121 | } |
| 3122 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3123 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3124 | Slog.e(TAG, s); |
| 3125 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3126 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3127 | private static <T> T checkNotNull(T value, String message) { |
| 3128 | if (value == null) { |
| 3129 | throw new NullPointerException(message); |
| 3130 | } |
| 3131 | return value; |
| 3132 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3133 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3134 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 3135 | * Prepare for a VPN application. |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3136 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 3137 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 3138 | * |
| 3139 | * @param oldPackage Package name of the application which currently controls VPN, which will |
| 3140 | * be replaced. If there is no such application, this should should either be |
| 3141 | * {@code null} or {@link VpnConfig.LEGACY_VPN}. |
| 3142 | * @param newPackage Package name of the application which should gain control of VPN, or |
| 3143 | * {@code null} to disable. |
| 3144 | * @param userId User for whom to prepare the new VPN. |
| 3145 | * |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3146 | * @hide |
| 3147 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3148 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3149 | public boolean prepareVpn(@Nullable String oldPackage, @Nullable String newPackage, |
| 3150 | int userId) { |
| 3151 | enforceCrossUserPermission(userId); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3152 | throwIfLockdownEnabled(); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3153 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3154 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3155 | Vpn vpn = mVpns.get(userId); |
| 3156 | if (vpn != null) { |
| 3157 | return vpn.prepare(oldPackage, newPackage); |
| 3158 | } else { |
| 3159 | return false; |
| 3160 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3161 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3162 | } |
| 3163 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3164 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3165 | * Set whether the VPN package has the ability to launch VPNs without user intervention. |
| 3166 | * This method is used by system-privileged apps. |
| 3167 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 3168 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 3169 | * |
| 3170 | * @param packageName The package for which authorization state should change. |
| 3171 | * @param userId User for whom {@code packageName} is installed. |
| 3172 | * @param authorized {@code true} if this app should be able to start a VPN connection without |
| 3173 | * explicit user approval, {@code false} if not. |
| 3174 | * |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3175 | * @hide |
| 3176 | */ |
| 3177 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3178 | public void setVpnPackageAuthorization(String packageName, int userId, boolean authorized) { |
| 3179 | enforceCrossUserPermission(userId); |
| 3180 | |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3181 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3182 | Vpn vpn = mVpns.get(userId); |
| 3183 | if (vpn != null) { |
| 3184 | vpn.setPackageAuthorization(packageName, authorized); |
| 3185 | } |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3186 | } |
| 3187 | } |
| 3188 | |
| 3189 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3190 | * Configure a TUN interface and return its file descriptor. Parameters |
| 3191 | * 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] | 3192 | * and not available in ConnectivityManager. Permissions are checked in |
| 3193 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3194 | * @hide |
| 3195 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3196 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3197 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3198 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3199 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3200 | synchronized(mVpns) { |
| 3201 | return mVpns.get(user).establish(config); |
| 3202 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3203 | } |
| 3204 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3205 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3206 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 3207 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3208 | */ |
| 3209 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3210 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3211 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3212 | final LinkProperties egress = getActiveLinkProperties(); |
| 3213 | if (egress == null) { |
| 3214 | throw new IllegalStateException("Missing active network connection"); |
| 3215 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3216 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3217 | synchronized(mVpns) { |
| 3218 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 3219 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | /** |
| 3223 | * Return the information of the ongoing legacy VPN. This method is used |
| 3224 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 3225 | * are checked in Vpn class. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3226 | */ |
| 3227 | @Override |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 3228 | public LegacyVpnInfo getLegacyVpnInfo(int userId) { |
| 3229 | enforceCrossUserPermission(userId); |
Hung-ying Tyan | 44c8c5c | 2015-07-29 12:39:21 +0800 | [diff] [blame] | 3230 | if (mLockdownEnabled) { |
| 3231 | return null; |
| 3232 | } |
| 3233 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3234 | synchronized(mVpns) { |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 3235 | return mVpns.get(userId).getLegacyVpnInfo(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3236 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3237 | } |
| 3238 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3239 | /** |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 3240 | * Return the information of all ongoing VPNs. This method is used by NetworkStatsService |
| 3241 | * and not available in ConnectivityManager. |
| 3242 | */ |
| 3243 | @Override |
| 3244 | public VpnInfo[] getAllVpnInfo() { |
| 3245 | enforceConnectivityInternalPermission(); |
| 3246 | if (mLockdownEnabled) { |
| 3247 | return new VpnInfo[0]; |
| 3248 | } |
| 3249 | |
| 3250 | synchronized(mVpns) { |
| 3251 | List<VpnInfo> infoList = new ArrayList<>(); |
| 3252 | for (int i = 0; i < mVpns.size(); i++) { |
| 3253 | VpnInfo info = createVpnInfo(mVpns.valueAt(i)); |
| 3254 | if (info != null) { |
| 3255 | infoList.add(info); |
| 3256 | } |
| 3257 | } |
| 3258 | return infoList.toArray(new VpnInfo[infoList.size()]); |
| 3259 | } |
| 3260 | } |
| 3261 | |
| 3262 | /** |
| 3263 | * @return VPN information for accounting, or null if we can't retrieve all required |
| 3264 | * information, e.g primary underlying iface. |
| 3265 | */ |
| 3266 | @Nullable |
| 3267 | private VpnInfo createVpnInfo(Vpn vpn) { |
| 3268 | VpnInfo info = vpn.getVpnInfo(); |
| 3269 | if (info == null) { |
| 3270 | return null; |
| 3271 | } |
| 3272 | Network[] underlyingNetworks = vpn.getUnderlyingNetworks(); |
| 3273 | // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret |
| 3274 | // the underlyingNetworks list. |
| 3275 | if (underlyingNetworks == null) { |
| 3276 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 3277 | if (defaultNetwork != null && defaultNetwork.linkProperties != null) { |
| 3278 | info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName(); |
| 3279 | } |
| 3280 | } else if (underlyingNetworks.length > 0) { |
| 3281 | LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]); |
| 3282 | if (linkProperties != null) { |
| 3283 | info.primaryUnderlyingIface = linkProperties.getInterfaceName(); |
| 3284 | } |
| 3285 | } |
| 3286 | return info.primaryUnderlyingIface == null ? null : info; |
| 3287 | } |
| 3288 | |
| 3289 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3290 | * Returns the information of the ongoing VPN for {@code userId}. This method is used by |
| 3291 | * VpnDialogs and not available in ConnectivityManager. |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3292 | * Permissions are checked in Vpn class. |
| 3293 | * @hide |
| 3294 | */ |
| 3295 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3296 | public VpnConfig getVpnConfig(int userId) { |
| 3297 | enforceCrossUserPermission(userId); |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3298 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3299 | Vpn vpn = mVpns.get(userId); |
| 3300 | if (vpn != null) { |
| 3301 | return vpn.getVpnConfig(); |
| 3302 | } else { |
| 3303 | return null; |
| 3304 | } |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3305 | } |
| 3306 | } |
| 3307 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3308 | @Override |
| 3309 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 3310 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 3311 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 3312 | return false; |
| 3313 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3314 | |
| 3315 | // Tear down existing lockdown if profile was removed |
| 3316 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 3317 | if (mLockdownEnabled) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3318 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 3319 | final VpnProfile profile = VpnProfile.decode( |
| 3320 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Lorenzo Colitti | 9b23f88 | 2015-10-13 15:21:21 +0900 | [diff] [blame] | 3321 | if (profile == null) { |
| 3322 | Slog.e(TAG, "Lockdown VPN configured invalid profile " + profileName); |
| 3323 | setLockdownTracker(null); |
| 3324 | return true; |
| 3325 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3326 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3327 | synchronized(mVpns) { |
Robin Lee | 18566c1 | 2016-03-14 13:08:48 +0000 | [diff] [blame] | 3328 | Vpn vpn = mVpns.get(user); |
| 3329 | if (vpn == null) { |
| 3330 | Slog.w(TAG, "VPN for user " + user + " not ready yet. Skipping lockdown"); |
| 3331 | return false; |
| 3332 | } |
| 3333 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, vpn, profile)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3334 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3335 | } else { |
| 3336 | setLockdownTracker(null); |
| 3337 | } |
| 3338 | |
| 3339 | return true; |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 3344 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 3345 | */ |
| 3346 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 3347 | // Shutdown any existing tracker |
| 3348 | final LockdownVpnTracker existing = mLockdownTracker; |
| 3349 | mLockdownTracker = null; |
| 3350 | if (existing != null) { |
| 3351 | existing.shutdown(); |
| 3352 | } |
| 3353 | |
| 3354 | try { |
| 3355 | if (tracker != null) { |
| 3356 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 3357 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3358 | mLockdownTracker = tracker; |
| 3359 | mLockdownTracker.init(); |
| 3360 | } else { |
| 3361 | mNetd.setFirewallEnabled(false); |
| 3362 | } |
| 3363 | } catch (RemoteException e) { |
| 3364 | // ignored; NMS lives inside system_server |
| 3365 | } |
| 3366 | } |
| 3367 | |
| 3368 | private void throwIfLockdownEnabled() { |
| 3369 | if (mLockdownEnabled) { |
| 3370 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 3371 | } |
| 3372 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3373 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3374 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3375 | * Starts the always-on VPN {@link VpnService} for user {@param userId}, which should perform |
| 3376 | * some setup and then call {@code establish()} to connect. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3377 | * |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3378 | * @return {@code true} if the service was started, the service was already connected, or there |
| 3379 | * was no always-on VPN to start. {@code false} otherwise. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3380 | */ |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3381 | private boolean startAlwaysOnVpn(int userId) { |
| 3382 | final String alwaysOnPackage; |
| 3383 | synchronized (mVpns) { |
| 3384 | Vpn vpn = mVpns.get(userId); |
| 3385 | if (vpn == null) { |
| 3386 | // Shouldn't happen as all codepaths that point here should have checked the Vpn |
| 3387 | // exists already. |
| 3388 | Slog.wtf(TAG, "User " + userId + " has no Vpn configuration"); |
| 3389 | return false; |
| 3390 | } |
| 3391 | alwaysOnPackage = vpn.getAlwaysOnPackage(); |
| 3392 | // Skip if there is no service to start. |
| 3393 | if (alwaysOnPackage == null) { |
| 3394 | return true; |
| 3395 | } |
| 3396 | // Skip if the service is already established. This isn't bulletproof: it's not bound |
| 3397 | // until after establish(), so if it's mid-setup onStartCommand will be sent twice, |
| 3398 | // which may restart the connection. |
| 3399 | if (vpn.getNetworkInfo().isConnected()) { |
| 3400 | return true; |
| 3401 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3402 | } |
| 3403 | |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3404 | // Start the VPN service declared in the app's manifest. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3405 | Intent serviceIntent = new Intent(VpnConfig.SERVICE_INTERFACE); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3406 | serviceIntent.setPackage(alwaysOnPackage); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3407 | try { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3408 | return mContext.startServiceAsUser(serviceIntent, UserHandle.of(userId)) != null; |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3409 | } catch (RuntimeException e) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3410 | Slog.w(TAG, "VpnService " + serviceIntent + " failed to start", e); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3411 | return false; |
| 3412 | } |
| 3413 | } |
| 3414 | |
| 3415 | @Override |
Robin Lee | dc67971 | 2016-05-03 13:23:03 +0100 | [diff] [blame] | 3416 | public boolean setAlwaysOnVpnPackage(int userId, String packageName, boolean lockdown) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3417 | enforceConnectivityInternalPermission(); |
| 3418 | enforceCrossUserPermission(userId); |
| 3419 | |
| 3420 | // Can't set always-on VPN if legacy VPN is already in lockdown mode. |
| 3421 | if (LockdownVpnTracker.isEnabled()) { |
| 3422 | return false; |
| 3423 | } |
| 3424 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3425 | synchronized (mVpns) { |
| 3426 | Vpn vpn = mVpns.get(userId); |
| 3427 | if (vpn == null) { |
| 3428 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 3429 | return false; |
| 3430 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3431 | // If the current VPN package is the same as the new one, this is a no-op |
| 3432 | if (TextUtils.equals(packageName, vpn.getAlwaysOnPackage())) { |
| 3433 | return true; |
| 3434 | } |
| 3435 | if (!vpn.setAlwaysOnPackage(packageName, lockdown)) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3436 | return false; |
| 3437 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3438 | if (!startAlwaysOnVpn(userId)) { |
| 3439 | vpn.setAlwaysOnPackage(null, false); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3440 | return false; |
| 3441 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3442 | |
| 3443 | // Save the configuration |
| 3444 | final long token = Binder.clearCallingIdentity(); |
| 3445 | try { |
| 3446 | final ContentResolver cr = mContext.getContentResolver(); |
| 3447 | Settings.Secure.putStringForUser(cr, Settings.Secure.ALWAYS_ON_VPN_APP, |
| 3448 | packageName, userId); |
| 3449 | Settings.Secure.putIntForUser(cr, Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, |
| 3450 | (lockdown ? 1 : 0), userId); |
| 3451 | } finally { |
| 3452 | Binder.restoreCallingIdentity(token); |
| 3453 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3454 | } |
| 3455 | return true; |
| 3456 | } |
| 3457 | |
| 3458 | @Override |
| 3459 | public String getAlwaysOnVpnPackage(int userId) { |
| 3460 | enforceConnectivityInternalPermission(); |
| 3461 | enforceCrossUserPermission(userId); |
| 3462 | |
| 3463 | synchronized (mVpns) { |
| 3464 | Vpn vpn = mVpns.get(userId); |
| 3465 | if (vpn == null) { |
| 3466 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 3467 | return null; |
| 3468 | } |
| 3469 | return vpn.getAlwaysOnPackage(); |
| 3470 | } |
| 3471 | } |
| 3472 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3473 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3474 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3475 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 3476 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3477 | } |
| 3478 | |
| 3479 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3480 | private static enum NotificationType { SIGN_IN, NO_INTERNET; }; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3481 | |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3482 | private void setProvNotificationVisible(boolean visible, int networkType, String action) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3483 | Intent intent = new Intent(action); |
| 3484 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3485 | // Concatenate the range of types onto the range of NetIDs. |
| 3486 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3487 | setProvNotificationVisibleIntent(visible, id, NotificationType.SIGN_IN, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3488 | networkType, null, pendingIntent, false); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3489 | } |
| 3490 | |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3491 | /** |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3492 | * Show or hide network provisioning notifications. |
| 3493 | * |
| 3494 | * We use notifications for two purposes: to notify that a network requires sign in |
| 3495 | * (NotificationType.SIGN_IN), or to notify that a network does not have Internet access |
| 3496 | * (NotificationType.NO_INTERNET). We display at most one notification per ID, so on a |
| 3497 | * particular network we can display the notification type that was most recently requested. |
| 3498 | * So for example if a captive portal fails to reply within a few seconds of connecting, we |
| 3499 | * might first display NO_INTERNET, and then when the captive portal check completes, display |
| 3500 | * SIGN_IN. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3501 | * |
| 3502 | * @param id an identifier that uniquely identifies this notification. This must match |
| 3503 | * between show and hide calls. We use the NetID value but for legacy callers |
| 3504 | * we concatenate the range of types with the range of NetIDs. |
| 3505 | */ |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3506 | private void setProvNotificationVisibleIntent(boolean visible, int id, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3507 | NotificationType notifyType, int networkType, String extraInfo, PendingIntent intent, |
| 3508 | boolean highPriority) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3509 | if (VDBG || (DBG && visible)) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3510 | log("setProvNotificationVisibleIntent " + notifyType + " visible=" + visible |
| 3511 | + " networkType=" + getNetworkTypeName(networkType) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3512 | + " extraInfo=" + extraInfo + " highPriority=" + highPriority); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3513 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3514 | |
| 3515 | Resources r = Resources.getSystem(); |
| 3516 | NotificationManager notificationManager = (NotificationManager) mContext |
| 3517 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 3518 | |
| 3519 | if (visible) { |
| 3520 | CharSequence title; |
| 3521 | CharSequence details; |
| 3522 | int icon; |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3523 | if (notifyType == NotificationType.NO_INTERNET && |
| 3524 | networkType == ConnectivityManager.TYPE_WIFI) { |
| 3525 | title = r.getString(R.string.wifi_no_internet, 0); |
| 3526 | details = r.getString(R.string.wifi_no_internet_detailed); |
| 3527 | icon = R.drawable.stat_notify_wifi_in_range; // TODO: Need new icon. |
| 3528 | } else if (notifyType == NotificationType.SIGN_IN) { |
| 3529 | switch (networkType) { |
| 3530 | case ConnectivityManager.TYPE_WIFI: |
| 3531 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 3532 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 3533 | extraInfo); |
| 3534 | icon = R.drawable.stat_notify_wifi_in_range; |
| 3535 | break; |
| 3536 | case ConnectivityManager.TYPE_MOBILE: |
| 3537 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
| 3538 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3539 | // TODO: Change this to pull from NetworkInfo once a printable |
| 3540 | // name has been added to it |
| 3541 | details = mTelephonyManager.getNetworkOperatorName(); |
| 3542 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3543 | break; |
| 3544 | default: |
| 3545 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3546 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 3547 | extraInfo); |
| 3548 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3549 | break; |
| 3550 | } |
| 3551 | } else { |
| 3552 | Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network type " |
| 3553 | + getNetworkTypeName(networkType)); |
| 3554 | return; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3555 | } |
| 3556 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3557 | Notification notification = new Notification.Builder(mContext) |
| 3558 | .setWhen(0) |
| 3559 | .setSmallIcon(icon) |
| 3560 | .setAutoCancel(true) |
| 3561 | .setTicker(title) |
| 3562 | .setColor(mContext.getColor( |
| 3563 | com.android.internal.R.color.system_notification_accent_color)) |
| 3564 | .setContentTitle(title) |
| 3565 | .setContentText(details) |
| 3566 | .setContentIntent(intent) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3567 | .setLocalOnly(true) |
| 3568 | .setPriority(highPriority ? |
| 3569 | Notification.PRIORITY_HIGH : |
| 3570 | Notification.PRIORITY_DEFAULT) |
Lorenzo Colitti | 46d50b7 | 2015-10-21 09:15:41 +0900 | [diff] [blame] | 3571 | .setDefaults(highPriority ? Notification.DEFAULT_ALL : 0) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3572 | .setOnlyAlertOnce(true) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3573 | .build(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3574 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3575 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3576 | notificationManager.notify(NOTIFICATION_ID, id, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3577 | } catch (NullPointerException npe) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3578 | loge("setNotificationVisible: visible notificationManager npe=" + npe); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3579 | npe.printStackTrace(); |
| 3580 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3581 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3582 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3583 | notificationManager.cancel(NOTIFICATION_ID, id); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3584 | } catch (NullPointerException npe) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3585 | loge("setNotificationVisible: cancel notificationManager npe=" + npe); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3586 | npe.printStackTrace(); |
| 3587 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3588 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3589 | } |
| 3590 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3591 | /** Location to an updatable file listing carrier provisioning urls. |
| 3592 | * An example: |
| 3593 | * |
| 3594 | * <?xml version="1.0" encoding="utf-8"?> |
| 3595 | * <provisioningUrls> |
| 3596 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3597 | * </provisioningUrls> |
| 3598 | */ |
| 3599 | private static final String PROVISIONING_URL_PATH = |
| 3600 | "/data/misc/radio/provisioning_urls.xml"; |
| 3601 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3602 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3603 | /** XML tag for root element. */ |
| 3604 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 3605 | /** XML tag for individual url */ |
| 3606 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3607 | /** XML attribute for mcc */ |
| 3608 | private static final String ATTR_MCC = "mcc"; |
| 3609 | /** XML attribute for mnc */ |
| 3610 | private static final String ATTR_MNC = "mnc"; |
| 3611 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3612 | private String getProvisioningUrlBaseFromFile() { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3613 | FileReader fileReader = null; |
| 3614 | XmlPullParser parser = null; |
| 3615 | Configuration config = mContext.getResources().getConfiguration(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3616 | |
| 3617 | try { |
| 3618 | fileReader = new FileReader(mProvisioningUrlFile); |
| 3619 | parser = Xml.newPullParser(); |
| 3620 | parser.setInput(fileReader); |
| 3621 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 3622 | |
| 3623 | while (true) { |
| 3624 | XmlUtils.nextElement(parser); |
| 3625 | |
| 3626 | String element = parser.getName(); |
| 3627 | if (element == null) break; |
| 3628 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3629 | if (element.equals(TAG_PROVISIONING_URL)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3630 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 3631 | try { |
| 3632 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 3633 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 3634 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 3635 | parser.next(); |
| 3636 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 3637 | return parser.getText(); |
| 3638 | } |
| 3639 | } |
| 3640 | } |
| 3641 | } catch (NumberFormatException e) { |
| 3642 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 3643 | } |
| 3644 | } |
| 3645 | } |
| 3646 | return null; |
| 3647 | } catch (FileNotFoundException e) { |
| 3648 | loge("Carrier Provisioning Urls file not found"); |
| 3649 | } catch (XmlPullParserException e) { |
| 3650 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 3651 | } catch (IOException e) { |
| 3652 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 3653 | } finally { |
| 3654 | if (fileReader != null) { |
| 3655 | try { |
| 3656 | fileReader.close(); |
| 3657 | } catch (IOException e) {} |
| 3658 | } |
| 3659 | } |
| 3660 | return null; |
| 3661 | } |
| 3662 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3663 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3664 | public String getMobileProvisioningUrl() { |
| 3665 | enforceConnectivityInternalPermission(); |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3666 | String url = getProvisioningUrlBaseFromFile(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3667 | if (TextUtils.isEmpty(url)) { |
| 3668 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3669 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3670 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3671 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3672 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3673 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3674 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3675 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 3676 | if (TextUtils.isEmpty(phoneNumber)) { |
| 3677 | phoneNumber = "0000000000"; |
| 3678 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3679 | url = String.format(url, |
| 3680 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 3681 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3682 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3683 | } |
| 3684 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3685 | return url; |
| 3686 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3687 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3688 | @Override |
| 3689 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3690 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3691 | enforceConnectivityInternalPermission(); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3692 | final long ident = Binder.clearCallingIdentity(); |
| 3693 | try { |
| 3694 | setProvNotificationVisible(visible, networkType, action); |
| 3695 | } finally { |
| 3696 | Binder.restoreCallingIdentity(ident); |
| 3697 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3698 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 3699 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3700 | @Override |
| 3701 | public void setAirplaneMode(boolean enable) { |
| 3702 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3703 | final long ident = Binder.clearCallingIdentity(); |
| 3704 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 3705 | final ContentResolver cr = mContext.getContentResolver(); |
| 3706 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 3707 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 3708 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 3709 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3710 | } finally { |
| 3711 | Binder.restoreCallingIdentity(ident); |
| 3712 | } |
| 3713 | } |
| 3714 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3715 | private void onUserStart(int userId) { |
| 3716 | synchronized(mVpns) { |
| 3717 | Vpn userVpn = mVpns.get(userId); |
| 3718 | if (userVpn != null) { |
| 3719 | loge("Starting user already has a VPN"); |
| 3720 | return; |
| 3721 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 3722 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3723 | mVpns.put(userId, userVpn); |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3724 | |
| 3725 | final ContentResolver cr = mContext.getContentResolver(); |
| 3726 | String alwaysOnPackage = Settings.Secure.getStringForUser(cr, |
| 3727 | Settings.Secure.ALWAYS_ON_VPN_APP, userId); |
| 3728 | final boolean alwaysOnLockdown = Settings.Secure.getIntForUser(cr, |
| 3729 | Settings.Secure.ALWAYS_ON_VPN_LOCKDOWN, /* default */ 0, userId) != 0; |
| 3730 | if (alwaysOnPackage != null) { |
| 3731 | userVpn.setAlwaysOnPackage(alwaysOnPackage, alwaysOnLockdown); |
| 3732 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3733 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3734 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 3735 | updateLockdownVpn(); |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3736 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3737 | } |
| 3738 | |
| 3739 | private void onUserStop(int userId) { |
| 3740 | synchronized(mVpns) { |
| 3741 | Vpn userVpn = mVpns.get(userId); |
| 3742 | if (userVpn == null) { |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 3743 | loge("Stopped user has no VPN"); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3744 | return; |
| 3745 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3746 | userVpn.onUserStopped(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3747 | mVpns.delete(userId); |
| 3748 | } |
| 3749 | } |
| 3750 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 3751 | private void onUserAdded(int userId) { |
| 3752 | synchronized(mVpns) { |
| 3753 | final int vpnsSize = mVpns.size(); |
| 3754 | for (int i = 0; i < vpnsSize; i++) { |
| 3755 | Vpn vpn = mVpns.valueAt(i); |
| 3756 | vpn.onUserAdded(userId); |
| 3757 | } |
| 3758 | } |
| 3759 | } |
| 3760 | |
| 3761 | private void onUserRemoved(int userId) { |
| 3762 | synchronized(mVpns) { |
| 3763 | final int vpnsSize = mVpns.size(); |
| 3764 | for (int i = 0; i < vpnsSize; i++) { |
| 3765 | Vpn vpn = mVpns.valueAt(i); |
| 3766 | vpn.onUserRemoved(userId); |
| 3767 | } |
| 3768 | } |
| 3769 | } |
| 3770 | |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 3771 | private void onUserUnlocked(int userId) { |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3772 | // User present may be sent because of an unlock, which might mean an unlocked keystore. |
| 3773 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 3774 | updateLockdownVpn(); |
| 3775 | } else { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 3776 | startAlwaysOnVpn(userId); |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3777 | } |
| 3778 | } |
| 3779 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3780 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 3781 | @Override |
| 3782 | public void onReceive(Context context, Intent intent) { |
| 3783 | final String action = intent.getAction(); |
| 3784 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 3785 | if (userId == UserHandle.USER_NULL) return; |
| 3786 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 3787 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3788 | onUserStart(userId); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 3789 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3790 | onUserStop(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 3791 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 3792 | onUserAdded(userId); |
| 3793 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 3794 | onUserRemoved(userId); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 3795 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 3796 | onUserUnlocked(userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3797 | } |
| 3798 | } |
| 3799 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3800 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3801 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 3802 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3803 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 3804 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3805 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3806 | private static final int MAX_NETWORK_REQUESTS_PER_UID = 100; |
| 3807 | // Map from UID to number of NetworkRequests that UID has filed. |
| 3808 | @GuardedBy("mUidToNetworkRequestCount") |
| 3809 | private final SparseIntArray mUidToNetworkRequestCount = new SparseIntArray(); |
| 3810 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3811 | private static class NetworkFactoryInfo { |
| 3812 | public final String name; |
| 3813 | public final Messenger messenger; |
| 3814 | public final AsyncChannel asyncChannel; |
| 3815 | |
| 3816 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 3817 | this.name = name; |
| 3818 | this.messenger = messenger; |
| 3819 | this.asyncChannel = asyncChannel; |
| 3820 | } |
| 3821 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3822 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3823 | /** |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3824 | * A NetworkRequest as registered by an application can be one of three |
| 3825 | * types: |
| 3826 | * |
| 3827 | * - "listen", for which the framework will issue callbacks about any |
| 3828 | * and all networks that match the specified NetworkCapabilities, |
| 3829 | * |
| 3830 | * - "request", capable of causing a specific network to be created |
| 3831 | * first (e.g. a telephony DUN request), the framework will issue |
| 3832 | * callbacks about the single, highest scoring current network |
| 3833 | * (if any) that matches the specified NetworkCapabilities, or |
| 3834 | * |
| 3835 | * - "track the default network", a hybrid of the two designed such |
| 3836 | * that the framework will issue callbacks for the single, highest |
| 3837 | * scoring current network (if any) that matches the capabilities of |
| 3838 | * the default Internet request (mDefaultRequest), but which cannot |
| 3839 | * cause the framework to either create or retain the existence of |
| 3840 | * any specific network. |
| 3841 | * |
| 3842 | */ |
| 3843 | private static enum NetworkRequestType { |
| 3844 | LISTEN, |
| 3845 | TRACK_DEFAULT, |
| 3846 | REQUEST |
| 3847 | }; |
| 3848 | |
| 3849 | /** |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3850 | * Tracks info about the requester. |
| 3851 | * Also used to notice when the calling process dies so we can self-expire |
| 3852 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3853 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3854 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3855 | final PendingIntent mPendingIntent; |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 3856 | boolean mPendingIntentSent; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3857 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3858 | final int mPid; |
| 3859 | final int mUid; |
| 3860 | final Messenger messenger; |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3861 | private final NetworkRequestType mType; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3862 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3863 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi, NetworkRequestType type) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3864 | request = r; |
| 3865 | mPendingIntent = pi; |
| 3866 | messenger = null; |
| 3867 | mBinder = null; |
| 3868 | mPid = getCallingPid(); |
| 3869 | mUid = getCallingUid(); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3870 | mType = type; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3871 | enforceRequestCountLimit(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3872 | } |
| 3873 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3874 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, NetworkRequestType type) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3875 | super(); |
| 3876 | messenger = m; |
| 3877 | request = r; |
| 3878 | mBinder = binder; |
| 3879 | mPid = getCallingPid(); |
| 3880 | mUid = getCallingUid(); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3881 | mType = type; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3882 | mPendingIntent = null; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3883 | enforceRequestCountLimit(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3884 | |
| 3885 | try { |
| 3886 | mBinder.linkToDeath(this, 0); |
| 3887 | } catch (RemoteException e) { |
| 3888 | binderDied(); |
| 3889 | } |
| 3890 | } |
| 3891 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3892 | private void enforceRequestCountLimit() { |
| 3893 | synchronized (mUidToNetworkRequestCount) { |
| 3894 | int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1; |
| 3895 | if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { |
| 3896 | throw new IllegalArgumentException("Too many NetworkRequests filed"); |
| 3897 | } |
| 3898 | mUidToNetworkRequestCount.put(mUid, networkRequests); |
| 3899 | } |
| 3900 | } |
| 3901 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3902 | private String typeString() { |
| 3903 | switch (mType) { |
| 3904 | case LISTEN: return "Listen"; |
| 3905 | case REQUEST: return "Request"; |
| 3906 | case TRACK_DEFAULT: return "Track default"; |
| 3907 | default: |
| 3908 | return "unknown type"; |
| 3909 | } |
| 3910 | } |
| 3911 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3912 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3913 | if (mBinder != null) { |
| 3914 | mBinder.unlinkToDeath(this, 0); |
| 3915 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | public void binderDied() { |
| 3919 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 3920 | request + ", " + mBinder + ")"); |
| 3921 | releaseNetworkRequest(request); |
| 3922 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3923 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3924 | /** |
| 3925 | * Returns true iff. the contained NetworkRequest is one that: |
| 3926 | * |
| 3927 | * - should be associated with at most one satisfying network |
| 3928 | * at a time; |
| 3929 | * |
| 3930 | * - should cause a network to be kept up if it is the only network |
| 3931 | * which can satisfy the NetworkReqeust. |
| 3932 | * |
| 3933 | * For full detail of how isRequest() is used for pairing Networks with |
| 3934 | * NetworkRequests read rematchNetworkAndRequests(). |
| 3935 | * |
| 3936 | * TODO: Rename to something more properly descriptive. |
| 3937 | */ |
| 3938 | public boolean isRequest() { |
| 3939 | return (mType == NetworkRequestType.TRACK_DEFAULT) || |
| 3940 | (mType == NetworkRequestType.REQUEST); |
| 3941 | } |
| 3942 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3943 | public String toString() { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3944 | return typeString() + |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3945 | " from uid/pid:" + mUid + "/" + mPid + |
| 3946 | " for " + request + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3947 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3948 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3949 | } |
| 3950 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 3951 | private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { |
| 3952 | final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); |
| 3953 | if (badCapability != null) { |
| 3954 | throw new IllegalArgumentException("Cannot request network with " + badCapability); |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3955 | } |
| 3956 | } |
| 3957 | |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3958 | private ArrayList<Integer> getSignalStrengthThresholds(NetworkAgentInfo nai) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3959 | final SortedSet<Integer> thresholds = new TreeSet(); |
| 3960 | synchronized (nai) { |
| 3961 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 3962 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3963 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3964 | thresholds.add(nri.request.networkCapabilities.getSignalStrength()); |
| 3965 | } |
| 3966 | } |
| 3967 | } |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3968 | return new ArrayList<Integer>(thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3969 | } |
| 3970 | |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3971 | private void updateSignalStrengthThresholds( |
| 3972 | NetworkAgentInfo nai, String reason, NetworkRequest request) { |
| 3973 | ArrayList<Integer> thresholdsArray = getSignalStrengthThresholds(nai); |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3974 | Bundle thresholds = new Bundle(); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3975 | thresholds.putIntegerArrayList("thresholds", thresholdsArray); |
| 3976 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3977 | if (VDBG || (DBG && !"CONNECT".equals(reason))) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3978 | String detail; |
| 3979 | if (request != null && request.networkCapabilities.hasSignalStrength()) { |
| 3980 | detail = reason + " " + request.networkCapabilities.getSignalStrength(); |
| 3981 | } else { |
| 3982 | detail = reason; |
| 3983 | } |
| 3984 | log(String.format("updateSignalStrengthThresholds: %s, sending %s to %s", |
| 3985 | detail, Arrays.toString(thresholdsArray.toArray()), nai.name())); |
| 3986 | } |
| 3987 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3988 | nai.asyncChannel.sendMessage( |
| 3989 | android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3990 | 0, 0, thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3991 | } |
| 3992 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3993 | @Override |
| 3994 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3995 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3996 | final NetworkRequestType type = (networkCapabilities == null) |
| 3997 | ? NetworkRequestType.TRACK_DEFAULT |
| 3998 | : NetworkRequestType.REQUEST; |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3999 | // If the requested networkCapabilities is null, take them instead from |
| 4000 | // the default network request. This allows callers to keep track of |
| 4001 | // the system default network. |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4002 | if (type == NetworkRequestType.TRACK_DEFAULT) { |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4003 | networkCapabilities = new NetworkCapabilities(mDefaultRequest.networkCapabilities); |
| 4004 | enforceAccessPermission(); |
| 4005 | } else { |
| 4006 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 4007 | enforceNetworkRequestPermissions(networkCapabilities); |
| 4008 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4009 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4010 | ensureRequestableCapabilities(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4011 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4012 | if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4013 | throw new IllegalArgumentException("Bad timeout specified"); |
| 4014 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4015 | |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 4016 | if (NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER |
| 4017 | .equals(networkCapabilities.getNetworkSpecifier())) { |
| 4018 | throw new IllegalArgumentException("Invalid network specifier - must not be '" |
| 4019 | + NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER + "'"); |
| 4020 | } |
| 4021 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4022 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
| 4023 | nextNetworkRequestId()); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4024 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, type); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4025 | if (DBG) log("requestNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4026 | |
| 4027 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4028 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4029 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4030 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4031 | } |
| 4032 | return networkRequest; |
| 4033 | } |
| 4034 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4035 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 4036 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4037 | enforceConnectivityInternalPermission(); |
| 4038 | } else { |
| 4039 | enforceChangePermission(); |
| 4040 | } |
| 4041 | } |
| 4042 | |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4043 | @Override |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4044 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4045 | enforceAccessPermission(); |
| 4046 | NetworkAgentInfo nai = null; |
| 4047 | if (network == null) { |
| 4048 | return false; |
| 4049 | } |
| 4050 | synchronized (mNetworkForNetId) { |
| 4051 | nai = mNetworkForNetId.get(network.netId); |
| 4052 | } |
| 4053 | if (nai != null) { |
| 4054 | nai.asyncChannel.sendMessage(android.net.NetworkAgent.CMD_REQUEST_BANDWIDTH_UPDATE); |
| 4055 | return true; |
| 4056 | } |
| 4057 | return false; |
| 4058 | } |
| 4059 | |
| 4060 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4061 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
| 4062 | // if UID is restricted, don't allow them to bring up metered APNs |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 4063 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) == false) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4064 | final int uidRules; |
| 4065 | final int uid = Binder.getCallingUid(); |
| 4066 | synchronized(mRulesLock) { |
| 4067 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 4068 | } |
Felipe Leme | a4aba6e | 2016-05-20 18:04:14 -0700 | [diff] [blame] | 4069 | if (mRestrictBackground && (uidRules & RULE_ALLOW_METERED) == 0 |
| 4070 | && (uidRules & RULE_TEMPORARY_ALLOW_METERED) == 0) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4071 | // we could silently fail or we can filter the available nets to only give |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 4072 | // them those they have access to. Chose the more useful option. |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 4073 | networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4074 | } |
| 4075 | } |
| 4076 | } |
| 4077 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4078 | @Override |
| 4079 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 4080 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4081 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 4082 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 4083 | enforceNetworkRequestPermissions(networkCapabilities); |
| 4084 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4085 | ensureRequestableCapabilities(networkCapabilities); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4086 | |
| 4087 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
| 4088 | nextNetworkRequestId()); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4089 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4090 | NetworkRequestType.REQUEST); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4091 | if (DBG) log("pendingRequest for " + nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4092 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 4093 | nri)); |
| 4094 | return networkRequest; |
| 4095 | } |
| 4096 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4097 | private void releasePendingNetworkRequestWithDelay(PendingIntent operation) { |
| 4098 | mHandler.sendMessageDelayed( |
| 4099 | mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 4100 | getCallingUid(), 0, operation), mReleasePendingIntentDelayMs); |
| 4101 | } |
| 4102 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4103 | @Override |
| 4104 | public void releasePendingNetworkRequest(PendingIntent operation) { |
Paul Jensen | 1a81c39 | 2015-05-21 08:15:08 -0400 | [diff] [blame] | 4105 | checkNotNull(operation, "PendingIntent cannot be null."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4106 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 4107 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4108 | } |
| 4109 | |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 4110 | // In order to implement the compatibility measure for pre-M apps that call |
| 4111 | // WifiManager.enableNetwork(..., true) without also binding to that network explicitly, |
| 4112 | // WifiManager registers a network listen for the purpose of calling setProcessDefaultNetwork. |
| 4113 | // This ensures it has permission to do so. |
| 4114 | private boolean hasWifiNetworkListenPermission(NetworkCapabilities nc) { |
| 4115 | if (nc == null) { |
| 4116 | return false; |
| 4117 | } |
| 4118 | int[] transportTypes = nc.getTransportTypes(); |
| 4119 | if (transportTypes.length != 1 || transportTypes[0] != NetworkCapabilities.TRANSPORT_WIFI) { |
| 4120 | return false; |
| 4121 | } |
| 4122 | try { |
| 4123 | mContext.enforceCallingOrSelfPermission( |
| 4124 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 4125 | "ConnectivityService"); |
| 4126 | } catch (SecurityException e) { |
| 4127 | return false; |
| 4128 | } |
| 4129 | return true; |
| 4130 | } |
| 4131 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4132 | @Override |
| 4133 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 4134 | Messenger messenger, IBinder binder) { |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 4135 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 4136 | enforceAccessPermission(); |
| 4137 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4138 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4139 | NetworkRequest networkRequest = new NetworkRequest( |
| 4140 | new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4141 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4142 | NetworkRequestType.LISTEN); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4143 | if (VDBG) log("listenForNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4144 | |
| 4145 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 4146 | return networkRequest; |
| 4147 | } |
| 4148 | |
| 4149 | @Override |
| 4150 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 4151 | PendingIntent operation) { |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4152 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 4153 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 4154 | enforceAccessPermission(); |
| 4155 | } |
| 4156 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4157 | NetworkRequest networkRequest = new NetworkRequest( |
| 4158 | new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4159 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4160 | NetworkRequestType.LISTEN); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4161 | if (VDBG) log("pendingListenForNetwork for " + nri); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4162 | |
| 4163 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4164 | } |
| 4165 | |
| 4166 | @Override |
| 4167 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 4168 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), |
| 4169 | 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4170 | } |
| 4171 | |
| 4172 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4173 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4174 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4175 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 4176 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4177 | } |
| 4178 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4179 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4180 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4181 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 4182 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 4183 | } |
| 4184 | |
| 4185 | @Override |
| 4186 | public void unregisterNetworkFactory(Messenger messenger) { |
| 4187 | enforceConnectivityInternalPermission(); |
| 4188 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 4189 | } |
| 4190 | |
| 4191 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 4192 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 4193 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4194 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4195 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4196 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4197 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4198 | } |
| 4199 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4200 | /** |
| 4201 | * NetworkAgentInfo supporting a request by requestId. |
| 4202 | * These have already been vetted (their Capabilities satisfy the request) |
| 4203 | * and the are the highest scored network available. |
| 4204 | * the are keyed off the Requests requestId. |
| 4205 | */ |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4206 | // TODO: Yikes, this is accessed on multiple threads: add synchronization. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4207 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 4208 | new SparseArray<NetworkAgentInfo>(); |
| 4209 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4210 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 4211 | @GuardedBy("mNetworkForNetId") |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4212 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 4213 | new SparseArray<NetworkAgentInfo>(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4214 | // NOTE: Accessed on multiple threads, synchronized with mNetworkForNetId. |
| 4215 | // An entry is first added to mNetIdInUse, prior to mNetworkForNetId, so |
| 4216 | // there may not be a strict 1:1 correlation between the two. |
| 4217 | @GuardedBy("mNetworkForNetId") |
| 4218 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4219 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4220 | // NetworkAgentInfo keyed off its connecting messenger |
| 4221 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4222 | // NOTE: Only should be accessed on ConnectivityServiceThread, except dump(). |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4223 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 4224 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 4225 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 4226 | @GuardedBy("mBlockedAppUids") |
| 4227 | private final HashSet<Integer> mBlockedAppUids = new HashSet(); |
| 4228 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 4229 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4230 | private final NetworkRequest mDefaultRequest; |
| 4231 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4232 | // Request used to optionally keep mobile data active even when higher |
| 4233 | // priority networks like Wi-Fi are active. |
| 4234 | private final NetworkRequest mDefaultMobileDataRequest; |
| 4235 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 4236 | private NetworkAgentInfo getDefaultNetwork() { |
| 4237 | return mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 4238 | } |
| 4239 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4240 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 4241 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4244 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4245 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4246 | int currentScore, NetworkMisc networkMisc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4247 | enforceConnectivityInternalPermission(); |
| 4248 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 4249 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 4250 | // satisfies mDefaultRequest. |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4251 | final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4252 | new Network(reserveNetId()), new NetworkInfo(networkInfo), new LinkProperties( |
| 4253 | linkProperties), new NetworkCapabilities(networkCapabilities), currentScore, |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4254 | mContext, mTrackerHandler, new NetworkMisc(networkMisc), mDefaultRequest, this); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 4255 | synchronized (this) { |
| 4256 | nai.networkMonitor.systemReady = mSystemReady; |
| 4257 | } |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 4258 | addValidationLogs(nai.networkMonitor.getValidationLogs(), nai.network); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4259 | if (DBG) log("registerNetworkAgent " + nai); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4260 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4261 | return nai.network.netId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4262 | } |
| 4263 | |
| 4264 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 4265 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 4266 | mNetworkAgentInfos.put(na.messenger, na); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4267 | synchronized (mNetworkForNetId) { |
| 4268 | mNetworkForNetId.put(na.network.netId, na); |
| 4269 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4270 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 4271 | NetworkInfo networkInfo = na.networkInfo; |
| 4272 | na.networkInfo = null; |
| 4273 | updateNetworkInfo(na, networkInfo); |
| 4274 | } |
| 4275 | |
| 4276 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 4277 | LinkProperties newLp = networkAgent.linkProperties; |
| 4278 | int netId = networkAgent.network.netId; |
| 4279 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4280 | // The NetworkAgentInfo does not know whether clatd is running on its network or not. Before |
| 4281 | // we do anything else, make sure its LinkProperties are accurate. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4282 | if (networkAgent.clatd != null) { |
| 4283 | networkAgent.clatd.fixupLinkProperties(oldLp); |
| 4284 | } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4285 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4286 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4287 | updateMtu(newLp, oldLp); |
| 4288 | // TODO - figure out what to do for clat |
| 4289 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 4290 | // updateMtu(lp, null); |
| 4291 | // } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4292 | updateTcpBufferSizes(networkAgent); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 4293 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4294 | updateRoutes(newLp, oldLp, netId); |
| 4295 | updateDnses(newLp, oldLp, netId); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 4296 | |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4297 | updateClat(newLp, oldLp, networkAgent); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4298 | if (isDefaultNetwork(networkAgent)) { |
| 4299 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
| 4300 | } else { |
| 4301 | updateProxy(newLp, oldLp, networkAgent); |
| 4302 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4303 | // TODO - move this check to cover the whole function |
| 4304 | if (!Objects.equals(newLp, oldLp)) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4305 | notifyIfacesChangedForNetworkStats(); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4306 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 4307 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 4308 | |
| 4309 | mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4310 | } |
| 4311 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4312 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { |
| 4313 | final boolean wasRunningClat = nai.clatd != null && nai.clatd.isStarted(); |
| 4314 | final boolean shouldRunClat = Nat464Xlat.requiresClat(nai); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4315 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4316 | if (!wasRunningClat && shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4317 | nai.clatd = new Nat464Xlat(mContext, mNetd, mTrackerHandler, nai); |
| 4318 | nai.clatd.start(); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4319 | } else if (wasRunningClat && !shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4320 | nai.clatd.stop(); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4321 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4322 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4323 | |
| 4324 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 4325 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 4326 | if (oldLp != null) { |
| 4327 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 4328 | } else if (newLp != null) { |
| 4329 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 4330 | } |
| 4331 | for (String iface : interfaceDiff.added) { |
| 4332 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4333 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4334 | mNetd.addInterfaceToNetwork(iface, netId); |
| 4335 | } catch (Exception e) { |
| 4336 | loge("Exception adding interface: " + e); |
| 4337 | } |
| 4338 | } |
| 4339 | for (String iface : interfaceDiff.removed) { |
| 4340 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4341 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4342 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 4343 | } catch (Exception e) { |
| 4344 | loge("Exception removing interface: " + e); |
| 4345 | } |
| 4346 | } |
| 4347 | } |
| 4348 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 4349 | /** |
| 4350 | * Have netd update routes from oldLp to newLp. |
| 4351 | * @return true if routes changed between oldLp and newLp |
| 4352 | */ |
| 4353 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4354 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 4355 | if (oldLp != null) { |
| 4356 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 4357 | } else if (newLp != null) { |
| 4358 | routeDiff.added = newLp.getAllRoutes(); |
| 4359 | } |
| 4360 | |
| 4361 | // add routes before removing old in case it helps with continuous connectivity |
| 4362 | |
| 4363 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 4364 | for (RouteInfo route : routeDiff.added) { |
| 4365 | if (route.hasGateway()) continue; |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4366 | if (VDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4367 | try { |
| 4368 | mNetd.addRoute(netId, route); |
| 4369 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4370 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 4371 | loge("Exception in addRoute for non-gateway: " + e); |
| 4372 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4373 | } |
| 4374 | } |
| 4375 | for (RouteInfo route : routeDiff.added) { |
| 4376 | if (route.hasGateway() == false) continue; |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4377 | if (VDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4378 | try { |
| 4379 | mNetd.addRoute(netId, route); |
| 4380 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4381 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 4382 | loge("Exception in addRoute for gateway: " + e); |
| 4383 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4384 | } |
| 4385 | } |
| 4386 | |
| 4387 | for (RouteInfo route : routeDiff.removed) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4388 | if (VDBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4389 | try { |
| 4390 | mNetd.removeRoute(netId, route); |
| 4391 | } catch (Exception e) { |
| 4392 | loge("Exception in removeRoute: " + e); |
| 4393 | } |
| 4394 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 4395 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4396 | } |
Erik Kline | 4136850 | 2015-06-17 13:19:54 +0900 | [diff] [blame] | 4397 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4398 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 4399 | if (oldLp != null && newLp.isIdenticalDnses(oldLp)) { |
| 4400 | return; // no updating necessary |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4401 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4402 | |
| 4403 | Collection<InetAddress> dnses = newLp.getDnsServers(); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4404 | if (DBG) log("Setting DNS servers for network " + netId + " to " + dnses); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4405 | try { |
Pierre Imai | bd8759b | 2016-04-28 17:00:04 +0900 | [diff] [blame] | 4406 | mNetd.setDnsConfigurationForNetwork( |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4407 | netId, NetworkUtils.makeStrings(dnses), newLp.getDomains()); |
| 4408 | } catch (Exception e) { |
Pierre Imai | bd8759b | 2016-04-28 17:00:04 +0900 | [diff] [blame] | 4409 | loge("Exception in setDnsConfigurationForNetwork: " + e); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4410 | } |
| 4411 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
| 4412 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 4413 | setDefaultDnsSystemProperties(dnses); |
| 4414 | } |
| 4415 | flushVmDnsCache(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4416 | } |
| 4417 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 4418 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 4419 | int last = 0; |
| 4420 | for (InetAddress dns : dnses) { |
| 4421 | ++last; |
| 4422 | String key = "net.dns" + last; |
| 4423 | String value = dns.getHostAddress(); |
| 4424 | SystemProperties.set(key, value); |
| 4425 | } |
| 4426 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 4427 | String key = "net.dns" + i; |
| 4428 | SystemProperties.set(key, ""); |
| 4429 | } |
| 4430 | mNumDnsEntries = last; |
| 4431 | } |
| 4432 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4433 | /** |
| 4434 | * Update the NetworkCapabilities for {@code networkAgent} to {@code networkCapabilities} |
| 4435 | * augmented with any stateful capabilities implied from {@code networkAgent} |
| 4436 | * (e.g., validated status and captive portal status). |
| 4437 | * |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4438 | * @param nai the network having its capabilities updated. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4439 | * @param networkCapabilities the new network capabilities. |
| 4440 | */ |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4441 | private void updateCapabilities(NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4442 | // Don't modify caller's NetworkCapabilities. |
| 4443 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4444 | if (nai.lastValidated) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4445 | networkCapabilities.addCapability(NET_CAPABILITY_VALIDATED); |
| 4446 | } else { |
| 4447 | networkCapabilities.removeCapability(NET_CAPABILITY_VALIDATED); |
| 4448 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4449 | if (nai.lastCaptivePortalDetected) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4450 | networkCapabilities.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
| 4451 | } else { |
| 4452 | networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
| 4453 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4454 | if (!Objects.equals(nai.networkCapabilities, networkCapabilities)) { |
| 4455 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 4456 | if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) != |
| 4457 | networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { |
| 4458 | try { |
| 4459 | mNetd.setNetworkPermission(nai.network.netId, |
| 4460 | networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) ? |
| 4461 | null : NetworkManagementService.PERMISSION_SYSTEM); |
| 4462 | } catch (RemoteException e) { |
| 4463 | loge("Exception in setNetworkPermission: " + e); |
| 4464 | } |
| 4465 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4466 | synchronized (nai) { |
| 4467 | nai.networkCapabilities = networkCapabilities; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4468 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4469 | rematchAllNetworksAndRequests(nai, oldScore); |
| 4470 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4471 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4472 | } |
| 4473 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4474 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
| 4475 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 4476 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 4477 | // Don't send listening requests to factories. b/17393458 |
| 4478 | if (!isRequest(nr)) continue; |
| 4479 | sendUpdatedScoreToFactories(nr, nai.getCurrentScore()); |
| 4480 | } |
| 4481 | } |
| 4482 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4483 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 4484 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4485 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 4486 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 4487 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4488 | } |
| 4489 | } |
| 4490 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4491 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 4492 | int notificationType) { |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4493 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE && !nri.mPendingIntentSent) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4494 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 4495 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 4496 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4497 | nri.mPendingIntentSent = true; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4498 | sendIntent(nri.mPendingIntent, intent); |
| 4499 | } |
| 4500 | // else not handled |
| 4501 | } |
| 4502 | |
| 4503 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 4504 | mPendingIntentWakeLock.acquire(); |
| 4505 | try { |
| 4506 | if (DBG) log("Sending " + pendingIntent); |
| 4507 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 4508 | } catch (PendingIntent.CanceledException e) { |
| 4509 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 4510 | mPendingIntentWakeLock.release(); |
| 4511 | releasePendingNetworkRequest(pendingIntent); |
| 4512 | } |
| 4513 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 4514 | } |
| 4515 | |
| 4516 | @Override |
| 4517 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 4518 | String resultData, Bundle resultExtras) { |
| 4519 | if (DBG) log("Finished sending " + pendingIntent); |
| 4520 | mPendingIntentWakeLock.release(); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4521 | // Release with a delay so the receiving client has an opportunity to put in its |
| 4522 | // own request. |
| 4523 | releasePendingNetworkRequestWithDelay(pendingIntent); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4524 | } |
| 4525 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4526 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4527 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4528 | if (nri.messenger == null) return; // Default request has no msgr |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4529 | Bundle bundle = new Bundle(); |
| 4530 | bundle.putParcelable(NetworkRequest.class.getSimpleName(), |
| 4531 | new NetworkRequest(nri.request)); |
| 4532 | Message msg = Message.obtain(); |
| 4533 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL && |
| 4534 | notificationType != ConnectivityManager.CALLBACK_RELEASED) { |
| 4535 | bundle.putParcelable(Network.class.getSimpleName(), networkAgent.network); |
| 4536 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4537 | switch (notificationType) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4538 | case ConnectivityManager.CALLBACK_LOSING: { |
| 4539 | msg.arg1 = 30 * 1000; // TODO - read this from NetworkMonitor |
| 4540 | break; |
| 4541 | } |
| 4542 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
| 4543 | bundle.putParcelable(NetworkCapabilities.class.getSimpleName(), |
| 4544 | new NetworkCapabilities(networkAgent.networkCapabilities)); |
| 4545 | break; |
| 4546 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4547 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4548 | bundle.putParcelable(LinkProperties.class.getSimpleName(), |
| 4549 | new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4550 | break; |
| 4551 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4552 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4553 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4554 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4555 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4556 | if (VDBG) { |
| 4557 | log("sending notification " + notifyTypeToName(notificationType) + |
| 4558 | " for " + nri.request); |
| 4559 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4560 | nri.messenger.send(msg); |
| 4561 | } catch (RemoteException e) { |
| 4562 | // may occur naturally in the race of binder death. |
| 4563 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 4564 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4565 | } |
| 4566 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4567 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
| 4568 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 4569 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 4570 | // Ignore listening requests. |
| 4571 | if (!isRequest(nr)) continue; |
| 4572 | loge("Dead network still had at least " + nr); |
| 4573 | break; |
| 4574 | } |
| 4575 | nai.asyncChannel.disconnect(); |
| 4576 | } |
| 4577 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4578 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 4579 | if (oldNetwork == null) { |
| 4580 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 4581 | return; |
| 4582 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4583 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 4584 | teardownUnneededNetwork(oldNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4585 | } |
| 4586 | |
Pierre Imai | 6b4e15b | 2016-04-07 12:21:05 +0900 | [diff] [blame] | 4587 | private void makeDefault(NetworkAgentInfo newNetwork, NetworkAgentInfo prevNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4588 | if (DBG) log("Switching to new default network: " + newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4589 | setupDataActivityTracking(newNetwork); |
| 4590 | try { |
| 4591 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 4592 | } catch (Exception e) { |
| 4593 | loge("Exception setting default network :" + e); |
| 4594 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4595 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4596 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 4597 | updateTcpBufferSizes(newNetwork); |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4598 | setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 4599 | logDefaultNetworkEvent(newNetwork, prevNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4600 | } |
| 4601 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4602 | // Handles a network appearing or improving its score. |
| 4603 | // |
| 4604 | // - Evaluates all current NetworkRequests that can be |
| 4605 | // satisfied by newNetwork, and reassigns to newNetwork |
| 4606 | // any such requests for which newNetwork is the best. |
| 4607 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4608 | // - Lingers any validated Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4609 | // needed. A network is needed if it is the best network for |
| 4610 | // one or more NetworkRequests, or if it is a VPN. |
| 4611 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4612 | // - Tears down newNetwork if it just became validated |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4613 | // but turns out to be unneeded. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4614 | // |
| 4615 | // - If reapUnvalidatedNetworks==REAP, tears down unvalidated |
| 4616 | // networks that have no chance (i.e. even if validated) |
| 4617 | // of becoming the highest scoring network. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4618 | // |
| 4619 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 4620 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 4621 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 4622 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 4623 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4624 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4625 | // @param newNetwork is the network to be matched against NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4626 | // @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be |
| 4627 | // performed to tear down unvalidated networks that have no chance (i.e. even if |
| 4628 | // validated) of becoming the highest scoring network. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4629 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4630 | ReapUnvalidatedNetworks reapUnvalidatedNetworks) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 4631 | if (!newNetwork.everConnected) return; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4632 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4633 | boolean isNewDefault = false; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4634 | NetworkAgentInfo oldDefaultNetwork = null; |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 4635 | if (VDBG) log("rematching " + newNetwork.name()); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4636 | // Find and migrate to this Network any NetworkRequests for |
| 4637 | // which this network is now the best. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4638 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4639 | ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<NetworkRequestInfo>(); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4640 | if (VDBG) log(" network has: " + newNetwork.networkCapabilities); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4641 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4642 | final NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 4643 | final boolean satisfies = newNetwork.satisfies(nri.request); |
| 4644 | if (newNetwork == currentNetwork && satisfies) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4645 | if (VDBG) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 4646 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 4647 | " request " + nri.request.requestId + ". No change."); |
| 4648 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4649 | keep = true; |
| 4650 | continue; |
| 4651 | } |
| 4652 | |
| 4653 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4654 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4655 | if (satisfies) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4656 | if (!nri.isRequest()) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4657 | // This is not a request, it's a callback listener. |
| 4658 | // Add it to newNetwork regardless of score. |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4659 | if (newNetwork.addRequest(nri.request)) addedRequests.add(nri); |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 4660 | continue; |
| 4661 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4662 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4663 | // next check if it's better than any current network we're using for |
| 4664 | // this request |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4665 | if (VDBG) { |
| 4666 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4667 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
| 4668 | ", newScore = " + newNetwork.getCurrentScore()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4669 | } |
| 4670 | if (currentNetwork == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4671 | currentNetwork.getCurrentScore() < newNetwork.getCurrentScore()) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4672 | if (VDBG) log("rematch for " + newNetwork.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4673 | if (currentNetwork != null) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4674 | if (VDBG) log(" accepting network in place of " + currentNetwork.name()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4675 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 4676 | currentNetwork.networkLingered.add(nri.request); |
| 4677 | affectedNetworks.add(currentNetwork); |
| 4678 | } else { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4679 | if (VDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4680 | } |
Paul Jensen | 573a035 | 2015-01-08 10:49:34 -0500 | [diff] [blame] | 4681 | unlinger(newNetwork); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4682 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4683 | if (!newNetwork.addRequest(nri.request)) { |
| 4684 | Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request); |
| 4685 | } |
| 4686 | addedRequests.add(nri); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4687 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4688 | // Tell NetworkFactories about the new score, so they can stop |
| 4689 | // trying to connect if they know they cannot match it. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4690 | // TODO - this could get expensive if we have alot of requests for this |
| 4691 | // network. Think about if there is a way to reduce this. Push |
| 4692 | // netid->request mapping to each factory? |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4693 | sendUpdatedScoreToFactories(nri.request, newNetwork.getCurrentScore()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4694 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4695 | isNewDefault = true; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4696 | oldDefaultNetwork = currentNetwork; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4697 | } |
| 4698 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4699 | } else if (newNetwork.networkRequests.get(nri.request.requestId) != null) { |
| 4700 | // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri", |
| 4701 | // mark it as no longer satisfying "nri". Because networks are processed by |
| 4702 | // rematchAllNetworkAndRequests() in descending score order, "currentNetwork" will |
| 4703 | // match "newNetwork" before this loop will encounter a "currentNetwork" with higher |
| 4704 | // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri". |
| 4705 | // This means this code doesn't have to handle the case where "currentNetwork" no |
| 4706 | // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork". |
| 4707 | if (DBG) { |
| 4708 | log("Network " + newNetwork.name() + " stopped satisfying" + |
| 4709 | " request " + nri.request.requestId); |
| 4710 | } |
| 4711 | newNetwork.networkRequests.remove(nri.request.requestId); |
| 4712 | if (currentNetwork == newNetwork) { |
| 4713 | mNetworkForRequestId.remove(nri.request.requestId); |
| 4714 | sendUpdatedScoreToFactories(nri.request, 0); |
| 4715 | } else { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4716 | if (nri.isRequest()) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4717 | Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + |
| 4718 | newNetwork.name() + |
| 4719 | " without updating mNetworkForRequestId or factories!"); |
| 4720 | } |
| 4721 | } |
| 4722 | // TODO: technically, sending CALLBACK_LOST here is |
| 4723 | // incorrect if nri is a request (not a listen) and there |
| 4724 | // is a replacement network currently connected that can |
| 4725 | // satisfy it. However, the only capability that can both |
| 4726 | // a) be requested and b) change is NET_CAPABILITY_TRUSTED, |
| 4727 | // so this code is only incorrect for a network that loses |
| 4728 | // the TRUSTED capability, which is a rare case. |
| 4729 | callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4730 | } |
| 4731 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4732 | // Linger any networks that are no longer needed. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4733 | for (NetworkAgentInfo nai : affectedNetworks) { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4734 | if (nai.lingering) { |
| 4735 | // Already lingered. Nothing to do. This can only happen if "nai" is in |
| 4736 | // "affectedNetworks" twice. The reasoning being that to get added to |
| 4737 | // "affectedNetworks", "nai" must have been satisfying a NetworkRequest |
| 4738 | // (i.e. not lingered) so it could have only been lingered by this loop. |
| 4739 | // unneeded(nai) will be false and we'll call unlinger() below which would |
| 4740 | // be bad, so handle it here. |
| 4741 | } else if (unneeded(nai)) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4742 | linger(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4743 | } else { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4744 | // Clear nai.networkLingered we might have added above. |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 4745 | unlinger(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4746 | } |
| 4747 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4748 | if (isNewDefault) { |
| 4749 | // Notify system services that this network is up. |
Pierre Imai | 6b4e15b | 2016-04-07 12:21:05 +0900 | [diff] [blame] | 4750 | makeDefault(newNetwork, oldDefaultNetwork); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4751 | synchronized (ConnectivityService.this) { |
| 4752 | // have a new default network, release the transition wakelock in |
| 4753 | // a second if it's held. The second pause is to allow apps |
| 4754 | // to reconnect over the new network |
| 4755 | if (mNetTransitionWakeLock.isHeld()) { |
| 4756 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 4757 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 4758 | mNetTransitionWakeLockSerialNumber, 0), |
| 4759 | 1000); |
| 4760 | } |
| 4761 | } |
| 4762 | } |
| 4763 | |
| 4764 | // do this after the default net is switched, but |
| 4765 | // before LegacyTypeTracker sends legacy broadcasts |
| 4766 | for (NetworkRequestInfo nri : addedRequests) notifyNetworkCallback(newNetwork, nri); |
| 4767 | |
| 4768 | if (isNewDefault) { |
| 4769 | // Maintain the illusion: since the legacy API only |
| 4770 | // understands one network at a time, we must pretend |
| 4771 | // that the current default network disconnected before |
| 4772 | // the new one connected. |
| 4773 | if (oldDefaultNetwork != null) { |
| 4774 | mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), |
| 4775 | oldDefaultNetwork, true); |
| 4776 | } |
| 4777 | mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0; |
| 4778 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
| 4779 | notifyLockdownVpn(newNetwork); |
| 4780 | } |
| 4781 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4782 | if (keep) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4783 | // Notify battery stats service about this network, both the normal |
| 4784 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4785 | // TODO: Avoid redoing this; this must only be done once when a network comes online. |
Dianne Hackborn | 2932513 | 2014-05-21 15:01:03 -0700 | [diff] [blame] | 4786 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4787 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 4788 | final int type = newNetwork.networkInfo.getType(); |
| 4789 | |
| 4790 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 4791 | bs.noteNetworkInterfaceType(baseIface, type); |
| 4792 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 4793 | final String stackedIface = stacked.getInterfaceName(); |
| 4794 | bs.noteNetworkInterfaceType(stackedIface, type); |
| 4795 | NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); |
| 4796 | } |
| 4797 | } catch (RemoteException ignored) { |
| 4798 | } |
| 4799 | |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 4800 | // This has to happen after the notifyNetworkCallbacks as that tickles each |
| 4801 | // ConnectivityManager instance so that legacy requests correctly bind dns |
| 4802 | // requests to this network. The legacy users are listening for this bcast |
| 4803 | // and will generally do a dns request so they can ensureRouteToHost and if |
| 4804 | // they do that before the callbacks happen they'll use the default network. |
| 4805 | // |
| 4806 | // TODO: Is there still a race here? We send the broadcast |
| 4807 | // after sending the callback, but if the app can receive the |
| 4808 | // broadcast before the callback, it might still break. |
| 4809 | // |
| 4810 | // This *does* introduce a race where if the user uses the new api |
| 4811 | // (notification callbacks) and then uses the old api (getNetworkInfo(type)) |
| 4812 | // they may get old info. Reverse this after the old startUsing api is removed. |
| 4813 | // This is on top of the multiple intent sequencing referenced in the todo above. |
| 4814 | for (int i = 0; i < newNetwork.networkRequests.size(); i++) { |
| 4815 | NetworkRequest nr = newNetwork.networkRequests.valueAt(i); |
| 4816 | if (nr.legacyType != TYPE_NONE && isRequest(nr)) { |
| 4817 | // legacy type tracker filters out repeat adds |
| 4818 | mLegacyTypeTracker.add(nr.legacyType, newNetwork); |
| 4819 | } |
| 4820 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 4821 | |
| 4822 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 4823 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 4824 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 4825 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 4826 | if (newNetwork.isVPN()) { |
| 4827 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 4828 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4829 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4830 | if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) { |
| 4831 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4832 | if (unneeded(nai)) { |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4833 | if (DBG) log("Reaping " + nai.name()); |
| 4834 | teardownUnneededNetwork(nai); |
| 4835 | } |
| 4836 | } |
| 4837 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4838 | } |
| 4839 | |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4840 | /** |
| 4841 | * Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 4842 | * being disconnected. |
| 4843 | * @param changed If only one Network's score or capabilities have been modified since the last |
| 4844 | * time this function was called, pass this Network in this argument, otherwise pass |
| 4845 | * null. |
| 4846 | * @param oldScore If only one Network has been changed but its NetworkCapabilities have not |
| 4847 | * changed, pass in the Network's score (from getCurrentScore()) prior to the change via |
| 4848 | * this argument, otherwise pass {@code changed.getCurrentScore()} or 0 if |
| 4849 | * {@code changed} is {@code null}. This is because NetworkCapabilities influence a |
| 4850 | * network's score. |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4851 | */ |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4852 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4853 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 4854 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 4855 | // example in the case where "changed"'s score decreases and another network should begin |
| 4856 | // satifying a NetworkRequest that "changed" currently satisfies. |
| 4857 | |
| 4858 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 4859 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 4860 | // rematchNetworkAndRequests() handles. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4861 | if (changed != null && oldScore < changed.getCurrentScore()) { |
| 4862 | rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4863 | } else { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4864 | final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray( |
| 4865 | new NetworkAgentInfo[mNetworkAgentInfos.size()]); |
| 4866 | // Rematch higher scoring networks first to prevent requests first matching a lower |
| 4867 | // scoring network and then a higher scoring network, which could produce multiple |
| 4868 | // callbacks and inadvertently unlinger networks. |
| 4869 | Arrays.sort(nais); |
| 4870 | for (NetworkAgentInfo nai : nais) { |
| 4871 | rematchNetworkAndRequests(nai, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4872 | // Only reap the last time through the loop. Reaping before all rematching |
| 4873 | // is complete could incorrectly teardown a network that hasn't yet been |
| 4874 | // rematched. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4875 | (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4876 | : ReapUnvalidatedNetworks.REAP); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4877 | } |
| 4878 | } |
| 4879 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4880 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 4881 | private void updateInetCondition(NetworkAgentInfo nai) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4882 | // Don't bother updating until we've graduated to validated at least once. |
Lorenzo Colitti | d3b8a3e | 2014-12-17 11:14:42 +0900 | [diff] [blame] | 4883 | if (!nai.everValidated) return; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4884 | // For now only update icons for default connection. |
| 4885 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 4886 | if (!isDefaultNetwork(nai)) return; |
| 4887 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 4888 | int newInetCondition = nai.lastValidated ? 100 : 0; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4889 | // Don't repeat publish. |
| 4890 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 4891 | |
| 4892 | mDefaultInetConditionPublished = newInetCondition; |
| 4893 | sendInetConditionBroadcast(nai.networkInfo); |
| 4894 | } |
| 4895 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4896 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
| 4897 | if (mLockdownTracker != null) { |
| 4898 | if (nai != null && nai.isVPN()) { |
| 4899 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 4900 | } else { |
| 4901 | mLockdownTracker.onNetworkInfoChanged(); |
| 4902 | } |
| 4903 | } |
| 4904 | } |
| 4905 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4906 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 4907 | NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4908 | NetworkInfo oldInfo = null; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4909 | final int oldScore = networkAgent.getCurrentScore(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4910 | synchronized (networkAgent) { |
| 4911 | oldInfo = networkAgent.networkInfo; |
| 4912 | networkAgent.networkInfo = newInfo; |
| 4913 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4914 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4915 | |
| 4916 | if (oldInfo != null && oldInfo.getState() == state) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4917 | if (oldInfo.isRoaming() != newInfo.isRoaming()) { |
| 4918 | if (VDBG) log("roaming status changed, notifying NetworkStatsService"); |
| 4919 | notifyIfacesChangedForNetworkStats(); |
| 4920 | } else if (VDBG) log("ignoring duplicate network state non-change"); |
| 4921 | // In either case, no further work should be needed. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4922 | return; |
| 4923 | } |
| 4924 | if (DBG) { |
| 4925 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 4926 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 4927 | " to " + state); |
| 4928 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4929 | |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 4930 | if (!networkAgent.created |
| 4931 | && (state == NetworkInfo.State.CONNECTED |
| 4932 | || (state == NetworkInfo.State.CONNECTING && networkAgent.isVPN()))) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4933 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4934 | // This should never fail. Specifying an already in use NetID will cause failure. |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4935 | if (networkAgent.isVPN()) { |
| 4936 | mNetd.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4937 | !networkAgent.linkProperties.getDnsServers().isEmpty(), |
| 4938 | (networkAgent.networkMisc == null || |
| 4939 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4940 | } else { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 4941 | mNetd.createPhysicalNetwork(networkAgent.network.netId, |
| 4942 | networkAgent.networkCapabilities.hasCapability( |
| 4943 | NET_CAPABILITY_NOT_RESTRICTED) ? |
| 4944 | null : NetworkManagementService.PERMISSION_SYSTEM); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4945 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4946 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4947 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 4948 | + e.getMessage()); |
| 4949 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4950 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4951 | networkAgent.created = true; |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | if (!networkAgent.everConnected && state == NetworkInfo.State.CONNECTED) { |
| 4955 | networkAgent.everConnected = true; |
| 4956 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4957 | updateLinkProperties(networkAgent, null); |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4958 | notifyIfacesChangedForNetworkStats(); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4959 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 4960 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4961 | scheduleUnvalidatedPrompt(networkAgent); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4962 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4963 | if (networkAgent.isVPN()) { |
| 4964 | // Temporarily disable the default proxy (not global). |
| 4965 | synchronized (mProxyLock) { |
| 4966 | if (!mDefaultProxyDisabled) { |
| 4967 | mDefaultProxyDisabled = true; |
| 4968 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4969 | sendProxyBroadcast(null); |
| 4970 | } |
| 4971 | } |
| 4972 | } |
| 4973 | // TODO: support proxy per network. |
| 4974 | } |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4975 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4976 | // Whether a particular NetworkRequest listen should cause signal strength thresholds to |
| 4977 | // be communicated to a particular NetworkAgent depends only on the network's immutable, |
| 4978 | // capabilities, so it only needs to be done once on initial connect, not every time the |
| 4979 | // network's capabilities change. Note that we do this before rematching the network, |
| 4980 | // so we could decide to tear it down immediately afterwards. That's fine though - on |
| 4981 | // disconnection NetworkAgents should stop any signal strength monitoring they have been |
| 4982 | // doing. |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4983 | updateSignalStrengthThresholds(networkAgent, "CONNECT", null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4984 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4985 | // Consider network even though it is not yet validated. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4986 | rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4987 | |
| 4988 | // This has to happen after matching the requests, because callbacks are just requests. |
| 4989 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4990 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4991 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4992 | if (networkAgent.isVPN()) { |
| 4993 | synchronized (mProxyLock) { |
| 4994 | if (mDefaultProxyDisabled) { |
| 4995 | mDefaultProxyDisabled = false; |
| 4996 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4997 | sendProxyBroadcast(mDefaultProxy); |
| 4998 | } |
| 4999 | } |
| 5000 | } |
| 5001 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 5002 | } else if ((oldInfo != null && oldInfo.getState() == NetworkInfo.State.SUSPENDED) || |
| 5003 | state == NetworkInfo.State.SUSPENDED) { |
| 5004 | // going into or coming out of SUSPEND: rescore and notify |
| 5005 | if (networkAgent.getCurrentScore() != oldScore) { |
Paul Jensen | 3b9ce37 | 2015-07-10 12:19:38 -0400 | [diff] [blame] | 5006 | rematchAllNetworksAndRequests(networkAgent, oldScore); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 5007 | } |
| 5008 | notifyNetworkCallbacks(networkAgent, (state == NetworkInfo.State.SUSPENDED ? |
| 5009 | ConnectivityManager.CALLBACK_SUSPENDED : |
| 5010 | ConnectivityManager.CALLBACK_RESUMED)); |
| 5011 | mLegacyTypeTracker.update(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5012 | } |
| 5013 | } |
| 5014 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 5015 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5016 | if (VDBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 5017 | if (score < 0) { |
| 5018 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 5019 | "). Bumping score to min of 0"); |
| 5020 | score = 0; |
| 5021 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 5022 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5023 | final int oldScore = nai.getCurrentScore(); |
| 5024 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 5025 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 5026 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5027 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5028 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 5029 | } |
| 5030 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5031 | // notify only this one new request of the current state |
| 5032 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 5033 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 5034 | // TODO - read state from monitor to decide what to send. |
| 5035 | // if (nai.networkMonitor.isLingering()) { |
| 5036 | // notifyType = NetworkCallbacks.LOSING; |
| 5037 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 5038 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 5039 | // } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5040 | if (nri.mPendingIntent == null) { |
| 5041 | callCallbackForRequest(nri, nai, notifyType); |
| 5042 | } else { |
| 5043 | sendPendingIntentForRequest(nri, nai, notifyType); |
| 5044 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5045 | } |
| 5046 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 5047 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, DetailedState state, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 5048 | // The NetworkInfo we actually send out has no bearing on the real |
| 5049 | // state of affairs. For example, if the default connection is mobile, |
| 5050 | // and a request for HIPRI has just gone away, we need to pretend that |
| 5051 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 5052 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 5053 | // and is still connected. |
| 5054 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 5055 | info.setType(type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 5056 | if (state != DetailedState.DISCONNECTED) { |
| 5057 | info.setDetailedState(state, null, info.getExtraInfo()); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 5058 | sendConnectedBroadcast(info); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5059 | } else { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 5060 | info.setDetailedState(state, info.getReason(), info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5061 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 5062 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 5063 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 5064 | if (info.isFailover()) { |
| 5065 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 5066 | nai.networkInfo.setFailover(false); |
| 5067 | } |
| 5068 | if (info.getReason() != null) { |
| 5069 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 5070 | } |
| 5071 | if (info.getExtraInfo() != null) { |
| 5072 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 5073 | } |
| 5074 | NetworkAgentInfo newDefaultAgent = null; |
| 5075 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 5076 | newDefaultAgent = getDefaultNetwork(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5077 | if (newDefaultAgent != null) { |
| 5078 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 5079 | newDefaultAgent.networkInfo); |
| 5080 | } else { |
| 5081 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 5082 | } |
| 5083 | } |
| 5084 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 5085 | mDefaultInetConditionPublished); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 5086 | sendStickyBroadcast(intent); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5087 | if (newDefaultAgent != null) { |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 5088 | sendConnectedBroadcast(newDefaultAgent.networkInfo); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 5089 | } |
| 5090 | } |
| 5091 | } |
| 5092 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5093 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5094 | if (VDBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5095 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5096 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 5097 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 5098 | if (VDBG) log(" sending notification for " + nr); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5099 | if (nri.mPendingIntent == null) { |
| 5100 | callCallbackForRequest(nri, networkAgent, notifyType); |
| 5101 | } else { |
| 5102 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 5103 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5104 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5105 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 5106 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5107 | private String notifyTypeToName(int notifyType) { |
| 5108 | switch (notifyType) { |
| 5109 | case ConnectivityManager.CALLBACK_PRECHECK: return "PRECHECK"; |
| 5110 | case ConnectivityManager.CALLBACK_AVAILABLE: return "AVAILABLE"; |
| 5111 | case ConnectivityManager.CALLBACK_LOSING: return "LOSING"; |
| 5112 | case ConnectivityManager.CALLBACK_LOST: return "LOST"; |
| 5113 | case ConnectivityManager.CALLBACK_UNAVAIL: return "UNAVAILABLE"; |
| 5114 | case ConnectivityManager.CALLBACK_CAP_CHANGED: return "CAP_CHANGED"; |
| 5115 | case ConnectivityManager.CALLBACK_IP_CHANGED: return "IP_CHANGED"; |
| 5116 | case ConnectivityManager.CALLBACK_RELEASED: return "RELEASED"; |
| 5117 | } |
| 5118 | return "UNKNOWN"; |
| 5119 | } |
| 5120 | |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5121 | /** |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5122 | * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the |
| 5123 | * properties tracked by NetworkStatsService on an active iface has changed. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5124 | */ |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5125 | private void notifyIfacesChangedForNetworkStats() { |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5126 | try { |
| 5127 | mStatsService.forceUpdateIfaces(); |
| 5128 | } catch (Exception ignored) { |
| 5129 | } |
| 5130 | } |
| 5131 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 5132 | @Override |
| 5133 | public boolean addVpnAddress(String address, int prefixLength) { |
| 5134 | throwIfLockdownEnabled(); |
| 5135 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 5136 | synchronized (mVpns) { |
| 5137 | return mVpns.get(user).addAddress(address, prefixLength); |
| 5138 | } |
| 5139 | } |
| 5140 | |
| 5141 | @Override |
| 5142 | public boolean removeVpnAddress(String address, int prefixLength) { |
| 5143 | throwIfLockdownEnabled(); |
| 5144 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 5145 | synchronized (mVpns) { |
| 5146 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 5147 | } |
| 5148 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5149 | |
| 5150 | @Override |
| 5151 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
| 5152 | throwIfLockdownEnabled(); |
| 5153 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5154 | boolean success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5155 | synchronized (mVpns) { |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5156 | success = mVpns.get(user).setUnderlyingNetworks(networks); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5157 | } |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5158 | if (success) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5159 | notifyIfacesChangedForNetworkStats(); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5160 | } |
| 5161 | return success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5162 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5163 | |
| 5164 | @Override |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 5165 | public String getCaptivePortalServerUrl() { |
| 5166 | return NetworkMonitor.getCaptivePortalServerUrl(mContext); |
| 5167 | } |
| 5168 | |
| 5169 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 5170 | public void startNattKeepalive(Network network, int intervalSeconds, Messenger messenger, |
| 5171 | IBinder binder, String srcAddr, int srcPort, String dstAddr) { |
| 5172 | enforceKeepalivePermission(); |
| 5173 | mKeepaliveTracker.startNattKeepalive( |
| 5174 | getNetworkAgentInfoForNetwork(network), |
| 5175 | intervalSeconds, messenger, binder, |
| 5176 | srcAddr, srcPort, dstAddr, ConnectivityManager.PacketKeepalive.NATT_PORT); |
| 5177 | } |
| 5178 | |
| 5179 | @Override |
| 5180 | public void stopKeepalive(Network network, int slot) { |
| 5181 | mHandler.sendMessage(mHandler.obtainMessage( |
| 5182 | NetworkAgent.CMD_STOP_PACKET_KEEPALIVE, slot, PacketKeepalive.SUCCESS, network)); |
| 5183 | } |
| 5184 | |
| 5185 | @Override |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5186 | public void factoryReset() { |
| 5187 | enforceConnectivityInternalPermission(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5188 | |
| 5189 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 5190 | return; |
| 5191 | } |
| 5192 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 5193 | final int userId = UserHandle.getCallingUserId(); |
| 5194 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5195 | // Turn airplane mode off |
| 5196 | setAirplaneMode(false); |
| 5197 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5198 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 5199 | // Untether |
| 5200 | for (String tether : getTetheredIfaces()) { |
| 5201 | untether(tether); |
| 5202 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5203 | } |
| 5204 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5205 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { |
| 5206 | // Turn VPN off |
| 5207 | VpnConfig vpnConfig = getVpnConfig(userId); |
| 5208 | if (vpnConfig != null) { |
| 5209 | if (vpnConfig.legacy) { |
| 5210 | prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, userId); |
| 5211 | } else { |
| 5212 | // Prevent this app (packagename = vpnConfig.user) from initiating VPN connections |
| 5213 | // in the future without user intervention. |
| 5214 | setVpnPackageAuthorization(vpnConfig.user, userId, false); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5215 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5216 | prepareVpn(vpnConfig.user, VpnConfig.LEGACY_VPN, userId); |
| 5217 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5218 | } |
| 5219 | } |
| 5220 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5221 | |
| 5222 | @VisibleForTesting |
| 5223 | public NetworkMonitor createNetworkMonitor(Context context, Handler handler, |
| 5224 | NetworkAgentInfo nai, NetworkRequest defaultRequest) { |
| 5225 | return new NetworkMonitor(context, handler, nai, defaultRequest); |
| 5226 | } |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5227 | |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 5228 | private static void logDefaultNetworkEvent(NetworkAgentInfo newNai, NetworkAgentInfo prevNai) { |
| 5229 | int newNetid = NETID_UNSET; |
| 5230 | int prevNetid = NETID_UNSET; |
| 5231 | int[] transports = new int[0]; |
| 5232 | boolean hadIPv4 = false; |
| 5233 | boolean hadIPv6 = false; |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5234 | |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 5235 | if (newNai != null) { |
| 5236 | newNetid = newNai.network.netId; |
| 5237 | transports = newNai.networkCapabilities.getTransportTypes(); |
| 5238 | } |
| 5239 | if (prevNai != null) { |
| 5240 | prevNetid = prevNai.network.netId; |
| 5241 | final LinkProperties lp = prevNai.linkProperties; |
| 5242 | hadIPv4 = lp.hasIPv4Address() && lp.hasIPv4DefaultRoute(); |
| 5243 | hadIPv6 = lp.hasGlobalIPv6Address() && lp.hasIPv6DefaultRoute(); |
| 5244 | } |
| 5245 | |
| 5246 | DefaultNetworkEvent.logEvent(newNetid, transports, prevNetid, hadIPv4, hadIPv6); |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5247 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5248 | } |