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