The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.RECEIVE_DATA_ACTIVITY_CHANGE; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 20 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.NETID_UNSET; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_NONE; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_VPN; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 24 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 25 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 26 | import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 27 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 28 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 29 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
| 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 31 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Amith Yamasani | 15e47235 | 2015-04-24 19:06:07 -0700 | [diff] [blame] | 32 | import static android.net.NetworkPolicyManager.RULE_REJECT_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 33 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 34 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 35 | import android.annotation.Nullable; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 36 | import android.app.BroadcastOptions; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 37 | import android.app.Notification; |
| 38 | import android.app.NotificationManager; |
| 39 | import android.app.PendingIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 40 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.content.ContentResolver; |
| 42 | import android.content.Context; |
| 43 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 44 | import android.content.IntentFilter; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.content.pm.PackageManager; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 46 | import android.content.res.Configuration; |
tk.mun | 148c7d0 | 2011-10-13 22:51:57 +0900 | [diff] [blame] | 47 | import android.content.res.Resources; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 48 | import android.database.ContentObserver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.net.ConnectivityManager; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 50 | import android.net.ConnectivityManager.PacketKeepalive; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | import android.net.IConnectivityManager; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 52 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 53 | import android.net.INetworkPolicyListener; |
| 54 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 55 | import android.net.INetworkStatsService; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 56 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 57 | import android.net.LinkProperties.CompareResult; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 58 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 59 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 60 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 61 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 63 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 64 | import android.net.NetworkMisc; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 65 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 66 | import android.net.NetworkRequest; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 67 | import android.net.NetworkState; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 68 | import android.net.NetworkUtils; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 69 | import android.net.Proxy; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 70 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 71 | import android.net.RouteInfo; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 72 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 73 | import android.net.Uri; |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 74 | import android.net.metrics.DefaultNetworkEvent; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 75 | import android.net.metrics.NetworkEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import android.os.Binder; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 77 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 78 | import android.os.Bundle; |
Mike Lockwood | da8bb74 | 2011-05-28 13:24:04 -0400 | [diff] [blame] | 79 | import android.os.FileUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 81 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 82 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 83 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.os.Looper; |
| 85 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 86 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 87 | import android.os.ParcelFileDescriptor; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 88 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 89 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 90 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 91 | import android.os.ResultReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 93 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 94 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 96 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 97 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 98 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 99 | import android.text.TextUtils; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 100 | import android.util.LocalLog; |
| 101 | import android.util.LocalLog.ReadOnlyLocalLog; |
| 102 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 103 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 104 | import android.util.SparseArray; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 105 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 106 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 107 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 109 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 110 | import com.android.internal.annotations.GuardedBy; |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 111 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 112 | import com.android.internal.app.IBatteryStats; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 113 | import com.android.internal.net.LegacyVpnInfo; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 114 | import com.android.internal.net.NetworkStatsFactory; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 115 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 116 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 117 | import com.android.internal.net.VpnProfile; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 118 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 119 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 120 | import com.android.internal.util.MessageUtils; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 121 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 122 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 123 | import com.android.server.connectivity.DataConnectionStats; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 124 | import com.android.server.connectivity.KeepaliveTracker; |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 125 | import com.android.server.connectivity.NetworkDiagnostics; |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 126 | import com.android.server.connectivity.Nat464Xlat; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 127 | import com.android.server.connectivity.NetworkAgentInfo; |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 128 | import com.android.server.connectivity.NetworkMonitor; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 129 | import com.android.server.connectivity.PacManager; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 130 | import com.android.server.connectivity.PermissionMonitor; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 131 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 132 | import com.android.server.connectivity.Vpn; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 133 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 134 | import com.android.server.net.LockdownVpnTracker; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 135 | import com.google.android.collect.Lists; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 136 | import com.google.android.collect.Sets; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 137 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 138 | import org.xmlpull.v1.XmlPullParser; |
| 139 | import org.xmlpull.v1.XmlPullParserException; |
| 140 | |
| 141 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 143 | import java.io.FileNotFoundException; |
| 144 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 145 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | import java.io.PrintWriter; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 147 | import java.net.Inet4Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 148 | import java.net.InetAddress; |
| 149 | import java.net.UnknownHostException; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 150 | import java.util.ArrayDeque; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 151 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 152 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 153 | import java.util.Collection; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 154 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 155 | import java.util.HashSet; |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 156 | import java.util.SortedSet; |
| 157 | import java.util.TreeSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 158 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 159 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 160 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * @hide |
| 164 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 165 | public class ConnectivityService extends IConnectivityManager.Stub |
| 166 | implements PendingIntent.OnFinished { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 167 | private static final String TAG = "ConnectivityService"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 169 | private static final boolean DBG = true; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 170 | private static final boolean VDBG = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
Jake Hamby | 786e71a | 2014-02-06 14:43:50 -0800 | [diff] [blame] | 172 | private static final boolean LOGD_RULES = false; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 173 | private static final boolean LOGD_BLOCKED_NETWORKINFO = true; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 174 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 175 | // TODO: create better separation between radio types and network types |
| 176 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 177 | // how long to wait before switching back to a radio's default network |
| 178 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 179 | // system property that can override the above value |
| 180 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 181 | "android.telephony.apn-restore"; |
| 182 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 183 | // How long to wait before putting up a "This network doesn't have an Internet connection, |
| 184 | // connect anyway?" dialog after the user selects a network that doesn't validate. |
| 185 | private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000; |
| 186 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 187 | // How long to delay to removal of a pending intent based request. |
| 188 | // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS |
| 189 | private final int mReleasePendingIntentDelayMs; |
| 190 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 191 | private Tethering mTethering; |
| 192 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 193 | private final PermissionMonitor mPermissionMonitor; |
| 194 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 195 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 196 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 197 | @GuardedBy("mVpns") |
| 198 | private final SparseArray<Vpn> mVpns = new SparseArray<Vpn>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 199 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 200 | private boolean mLockdownEnabled; |
| 201 | private LockdownVpnTracker mLockdownTracker; |
| 202 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 203 | /** Lock around {@link #mUidRules} and {@link #mMeteredIfaces}. */ |
| 204 | private Object mRulesLock = new Object(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 205 | /** Currently active network rules by UID. */ |
| 206 | private SparseIntArray mUidRules = new SparseIntArray(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 207 | /** Set of ifaces that are costly. */ |
| 208 | private HashSet<String> mMeteredIfaces = Sets.newHashSet(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 209 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 210 | final private Context mContext; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | private int mNetworkPreference; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 212 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 213 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | |
Robert Greenwalt | 0dd19a8 | 2013-02-11 15:25:10 -0800 | [diff] [blame] | 215 | private int mNumDnsEntries; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | |
| 217 | private boolean mTestMode; |
Joe Onorato | 0009287 | 2010-09-01 21:18:22 -0700 | [diff] [blame] | 218 | private static ConnectivityService sServiceInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 220 | private INetworkManagementService mNetd; |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 221 | private INetworkStatsService mStatsService; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 222 | private INetworkPolicyManager mPolicyManager; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 223 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 224 | private String mCurrentTcpBufferSizes; |
| 225 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 226 | private static final int ENABLED = 1; |
| 227 | private static final int DISABLED = 0; |
| 228 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 229 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
| 230 | new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class }); |
| 231 | |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 232 | private enum ReapUnvalidatedNetworks { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 233 | // Tear down networks that have no chance (e.g. even if validated) of becoming |
| 234 | // the highest scoring network satisfying a NetworkRequest. This should be passed when |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 235 | // all networks have been rematched against all NetworkRequests. |
| 236 | REAP, |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 237 | // Don't reap networks. This should be passed when some networks have not yet been |
| 238 | // rematched against all NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 239 | DONT_REAP |
| 240 | }; |
| 241 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 242 | /** |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 243 | * used internally to change our mobile data enabled flag |
| 244 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 245 | private static final int EVENT_CHANGE_MOBILE_DATA_ENABLED = 2; |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 246 | |
| 247 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 248 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 249 | * from one net to another. Clear happens when we get a new |
| 250 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 251 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 252 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 253 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 254 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 255 | /** |
| 256 | * used internally to reload global proxy settings |
| 257 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 258 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 259 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 260 | /** |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 261 | * PAC manager has received new port. |
| 262 | */ |
| 263 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 264 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 265 | /** |
| 266 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 267 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 268 | */ |
| 269 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 270 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 271 | /** |
| 272 | * used internally when registering NetworkAgents |
| 273 | * obj = Messenger |
| 274 | */ |
| 275 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 276 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 277 | /** |
| 278 | * used to add a network request |
| 279 | * includes a NetworkRequestInfo |
| 280 | */ |
| 281 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 282 | |
| 283 | /** |
| 284 | * indicates a timeout period is over - check if we had a network yet or not |
| 285 | * and if not, call the timeout calback (but leave the request live until they |
| 286 | * cancel it. |
| 287 | * includes a NetworkRequestInfo |
| 288 | */ |
| 289 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 290 | |
| 291 | /** |
| 292 | * used to add a network listener - no request |
| 293 | * includes a NetworkRequestInfo |
| 294 | */ |
| 295 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 296 | |
| 297 | /** |
| 298 | * 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] | 299 | * arg1 = UID of caller |
| 300 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 301 | */ |
| 302 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 303 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 304 | /** |
| 305 | * used internally when registering NetworkFactories |
| 306 | * obj = Messenger |
| 307 | */ |
| 308 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 309 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 310 | /** |
| 311 | * used internally to expire a wakelock when transitioning |
| 312 | * from one net to another. Expire happens when we fail to find |
| 313 | * a new network (typically after 1 minute) - |
| 314 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 315 | * a replacement network. |
| 316 | */ |
| 317 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 318 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 319 | /** |
| 320 | * Used internally to indicate the system is ready. |
| 321 | */ |
| 322 | private static final int EVENT_SYSTEM_READY = 25; |
| 323 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 324 | /** |
| 325 | * used to add a network request with a pending intent |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 326 | * obj = NetworkRequestInfo |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 327 | */ |
| 328 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 329 | |
| 330 | /** |
| 331 | * used to remove a pending intent and its associated network request. |
| 332 | * arg1 = UID of caller |
| 333 | * obj = PendingIntent |
| 334 | */ |
| 335 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 336 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 337 | /** |
| 338 | * used to specify whether a network should be used even if unvalidated. |
| 339 | * arg1 = whether to accept the network if it's unvalidated (1 or 0) |
| 340 | * arg2 = whether to remember this choice in the future (1 or 0) |
| 341 | * obj = network |
| 342 | */ |
| 343 | private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28; |
| 344 | |
| 345 | /** |
| 346 | * used to ask the user to confirm a connection to an unvalidated network. |
| 347 | * obj = network |
| 348 | */ |
| 349 | private static final int EVENT_PROMPT_UNVALIDATED = 29; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 350 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 351 | /** |
| 352 | * used internally to (re)configure mobile data always-on settings. |
| 353 | */ |
| 354 | private static final int EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON = 30; |
| 355 | |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 356 | /** |
| 357 | * used to add a network listener with a pending intent |
| 358 | * obj = NetworkRequestInfo |
| 359 | */ |
| 360 | private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31; |
| 361 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 362 | /** Handler thread used for both of the handlers below. */ |
| 363 | @VisibleForTesting |
| 364 | protected final HandlerThread mHandlerThread; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 365 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 366 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 367 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 368 | final private NetworkStateTrackerHandler mTrackerHandler; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 369 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 370 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 371 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 372 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 373 | private PowerManager.WakeLock mNetTransitionWakeLock; |
| 374 | private String mNetTransitionWakeLockCausedBy = ""; |
| 375 | private int mNetTransitionWakeLockSerialNumber; |
| 376 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 377 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 378 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 379 | // used in DBG mode to track inet condition reports |
| 380 | private static final int INET_CONDITION_LOG_MAX_SIZE = 15; |
| 381 | private ArrayList mInetLog; |
| 382 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 383 | // 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] | 384 | private volatile ProxyInfo mDefaultProxy = null; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 385 | private Object mProxyLock = new Object(); |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 386 | private boolean mDefaultProxyDisabled = false; |
| 387 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 388 | // track the global proxy. |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 389 | private ProxyInfo mGlobalProxy = null; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 390 | |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 391 | private PacManager mPacManager = null; |
| 392 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 393 | final private SettingsObserver mSettingsObserver; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 394 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 395 | private UserManager mUserManager; |
| 396 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 397 | NetworkConfig[] mNetConfigs; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 398 | int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 399 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 400 | // the set of network types that can only be enabled by system/sig apps |
| 401 | List mProtectedNetworks; |
| 402 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 403 | private DataConnectionStats mDataConnectionStats; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 404 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 405 | TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 406 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 407 | private KeepaliveTracker mKeepaliveTracker; |
| 408 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 409 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
| 410 | private final static int MIN_NET_ID = 100; // some reserved marks |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 411 | private final static int MAX_NET_ID = 65535; |
| 412 | private int mNextNetId = MIN_NET_ID; |
| 413 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 414 | // sequence number of NetworkRequests |
| 415 | private int mNextNetworkRequestId = 1; |
| 416 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 417 | // NetworkRequest activity String log entries. |
| 418 | private static final int MAX_NETWORK_REQUEST_LOGS = 20; |
| 419 | private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS); |
| 420 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 421 | // Array of <Network,ReadOnlyLocalLogs> tracking network validation and results |
| 422 | private static final int MAX_VALIDATION_LOGS = 10; |
| 423 | private final ArrayDeque<Pair<Network,ReadOnlyLocalLog>> mValidationLogs = |
| 424 | new ArrayDeque<Pair<Network,ReadOnlyLocalLog>>(MAX_VALIDATION_LOGS); |
| 425 | |
| 426 | private void addValidationLogs(ReadOnlyLocalLog log, Network network) { |
| 427 | synchronized(mValidationLogs) { |
| 428 | while (mValidationLogs.size() >= MAX_VALIDATION_LOGS) { |
| 429 | mValidationLogs.removeLast(); |
| 430 | } |
| 431 | mValidationLogs.addFirst(new Pair(network, log)); |
| 432 | } |
| 433 | } |
| 434 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 435 | /** |
| 436 | * Implements support for the legacy "one network per network type" model. |
| 437 | * |
| 438 | * We used to have a static array of NetworkStateTrackers, one for each |
| 439 | * network type, but that doesn't work any more now that we can have, |
| 440 | * for example, more that one wifi network. This class stores all the |
| 441 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 442 | * API will only see the first one. |
| 443 | * |
| 444 | * It serves two main purposes: |
| 445 | * |
| 446 | * 1. Provide information about "the network for a given type" (since this |
| 447 | * API only supports one). |
| 448 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 449 | * the first network for a given type changes, or if the default network |
| 450 | * changes. |
| 451 | */ |
| 452 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 453 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 454 | private static final boolean DBG = true; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 455 | private static final boolean VDBG = false; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 456 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 457 | /** |
| 458 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 459 | * Each list holds references to all NetworkAgentInfos that are used to |
| 460 | * satisfy requests for that network type. |
| 461 | * |
| 462 | * This array is built out at startup such that an unsupported network |
| 463 | * doesn't get an ArrayList instance, making this a tristate: |
| 464 | * unsupported, supported but not active and active. |
| 465 | * |
| 466 | * The actual lists are populated when we scan the network types that |
| 467 | * are supported on this device. |
| 468 | */ |
| 469 | private ArrayList<NetworkAgentInfo> mTypeLists[]; |
| 470 | |
| 471 | public LegacyTypeTracker() { |
| 472 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 473 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 474 | } |
| 475 | |
| 476 | public void addSupportedType(int type) { |
| 477 | if (mTypeLists[type] != null) { |
| 478 | throw new IllegalStateException( |
| 479 | "legacy list for type " + type + "already initialized"); |
| 480 | } |
| 481 | mTypeLists[type] = new ArrayList<NetworkAgentInfo>(); |
| 482 | } |
| 483 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 484 | public boolean isTypeSupported(int type) { |
| 485 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 486 | } |
| 487 | |
| 488 | public NetworkAgentInfo getNetworkForType(int type) { |
| 489 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 490 | return mTypeLists[type].get(0); |
| 491 | } else { |
| 492 | return null; |
| 493 | } |
| 494 | } |
| 495 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 496 | private void maybeLogBroadcast(NetworkAgentInfo nai, DetailedState state, int type, |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 497 | boolean isDefaultNetwork) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 498 | if (DBG) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 499 | log("Sending " + state + |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 500 | " broadcast for type " + type + " " + nai.name() + |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 501 | " isDefaultNetwork=" + isDefaultNetwork); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 502 | } |
| 503 | } |
| 504 | |
| 505 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 506 | public void add(int type, NetworkAgentInfo nai) { |
| 507 | if (!isTypeSupported(type)) { |
| 508 | return; // Invalid network type. |
| 509 | } |
| 510 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 511 | |
| 512 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 513 | if (list.contains(nai)) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 514 | return; |
| 515 | } |
| 516 | |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 517 | list.add(nai); |
| 518 | |
| 519 | // 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] | 520 | final boolean isDefaultNetwork = isDefaultNetwork(nai); |
| 521 | if (list.size() == 1 || isDefaultNetwork) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 522 | maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork); |
| 523 | sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 524 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 527 | /** Removes the given network from the specified legacy type list. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 528 | public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 529 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 530 | if (list == null || list.isEmpty()) { |
| 531 | return; |
| 532 | } |
| 533 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 534 | final boolean wasFirstNetwork = list.get(0).equals(nai); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 535 | |
| 536 | if (!list.remove(nai)) { |
| 537 | return; |
| 538 | } |
| 539 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 540 | final DetailedState state = DetailedState.DISCONNECTED; |
| 541 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 542 | if (wasFirstNetwork || wasDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 543 | maybeLogBroadcast(nai, state, type, wasDefault); |
| 544 | sendLegacyNetworkBroadcast(nai, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | if (!list.isEmpty() && wasFirstNetwork) { |
| 548 | if (DBG) log("Other network available for type " + type + |
| 549 | ", sending connected broadcast"); |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 550 | final NetworkAgentInfo replacement = list.get(0); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 551 | maybeLogBroadcast(replacement, state, type, isDefaultNetwork(replacement)); |
| 552 | sendLegacyNetworkBroadcast(replacement, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 553 | } |
| 554 | } |
| 555 | |
| 556 | /** Removes the given network from all legacy type lists. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 557 | public void remove(NetworkAgentInfo nai, boolean wasDefault) { |
| 558 | if (VDBG) log("Removing agent " + nai + " wasDefault=" + wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 559 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 560 | remove(type, nai, wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 561 | } |
| 562 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 563 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 564 | // send out another legacy broadcast - currently only used for suspend/unsuspend |
| 565 | // toggle |
| 566 | public void update(NetworkAgentInfo nai) { |
| 567 | final boolean isDefault = isDefaultNetwork(nai); |
| 568 | final DetailedState state = nai.networkInfo.getDetailedState(); |
| 569 | for (int type = 0; type < mTypeLists.length; type++) { |
| 570 | final ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 571 | final boolean contains = (list != null && list.contains(nai)); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 572 | final boolean isFirst = (list != null && list.size() > 0 && nai == list.get(0)); |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 573 | if (isFirst || (contains && isDefault)) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 574 | maybeLogBroadcast(nai, state, type, isDefault); |
| 575 | sendLegacyNetworkBroadcast(nai, state, type); |
| 576 | } |
| 577 | } |
| 578 | } |
| 579 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 580 | private String naiToString(NetworkAgentInfo nai) { |
| 581 | String name = (nai != null) ? nai.name() : "null"; |
| 582 | String state = (nai.networkInfo != null) ? |
| 583 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 584 | "???/???"; |
| 585 | return name + " " + state; |
| 586 | } |
| 587 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 588 | public void dump(IndentingPrintWriter pw) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 589 | pw.println("mLegacyTypeTracker:"); |
| 590 | pw.increaseIndent(); |
| 591 | pw.print("Supported types:"); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 592 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 593 | if (mTypeLists[type] != null) pw.print(" " + type); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 594 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 595 | pw.println(); |
| 596 | pw.println("Current state:"); |
| 597 | pw.increaseIndent(); |
| 598 | for (int type = 0; type < mTypeLists.length; type++) { |
| 599 | if (mTypeLists[type] == null|| mTypeLists[type].size() == 0) continue; |
| 600 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
| 601 | pw.println(type + " " + naiToString(nai)); |
| 602 | } |
| 603 | } |
| 604 | pw.decreaseIndent(); |
| 605 | pw.decreaseIndent(); |
| 606 | pw.println(); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 607 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 608 | } |
| 609 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 610 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 611 | @VisibleForTesting |
| 612 | protected HandlerThread createHandlerThread() { |
| 613 | return new HandlerThread("ConnectivityServiceThread"); |
| 614 | } |
| 615 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 616 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 617 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 618 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 619 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 620 | mDefaultRequest = createInternetRequestForTransport(-1); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 621 | NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 622 | new Binder(), NetworkRequestType.REQUEST); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 623 | mNetworkRequests.put(mDefaultRequest, defaultNRI); |
| 624 | mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 625 | |
| 626 | mDefaultMobileDataRequest = createInternetRequestForTransport( |
| 627 | NetworkCapabilities.TRANSPORT_CELLULAR); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 628 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 629 | mHandlerThread = createHandlerThread(); |
| 630 | mHandlerThread.start(); |
| 631 | mHandler = new InternalHandler(mHandlerThread.getLooper()); |
| 632 | mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 633 | |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 634 | // setup our unique device name |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 635 | if (TextUtils.isEmpty(SystemProperties.get("net.hostname"))) { |
| 636 | String id = Settings.Secure.getString(context.getContentResolver(), |
| 637 | Settings.Secure.ANDROID_ID); |
| 638 | if (id != null && id.length() > 0) { |
Irfan Sheriff | a10a3ad | 2011-09-20 15:17:07 -0700 | [diff] [blame] | 639 | String name = new String("android-").concat(id); |
Robert Greenwalt | 733c629 | 2010-12-06 09:30:17 -0800 | [diff] [blame] | 640 | SystemProperties.set("net.hostname", name); |
| 641 | } |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 642 | } |
| 643 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 644 | mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(), |
| 645 | Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000); |
| 646 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 647 | mContext = checkNotNull(context, "missing Context"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 648 | mNetd = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 649 | mStatsService = checkNotNull(statsService, "missing INetworkStatsService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 650 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 651 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 652 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 653 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 654 | try { |
| 655 | mPolicyManager.registerListener(mPolicyListener); |
| 656 | } catch (RemoteException e) { |
| 657 | // ouch, no rules updates means some processes may never get network |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 658 | loge("unable to register INetworkPolicyListener" + e.toString()); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 662 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 663 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 664 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 665 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 666 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 667 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 668 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 669 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 670 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
| 671 | boolean wifiOnly = SystemProperties.getBoolean("ro.radio.noril", false); |
| 672 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 673 | String[] naStrings = context.getResources().getStringArray( |
| 674 | com.android.internal.R.array.networkAttributes); |
| 675 | for (String naString : naStrings) { |
| 676 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 677 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 678 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 679 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 680 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 681 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 682 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 683 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 684 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 685 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 686 | n.type); |
| 687 | continue; |
| 688 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 689 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 690 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 691 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 692 | continue; |
| 693 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 694 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 695 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 696 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 697 | mNetworksDefined++; |
| 698 | } catch(Exception e) { |
| 699 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 702 | |
| 703 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 704 | if (mNetConfigs[TYPE_VPN] == null) { |
| 705 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 706 | // don't need to add TYPE_VPN to mNetConfigs. |
| 707 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 708 | mNetworksDefined++; // used only in the log() statement below. |
| 709 | } |
| 710 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 711 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 712 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 713 | mProtectedNetworks = new ArrayList<Integer>(); |
| 714 | int[] protectedNetworks = context.getResources().getIntArray( |
| 715 | com.android.internal.R.array.config_protectedNetworks); |
| 716 | for (int p : protectedNetworks) { |
| 717 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 718 | mProtectedNetworks.add(p); |
| 719 | } else { |
| 720 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 721 | } |
| 722 | } |
| 723 | |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 724 | mTestMode = SystemProperties.get("cm.test.mode").equals("true") |
| 725 | && SystemProperties.get("ro.build.type").equals("eng"); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 726 | |
Jeremy Klein | fa8712b | 2016-01-26 11:10:55 -0800 | [diff] [blame] | 727 | mTethering = new Tethering(mContext, mNetd, statsService); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 728 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 729 | mPermissionMonitor = new PermissionMonitor(mContext, mNetd); |
| 730 | |
Robert Greenwalt | bfc7634 | 2013-07-19 14:30:49 -0700 | [diff] [blame] | 731 | //set up the listener for user state for creating user VPNs |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 732 | IntentFilter intentFilter = new IntentFilter(); |
| 733 | intentFilter.addAction(Intent.ACTION_USER_STARTING); |
| 734 | intentFilter.addAction(Intent.ACTION_USER_STOPPING); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 735 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 736 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 737 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 738 | mContext.registerReceiverAsUser( |
| 739 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 740 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 741 | try { |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 742 | mNetd.registerObserver(mTethering); |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 743 | mNetd.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 744 | } catch (RemoteException e) { |
| 745 | loge("Error registering observer :" + e); |
| 746 | } |
| 747 | |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 748 | if (DBG) { |
| 749 | mInetLog = new ArrayList(); |
| 750 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 751 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 752 | mSettingsObserver = new SettingsObserver(mContext, mHandler); |
| 753 | registerSettingsCallbacks(); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 754 | |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 755 | mDataConnectionStats = new DataConnectionStats(mContext); |
| 756 | mDataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 757 | |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 758 | mPacManager = new PacManager(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 759 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 760 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 761 | |
| 762 | mKeepaliveTracker = new KeepaliveTracker(mHandler); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 764 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 765 | private NetworkRequest createInternetRequestForTransport(int transportType) { |
| 766 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 767 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 768 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 769 | if (transportType > -1) { |
| 770 | netCap.addTransportType(transportType); |
| 771 | } |
| 772 | return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId()); |
| 773 | } |
| 774 | |
| 775 | private void handleMobileDataAlwaysOn() { |
| 776 | final boolean enable = (Settings.Global.getInt( |
| 777 | mContext.getContentResolver(), Settings.Global.MOBILE_DATA_ALWAYS_ON, 0) == 1); |
| 778 | final boolean isEnabled = (mNetworkRequests.get(mDefaultMobileDataRequest) != null); |
| 779 | if (enable == isEnabled) { |
| 780 | return; // Nothing to do. |
| 781 | } |
| 782 | |
| 783 | if (enable) { |
| 784 | handleRegisterNetworkRequest(new NetworkRequestInfo( |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 785 | null, mDefaultMobileDataRequest, new Binder(), NetworkRequestType.REQUEST)); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 786 | } else { |
| 787 | handleReleaseNetworkRequest(mDefaultMobileDataRequest, Process.SYSTEM_UID); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | private void registerSettingsCallbacks() { |
| 792 | // Watch for global HTTP proxy changes. |
| 793 | mSettingsObserver.observe( |
| 794 | Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), |
| 795 | EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 796 | |
| 797 | // Watch for whether or not to keep mobile data always on. |
| 798 | mSettingsObserver.observe( |
| 799 | Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), |
| 800 | EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON); |
| 801 | } |
| 802 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 803 | private synchronized int nextNetworkRequestId() { |
| 804 | return mNextNetworkRequestId++; |
| 805 | } |
| 806 | |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 807 | @VisibleForTesting |
| 808 | protected int reserveNetId() { |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 809 | synchronized (mNetworkForNetId) { |
| 810 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 811 | int netId = mNextNetId; |
| 812 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 813 | // Make sure NetID unused. http://b/16815182 |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 814 | if (!mNetIdInUse.get(netId)) { |
| 815 | mNetIdInUse.put(netId, true); |
| 816 | return netId; |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 817 | } |
| 818 | } |
| 819 | } |
| 820 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 821 | } |
| 822 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 823 | private NetworkState getFilteredNetworkState(int networkType, int uid) { |
| 824 | NetworkInfo info = null; |
| 825 | LinkProperties lp = null; |
| 826 | NetworkCapabilities nc = null; |
| 827 | Network network = null; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 828 | String subscriberId = null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 829 | |
| 830 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
| 831 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 832 | if (nai != null) { |
| 833 | synchronized (nai) { |
| 834 | info = new NetworkInfo(nai.networkInfo); |
| 835 | lp = new LinkProperties(nai.linkProperties); |
| 836 | nc = new NetworkCapabilities(nai.networkCapabilities); |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 837 | // Network objects are outwardly immutable so there is no point to duplicating. |
| 838 | // Duplicating also precludes sharing socket factories and connection pools. |
| 839 | network = nai.network; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 840 | subscriberId = (nai.networkMisc != null) ? nai.networkMisc.subscriberId : null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 841 | } |
| 842 | info.setType(networkType); |
| 843 | } else { |
| 844 | info = new NetworkInfo(networkType, 0, getNetworkTypeName(networkType), ""); |
| 845 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 846 | info.setIsAvailable(true); |
| 847 | lp = new LinkProperties(); |
| 848 | nc = new NetworkCapabilities(); |
| 849 | network = null; |
| 850 | } |
| 851 | info = getFilteredNetworkInfo(info, lp, uid); |
| 852 | } |
| 853 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 854 | return new NetworkState(info, lp, nc, network, subscriberId, null); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 855 | } |
| 856 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 857 | private NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
| 858 | if (network == null) { |
| 859 | return null; |
| 860 | } |
| 861 | synchronized (mNetworkForNetId) { |
| 862 | return mNetworkForNetId.get(network.netId); |
| 863 | } |
| 864 | }; |
| 865 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 866 | private Network[] getVpnUnderlyingNetworks(int uid) { |
| 867 | if (!mLockdownEnabled) { |
| 868 | int user = UserHandle.getUserId(uid); |
| 869 | synchronized (mVpns) { |
| 870 | Vpn vpn = mVpns.get(user); |
| 871 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 872 | return vpn.getUnderlyingNetworks(); |
| 873 | } |
| 874 | } |
| 875 | } |
| 876 | return null; |
| 877 | } |
| 878 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 879 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
| 880 | NetworkInfo info = null; |
| 881 | LinkProperties lp = null; |
| 882 | NetworkCapabilities nc = null; |
| 883 | Network network = null; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 884 | String subscriberId = null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 885 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 886 | NetworkAgentInfo nai = getDefaultNetwork(); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 887 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 888 | final Network[] networks = getVpnUnderlyingNetworks(uid); |
| 889 | if (networks != null) { |
| 890 | // getUnderlyingNetworks() returns: |
| 891 | // null => there was no VPN, or the VPN didn't specify anything, so we use the default. |
| 892 | // empty array => the VPN explicitly said "no default network". |
| 893 | // non-empty array => the VPN specified one or more default networks; we use the |
| 894 | // first one. |
| 895 | if (networks.length > 0) { |
| 896 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 897 | } else { |
| 898 | nai = null; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 899 | } |
| 900 | } |
| 901 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 902 | if (nai != null) { |
| 903 | synchronized (nai) { |
| 904 | info = new NetworkInfo(nai.networkInfo); |
| 905 | lp = new LinkProperties(nai.linkProperties); |
| 906 | nc = new NetworkCapabilities(nai.networkCapabilities); |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 907 | // Network objects are outwardly immutable so there is no point to duplicating. |
| 908 | // Duplicating also precludes sharing socket factories and connection pools. |
| 909 | network = nai.network; |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 910 | subscriberId = (nai.networkMisc != null) ? nai.networkMisc.subscriberId : null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 914 | return new NetworkState(info, lp, nc, network, subscriberId, null); |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 915 | } |
| 916 | |
| 917 | /** |
| 918 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 919 | */ |
| 920 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 921 | final boolean networkCostly; |
| 922 | final int uidRules; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 923 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 924 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 925 | synchronized (mRulesLock) { |
| 926 | networkCostly = mMeteredIfaces.contains(iface); |
| 927 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 928 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 929 | |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 930 | if (uidRules == RULE_REJECT_ALL) { |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 931 | return true; |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 932 | } else if ((uidRules == RULE_REJECT_METERED) && networkCostly) { |
| 933 | return true; |
| 934 | } else { |
| 935 | return false; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 936 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 937 | } |
| 938 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 939 | private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) { |
| 940 | if (ni == null || !LOGD_BLOCKED_NETWORKINFO) return; |
| 941 | boolean removed = false; |
| 942 | boolean added = false; |
| 943 | synchronized (mBlockedAppUids) { |
| 944 | if (ni.getDetailedState() == DetailedState.BLOCKED && mBlockedAppUids.add(uid)) { |
| 945 | added = true; |
| 946 | } else if (ni.isConnected() && mBlockedAppUids.remove(uid)) { |
| 947 | removed = true; |
| 948 | } |
| 949 | } |
| 950 | if (added) log("Returning blocked NetworkInfo to uid=" + uid); |
| 951 | else if (removed) log("Returning unblocked NetworkInfo to uid=" + uid); |
| 952 | } |
| 953 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 954 | /** |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 955 | * Return a filtered {@link NetworkInfo}, potentially marked |
| 956 | * {@link DetailedState#BLOCKED} based on |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 957 | * {@link #isNetworkWithLinkPropertiesBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 958 | */ |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 959 | private NetworkInfo getFilteredNetworkInfo(NetworkInfo info, LinkProperties lp, int uid) { |
| 960 | if (info != null && isNetworkWithLinkPropertiesBlocked(lp, uid)) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 961 | // network is blocked; clone and override state |
| 962 | info = new NetworkInfo(info); |
| 963 | info.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 964 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 965 | if (info != null && mLockdownTracker != null) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 966 | info = mLockdownTracker.augmentNetworkInfo(info); |
Lorenzo Colitti | 48a423f | 2015-06-05 10:57:29 +0900 | [diff] [blame] | 967 | if (VDBG) log("returning Locked NetworkInfo"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 968 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 969 | return info; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 970 | } |
| 971 | |
| 972 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 973 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 974 | * It is assumed that at most one network is active at a time. If more |
| 975 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 976 | * @return the info for the active network, or {@code null} if none is |
| 977 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 979 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 980 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 981 | enforceAccessPermission(); |
| 982 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 983 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 984 | NetworkInfo ni = getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); |
| 985 | maybeLogBlockedNetworkInfo(ni, uid); |
| 986 | return ni; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 987 | } |
| 988 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 989 | @Override |
| 990 | public Network getActiveNetwork() { |
| 991 | enforceAccessPermission(); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 992 | return getActiveNetworkForUidInternal(Binder.getCallingUid()); |
| 993 | } |
| 994 | |
| 995 | @Override |
| 996 | public Network getActiveNetworkForUid(int uid) { |
| 997 | enforceConnectivityInternalPermission(); |
| 998 | return getActiveNetworkForUidInternal(uid); |
| 999 | } |
| 1000 | |
| 1001 | private Network getActiveNetworkForUidInternal(final int uid) { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1002 | final int user = UserHandle.getUserId(uid); |
| 1003 | int vpnNetId = NETID_UNSET; |
| 1004 | synchronized (mVpns) { |
| 1005 | final Vpn vpn = mVpns.get(user); |
| 1006 | if (vpn != null && vpn.appliesToUid(uid)) vpnNetId = vpn.getNetId(); |
| 1007 | } |
| 1008 | NetworkAgentInfo nai; |
| 1009 | if (vpnNetId != NETID_UNSET) { |
| 1010 | synchronized (mNetworkForNetId) { |
| 1011 | nai = mNetworkForNetId.get(vpnNetId); |
| 1012 | } |
| 1013 | if (nai != null) return nai.network; |
| 1014 | } |
| 1015 | nai = getDefaultNetwork(); |
| 1016 | if (nai != null && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid)) nai = null; |
| 1017 | return nai != null ? nai.network : null; |
| 1018 | } |
| 1019 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1020 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1021 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1022 | final int uid = Binder.getCallingUid(); |
| 1023 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1024 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1025 | } |
| 1026 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1027 | @Override |
| 1028 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1029 | enforceConnectivityInternalPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1030 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1031 | return getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1036 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1037 | final int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1038 | if (getVpnUnderlyingNetworks(uid) != null) { |
| 1039 | // A VPN is active, so we may need to return one of its underlying networks. This |
| 1040 | // information is not available in LegacyTypeTracker, so we have to get it from |
| 1041 | // getUnfilteredActiveNetworkState. |
| 1042 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1043 | if (state.networkInfo != null && state.networkInfo.getType() == networkType) { |
| 1044 | return getFilteredNetworkInfo(state.networkInfo, state.linkProperties, uid); |
| 1045 | } |
| 1046 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1047 | NetworkState state = getFilteredNetworkState(networkType, uid); |
| 1048 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1051 | @Override |
| 1052 | public NetworkInfo getNetworkInfoForNetwork(Network network) { |
| 1053 | enforceAccessPermission(); |
| 1054 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1055 | NetworkInfo info = null; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1056 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1057 | if (nai != null) { |
| 1058 | synchronized (nai) { |
| 1059 | info = new NetworkInfo(nai.networkInfo); |
| 1060 | info = getFilteredNetworkInfo(info, nai.linkProperties, uid); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1061 | } |
| 1062 | } |
| 1063 | return info; |
| 1064 | } |
| 1065 | |
| 1066 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | public NetworkInfo[] getAllNetworkInfo() { |
| 1068 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1069 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1070 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1071 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1072 | NetworkInfo info = getNetworkInfo(networkType); |
| 1073 | if (info != null) { |
| 1074 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1075 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1076 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1077 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | } |
| 1079 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1080 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1081 | public Network getNetworkForType(int networkType) { |
| 1082 | enforceAccessPermission(); |
| 1083 | final int uid = Binder.getCallingUid(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1084 | NetworkState state = getFilteredNetworkState(networkType, uid); |
| 1085 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid)) { |
| 1086 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1087 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1088 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
| 1091 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1092 | public Network[] getAllNetworks() { |
| 1093 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1094 | synchronized (mNetworkForNetId) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1095 | final Network[] result = new Network[mNetworkForNetId.size()]; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1096 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1097 | result[i] = mNetworkForNetId.valueAt(i).network; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1098 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1099 | return result; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1100 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1103 | @Override |
| 1104 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1105 | // The basic principle is: if an app's traffic could possibly go over a |
| 1106 | // network, without the app doing anything multinetwork-specific, |
| 1107 | // (hence, by "default"), then include that network's capabilities in |
| 1108 | // the array. |
| 1109 | // |
| 1110 | // In the normal case, app traffic only goes over the system's default |
| 1111 | // network connection, so that's the only network returned. |
| 1112 | // |
| 1113 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1114 | // over whatever underlying networks the VPN specifies, while other app |
| 1115 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1116 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1117 | // returned includes the VPN's underlying networks and the system |
| 1118 | // default. |
| 1119 | enforceAccessPermission(); |
| 1120 | |
| 1121 | HashMap<Network, NetworkCapabilities> result = new HashMap<Network, NetworkCapabilities>(); |
| 1122 | |
| 1123 | NetworkAgentInfo nai = getDefaultNetwork(); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1124 | NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1125 | if (nc != null) { |
| 1126 | result.put(nai.network, nc); |
| 1127 | } |
| 1128 | |
| 1129 | if (!mLockdownEnabled) { |
| 1130 | synchronized (mVpns) { |
| 1131 | Vpn vpn = mVpns.get(userId); |
| 1132 | if (vpn != null) { |
| 1133 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1134 | if (networks != null) { |
| 1135 | for (Network network : networks) { |
| 1136 | nai = getNetworkAgentInfoForNetwork(network); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1137 | nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1138 | if (nc != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1139 | result.put(network, nc); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1148 | out = result.values().toArray(out); |
| 1149 | return out; |
| 1150 | } |
| 1151 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1152 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1153 | public boolean isNetworkSupported(int networkType) { |
| 1154 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1155 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1158 | /** |
| 1159 | * Return LinkProperties for the active (i.e., connected) default |
| 1160 | * network interface. It is assumed that at most one default network |
| 1161 | * is active at a time. If more than one is active, it is indeterminate |
| 1162 | * which will be returned. |
| 1163 | * @return the ip properties for the active network, or {@code null} if |
| 1164 | * none is active |
| 1165 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1166 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1167 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1168 | enforceAccessPermission(); |
| 1169 | final int uid = Binder.getCallingUid(); |
| 1170 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1171 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1172 | } |
| 1173 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1174 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1175 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1176 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1177 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1178 | if (nai != null) { |
| 1179 | synchronized (nai) { |
| 1180 | return new LinkProperties(nai.linkProperties); |
| 1181 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1182 | } |
| 1183 | return null; |
| 1184 | } |
| 1185 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1186 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1187 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1188 | public LinkProperties getLinkProperties(Network network) { |
| 1189 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1190 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1191 | if (nai != null) { |
| 1192 | synchronized (nai) { |
| 1193 | return new LinkProperties(nai.linkProperties); |
| 1194 | } |
| 1195 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1196 | return null; |
| 1197 | } |
| 1198 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1199 | private NetworkCapabilities getNetworkCapabilitiesInternal(NetworkAgentInfo nai) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1200 | if (nai != null) { |
| 1201 | synchronized (nai) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1202 | if (nai.networkCapabilities != null) { |
| 1203 | return new NetworkCapabilities(nai.networkCapabilities); |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1204 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1205 | } |
| 1206 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1207 | return null; |
| 1208 | } |
| 1209 | |
| 1210 | @Override |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1211 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1212 | enforceAccessPermission(); |
| 1213 | return getNetworkCapabilitiesInternal(getNetworkAgentInfoForNetwork(network)); |
| 1214 | } |
| 1215 | |
| 1216 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1217 | public NetworkState[] getAllNetworkState() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1218 | // Require internal since we're handing out IMSI details |
| 1219 | enforceConnectivityInternalPermission(); |
| 1220 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1221 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1222 | for (Network network : getAllNetworks()) { |
| 1223 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1224 | if (nai != null) { |
| 1225 | synchronized (nai) { |
| 1226 | final String subscriberId = (nai.networkMisc != null) |
| 1227 | ? nai.networkMisc.subscriberId : null; |
| 1228 | result.add(new NetworkState(nai.networkInfo, nai.linkProperties, |
| 1229 | nai.networkCapabilities, network, subscriberId, null)); |
| 1230 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | return result.toArray(new NetworkState[result.size()]); |
| 1234 | } |
| 1235 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1236 | @Override |
| 1237 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 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 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1243 | if (state.networkInfo != null) { |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1244 | try { |
| 1245 | return mPolicyManager.getNetworkQuotaInfo(state); |
| 1246 | } catch (RemoteException e) { |
| 1247 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1248 | } |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1249 | return null; |
| 1250 | } finally { |
| 1251 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1252 | } |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1253 | } |
| 1254 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1255 | @Override |
| 1256 | public boolean isActiveNetworkMetered() { |
| 1257 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1258 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1259 | final long token = Binder.clearCallingIdentity(); |
| 1260 | try { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1261 | return isActiveNetworkMeteredUnchecked(uid); |
Jeff Sharkey | 15ec7d6 | 2012-04-17 12:23:40 -0700 | [diff] [blame] | 1262 | } finally { |
| 1263 | Binder.restoreCallingIdentity(token); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1264 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1267 | private boolean isActiveNetworkMeteredUnchecked(int uid) { |
| 1268 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1269 | if (state.networkInfo != null) { |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1270 | try { |
| 1271 | return mPolicyManager.isNetworkMetered(state); |
| 1272 | } catch (RemoteException e) { |
| 1273 | } |
| 1274 | } |
| 1275 | return false; |
| 1276 | } |
| 1277 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1278 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1279 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1280 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1281 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1282 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1283 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1284 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1285 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1286 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | * Ensure that a network route exists to deliver traffic to the specified |
| 1288 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1289 | * @param networkType the type of the network over which traffic to the |
| 1290 | * specified host is to be routed |
| 1291 | * @param hostAddress the IP address of the host to which the route is |
| 1292 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | * @return {@code true} on success, {@code false} on failure |
| 1294 | */ |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1295 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1297 | if (mProtectedNetworks.contains(networkType)) { |
| 1298 | enforceConnectivityInternalPermission(); |
| 1299 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1300 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1301 | InetAddress addr; |
| 1302 | try { |
| 1303 | addr = InetAddress.getByAddress(hostAddress); |
| 1304 | } catch (UnknownHostException e) { |
| 1305 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1306 | return false; |
| 1307 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1308 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1310 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1313 | |
| 1314 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1315 | if (nai == null) { |
| 1316 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1317 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1318 | } else { |
| 1319 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1320 | } |
| 1321 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1322 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1323 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1324 | DetailedState netState; |
| 1325 | synchronized (nai) { |
| 1326 | netState = nai.networkInfo.getDetailedState(); |
| 1327 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1328 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1329 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1330 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1331 | log("requestRouteToHostAddress on down network " |
| 1332 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1333 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1334 | } |
| 1335 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1336 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1337 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1338 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1339 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1340 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1341 | LinkProperties lp; |
| 1342 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1343 | synchronized (nai) { |
| 1344 | lp = nai.linkProperties; |
| 1345 | netId = nai.network.netId; |
| 1346 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1347 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1348 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1349 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1350 | } finally { |
| 1351 | Binder.restoreCallingIdentity(token); |
| 1352 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1353 | } |
| 1354 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1355 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1356 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1357 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1358 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1359 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1360 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1361 | if (bestRoute.getGateway().equals(addr)) { |
| 1362 | // 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] | 1363 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1364 | } else { |
| 1365 | // if we will connect to this through another route, add a direct route |
| 1366 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1367 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1368 | } |
| 1369 | } |
Lorenzo Colitti | aa281e2 | 2015-09-04 13:12:42 +0900 | [diff] [blame] | 1370 | if (DBG) log("Adding legacy route " + bestRoute + |
| 1371 | " for UID/PID " + uid + "/" + Binder.getCallingPid()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1372 | try { |
| 1373 | mNetd.addLegacyRouteForNetId(netId, bestRoute, uid); |
| 1374 | } catch (Exception e) { |
| 1375 | // never crash - catch them all |
| 1376 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1379 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1380 | } |
| 1381 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1382 | private INetworkPolicyListener mPolicyListener = new INetworkPolicyListener.Stub() { |
| 1383 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1384 | public void onUidRulesChanged(int uid, int uidRules) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1385 | // caller is NPMS, since we only register with them |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1386 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1387 | log("onUidRulesChanged(uid=" + uid + ", uidRules=" + uidRules + ")"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1390 | synchronized (mRulesLock) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1391 | // skip update when we've already applied rules |
| 1392 | final int oldRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 1393 | if (oldRules == uidRules) return; |
| 1394 | |
| 1395 | mUidRules.put(uid, uidRules); |
| 1396 | } |
| 1397 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1398 | // TODO: notify UID when it has requested targeted updates |
| 1399 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1400 | |
| 1401 | @Override |
| 1402 | public void onMeteredIfacesChanged(String[] meteredIfaces) { |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1403 | // caller is NPMS, since we only register with them |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1404 | if (LOGD_RULES) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1405 | log("onMeteredIfacesChanged(ifaces=" + Arrays.toString(meteredIfaces) + ")"); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | synchronized (mRulesLock) { |
| 1409 | mMeteredIfaces.clear(); |
| 1410 | for (String iface : meteredIfaces) { |
| 1411 | mMeteredIfaces.add(iface); |
| 1412 | } |
| 1413 | } |
| 1414 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1415 | |
| 1416 | @Override |
| 1417 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
| 1418 | // caller is NPMS, since we only register with them |
| 1419 | if (LOGD_RULES) { |
| 1420 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1421 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1422 | } |
Felipe Leme | 019fcd2 | 2016-04-19 10:24:39 -0700 | [diff] [blame] | 1423 | |
| 1424 | @Override |
| 1425 | public void onRestrictBackgroundWhitelistChanged(int uid, boolean whitelisted) { |
| 1426 | if (LOGD_RULES) { |
| 1427 | // caller is NPMS, since we only register with them |
| 1428 | log("onRestrictBackgroundWhitelistChanged(uid=" + uid + ", whitelisted=" |
| 1429 | + whitelisted + ")"); |
| 1430 | } |
| 1431 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1432 | }; |
| 1433 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 1434 | /** |
| 1435 | * Require that the caller is either in the same user or has appropriate permission to interact |
| 1436 | * across users. |
| 1437 | * |
| 1438 | * @param userId Target user for whatever operation the current IPC is supposed to perform. |
| 1439 | */ |
| 1440 | private void enforceCrossUserPermission(int userId) { |
| 1441 | if (userId == UserHandle.getCallingUserId()) { |
| 1442 | // Not a cross-user call. |
| 1443 | return; |
| 1444 | } |
| 1445 | mContext.enforceCallingOrSelfPermission( |
| 1446 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1447 | "ConnectivityService"); |
| 1448 | } |
| 1449 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1450 | private void enforceInternetPermission() { |
| 1451 | mContext.enforceCallingOrSelfPermission( |
| 1452 | android.Manifest.permission.INTERNET, |
| 1453 | "ConnectivityService"); |
| 1454 | } |
| 1455 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1456 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1457 | mContext.enforceCallingOrSelfPermission( |
| 1458 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1459 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | private void enforceChangePermission() { |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1463 | ConnectivityManager.enforceChangePermission(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1464 | } |
| 1465 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1466 | private void enforceTetherAccessPermission() { |
| 1467 | mContext.enforceCallingOrSelfPermission( |
| 1468 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1469 | "ConnectivityService"); |
| 1470 | } |
| 1471 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1472 | private void enforceConnectivityInternalPermission() { |
| 1473 | mContext.enforceCallingOrSelfPermission( |
| 1474 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
| 1475 | "ConnectivityService"); |
| 1476 | } |
| 1477 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1478 | private void enforceKeepalivePermission() { |
Lorenzo Colitti | 7914ce5 | 2015-09-08 13:21:48 +0900 | [diff] [blame] | 1479 | mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1480 | } |
| 1481 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1482 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1483 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1484 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1488 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1489 | } |
| 1490 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1491 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1492 | if (mLockdownTracker != null) { |
| 1493 | info = mLockdownTracker.augmentNetworkInfo(info); |
| 1494 | } |
| 1495 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1496 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1497 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1498 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1499 | if (info.isFailover()) { |
| 1500 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1501 | info.setFailover(false); |
| 1502 | } |
| 1503 | if (info.getReason() != null) { |
| 1504 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1505 | } |
| 1506 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1507 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1508 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1509 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1510 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1511 | return intent; |
| 1512 | } |
| 1513 | |
| 1514 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1515 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1516 | } |
| 1517 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1518 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1519 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1520 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1521 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1522 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1523 | final long ident = Binder.clearCallingIdentity(); |
| 1524 | try { |
| 1525 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1526 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 1527 | } finally { |
| 1528 | Binder.restoreCallingIdentity(ident); |
| 1529 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1530 | } |
| 1531 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1532 | private void sendStickyBroadcast(Intent intent) { |
| 1533 | synchronized(this) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1534 | if (!mSystemReady) { |
| 1535 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1536 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1537 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1538 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1539 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1540 | } |
| 1541 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1542 | Bundle options = null; |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1543 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1544 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 1545 | final NetworkInfo ni = intent.getParcelableExtra( |
| 1546 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 1547 | if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) { |
| 1548 | intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL); |
| 1549 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1550 | } else { |
| 1551 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 1552 | opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M); |
| 1553 | options = opts.toBundle(); |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 1554 | } |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1555 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 1556 | try { |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 1557 | bs.noteConnectivityChanged(intent.getIntExtra( |
| 1558 | ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE), |
| 1559 | ni != null ? ni.getState().toString() : "?"); |
| 1560 | } catch (RemoteException e) { |
| 1561 | } |
| 1562 | } |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1563 | try { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1564 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL, options); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1565 | } finally { |
| 1566 | Binder.restoreCallingIdentity(ident); |
| 1567 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1568 | } |
| 1569 | } |
| 1570 | |
| 1571 | void systemReady() { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 1572 | loadGlobalProxy(); |
| 1573 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1574 | synchronized(this) { |
| 1575 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1576 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 1577 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 1578 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 1579 | } |
| 1580 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1581 | // load the global proxy at startup |
| 1582 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_APPLY_GLOBAL_HTTP_PROXY)); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1583 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1584 | // Try bringing up tracker, but KeyStore won't be ready yet for secondary users so wait |
| 1585 | // for user to unlock device too. |
| 1586 | updateLockdownVpn(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1587 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1588 | // Configure whether mobile data is always on. |
| 1589 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON)); |
| 1590 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 1591 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 1592 | |
| 1593 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1594 | } |
| 1595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1597 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1598 | * |
| 1599 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1600 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1601 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1602 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1603 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1604 | |
| 1605 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1606 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1607 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1608 | if (networkAgent.networkCapabilities.hasTransport( |
| 1609 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1610 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1611 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Adam Lesinski | ed6160d | 2015-08-18 11:47:07 -0700 | [diff] [blame] | 1612 | 10); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1613 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1614 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 1615 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 1616 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 1617 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 1618 | 15); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1619 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1620 | } else { |
| 1621 | // do not track any other networks |
| 1622 | timeout = 0; |
| 1623 | } |
| 1624 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1625 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1626 | try { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1627 | mNetd.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1628 | } catch (Exception e) { |
| 1629 | // You shall not crash! |
| 1630 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1631 | } |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | /** |
| 1636 | * Remove data activity tracking when network disconnects. |
| 1637 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1638 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 1639 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 1640 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1641 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1642 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 1643 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1644 | try { |
| 1645 | // the call fails silently if no idletimer setup for this interface |
| 1646 | mNetd.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 1647 | } catch (Exception e) { |
| 1648 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | } |
| 1652 | |
| 1653 | /** |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1654 | * Reads the network specific MTU size from reources. |
| 1655 | * and set it on it's iface. |
| 1656 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1657 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 1658 | final String iface = newLp.getInterfaceName(); |
| 1659 | final int mtu = newLp.getMtu(); |
| 1660 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 1661 | if (VDBG) log("identical MTU - not setting"); |
| 1662 | return; |
| 1663 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1664 | |
Robert Greenwalt | 4307403 | 2014-08-15 17:53:05 -0700 | [diff] [blame] | 1665 | if (LinkProperties.isValidMtu(mtu, newLp.hasGlobalIPv6Address()) == false) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1666 | if (mtu != 0) loge("Unexpected mtu value: " + mtu + ", " + iface); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1667 | return; |
| 1668 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 1669 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 1670 | // Cannot set MTU without interface name |
| 1671 | if (TextUtils.isEmpty(iface)) { |
| 1672 | loge("Setting MTU size with null iface."); |
| 1673 | return; |
| 1674 | } |
| 1675 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1676 | try { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1677 | if (VDBG) log("Setting MTU size: " + iface + ", " + mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1678 | mNetd.setMtu(iface, mtu); |
| 1679 | } catch (Exception e) { |
| 1680 | Slog.e(TAG, "exception in setMtu()" + e); |
| 1681 | } |
| 1682 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1683 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1684 | 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] | 1685 | private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd"; |
| 1686 | |
| 1687 | // Overridden for testing purposes to avoid writing to SystemProperties. |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 1688 | @VisibleForTesting |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1689 | protected int getDefaultTcpRwnd() { |
| 1690 | return SystemProperties.getInt(DEFAULT_TCP_RWND_KEY, 0); |
| 1691 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1692 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1693 | private void updateTcpBufferSizes(NetworkAgentInfo nai) { |
| 1694 | if (isDefaultNetwork(nai) == false) { |
| 1695 | return; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1696 | } |
| 1697 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1698 | String tcpBufferSizes = nai.linkProperties.getTcpBufferSizes(); |
| 1699 | String[] values = null; |
| 1700 | if (tcpBufferSizes != null) { |
| 1701 | values = tcpBufferSizes.split(","); |
| 1702 | } |
| 1703 | |
| 1704 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1705 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1706 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 1707 | values = tcpBufferSizes.split(","); |
| 1708 | } |
| 1709 | |
| 1710 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 1711 | |
| 1712 | try { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 1713 | if (VDBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 1714 | |
| 1715 | final String prefix = "/sys/kernel/ipv4/tcp_"; |
| 1716 | FileUtils.stringToFile(prefix + "rmem_min", values[0]); |
| 1717 | FileUtils.stringToFile(prefix + "rmem_def", values[1]); |
| 1718 | FileUtils.stringToFile(prefix + "rmem_max", values[2]); |
| 1719 | FileUtils.stringToFile(prefix + "wmem_min", values[3]); |
| 1720 | FileUtils.stringToFile(prefix + "wmem_def", values[4]); |
| 1721 | FileUtils.stringToFile(prefix + "wmem_max", values[5]); |
| 1722 | mCurrentTcpBufferSizes = tcpBufferSizes; |
| 1723 | } catch (IOException e) { |
| 1724 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1725 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1726 | |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1727 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 1728 | Settings.Global.TCP_DEFAULT_INIT_RWND, getDefaultTcpRwnd()); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 1729 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 1730 | if (rwndValue != 0) { |
| 1731 | SystemProperties.set(sysctlKey, rwndValue.toString()); |
| 1732 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1733 | } |
| 1734 | |
Mattias Falk | 8b47b36 | 2011-08-23 14:15:13 +0200 | [diff] [blame] | 1735 | private void flushVmDnsCache() { |
Robert Greenwalt | 03595d0 | 2010-11-02 14:08:23 -0700 | [diff] [blame] | 1736 | /* |
| 1737 | * Tell the VMs to toss their DNS caches |
| 1738 | */ |
| 1739 | Intent intent = new Intent(Intent.ACTION_CLEAR_DNS_CACHE); |
| 1740 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); |
Stan Chesnutt | 3d1db86 | 2011-01-05 17:14:03 -0800 | [diff] [blame] | 1741 | /* |
| 1742 | * Connectivity events can happen before boot has completed ... |
| 1743 | */ |
| 1744 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1745 | final long ident = Binder.clearCallingIdentity(); |
| 1746 | try { |
| 1747 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
| 1748 | } finally { |
| 1749 | Binder.restoreCallingIdentity(ident); |
| 1750 | } |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1753 | @Override |
| 1754 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1755 | String restoreDefaultNetworkDelayStr = SystemProperties.get( |
| 1756 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 1757 | if(restoreDefaultNetworkDelayStr != null && |
| 1758 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 1759 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 1760 | return Integer.parseInt(restoreDefaultNetworkDelayStr); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1761 | } catch (NumberFormatException e) { |
| 1762 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1763 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 1764 | // if the system property isn't set, use the value for the apn type |
| 1765 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 1766 | |
| 1767 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 1768 | (mNetConfigs[networkType] != null)) { |
| 1769 | ret = mNetConfigs[networkType].restoreTime; |
| 1770 | } |
| 1771 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1772 | } |
| 1773 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1774 | private boolean argsContain(String[] args, String target) { |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1775 | for (String arg : args) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1776 | if (arg.equals(target)) return true; |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1777 | } |
| 1778 | return false; |
| 1779 | } |
| 1780 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1781 | private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { |
| 1782 | final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); |
| 1783 | final long DIAG_TIME_MS = 5000; |
| 1784 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1785 | // Start gathering diagnostic information. |
| 1786 | netDiags.add(new NetworkDiagnostics( |
| 1787 | nai.network, |
| 1788 | new LinkProperties(nai.linkProperties), // Must be a copy. |
| 1789 | DIAG_TIME_MS)); |
| 1790 | } |
| 1791 | |
| 1792 | for (NetworkDiagnostics netDiag : netDiags) { |
| 1793 | pw.println(); |
| 1794 | netDiag.waitForMeasurements(); |
| 1795 | netDiag.dump(pw); |
| 1796 | } |
| 1797 | } |
| 1798 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1799 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1800 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
| 1801 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1802 | if (mContext.checkCallingOrSelfPermission( |
| 1803 | android.Manifest.permission.DUMP) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1804 | != PackageManager.PERMISSION_GRANTED) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1805 | pw.println("Permission Denial: can't dump ConnectivityService " + |
| 1806 | "from from pid=" + Binder.getCallingPid() + ", uid=" + |
| 1807 | Binder.getCallingUid()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1808 | return; |
| 1809 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1810 | |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1811 | if (argsContain(args, "--diag")) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1812 | dumpNetworkDiagnostics(pw); |
| 1813 | return; |
| 1814 | } |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 1815 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1816 | pw.print("NetworkFactories for:"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1817 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1818 | pw.print(" " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1819 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1820 | pw.println(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 1821 | pw.println(); |
| 1822 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 1823 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1824 | pw.print("Active default network: "); |
| 1825 | if (defaultNai == null) { |
| 1826 | pw.println("none"); |
| 1827 | } else { |
| 1828 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1829 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1830 | pw.println(); |
| 1831 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1832 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1833 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1834 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1835 | pw.println(nai.toString()); |
| 1836 | pw.increaseIndent(); |
| 1837 | pw.println("Requests:"); |
| 1838 | pw.increaseIndent(); |
| 1839 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 1840 | pw.println(nai.networkRequests.valueAt(i).toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1841 | } |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1842 | pw.decreaseIndent(); |
| 1843 | pw.println("Lingered:"); |
| 1844 | pw.increaseIndent(); |
| 1845 | for (NetworkRequest nr : nai.networkLingered) pw.println(nr.toString()); |
| 1846 | pw.decreaseIndent(); |
| 1847 | pw.decreaseIndent(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1848 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1849 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1850 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1851 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1852 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1853 | pw.increaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 1854 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 1855 | pw.println(nri.toString()); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 1856 | } |
| 1857 | pw.println(); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1858 | pw.decreaseIndent(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1859 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1860 | mLegacyTypeTracker.dump(pw); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 1861 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1862 | synchronized (this) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1863 | pw.print("mNetTransitionWakeLock: currently " + |
| 1864 | (mNetTransitionWakeLock.isHeld() ? "" : "not ") + "held"); |
| 1865 | if (!TextUtils.isEmpty(mNetTransitionWakeLockCausedBy)) { |
| 1866 | pw.println(", last requested for " + mNetTransitionWakeLockCausedBy); |
| 1867 | } else { |
| 1868 | pw.println(", last requested never"); |
| 1869 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1870 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1871 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1872 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1873 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1874 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1875 | pw.println(); |
| 1876 | mKeepaliveTracker.dump(pw); |
| 1877 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1878 | pw.println(); |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 1879 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 1880 | if (mInetLog != null && mInetLog.size() > 0) { |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1881 | pw.println(); |
| 1882 | pw.println("Inet condition reports:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1883 | pw.increaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1884 | for(int i = 0; i < mInetLog.size(); i++) { |
| 1885 | pw.println(mInetLog.get(i)); |
| 1886 | } |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 1887 | pw.decreaseIndent(); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 1888 | } |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1889 | |
| 1890 | if (argsContain(args, "--short") == false) { |
| 1891 | pw.println(); |
| 1892 | synchronized (mValidationLogs) { |
| 1893 | pw.println("mValidationLogs (most recent first):"); |
| 1894 | for (Pair<Network,ReadOnlyLocalLog> p : mValidationLogs) { |
| 1895 | pw.println(p.first); |
| 1896 | pw.increaseIndent(); |
| 1897 | p.second.dump(fd, pw, args); |
| 1898 | pw.decreaseIndent(); |
| 1899 | } |
| 1900 | } |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 1901 | |
| 1902 | pw.println(); |
| 1903 | pw.println("mNetworkRequestInfoLogs (most recent first):"); |
| 1904 | pw.increaseIndent(); |
| 1905 | mNetworkRequestInfoLogs.reverseDump(fd, pw, args); |
| 1906 | pw.decreaseIndent(); |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 1907 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1910 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 1911 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1912 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1913 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 1914 | if (officialNai != null || VDBG) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1915 | final String msg = sMagicDecoderRing.get(what, Integer.toString(what)); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 1916 | loge(msg + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 1917 | " - " + nai); |
| 1918 | } |
| 1919 | return false; |
| 1920 | } |
| 1921 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1922 | private boolean isRequest(NetworkRequest request) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 1923 | return mNetworkRequests.get(request).isRequest(); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 1924 | } |
| 1925 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 1926 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1927 | private class NetworkStateTrackerHandler extends Handler { |
| 1928 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 1929 | super(looper); |
| 1930 | } |
| 1931 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1932 | private boolean maybeHandleAsyncChannelMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | switch (msg.what) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1934 | default: |
| 1935 | return false; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1936 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1937 | handleAsyncChannelHalfConnect(msg); |
| 1938 | break; |
| 1939 | } |
| 1940 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 1941 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1942 | if (nai != null) nai.asyncChannel.disconnect(); |
| 1943 | break; |
| 1944 | } |
| 1945 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 1946 | handleAsyncChannelDisconnected(msg); |
| 1947 | break; |
| 1948 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1949 | } |
| 1950 | return true; |
| 1951 | } |
| 1952 | |
| 1953 | private void maybeHandleNetworkAgentMessage(Message msg) { |
| 1954 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 1955 | if (nai == null) { |
| 1956 | if (VDBG) { |
| 1957 | final String what = sMagicDecoderRing.get(msg.what, Integer.toString(msg.what)); |
| 1958 | log(String.format("%s from unknown NetworkAgent", what)); |
| 1959 | } |
| 1960 | return; |
| 1961 | } |
| 1962 | |
| 1963 | switch (msg.what) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1964 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1965 | final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj; |
| 1966 | if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL) || |
| 1967 | networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED)) { |
| 1968 | Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1969 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1970 | if (nai.created && !nai.networkCapabilities.equalImmutableCapabilities( |
| 1971 | networkCapabilities)) { |
| 1972 | Slog.wtf(TAG, "BUG: " + nai + " changed immutable capabilities: " |
| 1973 | + nai.networkCapabilities + " -> " + networkCapabilities); |
| 1974 | } |
| 1975 | updateCapabilities(nai, networkCapabilities); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 1976 | break; |
| 1977 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1978 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1979 | if (VDBG) { |
| 1980 | log("Update of LinkProperties for " + nai.name() + |
| 1981 | "; created=" + nai.created); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1982 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1983 | LinkProperties oldLp = nai.linkProperties; |
| 1984 | synchronized (nai) { |
| 1985 | nai.linkProperties = (LinkProperties)msg.obj; |
| 1986 | } |
| 1987 | if (nai.created) updateLinkProperties(nai, oldLp); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1988 | break; |
| 1989 | } |
| 1990 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 1991 | NetworkInfo info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 1992 | updateNetworkInfo(nai, info); |
| 1993 | break; |
| 1994 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1995 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1996 | Integer score = (Integer) msg.obj; |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 1997 | if (score != null) updateNetworkScore(nai, score.intValue()); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 1998 | break; |
| 1999 | } |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2000 | case NetworkAgent.EVENT_UID_RANGES_ADDED: { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2001 | try { |
| 2002 | mNetd.addVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 2003 | } catch (Exception e) { |
| 2004 | // Never crash! |
| 2005 | loge("Exception in addVpnUidRanges: " + e); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2006 | } |
| 2007 | break; |
| 2008 | } |
| 2009 | case NetworkAgent.EVENT_UID_RANGES_REMOVED: { |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 2010 | try { |
| 2011 | mNetd.removeVpnUidRanges(nai.network.netId, (UidRange[])msg.obj); |
Sreeram Ramachandran | 42065ac | 2014-07-27 00:37:35 -0700 | [diff] [blame] | 2012 | } catch (Exception e) { |
| 2013 | // Never crash! |
| 2014 | loge("Exception in removeVpnUidRanges: " + e); |
| 2015 | } |
| 2016 | break; |
| 2017 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2018 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2019 | if (nai.created && !nai.networkMisc.explicitlySelected) { |
| 2020 | loge("ERROR: created network explicitly selected."); |
| 2021 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2022 | nai.networkMisc.explicitlySelected = true; |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2023 | nai.networkMisc.acceptUnvalidated = (boolean) msg.obj; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2024 | break; |
| 2025 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2026 | case NetworkAgent.EVENT_PACKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2027 | mKeepaliveTracker.handleEventPacketKeepalive(nai, msg); |
| 2028 | break; |
| 2029 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2030 | } |
| 2031 | } |
| 2032 | |
| 2033 | private boolean maybeHandleNetworkMonitorMessage(Message msg) { |
| 2034 | switch (msg.what) { |
| 2035 | default: |
| 2036 | return false; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2037 | case NetworkMonitor.EVENT_NETWORK_TESTED: { |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2038 | final NetworkAgentInfo nai; |
| 2039 | synchronized (mNetworkForNetId) { |
| 2040 | nai = mNetworkForNetId.get(msg.arg2); |
| 2041 | } |
| 2042 | if (nai != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 2043 | final boolean valid = |
| 2044 | (msg.arg1 == NetworkMonitor.NETWORK_TEST_RESULT_VALID); |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2045 | if (DBG) log(nai.name() + " validation " + (valid ? "passed" : "failed") + |
| 2046 | (msg.obj == null ? "" : " with redirect to " + (String)msg.obj)); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2047 | if (valid != nai.lastValidated) { |
| 2048 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2049 | nai.lastValidated = valid; |
| 2050 | nai.everValidated |= valid; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2051 | updateCapabilities(nai, nai.networkCapabilities); |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2052 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2053 | if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2054 | } |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 2055 | updateInetCondition(nai); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2056 | // Let the NetworkAgent know the state of its network |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2057 | Bundle redirectUrlBundle = new Bundle(); |
| 2058 | redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, (String)msg.obj); |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2059 | nai.asyncChannel.sendMessage( |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2060 | NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
Robert Greenwalt | 49f63fb | 2014-09-13 12:04:12 -0700 | [diff] [blame] | 2061 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
Paul Jensen | 23243731 | 2016-04-06 09:51:26 -0400 | [diff] [blame] | 2062 | 0, redirectUrlBundle); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2063 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2064 | break; |
| 2065 | } |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2066 | case NetworkMonitor.EVENT_NETWORK_LINGER_COMPLETE: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2067 | NetworkAgentInfo nai = (NetworkAgentInfo)msg.obj; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2068 | if (isLiveNetworkAgent(nai, msg.what)) { |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2069 | handleLingerComplete(nai); |
| 2070 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2071 | break; |
| 2072 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2073 | case NetworkMonitor.EVENT_PROVISIONING_NOTIFICATION: { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2074 | final int netId = msg.arg2; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2075 | final boolean visible = (msg.arg1 != 0); |
| 2076 | final NetworkAgentInfo nai; |
| 2077 | synchronized (mNetworkForNetId) { |
| 2078 | nai = mNetworkForNetId.get(netId); |
| 2079 | } |
| 2080 | // If captive portal status has changed, update capabilities. |
| 2081 | if (nai != null && (visible != nai.lastCaptivePortalDetected)) { |
| 2082 | nai.lastCaptivePortalDetected = visible; |
| 2083 | nai.everCaptivePortalDetected |= visible; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2084 | updateCapabilities(nai, nai.networkCapabilities); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2085 | } |
| 2086 | if (!visible) { |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2087 | setProvNotificationVisibleIntent(false, netId, null, 0, null, null, false); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2088 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2089 | if (nai == null) { |
| 2090 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2091 | break; |
| 2092 | } |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2093 | setProvNotificationVisibleIntent(true, netId, NotificationType.SIGN_IN, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2094 | nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(), |
| 2095 | (PendingIntent)msg.obj, nai.networkMisc.explicitlySelected); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2096 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2097 | break; |
| 2098 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2099 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2100 | return true; |
| 2101 | } |
| 2102 | |
| 2103 | @Override |
| 2104 | public void handleMessage(Message msg) { |
| 2105 | if (!maybeHandleAsyncChannelMessage(msg) && !maybeHandleNetworkMonitorMessage(msg)) { |
| 2106 | maybeHandleNetworkAgentMessage(msg); |
| 2107 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2111 | private void linger(NetworkAgentInfo nai) { |
| 2112 | nai.lingering = true; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 2113 | NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_LINGER); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2114 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_LINGER); |
| 2115 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING); |
| 2116 | } |
| 2117 | |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2118 | // Cancel any lingering so the linger timeout doesn't teardown a network. |
| 2119 | // This should be called when a network begins satisfying a NetworkRequest. |
| 2120 | // Note: depending on what state the NetworkMonitor is in (e.g., |
| 2121 | // if it's awaiting captive portal login, or if validation failed), this |
| 2122 | // may trigger a re-evaluation of the network. |
| 2123 | private void unlinger(NetworkAgentInfo nai) { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2124 | nai.networkLingered.clear(); |
| 2125 | if (!nai.lingering) return; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2126 | nai.lingering = false; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 2127 | NetworkEvent.logEvent(nai.network.netId, NetworkEvent.NETWORK_UNLINGER); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2128 | if (VDBG) log("Canceling linger of " + nai.name()); |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2129 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
| 2130 | } |
| 2131 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2132 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2133 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2134 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2135 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2136 | if (VDBG) log("NetworkFactory connected"); |
| 2137 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2138 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2139 | if (!nri.isRequest()) continue; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2140 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2141 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2142 | (nai != null ? nai.getCurrentScore() : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2143 | } |
| 2144 | } else { |
| 2145 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2146 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2147 | } |
| 2148 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2149 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2150 | if (VDBG) log("NetworkAgent connected"); |
| 2151 | // A network agent has requested a connection. Establish the connection. |
| 2152 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2153 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2154 | } else { |
| 2155 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2156 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2157 | if (nai != null) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2158 | final boolean wasDefault = isDefaultNetwork(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2159 | synchronized (mNetworkForNetId) { |
| 2160 | mNetworkForNetId.remove(nai.network.netId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2161 | mNetIdInUse.delete(nai.network.netId); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2162 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2163 | // Just in case. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2164 | mLegacyTypeTracker.remove(nai, wasDefault); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2165 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2166 | } |
| 2167 | } |
| 2168 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2169 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2170 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2171 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2172 | if (nai != null) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2173 | if (DBG) { |
| 2174 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.networkRequests.size()); |
| 2175 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2176 | // A network agent has disconnected. |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2177 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2178 | // because they lost all their requests or because their score isn't good) |
| 2179 | // then they would disconnect organically, report their new state and then |
| 2180 | // disconnect the channel. |
| 2181 | if (nai.networkInfo.isConnected()) { |
| 2182 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2183 | null, null); |
| 2184 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2185 | final boolean wasDefault = isDefaultNetwork(nai); |
| 2186 | if (wasDefault) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2187 | mDefaultInetConditionPublished = 0; |
| 2188 | } |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 2189 | notifyIfacesChangedForNetworkStats(); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 2190 | // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied |
| 2191 | // by other networks that are already connected. Perhaps that can be done by |
| 2192 | // sending all CALLBACK_LOST messages (for requests, not listens) at the end |
| 2193 | // of rematchAllNetworksAndRequests |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2194 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2195 | mKeepaliveTracker.handleStopAllKeepalives(nai, |
| 2196 | ConnectivityManager.PacketKeepalive.ERROR_INVALID_NETWORK); |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 2197 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_DISCONNECTED); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 2198 | mNetworkAgentInfos.remove(msg.replyTo); |
| 2199 | updateClat(null, nai.linkProperties, nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2200 | synchronized (mNetworkForNetId) { |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2201 | // Remove the NetworkAgent, but don't mark the netId as |
| 2202 | // available until we've told netd to delete it below. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2203 | mNetworkForNetId.remove(nai.network.netId); |
| 2204 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2205 | // Remove all previously satisfied requests. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2206 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2207 | NetworkRequest request = nai.networkRequests.valueAt(i); |
| 2208 | NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(request.requestId); |
| 2209 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 2210 | mNetworkForRequestId.remove(request.requestId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2211 | sendUpdatedScoreToFactories(request, 0); |
| 2212 | } |
| 2213 | } |
| 2214 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
| 2215 | removeDataActivityTracking(nai); |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 2216 | notifyLockdownVpn(nai); |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2217 | requestNetworkTransitionWakelock(nai.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2218 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2219 | mLegacyTypeTracker.remove(nai, wasDefault); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2220 | rematchAllNetworksAndRequests(null, 0); |
Pierre Imai | 04e22225 | 2016-04-12 18:02:58 +0900 | [diff] [blame] | 2221 | if (wasDefault && getDefaultNetwork() == null) { |
| 2222 | // Log that we lost the default network and there is no replacement. |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 2223 | logDefaultNetworkEvent(null, nai); |
Pierre Imai | 04e22225 | 2016-04-12 18:02:58 +0900 | [diff] [blame] | 2224 | } |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2225 | if (nai.created) { |
| 2226 | // Tell netd to clean up the configuration for this network |
| 2227 | // (routing rules, DNS, etc). |
| 2228 | // This may be slow as it requires a lot of netd shelling out to ip and |
| 2229 | // ip[6]tables to flush routes and remove the incoming packet mark rule, so do it |
| 2230 | // after we've rematched networks with requests which should make a potential |
| 2231 | // fallback network the default or requested a new network from the |
| 2232 | // NetworkFactories, so network traffic isn't interrupted for an unnecessarily |
| 2233 | // long time. |
| 2234 | try { |
| 2235 | mNetd.removeNetwork(nai.network.netId); |
| 2236 | } catch (Exception e) { |
| 2237 | loge("Exception removing network: " + e); |
| 2238 | } |
| 2239 | } |
| 2240 | synchronized (mNetworkForNetId) { |
| 2241 | mNetIdInUse.delete(nai.network.netId); |
| 2242 | } |
| 2243 | } else { |
| 2244 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(msg.replyTo); |
| 2245 | if (DBG && nfi != null) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2246 | } |
| 2247 | } |
| 2248 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2249 | // If this method proves to be too slow then we can maintain a separate |
| 2250 | // pendingIntent => NetworkRequestInfo map. |
| 2251 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 2252 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 2253 | Intent intent = pendingIntent.getIntent(); |
| 2254 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 2255 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 2256 | if (existingPendingIntent != null && |
| 2257 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 2258 | return entry.getValue(); |
| 2259 | } |
| 2260 | } |
| 2261 | return null; |
| 2262 | } |
| 2263 | |
| 2264 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 2265 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 2266 | |
| 2267 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 2268 | if (existingRequest != null) { // remove the existing request. |
| 2269 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 2270 | + nri.request + " because their intents matched."); |
| 2271 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid()); |
| 2272 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2273 | handleRegisterNetworkRequest(nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2276 | private void handleRegisterNetworkRequest(NetworkRequestInfo nri) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2277 | mNetworkRequests.put(nri.request, nri); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2278 | mNetworkRequestInfoLogs.log("REGISTER " + nri); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2279 | if (!nri.isRequest()) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2280 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 2281 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 2282 | network.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 2283 | updateSignalStrengthThresholds(network, "REGISTER", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2284 | } |
| 2285 | } |
| 2286 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2287 | rematchAllNetworksAndRequests(null, 0); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2288 | if (nri.isRequest() && mNetworkForRequestId.get(nri.request.requestId) == null) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2289 | sendUpdatedScoreToFactories(nri.request, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2290 | } |
| 2291 | } |
| 2292 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2293 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 2294 | int callingUid) { |
| 2295 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 2296 | if (nri != null) { |
| 2297 | handleReleaseNetworkRequest(nri.request, callingUid); |
| 2298 | } |
| 2299 | } |
| 2300 | |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2301 | // Is nai unneeded by all NetworkRequests (and should be disconnected)? |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2302 | // This is whether it is satisfying any NetworkRequests or were it to become validated, |
| 2303 | // would it have a chance of satisfying any NetworkRequests. |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2304 | private boolean unneeded(NetworkAgentInfo nai) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2305 | if (!nai.created || nai.isVPN() || nai.lingering) return false; |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2306 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 2307 | // If this Network is already the highest scoring Network for a request, or if |
| 2308 | // there is hope for it to become one if it validated, then it is needed. |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2309 | if (nri.isRequest() && nai.satisfies(nri.request) && |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2310 | (nai.networkRequests.get(nri.request.requestId) != null || |
| 2311 | // Note that this catches two important cases: |
| 2312 | // 1. Unvalidated cellular will not be reaped when unvalidated WiFi |
| 2313 | // is currently satisfying the request. This is desirable when |
| 2314 | // cellular ends up validating but WiFi does not. |
| 2315 | // 2. Unvalidated WiFi will not be reaped when validated cellular |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 2316 | // is currently satisfying the request. This is desirable when |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2317 | // WiFi ends up validating and out scoring cellular. |
| 2318 | mNetworkForRequestId.get(nri.request.requestId).getCurrentScore() < |
| 2319 | nai.getCurrentScoreAsValidated())) { |
| 2320 | return false; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2321 | } |
| 2322 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 2323 | return true; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2324 | } |
| 2325 | |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2326 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
| 2327 | NetworkRequestInfo nri = mNetworkRequests.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2328 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 2329 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2330 | if (DBG) log("Attempt to release unowned NetworkRequest " + request); |
| 2331 | return; |
| 2332 | } |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2333 | if (VDBG || (DBG && nri.isRequest())) log("releasing NetworkRequest " + request); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 2334 | nri.unlinkDeathRecipient(); |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2335 | mNetworkRequests.remove(request); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 2336 | synchronized (mUidToNetworkRequestCount) { |
| 2337 | int requests = mUidToNetworkRequestCount.get(nri.mUid, 0); |
| 2338 | if (requests < 1) { |
| 2339 | Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + |
| 2340 | nri.mUid); |
| 2341 | } else if (requests == 1) { |
| 2342 | mUidToNetworkRequestCount.removeAt( |
| 2343 | mUidToNetworkRequestCount.indexOfKey(nri.mUid)); |
| 2344 | } else { |
| 2345 | mUidToNetworkRequestCount.put(nri.mUid, requests - 1); |
| 2346 | } |
| 2347 | } |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2348 | mNetworkRequestInfoLogs.log("RELEASE " + nri); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 2349 | if (nri.isRequest()) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2350 | // Find all networks that are satisfying this request and remove the request |
| 2351 | // from their request lists. |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2352 | // TODO - it's my understanding that for a request there is only a single |
| 2353 | // network satisfying it, so this loop is wasteful |
| 2354 | boolean wasKept = false; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2355 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2356 | if (nai.networkRequests.get(nri.request.requestId) != null) { |
| 2357 | nai.networkRequests.remove(nri.request.requestId); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2358 | if (VDBG) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2359 | log(" Removing from current network " + nai.name() + |
| 2360 | ", leaving " + nai.networkRequests.size() + |
| 2361 | " requests."); |
| 2362 | } |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2363 | if (unneeded(nai)) { |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2364 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 2365 | teardownUnneededNetwork(nai); |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2366 | } else { |
| 2367 | // suspect there should only be one pass through here |
| 2368 | // but if any were kept do the check below |
| 2369 | wasKept |= true; |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2370 | } |
| 2371 | } |
| 2372 | } |
| 2373 | |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2374 | NetworkAgentInfo nai = mNetworkForRequestId.get(nri.request.requestId); |
| 2375 | if (nai != null) { |
| 2376 | mNetworkForRequestId.remove(nri.request.requestId); |
| 2377 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2378 | // Maintain the illusion. When this request arrived, we might have pretended |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2379 | // that a network connected to serve it, even though the network was already |
| 2380 | // connected. Now that this request has gone away, we might have to pretend |
| 2381 | // that the network disconnected. LegacyTypeTracker will generate that |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2382 | // phantom disconnect for this type. |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 2383 | if (nri.request.legacyType != TYPE_NONE && nai != null) { |
| 2384 | boolean doRemove = true; |
| 2385 | if (wasKept) { |
| 2386 | // check if any of the remaining requests for this network are for the |
| 2387 | // same legacy type - if so, don't remove the nai |
| 2388 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 2389 | NetworkRequest otherRequest = nai.networkRequests.valueAt(i); |
| 2390 | if (otherRequest.legacyType == nri.request.legacyType && |
| 2391 | isRequest(otherRequest)) { |
| 2392 | if (DBG) log(" still have other legacy request - leaving"); |
| 2393 | doRemove = false; |
| 2394 | } |
| 2395 | } |
| 2396 | } |
| 2397 | |
| 2398 | if (doRemove) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2399 | mLegacyTypeTracker.remove(nri.request.legacyType, nai, false); |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2400 | } |
| 2401 | } |
| 2402 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2403 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2404 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 2405 | nri.request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2406 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 2407 | } else { |
| 2408 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 2409 | // if this listen request applies and remove it. |
| 2410 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2411 | nai.networkRequests.remove(nri.request.requestId); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 2412 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 2413 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 2414 | updateSignalStrengthThresholds(nai, "RELEASE", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2415 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2416 | } |
| 2417 | } |
| 2418 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_RELEASED); |
| 2419 | } |
| 2420 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2421 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2422 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2423 | enforceConnectivityInternalPermission(); |
| 2424 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED, |
| 2425 | accept ? 1 : 0, always ? 1: 0, network)); |
| 2426 | } |
| 2427 | |
| 2428 | private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2429 | if (DBG) log("handleSetAcceptUnvalidated network=" + network + |
| 2430 | " accept=" + accept + " always=" + always); |
| 2431 | |
| 2432 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2433 | if (nai == null) { |
| 2434 | // Nothing to do. |
| 2435 | return; |
| 2436 | } |
| 2437 | |
| 2438 | if (nai.everValidated) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2439 | // The network validated while the dialog box was up. Take no action. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2440 | return; |
| 2441 | } |
| 2442 | |
| 2443 | if (!nai.networkMisc.explicitlySelected) { |
| 2444 | Slog.wtf(TAG, "BUG: setAcceptUnvalidated non non-explicitly selected network"); |
| 2445 | } |
| 2446 | |
| 2447 | if (accept != nai.networkMisc.acceptUnvalidated) { |
| 2448 | int oldScore = nai.getCurrentScore(); |
| 2449 | nai.networkMisc.acceptUnvalidated = accept; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2450 | rematchAllNetworksAndRequests(nai, oldScore); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2451 | sendUpdatedScoreToFactories(nai); |
| 2452 | } |
| 2453 | |
| 2454 | if (always) { |
| 2455 | nai.asyncChannel.sendMessage( |
| 2456 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, accept ? 1 : 0); |
| 2457 | } |
| 2458 | |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2459 | if (!accept) { |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 2460 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 2461 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 2462 | // Teardown the nework. |
| 2463 | teardownUnneededNetwork(nai); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2464 | } |
| 2465 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2466 | } |
| 2467 | |
| 2468 | private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2469 | if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2470 | mHandler.sendMessageDelayed( |
| 2471 | mHandler.obtainMessage(EVENT_PROMPT_UNVALIDATED, nai.network), |
| 2472 | PROMPT_UNVALIDATED_DELAY_MS); |
| 2473 | } |
| 2474 | |
| 2475 | private void handlePromptUnvalidated(Network network) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2476 | if (VDBG) log("handlePromptUnvalidated " + network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2477 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2478 | |
| 2479 | // Only prompt if the network is unvalidated and was explicitly selected by the user, and if |
| 2480 | // 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] | 2481 | // Also don't prompt on captive portals because we're already prompting the user to sign in. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2482 | if (nai == null || nai.everValidated || nai.everCaptivePortalDetected || |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2483 | !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated) { |
| 2484 | return; |
| 2485 | } |
| 2486 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2487 | Intent intent = new Intent(ConnectivityManager.ACTION_PROMPT_UNVALIDATED); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2488 | intent.setData(Uri.fromParts("netId", Integer.toString(network.netId), null)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2489 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 2490 | intent.setClassName("com.android.settings", |
| 2491 | "com.android.settings.wifi.WifiNoInternetDialog"); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2492 | |
| 2493 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 2494 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 2495 | setProvNotificationVisibleIntent(true, nai.network.netId, NotificationType.NO_INTERNET, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 2496 | nai.networkInfo.getType(), nai.networkInfo.getExtraInfo(), pendingIntent, true); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2497 | } |
| 2498 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2499 | private class InternalHandler extends Handler { |
| 2500 | public InternalHandler(Looper looper) { |
| 2501 | super(looper); |
| 2502 | } |
| 2503 | |
| 2504 | @Override |
| 2505 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2506 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2507 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2508 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2509 | String causedBy = null; |
| 2510 | synchronized (ConnectivityService.this) { |
| 2511 | if (msg.arg1 == mNetTransitionWakeLockSerialNumber && |
| 2512 | mNetTransitionWakeLock.isHeld()) { |
| 2513 | mNetTransitionWakeLock.release(); |
| 2514 | causedBy = mNetTransitionWakeLockCausedBy; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2515 | } else { |
| 2516 | break; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2517 | } |
| 2518 | } |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2519 | if (VDBG) { |
| 2520 | if (msg.what == EVENT_EXPIRE_NET_TRANSITION_WAKELOCK) { |
| 2521 | log("Failed to find a new network - expiring NetTransition Wakelock"); |
| 2522 | } else { |
| 2523 | log("NetTransition Wakelock (" + |
| 2524 | (causedBy == null ? "unknown" : causedBy) + |
| 2525 | " cleared because we found a replacement network"); |
| 2526 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2527 | } |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 2528 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2529 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 2530 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2531 | handleDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 2532 | break; |
| 2533 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2534 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2535 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 2536 | break; |
| 2537 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2538 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2539 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 2540 | break; |
| 2541 | } |
| 2542 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 2543 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2544 | break; |
| 2545 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2546 | case EVENT_REGISTER_NETWORK_AGENT: { |
| 2547 | handleRegisterNetworkAgent((NetworkAgentInfo)msg.obj); |
| 2548 | break; |
| 2549 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2550 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 2551 | case EVENT_REGISTER_NETWORK_LISTENER: { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2552 | handleRegisterNetworkRequest((NetworkRequestInfo) msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2553 | break; |
| 2554 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2555 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: |
| 2556 | case EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT: { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2557 | handleRegisterNetworkRequestWithIntent(msg); |
| 2558 | break; |
| 2559 | } |
| 2560 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 2561 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 2562 | break; |
| 2563 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2564 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 2565 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2566 | break; |
| 2567 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2568 | case EVENT_SET_ACCEPT_UNVALIDATED: { |
| 2569 | handleSetAcceptUnvalidated((Network) msg.obj, msg.arg1 != 0, msg.arg2 != 0); |
| 2570 | break; |
| 2571 | } |
| 2572 | case EVENT_PROMPT_UNVALIDATED: { |
| 2573 | handlePromptUnvalidated((Network) msg.obj); |
| 2574 | break; |
| 2575 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2576 | case EVENT_CONFIGURE_MOBILE_DATA_ALWAYS_ON: { |
| 2577 | handleMobileDataAlwaysOn(); |
| 2578 | break; |
| 2579 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2580 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
| 2581 | case NetworkAgent.CMD_START_PACKET_KEEPALIVE: { |
| 2582 | mKeepaliveTracker.handleStartKeepalive(msg); |
| 2583 | break; |
| 2584 | } |
| 2585 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
| 2586 | case NetworkAgent.CMD_STOP_PACKET_KEEPALIVE: { |
| 2587 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); |
| 2588 | int slot = msg.arg1; |
| 2589 | int reason = msg.arg2; |
| 2590 | mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); |
| 2591 | break; |
| 2592 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2593 | case EVENT_SYSTEM_READY: { |
| 2594 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 2595 | nai.networkMonitor.systemReady = true; |
| 2596 | } |
| 2597 | break; |
| 2598 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | } |
| 2600 | } |
| 2601 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2602 | |
| 2603 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2604 | public int tether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2605 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2606 | if (isTetheringSupported()) { |
| 2607 | return mTethering.tether(iface); |
| 2608 | } else { |
| 2609 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2610 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2611 | } |
| 2612 | |
| 2613 | // javadoc from interface |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2614 | public int untether(String iface) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2615 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2616 | |
| 2617 | if (isTetheringSupported()) { |
| 2618 | return mTethering.untether(iface); |
| 2619 | } else { |
| 2620 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2621 | } |
| 2622 | } |
| 2623 | |
| 2624 | // javadoc from interface |
| 2625 | public int getLastTetherError(String iface) { |
| 2626 | enforceTetherAccessPermission(); |
| 2627 | |
| 2628 | if (isTetheringSupported()) { |
| 2629 | return mTethering.getLastTetherError(iface); |
| 2630 | } else { |
| 2631 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2632 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | // TODO - proper iface API for selection by property, inspection, etc |
| 2636 | public String[] getTetherableUsbRegexs() { |
| 2637 | enforceTetherAccessPermission(); |
| 2638 | if (isTetheringSupported()) { |
| 2639 | return mTethering.getTetherableUsbRegexs(); |
| 2640 | } else { |
| 2641 | return new String[0]; |
| 2642 | } |
| 2643 | } |
| 2644 | |
| 2645 | public String[] getTetherableWifiRegexs() { |
| 2646 | enforceTetherAccessPermission(); |
| 2647 | if (isTetheringSupported()) { |
| 2648 | return mTethering.getTetherableWifiRegexs(); |
| 2649 | } else { |
| 2650 | return new String[0]; |
| 2651 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2652 | } |
| 2653 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2654 | public String[] getTetherableBluetoothRegexs() { |
| 2655 | enforceTetherAccessPermission(); |
| 2656 | if (isTetheringSupported()) { |
| 2657 | return mTethering.getTetherableBluetoothRegexs(); |
| 2658 | } else { |
| 2659 | return new String[0]; |
| 2660 | } |
| 2661 | } |
| 2662 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2663 | public int setUsbTethering(boolean enable) { |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2664 | ConnectivityManager.enforceTetherChangePermission(mContext); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2665 | if (isTetheringSupported()) { |
| 2666 | return mTethering.setUsbTethering(enable); |
| 2667 | } else { |
| 2668 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 2669 | } |
| 2670 | } |
| 2671 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2672 | // TODO - move iface listing, queries, etc to new module |
| 2673 | // javadoc from interface |
| 2674 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2675 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2676 | return mTethering.getTetherableIfaces(); |
| 2677 | } |
| 2678 | |
| 2679 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2680 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2681 | return mTethering.getTetheredIfaces(); |
| 2682 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2683 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2684 | public String[] getTetheringErroredIfaces() { |
| 2685 | enforceTetherAccessPermission(); |
| 2686 | return mTethering.getErroredIfaces(); |
| 2687 | } |
| 2688 | |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2689 | public String[] getTetheredDhcpRanges() { |
| 2690 | enforceConnectivityInternalPermission(); |
| 2691 | return mTethering.getTetheredDhcpRanges(); |
| 2692 | } |
| 2693 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2694 | // if ro.tether.denied = true we default to no tethering |
| 2695 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 2696 | // had previously been turned off. |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2697 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2698 | public boolean isTetheringSupported() { |
| 2699 | enforceTetherAccessPermission(); |
| 2700 | int defaultVal = (SystemProperties.get("ro.tether.denied").equals("true") ? 0 : 1); |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2701 | boolean tetherEnabledInSettings = (Settings.Global.getInt(mContext.getContentResolver(), |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 2702 | Settings.Global.TETHER_SUPPORTED, defaultVal) != 0) |
| 2703 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2704 | return tetherEnabledInSettings && mUserManager.isAdminUser() && |
| 2705 | ((mTethering.getTetherableUsbRegexs().length != 0 || |
Robert Greenwalt | c13368b | 2013-07-18 14:24:42 -0700 | [diff] [blame] | 2706 | mTethering.getTetherableWifiRegexs().length != 0 || |
| 2707 | mTethering.getTetherableBluetoothRegexs().length != 0) && |
| 2708 | mTethering.getUpstreamIfaceTypes().length != 0); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2709 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2710 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2711 | @Override |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2712 | public void startTethering(int type, ResultReceiver receiver, |
| 2713 | boolean showProvisioningUi) { |
| 2714 | ConnectivityManager.enforceTetherChangePermission(mContext); |
| 2715 | if (!isTetheringSupported()) { |
| 2716 | receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null); |
| 2717 | return; |
| 2718 | } |
| 2719 | mTethering.startTethering(type, receiver, showProvisioningUi); |
| 2720 | } |
| 2721 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 2722 | @Override |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2723 | public void stopTethering(int type) { |
| 2724 | ConnectivityManager.enforceTetherChangePermission(mContext); |
| 2725 | mTethering.stopTethering(type); |
| 2726 | } |
| 2727 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 2728 | // Called when we lose the default network and have no replacement yet. |
| 2729 | // This will automatically be cleared after X seconds or a new default network |
| 2730 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 2731 | // first caller and not restarted by subsequent callers. |
| 2732 | private void requestNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2733 | int serialNum = 0; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2734 | synchronized (this) { |
| 2735 | if (mNetTransitionWakeLock.isHeld()) return; |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2736 | serialNum = ++mNetTransitionWakeLockSerialNumber; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2737 | mNetTransitionWakeLock.acquire(); |
| 2738 | mNetTransitionWakeLockCausedBy = forWhom; |
| 2739 | } |
| 2740 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 2741 | EVENT_EXPIRE_NET_TRANSITION_WAKELOCK, serialNum, 0), |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2742 | mNetTransitionWakeLockTimeout); |
| 2743 | return; |
| 2744 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 2745 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2746 | // 100 percent is full good, 0 is full bad. |
| 2747 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 2748 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Lorenzo Colitti | 0831f66 | 2015-02-11 07:39:20 +0900 | [diff] [blame] | 2749 | if (nai == null) return; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2750 | reportNetworkConnectivity(nai.network, percentage > 50); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 2751 | } |
| 2752 | |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2753 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2754 | enforceAccessPermission(); |
| 2755 | enforceInternetPermission(); |
| 2756 | |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2757 | NetworkAgentInfo nai; |
| 2758 | if (network == null) { |
| 2759 | nai = getDefaultNetwork(); |
| 2760 | } else { |
| 2761 | nai = getNetworkAgentInfoForNetwork(network); |
| 2762 | } |
Paul Jensen | 4e8050e | 2015-06-25 10:28:34 -0400 | [diff] [blame] | 2763 | if (nai == null || nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTING || |
| 2764 | nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTED) { |
| 2765 | return; |
| 2766 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2767 | // Revalidate if the app report does not match our current validated state. |
| 2768 | if (hasConnectivity == nai.lastValidated) return; |
| 2769 | final int uid = Binder.getCallingUid(); |
| 2770 | if (DBG) { |
| 2771 | log("reportNetworkConnectivity(" + nai.network.netId + ", " + hasConnectivity + |
| 2772 | ") by " + uid); |
| 2773 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2774 | synchronized (nai) { |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 2775 | // Validating an uncreated network could result in a call to rematchNetworkAndRequests() |
| 2776 | // which isn't meant to work on uncreated networks. |
| 2777 | if (!nai.created) return; |
| 2778 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2779 | if (isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid)) return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 2780 | |
| 2781 | nai.networkMonitor.sendMessage(NetworkMonitor.CMD_FORCE_REEVALUATION, uid); |
| 2782 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2783 | } |
| 2784 | |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2785 | private ProxyInfo getDefaultProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2786 | // this information is already available as a world read/writable jvm property |
| 2787 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2788 | // of proxy info to all the JVMs. |
| 2789 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2790 | synchronized (mProxyLock) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2791 | ProxyInfo ret = mGlobalProxy; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2792 | if ((ret == null) && !mDefaultProxyDisabled) ret = mDefaultProxy; |
| 2793 | return ret; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2794 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 2797 | public ProxyInfo getProxyForNetwork(Network network) { |
| 2798 | if (network == null) return getDefaultProxy(); |
| 2799 | final ProxyInfo globalProxy = getGlobalProxy(); |
| 2800 | if (globalProxy != null) return globalProxy; |
| 2801 | if (!NetworkUtils.queryUserAccess(Binder.getCallingUid(), network.netId)) return null; |
| 2802 | // Don't call getLinkProperties() as it requires ACCESS_NETWORK_STATE permission, which |
| 2803 | // caller may not have. |
| 2804 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 2805 | if (nai == null) return null; |
| 2806 | synchronized (nai) { |
| 2807 | final ProxyInfo proxyInfo = nai.linkProperties.getHttpProxy(); |
| 2808 | if (proxyInfo == null) return null; |
| 2809 | return new ProxyInfo(proxyInfo); |
| 2810 | } |
| 2811 | } |
| 2812 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2813 | // Convert empty ProxyInfo's to null as null-checks are used to determine if proxies are present |
| 2814 | // (e.g. if mGlobalProxy==null fall back to network-specific proxy, if network-specific |
| 2815 | // proxy is null then there is no proxy in place). |
| 2816 | private ProxyInfo canonicalizeProxyInfo(ProxyInfo proxy) { |
| 2817 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
| 2818 | && (proxy.getPacFileUrl() == null || Uri.EMPTY.equals(proxy.getPacFileUrl()))) { |
| 2819 | proxy = null; |
| 2820 | } |
| 2821 | return proxy; |
| 2822 | } |
| 2823 | |
| 2824 | // ProxyInfo equality function with a couple modifications over ProxyInfo.equals() to make it |
| 2825 | // better for determining if a new proxy broadcast is necessary: |
| 2826 | // 1. Canonicalize empty ProxyInfos to null so an empty proxy compares equal to null so as to |
| 2827 | // avoid unnecessary broadcasts. |
| 2828 | // 2. Make sure all parts of the ProxyInfo's compare true, including the host when a PAC URL |
| 2829 | // is in place. This is important so legacy PAC resolver (see com.android.proxyhandler) |
| 2830 | // changes aren't missed. The legacy PAC resolver pretends to be a simple HTTP proxy but |
| 2831 | // actually uses the PAC to resolve; this results in ProxyInfo's with PAC URL, host and port |
| 2832 | // all set. |
| 2833 | private boolean proxyInfoEqual(ProxyInfo a, ProxyInfo b) { |
| 2834 | a = canonicalizeProxyInfo(a); |
| 2835 | b = canonicalizeProxyInfo(b); |
| 2836 | // ProxyInfo.equals() doesn't check hosts when PAC URLs are present, but we need to check |
| 2837 | // hosts even when PAC URLs are present to account for the legacy PAC resolver. |
| 2838 | return Objects.equals(a, b) && (a == null || Objects.equals(a.getHost(), b.getHost())); |
| 2839 | } |
| 2840 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2841 | public void setGlobalProxy(ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2842 | enforceConnectivityInternalPermission(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2843 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2844 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2845 | if (proxyProperties == mGlobalProxy) return; |
| 2846 | if (proxyProperties != null && proxyProperties.equals(mGlobalProxy)) return; |
| 2847 | if (mGlobalProxy != null && mGlobalProxy.equals(proxyProperties)) return; |
| 2848 | |
| 2849 | String host = ""; |
| 2850 | int port = 0; |
| 2851 | String exclList = ""; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2852 | String pacFileUrl = ""; |
| 2853 | if (proxyProperties != null && (!TextUtils.isEmpty(proxyProperties.getHost()) || |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2854 | !Uri.EMPTY.equals(proxyProperties.getPacFileUrl()))) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2855 | if (!proxyProperties.isValid()) { |
| 2856 | if (DBG) |
| 2857 | log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2858 | return; |
| 2859 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2860 | mGlobalProxy = new ProxyInfo(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2861 | host = mGlobalProxy.getHost(); |
| 2862 | port = mGlobalProxy.getPort(); |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2863 | exclList = mGlobalProxy.getExclusionListAsString(); |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2864 | if (!Uri.EMPTY.equals(proxyProperties.getPacFileUrl())) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2865 | pacFileUrl = proxyProperties.getPacFileUrl().toString(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2866 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2867 | } else { |
| 2868 | mGlobalProxy = null; |
| 2869 | } |
| 2870 | ContentResolver res = mContext.getContentResolver(); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2871 | final long token = Binder.clearCallingIdentity(); |
| 2872 | try { |
| 2873 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST, host); |
| 2874 | Settings.Global.putInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, port); |
| 2875 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST, |
| 2876 | exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2877 | Settings.Global.putString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC, pacFileUrl); |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 2878 | } finally { |
| 2879 | Binder.restoreCallingIdentity(token); |
| 2880 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2881 | |
Jason Monk | cf0f97a | 2014-10-02 15:39:38 -0400 | [diff] [blame] | 2882 | if (mGlobalProxy == null) { |
| 2883 | proxyProperties = mDefaultProxy; |
| 2884 | } |
| 2885 | sendProxyBroadcast(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2886 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2887 | } |
| 2888 | |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2889 | private void loadGlobalProxy() { |
| 2890 | ContentResolver res = mContext.getContentResolver(); |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2891 | String host = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_HOST); |
| 2892 | int port = Settings.Global.getInt(res, Settings.Global.GLOBAL_HTTP_PROXY_PORT, 0); |
| 2893 | String exclList = Settings.Global.getString(res, |
| 2894 | Settings.Global.GLOBAL_HTTP_PROXY_EXCLUSION_LIST); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2895 | String pacFileUrl = Settings.Global.getString(res, Settings.Global.GLOBAL_HTTP_PROXY_PAC); |
| 2896 | if (!TextUtils.isEmpty(host) || !TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2897 | ProxyInfo proxyProperties; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2898 | if (!TextUtils.isEmpty(pacFileUrl)) { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2899 | proxyProperties = new ProxyInfo(pacFileUrl); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2900 | } else { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2901 | proxyProperties = new ProxyInfo(host, port, exclList); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2902 | } |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2903 | if (!proxyProperties.isValid()) { |
| 2904 | if (DBG) log("Invalid proxy properties, ignoring: " + proxyProperties.toString()); |
| 2905 | return; |
| 2906 | } |
| 2907 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2908 | synchronized (mProxyLock) { |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 2909 | mGlobalProxy = proxyProperties; |
| 2910 | } |
| 2911 | } |
| 2912 | } |
| 2913 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2914 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | e436e4f | 2013-02-22 14:57:00 -0800 | [diff] [blame] | 2915 | // this information is already available as a world read/writable jvm property |
| 2916 | // so this API change wouldn't have a benifit. It also breaks the passing |
| 2917 | // of proxy info to all the JVMs. |
| 2918 | // enforceAccessPermission(); |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2919 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2920 | return mGlobalProxy; |
| 2921 | } |
| 2922 | } |
| 2923 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2924 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 2925 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2926 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2927 | proxy = null; |
| 2928 | } |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2929 | synchronized (mProxyLock) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2930 | if (mDefaultProxy != null && mDefaultProxy.equals(proxy)) return; |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2931 | if (mDefaultProxy == proxy) return; // catches repeated nulls |
Robert Greenwalt | a8dae99 | 2013-11-18 09:43:59 -0800 | [diff] [blame] | 2932 | if (proxy != null && !proxy.isValid()) { |
Raj Mamadgi | 92d02491 | 2013-11-11 13:52:58 -0800 | [diff] [blame] | 2933 | if (DBG) log("Invalid proxy properties, ignoring: " + proxy.toString()); |
| 2934 | return; |
| 2935 | } |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 2936 | |
| 2937 | // This call could be coming from the PacManager, containing the port of the local |
| 2938 | // proxy. If this new proxy matches the global proxy then copy this proxy to the |
| 2939 | // global (to get the correct local port), and send a broadcast. |
| 2940 | // TODO: Switch PacManager to have its own message to send back rather than |
| 2941 | // reusing EVENT_HAS_CHANGED_PROXY and this call to handleApplyDefaultProxy. |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 2942 | if ((mGlobalProxy != null) && (proxy != null) |
| 2943 | && (!Uri.EMPTY.equals(proxy.getPacFileUrl())) |
Jason Monk | 56cf1ab | 2014-04-28 14:57:27 -0400 | [diff] [blame] | 2944 | && proxy.getPacFileUrl().equals(mGlobalProxy.getPacFileUrl())) { |
| 2945 | mGlobalProxy = proxy; |
| 2946 | sendProxyBroadcast(mGlobalProxy); |
| 2947 | return; |
| 2948 | } |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2949 | mDefaultProxy = proxy; |
| 2950 | |
Robert Greenwalt | 00e8d4c | 2013-04-05 17:14:19 -0700 | [diff] [blame] | 2951 | if (mGlobalProxy != null) return; |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 2952 | if (!mDefaultProxyDisabled) { |
| 2953 | sendProxyBroadcast(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2954 | } |
| 2955 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2958 | // If the proxy has changed from oldLp to newLp, resend proxy broadcast with default proxy. |
| 2959 | // This method gets called when any network changes proxy, but the broadcast only ever contains |
| 2960 | // the default proxy (even if it hasn't changed). |
| 2961 | // TODO: Deprecate the broadcast extras as they aren't necessarily applicable in a multi-network |
| 2962 | // world where an app might be bound to a non-default network. |
| 2963 | private void updateProxy(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { |
| 2964 | ProxyInfo newProxyInfo = newLp == null ? null : newLp.getHttpProxy(); |
| 2965 | ProxyInfo oldProxyInfo = oldLp == null ? null : oldLp.getHttpProxy(); |
| 2966 | |
| 2967 | if (!proxyInfoEqual(newProxyInfo, oldProxyInfo)) { |
| 2968 | sendProxyBroadcast(getDefaultProxy()); |
| 2969 | } |
| 2970 | } |
| 2971 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2972 | private void handleDeprecatedGlobalHttpProxy() { |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 2973 | String proxy = Settings.Global.getString(mContext.getContentResolver(), |
| 2974 | Settings.Global.HTTP_PROXY); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2975 | if (!TextUtils.isEmpty(proxy)) { |
| 2976 | String data[] = proxy.split(":"); |
Andreas Huber | 7b8e1ea | 2013-05-28 15:17:37 -0700 | [diff] [blame] | 2977 | if (data.length == 0) { |
| 2978 | return; |
| 2979 | } |
| 2980 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2981 | String proxyHost = data[0]; |
| 2982 | int proxyPort = 8080; |
| 2983 | if (data.length > 1) { |
| 2984 | try { |
| 2985 | proxyPort = Integer.parseInt(data[1]); |
| 2986 | } catch (NumberFormatException e) { |
| 2987 | return; |
| 2988 | } |
| 2989 | } |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2990 | ProxyInfo p = new ProxyInfo(data[0], proxyPort, ""); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2991 | setGlobalProxy(p); |
| 2992 | } |
| 2993 | } |
| 2994 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2995 | private void sendProxyBroadcast(ProxyInfo proxy) { |
| 2996 | if (proxy == null) proxy = new ProxyInfo("", 0, ""); |
Jason Monk | 6f8a68f | 2013-08-23 19:21:25 -0400 | [diff] [blame] | 2997 | if (mPacManager.setCurrentProxyScriptUrl(proxy)) return; |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 2998 | if (DBG) log("sending Proxy Broadcast for " + proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2999 | Intent intent = new Intent(Proxy.PROXY_CHANGE_ACTION); |
Stan Chesnutt | b35d67a | 2011-01-06 11:00:19 -0800 | [diff] [blame] | 3000 | intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING | |
| 3001 | Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3002 | intent.putExtra(Proxy.EXTRA_PROXY_INFO, proxy); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 3003 | final long ident = Binder.clearCallingIdentity(); |
| 3004 | try { |
| 3005 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 3006 | } finally { |
| 3007 | Binder.restoreCallingIdentity(ident); |
| 3008 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | private static class SettingsObserver extends ContentObserver { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3012 | final private HashMap<Uri, Integer> mUriEventMap; |
| 3013 | final private Context mContext; |
| 3014 | final private Handler mHandler; |
| 3015 | |
| 3016 | SettingsObserver(Context context, Handler handler) { |
| 3017 | super(null); |
| 3018 | mUriEventMap = new HashMap<Uri, Integer>(); |
| 3019 | mContext = context; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3020 | mHandler = handler; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3021 | } |
| 3022 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3023 | void observe(Uri uri, int what) { |
| 3024 | mUriEventMap.put(uri, what); |
| 3025 | final ContentResolver resolver = mContext.getContentResolver(); |
| 3026 | resolver.registerContentObserver(uri, false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | @Override |
| 3030 | public void onChange(boolean selfChange) { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3031 | Slog.wtf(TAG, "Should never be reached."); |
| 3032 | } |
| 3033 | |
| 3034 | @Override |
| 3035 | public void onChange(boolean selfChange, Uri uri) { |
| 3036 | final Integer what = mUriEventMap.get(uri); |
| 3037 | if (what != null) { |
| 3038 | mHandler.obtainMessage(what.intValue()).sendToTarget(); |
| 3039 | } else { |
| 3040 | loge("No matching event to send for URI=" + uri); |
| 3041 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3042 | } |
| 3043 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3044 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3045 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3046 | Slog.d(TAG, s); |
| 3047 | } |
| 3048 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3049 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3050 | Slog.e(TAG, s); |
| 3051 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3052 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 3053 | private static <T> T checkNotNull(T value, String message) { |
| 3054 | if (value == null) { |
| 3055 | throw new NullPointerException(message); |
| 3056 | } |
| 3057 | return value; |
| 3058 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3059 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3060 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 3061 | * Prepare for a VPN application. |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3062 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 3063 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 3064 | * |
| 3065 | * @param oldPackage Package name of the application which currently controls VPN, which will |
| 3066 | * be replaced. If there is no such application, this should should either be |
| 3067 | * {@code null} or {@link VpnConfig.LEGACY_VPN}. |
| 3068 | * @param newPackage Package name of the application which should gain control of VPN, or |
| 3069 | * {@code null} to disable. |
| 3070 | * @param userId User for whom to prepare the new VPN. |
| 3071 | * |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3072 | * @hide |
| 3073 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3074 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3075 | public boolean prepareVpn(@Nullable String oldPackage, @Nullable String newPackage, |
| 3076 | int userId) { |
| 3077 | enforceCrossUserPermission(userId); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3078 | throwIfLockdownEnabled(); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3079 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3080 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3081 | Vpn vpn = mVpns.get(userId); |
| 3082 | if (vpn != null) { |
| 3083 | return vpn.prepare(oldPackage, newPackage); |
| 3084 | } else { |
| 3085 | return false; |
| 3086 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3087 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3088 | } |
| 3089 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3090 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3091 | * Set whether the VPN package has the ability to launch VPNs without user intervention. |
| 3092 | * This method is used by system-privileged apps. |
| 3093 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 3094 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 3095 | * |
| 3096 | * @param packageName The package for which authorization state should change. |
| 3097 | * @param userId User for whom {@code packageName} is installed. |
| 3098 | * @param authorized {@code true} if this app should be able to start a VPN connection without |
| 3099 | * explicit user approval, {@code false} if not. |
| 3100 | * |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3101 | * @hide |
| 3102 | */ |
| 3103 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3104 | public void setVpnPackageAuthorization(String packageName, int userId, boolean authorized) { |
| 3105 | enforceCrossUserPermission(userId); |
| 3106 | |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3107 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3108 | Vpn vpn = mVpns.get(userId); |
| 3109 | if (vpn != null) { |
| 3110 | vpn.setPackageAuthorization(packageName, authorized); |
| 3111 | } |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 3112 | } |
| 3113 | } |
| 3114 | |
| 3115 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3116 | * Configure a TUN interface and return its file descriptor. Parameters |
| 3117 | * 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] | 3118 | * and not available in ConnectivityManager. Permissions are checked in |
| 3119 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3120 | * @hide |
| 3121 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3122 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3123 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3124 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3125 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3126 | synchronized(mVpns) { |
| 3127 | return mVpns.get(user).establish(config); |
| 3128 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3129 | } |
| 3130 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3131 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3132 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 3133 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3134 | */ |
| 3135 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3136 | public void startLegacyVpn(VpnProfile profile) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3137 | throwIfLockdownEnabled(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 3138 | final LinkProperties egress = getActiveLinkProperties(); |
| 3139 | if (egress == null) { |
| 3140 | throw new IllegalStateException("Missing active network connection"); |
| 3141 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3142 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3143 | synchronized(mVpns) { |
| 3144 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 3145 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
| 3148 | /** |
| 3149 | * Return the information of the ongoing legacy VPN. This method is used |
| 3150 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 3151 | * are checked in Vpn class. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 3152 | */ |
| 3153 | @Override |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 3154 | public LegacyVpnInfo getLegacyVpnInfo(int userId) { |
| 3155 | enforceCrossUserPermission(userId); |
Hung-ying Tyan | 44c8c5c | 2015-07-29 12:39:21 +0800 | [diff] [blame] | 3156 | if (mLockdownEnabled) { |
| 3157 | return null; |
| 3158 | } |
| 3159 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3160 | synchronized(mVpns) { |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 3161 | return mVpns.get(userId).getLegacyVpnInfo(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3162 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 3163 | } |
| 3164 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3165 | /** |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 3166 | * Return the information of all ongoing VPNs. This method is used by NetworkStatsService |
| 3167 | * and not available in ConnectivityManager. |
| 3168 | */ |
| 3169 | @Override |
| 3170 | public VpnInfo[] getAllVpnInfo() { |
| 3171 | enforceConnectivityInternalPermission(); |
| 3172 | if (mLockdownEnabled) { |
| 3173 | return new VpnInfo[0]; |
| 3174 | } |
| 3175 | |
| 3176 | synchronized(mVpns) { |
| 3177 | List<VpnInfo> infoList = new ArrayList<>(); |
| 3178 | for (int i = 0; i < mVpns.size(); i++) { |
| 3179 | VpnInfo info = createVpnInfo(mVpns.valueAt(i)); |
| 3180 | if (info != null) { |
| 3181 | infoList.add(info); |
| 3182 | } |
| 3183 | } |
| 3184 | return infoList.toArray(new VpnInfo[infoList.size()]); |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | /** |
| 3189 | * @return VPN information for accounting, or null if we can't retrieve all required |
| 3190 | * information, e.g primary underlying iface. |
| 3191 | */ |
| 3192 | @Nullable |
| 3193 | private VpnInfo createVpnInfo(Vpn vpn) { |
| 3194 | VpnInfo info = vpn.getVpnInfo(); |
| 3195 | if (info == null) { |
| 3196 | return null; |
| 3197 | } |
| 3198 | Network[] underlyingNetworks = vpn.getUnderlyingNetworks(); |
| 3199 | // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret |
| 3200 | // the underlyingNetworks list. |
| 3201 | if (underlyingNetworks == null) { |
| 3202 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 3203 | if (defaultNetwork != null && defaultNetwork.linkProperties != null) { |
| 3204 | info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName(); |
| 3205 | } |
| 3206 | } else if (underlyingNetworks.length > 0) { |
| 3207 | LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]); |
| 3208 | if (linkProperties != null) { |
| 3209 | info.primaryUnderlyingIface = linkProperties.getInterfaceName(); |
| 3210 | } |
| 3211 | } |
| 3212 | return info.primaryUnderlyingIface == null ? null : info; |
| 3213 | } |
| 3214 | |
| 3215 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3216 | * Returns the information of the ongoing VPN for {@code userId}. This method is used by |
| 3217 | * VpnDialogs and not available in ConnectivityManager. |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3218 | * Permissions are checked in Vpn class. |
| 3219 | * @hide |
| 3220 | */ |
| 3221 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3222 | public VpnConfig getVpnConfig(int userId) { |
| 3223 | enforceCrossUserPermission(userId); |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3224 | synchronized(mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 3225 | Vpn vpn = mVpns.get(userId); |
| 3226 | if (vpn != null) { |
| 3227 | return vpn.getVpnConfig(); |
| 3228 | } else { |
| 3229 | return null; |
| 3230 | } |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 3231 | } |
| 3232 | } |
| 3233 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3234 | @Override |
| 3235 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 3236 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 3237 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 3238 | return false; |
| 3239 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3240 | |
| 3241 | // Tear down existing lockdown if profile was removed |
| 3242 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 3243 | if (mLockdownEnabled) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3244 | final String profileName = new String(mKeyStore.get(Credentials.LOCKDOWN_VPN)); |
| 3245 | final VpnProfile profile = VpnProfile.decode( |
| 3246 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
Lorenzo Colitti | 9b23f88 | 2015-10-13 15:21:21 +0900 | [diff] [blame] | 3247 | if (profile == null) { |
| 3248 | Slog.e(TAG, "Lockdown VPN configured invalid profile " + profileName); |
| 3249 | setLockdownTracker(null); |
| 3250 | return true; |
| 3251 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3252 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 3253 | synchronized(mVpns) { |
Robin Lee | 18566c1 | 2016-03-14 13:08:48 +0000 | [diff] [blame] | 3254 | Vpn vpn = mVpns.get(user); |
| 3255 | if (vpn == null) { |
| 3256 | Slog.w(TAG, "VPN for user " + user + " not ready yet. Skipping lockdown"); |
| 3257 | return false; |
| 3258 | } |
| 3259 | setLockdownTracker(new LockdownVpnTracker(mContext, mNetd, this, vpn, profile)); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3260 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3261 | } else { |
| 3262 | setLockdownTracker(null); |
| 3263 | } |
| 3264 | |
| 3265 | return true; |
| 3266 | } |
| 3267 | |
| 3268 | /** |
| 3269 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 3270 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 3271 | */ |
| 3272 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 3273 | // Shutdown any existing tracker |
| 3274 | final LockdownVpnTracker existing = mLockdownTracker; |
| 3275 | mLockdownTracker = null; |
| 3276 | if (existing != null) { |
| 3277 | existing.shutdown(); |
| 3278 | } |
| 3279 | |
| 3280 | try { |
| 3281 | if (tracker != null) { |
| 3282 | mNetd.setFirewallEnabled(true); |
Jeff Sharkey | 812085b | 2013-02-28 16:57:58 -0800 | [diff] [blame] | 3283 | mNetd.setFirewallInterfaceRule("lo", true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3284 | mLockdownTracker = tracker; |
| 3285 | mLockdownTracker.init(); |
| 3286 | } else { |
| 3287 | mNetd.setFirewallEnabled(false); |
| 3288 | } |
| 3289 | } catch (RemoteException e) { |
| 3290 | // ignored; NMS lives inside system_server |
| 3291 | } |
| 3292 | } |
| 3293 | |
| 3294 | private void throwIfLockdownEnabled() { |
| 3295 | if (mLockdownEnabled) { |
| 3296 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 3297 | } |
| 3298 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 3299 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 3300 | /** |
| 3301 | * Sets up or tears down the always-on VPN for user {@param user} as appropriate. |
| 3302 | * |
| 3303 | * @return {@code false} in case of errors; {@code true} otherwise. |
| 3304 | */ |
| 3305 | private boolean updateAlwaysOnVpn(int user) { |
| 3306 | final String lockdownPackage = getAlwaysOnVpnPackage(user); |
| 3307 | if (lockdownPackage == null) { |
| 3308 | return true; |
| 3309 | } |
| 3310 | |
| 3311 | // Create an intent to start the VPN service declared in the app's manifest. |
| 3312 | Intent serviceIntent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 3313 | serviceIntent.setPackage(lockdownPackage); |
| 3314 | |
| 3315 | try { |
| 3316 | return mContext.startServiceAsUser(serviceIntent, UserHandle.of(user)) != null; |
| 3317 | } catch (RuntimeException e) { |
| 3318 | return false; |
| 3319 | } |
| 3320 | } |
| 3321 | |
| 3322 | @Override |
| 3323 | public boolean setAlwaysOnVpnPackage(int userId, String packageName) { |
| 3324 | enforceConnectivityInternalPermission(); |
| 3325 | enforceCrossUserPermission(userId); |
| 3326 | |
| 3327 | // Can't set always-on VPN if legacy VPN is already in lockdown mode. |
| 3328 | if (LockdownVpnTracker.isEnabled()) { |
| 3329 | return false; |
| 3330 | } |
| 3331 | |
| 3332 | // If the current VPN package is the same as the new one, this is a no-op |
| 3333 | final String oldPackage = getAlwaysOnVpnPackage(userId); |
| 3334 | if (TextUtils.equals(oldPackage, packageName)) { |
| 3335 | return true; |
| 3336 | } |
| 3337 | |
| 3338 | synchronized (mVpns) { |
| 3339 | Vpn vpn = mVpns.get(userId); |
| 3340 | if (vpn == null) { |
| 3341 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 3342 | return false; |
| 3343 | } |
| 3344 | if (!vpn.setAlwaysOnPackage(packageName)) { |
| 3345 | return false; |
| 3346 | } |
| 3347 | if (!updateAlwaysOnVpn(userId)) { |
| 3348 | vpn.setAlwaysOnPackage(null); |
| 3349 | return false; |
| 3350 | } |
| 3351 | } |
| 3352 | return true; |
| 3353 | } |
| 3354 | |
| 3355 | @Override |
| 3356 | public String getAlwaysOnVpnPackage(int userId) { |
| 3357 | enforceConnectivityInternalPermission(); |
| 3358 | enforceCrossUserPermission(userId); |
| 3359 | |
| 3360 | synchronized (mVpns) { |
| 3361 | Vpn vpn = mVpns.get(userId); |
| 3362 | if (vpn == null) { |
| 3363 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 3364 | return null; |
| 3365 | } |
| 3366 | return vpn.getAlwaysOnPackage(); |
| 3367 | } |
| 3368 | } |
| 3369 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3370 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3371 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3372 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 3373 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3374 | } |
| 3375 | |
| 3376 | private static final String NOTIFICATION_ID = "CaptivePortal.Notification"; |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3377 | private static enum NotificationType { SIGN_IN, NO_INTERNET; }; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3378 | |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3379 | private void setProvNotificationVisible(boolean visible, int networkType, String action) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3380 | Intent intent = new Intent(action); |
| 3381 | PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3382 | // Concatenate the range of types onto the range of NetIDs. |
| 3383 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3384 | setProvNotificationVisibleIntent(visible, id, NotificationType.SIGN_IN, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3385 | networkType, null, pendingIntent, false); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3386 | } |
| 3387 | |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3388 | /** |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3389 | * Show or hide network provisioning notifications. |
| 3390 | * |
| 3391 | * We use notifications for two purposes: to notify that a network requires sign in |
| 3392 | * (NotificationType.SIGN_IN), or to notify that a network does not have Internet access |
| 3393 | * (NotificationType.NO_INTERNET). We display at most one notification per ID, so on a |
| 3394 | * particular network we can display the notification type that was most recently requested. |
| 3395 | * So for example if a captive portal fails to reply within a few seconds of connecting, we |
| 3396 | * might first display NO_INTERNET, and then when the captive portal check completes, display |
| 3397 | * SIGN_IN. |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3398 | * |
| 3399 | * @param id an identifier that uniquely identifies this notification. This must match |
| 3400 | * between show and hide calls. We use the NetID value but for legacy callers |
| 3401 | * we concatenate the range of types with the range of NetIDs. |
| 3402 | */ |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3403 | private void setProvNotificationVisibleIntent(boolean visible, int id, |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3404 | NotificationType notifyType, int networkType, String extraInfo, PendingIntent intent, |
| 3405 | boolean highPriority) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3406 | if (VDBG || (DBG && visible)) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3407 | log("setProvNotificationVisibleIntent " + notifyType + " visible=" + visible |
| 3408 | + " networkType=" + getNetworkTypeName(networkType) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3409 | + " extraInfo=" + extraInfo + " highPriority=" + highPriority); |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 3410 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3411 | |
| 3412 | Resources r = Resources.getSystem(); |
| 3413 | NotificationManager notificationManager = (NotificationManager) mContext |
| 3414 | .getSystemService(Context.NOTIFICATION_SERVICE); |
| 3415 | |
| 3416 | if (visible) { |
| 3417 | CharSequence title; |
| 3418 | CharSequence details; |
| 3419 | int icon; |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3420 | if (notifyType == NotificationType.NO_INTERNET && |
| 3421 | networkType == ConnectivityManager.TYPE_WIFI) { |
| 3422 | title = r.getString(R.string.wifi_no_internet, 0); |
| 3423 | details = r.getString(R.string.wifi_no_internet_detailed); |
| 3424 | icon = R.drawable.stat_notify_wifi_in_range; // TODO: Need new icon. |
| 3425 | } else if (notifyType == NotificationType.SIGN_IN) { |
| 3426 | switch (networkType) { |
| 3427 | case ConnectivityManager.TYPE_WIFI: |
| 3428 | title = r.getString(R.string.wifi_available_sign_in, 0); |
| 3429 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 3430 | extraInfo); |
| 3431 | icon = R.drawable.stat_notify_wifi_in_range; |
| 3432 | break; |
| 3433 | case ConnectivityManager.TYPE_MOBILE: |
| 3434 | case ConnectivityManager.TYPE_MOBILE_HIPRI: |
| 3435 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3436 | // TODO: Change this to pull from NetworkInfo once a printable |
| 3437 | // name has been added to it |
| 3438 | details = mTelephonyManager.getNetworkOperatorName(); |
| 3439 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3440 | break; |
| 3441 | default: |
| 3442 | title = r.getString(R.string.network_available_sign_in, 0); |
| 3443 | details = r.getString(R.string.network_available_sign_in_detailed, |
| 3444 | extraInfo); |
| 3445 | icon = R.drawable.stat_notify_rssi_in_range; |
| 3446 | break; |
| 3447 | } |
| 3448 | } else { |
| 3449 | Slog.wtf(TAG, "Unknown notification type " + notifyType + "on network type " |
| 3450 | + getNetworkTypeName(networkType)); |
| 3451 | return; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3452 | } |
| 3453 | |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3454 | Notification notification = new Notification.Builder(mContext) |
| 3455 | .setWhen(0) |
| 3456 | .setSmallIcon(icon) |
| 3457 | .setAutoCancel(true) |
| 3458 | .setTicker(title) |
| 3459 | .setColor(mContext.getColor( |
| 3460 | com.android.internal.R.color.system_notification_accent_color)) |
| 3461 | .setContentTitle(title) |
| 3462 | .setContentText(details) |
| 3463 | .setContentIntent(intent) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3464 | .setLocalOnly(true) |
| 3465 | .setPriority(highPriority ? |
| 3466 | Notification.PRIORITY_HIGH : |
| 3467 | Notification.PRIORITY_DEFAULT) |
Lorenzo Colitti | 46d50b7 | 2015-10-21 09:15:41 +0900 | [diff] [blame] | 3468 | .setDefaults(highPriority ? Notification.DEFAULT_ALL : 0) |
Lorenzo Colitti | 9ce8a9d | 2015-06-24 17:13:08 +0900 | [diff] [blame] | 3469 | .setOnlyAlertOnce(true) |
Chris Wren | 1ce4b6d | 2015-06-11 10:19:43 -0400 | [diff] [blame] | 3470 | .build(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3471 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3472 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3473 | notificationManager.notify(NOTIFICATION_ID, id, notification); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3474 | } catch (NullPointerException npe) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3475 | loge("setNotificationVisible: visible notificationManager npe=" + npe); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3476 | npe.printStackTrace(); |
| 3477 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3478 | } else { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3479 | try { |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 3480 | notificationManager.cancel(NOTIFICATION_ID, id); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3481 | } catch (NullPointerException npe) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3482 | loge("setNotificationVisible: cancel notificationManager npe=" + npe); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3483 | npe.printStackTrace(); |
| 3484 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3485 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3486 | } |
| 3487 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3488 | /** Location to an updatable file listing carrier provisioning urls. |
| 3489 | * An example: |
| 3490 | * |
| 3491 | * <?xml version="1.0" encoding="utf-8"?> |
| 3492 | * <provisioningUrls> |
| 3493 | * <provisioningUrl mcc="310" mnc="4">http://myserver.com/foo?mdn=%3$s&iccid=%1$s&imei=%2$s</provisioningUrl> |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3494 | * </provisioningUrls> |
| 3495 | */ |
| 3496 | private static final String PROVISIONING_URL_PATH = |
| 3497 | "/data/misc/radio/provisioning_urls.xml"; |
| 3498 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3499 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3500 | /** XML tag for root element. */ |
| 3501 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 3502 | /** XML tag for individual url */ |
| 3503 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3504 | /** XML attribute for mcc */ |
| 3505 | private static final String ATTR_MCC = "mcc"; |
| 3506 | /** XML attribute for mnc */ |
| 3507 | private static final String ATTR_MNC = "mnc"; |
| 3508 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3509 | private String getProvisioningUrlBaseFromFile() { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3510 | FileReader fileReader = null; |
| 3511 | XmlPullParser parser = null; |
| 3512 | Configuration config = mContext.getResources().getConfiguration(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3513 | |
| 3514 | try { |
| 3515 | fileReader = new FileReader(mProvisioningUrlFile); |
| 3516 | parser = Xml.newPullParser(); |
| 3517 | parser.setInput(fileReader); |
| 3518 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 3519 | |
| 3520 | while (true) { |
| 3521 | XmlUtils.nextElement(parser); |
| 3522 | |
| 3523 | String element = parser.getName(); |
| 3524 | if (element == null) break; |
| 3525 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3526 | if (element.equals(TAG_PROVISIONING_URL)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3527 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 3528 | try { |
| 3529 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 3530 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 3531 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 3532 | parser.next(); |
| 3533 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 3534 | return parser.getText(); |
| 3535 | } |
| 3536 | } |
| 3537 | } |
| 3538 | } catch (NumberFormatException e) { |
| 3539 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 3540 | } |
| 3541 | } |
| 3542 | } |
| 3543 | return null; |
| 3544 | } catch (FileNotFoundException e) { |
| 3545 | loge("Carrier Provisioning Urls file not found"); |
| 3546 | } catch (XmlPullParserException e) { |
| 3547 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 3548 | } catch (IOException e) { |
| 3549 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 3550 | } finally { |
| 3551 | if (fileReader != null) { |
| 3552 | try { |
| 3553 | fileReader.close(); |
| 3554 | } catch (IOException e) {} |
| 3555 | } |
| 3556 | } |
| 3557 | return null; |
| 3558 | } |
| 3559 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3560 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3561 | public String getMobileProvisioningUrl() { |
| 3562 | enforceConnectivityInternalPermission(); |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 3563 | String url = getProvisioningUrlBaseFromFile(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3564 | if (TextUtils.isEmpty(url)) { |
| 3565 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3566 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3567 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3568 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3569 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3570 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3571 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3572 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 3573 | if (TextUtils.isEmpty(phoneNumber)) { |
| 3574 | phoneNumber = "0000000000"; |
| 3575 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3576 | url = String.format(url, |
| 3577 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 3578 | mTelephonyManager.getDeviceId() /* IMEI */, |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 3579 | phoneNumber /* Phone numer */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3580 | } |
| 3581 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3582 | return url; |
| 3583 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3584 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3585 | @Override |
| 3586 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3587 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3588 | enforceConnectivityInternalPermission(); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3589 | final long ident = Binder.clearCallingIdentity(); |
| 3590 | try { |
| 3591 | setProvNotificationVisible(visible, networkType, action); |
| 3592 | } finally { |
| 3593 | Binder.restoreCallingIdentity(ident); |
| 3594 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3595 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 3596 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3597 | @Override |
| 3598 | public void setAirplaneMode(boolean enable) { |
| 3599 | enforceConnectivityInternalPermission(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3600 | final long ident = Binder.clearCallingIdentity(); |
| 3601 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 3602 | final ContentResolver cr = mContext.getContentResolver(); |
| 3603 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, enable ? 1 : 0); |
| 3604 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 3605 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 3606 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3607 | } finally { |
| 3608 | Binder.restoreCallingIdentity(ident); |
| 3609 | } |
| 3610 | } |
| 3611 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3612 | private void onUserStart(int userId) { |
| 3613 | synchronized(mVpns) { |
| 3614 | Vpn userVpn = mVpns.get(userId); |
| 3615 | if (userVpn != null) { |
| 3616 | loge("Starting user already has a VPN"); |
| 3617 | return; |
| 3618 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 3619 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNetd, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3620 | mVpns.put(userId, userVpn); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3621 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3622 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 3623 | updateLockdownVpn(); |
| 3624 | } else { |
| 3625 | updateAlwaysOnVpn(userId); |
| 3626 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3627 | } |
| 3628 | |
| 3629 | private void onUserStop(int userId) { |
| 3630 | synchronized(mVpns) { |
| 3631 | Vpn userVpn = mVpns.get(userId); |
| 3632 | if (userVpn == null) { |
| 3633 | loge("Stopping user has no VPN"); |
| 3634 | return; |
| 3635 | } |
| 3636 | mVpns.delete(userId); |
| 3637 | } |
| 3638 | } |
| 3639 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 3640 | private void onUserAdded(int userId) { |
| 3641 | synchronized(mVpns) { |
| 3642 | final int vpnsSize = mVpns.size(); |
| 3643 | for (int i = 0; i < vpnsSize; i++) { |
| 3644 | Vpn vpn = mVpns.valueAt(i); |
| 3645 | vpn.onUserAdded(userId); |
| 3646 | } |
| 3647 | } |
| 3648 | } |
| 3649 | |
| 3650 | private void onUserRemoved(int userId) { |
| 3651 | synchronized(mVpns) { |
| 3652 | final int vpnsSize = mVpns.size(); |
| 3653 | for (int i = 0; i < vpnsSize; i++) { |
| 3654 | Vpn vpn = mVpns.valueAt(i); |
| 3655 | vpn.onUserRemoved(userId); |
| 3656 | } |
| 3657 | } |
| 3658 | } |
| 3659 | |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 3660 | private void onUserUnlocked(int userId) { |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 3661 | // User present may be sent because of an unlock, which might mean an unlocked keystore. |
| 3662 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 3663 | updateLockdownVpn(); |
| 3664 | } else { |
| 3665 | updateAlwaysOnVpn(userId); |
| 3666 | } |
| 3667 | } |
| 3668 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3669 | private BroadcastReceiver mUserIntentReceiver = new BroadcastReceiver() { |
| 3670 | @Override |
| 3671 | public void onReceive(Context context, Intent intent) { |
| 3672 | final String action = intent.getAction(); |
| 3673 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
| 3674 | if (userId == UserHandle.USER_NULL) return; |
| 3675 | |
| 3676 | if (Intent.ACTION_USER_STARTING.equals(action)) { |
| 3677 | onUserStart(userId); |
| 3678 | } else if (Intent.ACTION_USER_STOPPING.equals(action)) { |
| 3679 | onUserStop(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 3680 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 3681 | onUserAdded(userId); |
| 3682 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 3683 | onUserRemoved(userId); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 3684 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 3685 | onUserUnlocked(userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 3686 | } |
| 3687 | } |
| 3688 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 3689 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3690 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = |
| 3691 | new HashMap<Messenger, NetworkFactoryInfo>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3692 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = |
| 3693 | new HashMap<NetworkRequest, NetworkRequestInfo>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3694 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3695 | private static final int MAX_NETWORK_REQUESTS_PER_UID = 100; |
| 3696 | // Map from UID to number of NetworkRequests that UID has filed. |
| 3697 | @GuardedBy("mUidToNetworkRequestCount") |
| 3698 | private final SparseIntArray mUidToNetworkRequestCount = new SparseIntArray(); |
| 3699 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3700 | private static class NetworkFactoryInfo { |
| 3701 | public final String name; |
| 3702 | public final Messenger messenger; |
| 3703 | public final AsyncChannel asyncChannel; |
| 3704 | |
| 3705 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 3706 | this.name = name; |
| 3707 | this.messenger = messenger; |
| 3708 | this.asyncChannel = asyncChannel; |
| 3709 | } |
| 3710 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3711 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3712 | /** |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3713 | * A NetworkRequest as registered by an application can be one of three |
| 3714 | * types: |
| 3715 | * |
| 3716 | * - "listen", for which the framework will issue callbacks about any |
| 3717 | * and all networks that match the specified NetworkCapabilities, |
| 3718 | * |
| 3719 | * - "request", capable of causing a specific network to be created |
| 3720 | * first (e.g. a telephony DUN request), the framework will issue |
| 3721 | * callbacks about the single, highest scoring current network |
| 3722 | * (if any) that matches the specified NetworkCapabilities, or |
| 3723 | * |
| 3724 | * - "track the default network", a hybrid of the two designed such |
| 3725 | * that the framework will issue callbacks for the single, highest |
| 3726 | * scoring current network (if any) that matches the capabilities of |
| 3727 | * the default Internet request (mDefaultRequest), but which cannot |
| 3728 | * cause the framework to either create or retain the existence of |
| 3729 | * any specific network. |
| 3730 | * |
| 3731 | */ |
| 3732 | private static enum NetworkRequestType { |
| 3733 | LISTEN, |
| 3734 | TRACK_DEFAULT, |
| 3735 | REQUEST |
| 3736 | }; |
| 3737 | |
| 3738 | /** |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3739 | * Tracks info about the requester. |
| 3740 | * Also used to notice when the calling process dies so we can self-expire |
| 3741 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3742 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3743 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3744 | final PendingIntent mPendingIntent; |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 3745 | boolean mPendingIntentSent; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3746 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3747 | final int mPid; |
| 3748 | final int mUid; |
| 3749 | final Messenger messenger; |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3750 | private final NetworkRequestType mType; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3751 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3752 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi, NetworkRequestType type) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3753 | request = r; |
| 3754 | mPendingIntent = pi; |
| 3755 | messenger = null; |
| 3756 | mBinder = null; |
| 3757 | mPid = getCallingPid(); |
| 3758 | mUid = getCallingUid(); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3759 | mType = type; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3760 | enforceRequestCountLimit(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3761 | } |
| 3762 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3763 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder, NetworkRequestType type) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3764 | super(); |
| 3765 | messenger = m; |
| 3766 | request = r; |
| 3767 | mBinder = binder; |
| 3768 | mPid = getCallingPid(); |
| 3769 | mUid = getCallingUid(); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3770 | mType = type; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3771 | mPendingIntent = null; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3772 | enforceRequestCountLimit(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3773 | |
| 3774 | try { |
| 3775 | mBinder.linkToDeath(this, 0); |
| 3776 | } catch (RemoteException e) { |
| 3777 | binderDied(); |
| 3778 | } |
| 3779 | } |
| 3780 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3781 | private void enforceRequestCountLimit() { |
| 3782 | synchronized (mUidToNetworkRequestCount) { |
| 3783 | int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1; |
| 3784 | if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { |
| 3785 | throw new IllegalArgumentException("Too many NetworkRequests filed"); |
| 3786 | } |
| 3787 | mUidToNetworkRequestCount.put(mUid, networkRequests); |
| 3788 | } |
| 3789 | } |
| 3790 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3791 | private String typeString() { |
| 3792 | switch (mType) { |
| 3793 | case LISTEN: return "Listen"; |
| 3794 | case REQUEST: return "Request"; |
| 3795 | case TRACK_DEFAULT: return "Track default"; |
| 3796 | default: |
| 3797 | return "unknown type"; |
| 3798 | } |
| 3799 | } |
| 3800 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3801 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3802 | if (mBinder != null) { |
| 3803 | mBinder.unlinkToDeath(this, 0); |
| 3804 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3805 | } |
| 3806 | |
| 3807 | public void binderDied() { |
| 3808 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 3809 | request + ", " + mBinder + ")"); |
| 3810 | releaseNetworkRequest(request); |
| 3811 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3812 | |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3813 | /** |
| 3814 | * Returns true iff. the contained NetworkRequest is one that: |
| 3815 | * |
| 3816 | * - should be associated with at most one satisfying network |
| 3817 | * at a time; |
| 3818 | * |
| 3819 | * - should cause a network to be kept up if it is the only network |
| 3820 | * which can satisfy the NetworkReqeust. |
| 3821 | * |
| 3822 | * For full detail of how isRequest() is used for pairing Networks with |
| 3823 | * NetworkRequests read rematchNetworkAndRequests(). |
| 3824 | * |
| 3825 | * TODO: Rename to something more properly descriptive. |
| 3826 | */ |
| 3827 | public boolean isRequest() { |
| 3828 | return (mType == NetworkRequestType.TRACK_DEFAULT) || |
| 3829 | (mType == NetworkRequestType.REQUEST); |
| 3830 | } |
| 3831 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3832 | public String toString() { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3833 | return typeString() + |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3834 | " from uid/pid:" + mUid + "/" + mPid + |
| 3835 | " for " + request + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3836 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3837 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3838 | } |
| 3839 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 3840 | private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { |
| 3841 | final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); |
| 3842 | if (badCapability != null) { |
| 3843 | throw new IllegalArgumentException("Cannot request network with " + badCapability); |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3844 | } |
| 3845 | } |
| 3846 | |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3847 | private ArrayList<Integer> getSignalStrengthThresholds(NetworkAgentInfo nai) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3848 | final SortedSet<Integer> thresholds = new TreeSet(); |
| 3849 | synchronized (nai) { |
| 3850 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 3851 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3852 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3853 | thresholds.add(nri.request.networkCapabilities.getSignalStrength()); |
| 3854 | } |
| 3855 | } |
| 3856 | } |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3857 | return new ArrayList<Integer>(thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3858 | } |
| 3859 | |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3860 | private void updateSignalStrengthThresholds( |
| 3861 | NetworkAgentInfo nai, String reason, NetworkRequest request) { |
| 3862 | ArrayList<Integer> thresholdsArray = getSignalStrengthThresholds(nai); |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3863 | Bundle thresholds = new Bundle(); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3864 | thresholds.putIntegerArrayList("thresholds", thresholdsArray); |
| 3865 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3866 | if (VDBG || (DBG && !"CONNECT".equals(reason))) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3867 | String detail; |
| 3868 | if (request != null && request.networkCapabilities.hasSignalStrength()) { |
| 3869 | detail = reason + " " + request.networkCapabilities.getSignalStrength(); |
| 3870 | } else { |
| 3871 | detail = reason; |
| 3872 | } |
| 3873 | log(String.format("updateSignalStrengthThresholds: %s, sending %s to %s", |
| 3874 | detail, Arrays.toString(thresholdsArray.toArray()), nai.name())); |
| 3875 | } |
| 3876 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3877 | nai.asyncChannel.sendMessage( |
| 3878 | android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 3879 | 0, 0, thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3880 | } |
| 3881 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3882 | @Override |
| 3883 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3884 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3885 | final NetworkRequestType type = (networkCapabilities == null) |
| 3886 | ? NetworkRequestType.TRACK_DEFAULT |
| 3887 | : NetworkRequestType.REQUEST; |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3888 | // If the requested networkCapabilities is null, take them instead from |
| 3889 | // the default network request. This allows callers to keep track of |
| 3890 | // the system default network. |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3891 | if (type == NetworkRequestType.TRACK_DEFAULT) { |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3892 | networkCapabilities = new NetworkCapabilities(mDefaultRequest.networkCapabilities); |
| 3893 | enforceAccessPermission(); |
| 3894 | } else { |
| 3895 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 3896 | enforceNetworkRequestPermissions(networkCapabilities); |
| 3897 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3898 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 3899 | ensureRequestableCapabilities(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3900 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3901 | if (timeoutMs < 0 || timeoutMs > ConnectivityManager.MAX_NETWORK_REQUEST_TIMEOUT_MS) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3902 | throw new IllegalArgumentException("Bad timeout specified"); |
| 3903 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3904 | |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 3905 | if (NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER |
| 3906 | .equals(networkCapabilities.getNetworkSpecifier())) { |
| 3907 | throw new IllegalArgumentException("Invalid network specifier - must not be '" |
| 3908 | + NetworkCapabilities.MATCH_ALL_REQUESTS_NETWORK_SPECIFIER + "'"); |
| 3909 | } |
| 3910 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 3911 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
| 3912 | nextNetworkRequestId()); |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3913 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, type); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3914 | if (DBG) log("requestNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3915 | |
| 3916 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3917 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3918 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3919 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3920 | } |
| 3921 | return networkRequest; |
| 3922 | } |
| 3923 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3924 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 3925 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3926 | enforceConnectivityInternalPermission(); |
| 3927 | } else { |
| 3928 | enforceChangePermission(); |
| 3929 | } |
| 3930 | } |
| 3931 | |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3932 | @Override |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 3933 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 3934 | enforceAccessPermission(); |
| 3935 | NetworkAgentInfo nai = null; |
| 3936 | if (network == null) { |
| 3937 | return false; |
| 3938 | } |
| 3939 | synchronized (mNetworkForNetId) { |
| 3940 | nai = mNetworkForNetId.get(network.netId); |
| 3941 | } |
| 3942 | if (nai != null) { |
| 3943 | nai.asyncChannel.sendMessage(android.net.NetworkAgent.CMD_REQUEST_BANDWIDTH_UPDATE); |
| 3944 | return true; |
| 3945 | } |
| 3946 | return false; |
| 3947 | } |
| 3948 | |
| 3949 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3950 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
| 3951 | // 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] | 3952 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED) == false) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3953 | final int uidRules; |
| 3954 | final int uid = Binder.getCallingUid(); |
| 3955 | synchronized(mRulesLock) { |
| 3956 | uidRules = mUidRules.get(uid, RULE_ALLOW_ALL); |
| 3957 | } |
Jeff Sharkey | dc98806 | 2015-09-14 10:09:47 -0700 | [diff] [blame] | 3958 | if (uidRules != RULE_ALLOW_ALL) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3959 | // we could silently fail or we can filter the available nets to only give |
| 3960 | // them those they have access to. Chose the more useful |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 3961 | networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3966 | @Override |
| 3967 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 3968 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3969 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 3970 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 3971 | enforceNetworkRequestPermissions(networkCapabilities); |
| 3972 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 3973 | ensureRequestableCapabilities(networkCapabilities); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3974 | |
| 3975 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
| 3976 | nextNetworkRequestId()); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3977 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 3978 | NetworkRequestType.REQUEST); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3979 | if (DBG) log("pendingRequest for " + nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3980 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 3981 | nri)); |
| 3982 | return networkRequest; |
| 3983 | } |
| 3984 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 3985 | private void releasePendingNetworkRequestWithDelay(PendingIntent operation) { |
| 3986 | mHandler.sendMessageDelayed( |
| 3987 | mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 3988 | getCallingUid(), 0, operation), mReleasePendingIntentDelayMs); |
| 3989 | } |
| 3990 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3991 | @Override |
| 3992 | public void releasePendingNetworkRequest(PendingIntent operation) { |
Paul Jensen | 1a81c39 | 2015-05-21 08:15:08 -0400 | [diff] [blame] | 3993 | checkNotNull(operation, "PendingIntent cannot be null."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3994 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 3995 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3996 | } |
| 3997 | |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 3998 | // In order to implement the compatibility measure for pre-M apps that call |
| 3999 | // WifiManager.enableNetwork(..., true) without also binding to that network explicitly, |
| 4000 | // WifiManager registers a network listen for the purpose of calling setProcessDefaultNetwork. |
| 4001 | // This ensures it has permission to do so. |
| 4002 | private boolean hasWifiNetworkListenPermission(NetworkCapabilities nc) { |
| 4003 | if (nc == null) { |
| 4004 | return false; |
| 4005 | } |
| 4006 | int[] transportTypes = nc.getTransportTypes(); |
| 4007 | if (transportTypes.length != 1 || transportTypes[0] != NetworkCapabilities.TRANSPORT_WIFI) { |
| 4008 | return false; |
| 4009 | } |
| 4010 | try { |
| 4011 | mContext.enforceCallingOrSelfPermission( |
| 4012 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 4013 | "ConnectivityService"); |
| 4014 | } catch (SecurityException e) { |
| 4015 | return false; |
| 4016 | } |
| 4017 | return true; |
| 4018 | } |
| 4019 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4020 | @Override |
| 4021 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 4022 | Messenger messenger, IBinder binder) { |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 4023 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 4024 | enforceAccessPermission(); |
| 4025 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4026 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4027 | NetworkRequest networkRequest = new NetworkRequest( |
| 4028 | new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4029 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4030 | NetworkRequestType.LISTEN); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4031 | if (VDBG) log("listenForNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4032 | |
| 4033 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 4034 | return networkRequest; |
| 4035 | } |
| 4036 | |
| 4037 | @Override |
| 4038 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 4039 | PendingIntent operation) { |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4040 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 4041 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 4042 | enforceAccessPermission(); |
| 4043 | } |
| 4044 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4045 | NetworkRequest networkRequest = new NetworkRequest( |
| 4046 | new NetworkCapabilities(networkCapabilities), TYPE_NONE, nextNetworkRequestId()); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4047 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation, |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4048 | NetworkRequestType.LISTEN); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4049 | if (VDBG) log("pendingListenForNetwork for " + nri); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4050 | |
| 4051 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4052 | } |
| 4053 | |
| 4054 | @Override |
| 4055 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 4056 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), |
| 4057 | 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4058 | } |
| 4059 | |
| 4060 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4061 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4062 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4063 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 4064 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4065 | } |
| 4066 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4067 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4068 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4069 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 4070 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 4071 | } |
| 4072 | |
| 4073 | @Override |
| 4074 | public void unregisterNetworkFactory(Messenger messenger) { |
| 4075 | enforceConnectivityInternalPermission(); |
| 4076 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 4077 | } |
| 4078 | |
| 4079 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 4080 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 4081 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4082 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4083 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4084 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4085 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4086 | } |
| 4087 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4088 | /** |
| 4089 | * NetworkAgentInfo supporting a request by requestId. |
| 4090 | * These have already been vetted (their Capabilities satisfy the request) |
| 4091 | * and the are the highest scored network available. |
| 4092 | * the are keyed off the Requests requestId. |
| 4093 | */ |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4094 | // TODO: Yikes, this is accessed on multiple threads: add synchronization. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4095 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = |
| 4096 | new SparseArray<NetworkAgentInfo>(); |
| 4097 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4098 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 4099 | @GuardedBy("mNetworkForNetId") |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4100 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = |
| 4101 | new SparseArray<NetworkAgentInfo>(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4102 | // NOTE: Accessed on multiple threads, synchronized with mNetworkForNetId. |
| 4103 | // An entry is first added to mNetIdInUse, prior to mNetworkForNetId, so |
| 4104 | // there may not be a strict 1:1 correlation between the two. |
| 4105 | @GuardedBy("mNetworkForNetId") |
| 4106 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4107 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4108 | // NetworkAgentInfo keyed off its connecting messenger |
| 4109 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4110 | // NOTE: Only should be accessed on ConnectivityServiceThread, except dump(). |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4111 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = |
| 4112 | new HashMap<Messenger, NetworkAgentInfo>(); |
| 4113 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 4114 | @GuardedBy("mBlockedAppUids") |
| 4115 | private final HashSet<Integer> mBlockedAppUids = new HashSet(); |
| 4116 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 4117 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4118 | private final NetworkRequest mDefaultRequest; |
| 4119 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4120 | // Request used to optionally keep mobile data active even when higher |
| 4121 | // priority networks like Wi-Fi are active. |
| 4122 | private final NetworkRequest mDefaultMobileDataRequest; |
| 4123 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 4124 | private NetworkAgentInfo getDefaultNetwork() { |
| 4125 | return mNetworkForRequestId.get(mDefaultRequest.requestId); |
| 4126 | } |
| 4127 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4128 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 4129 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4130 | } |
| 4131 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4132 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4133 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4134 | int currentScore, NetworkMisc networkMisc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4135 | enforceConnectivityInternalPermission(); |
| 4136 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 4137 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 4138 | // satisfies mDefaultRequest. |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4139 | final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4140 | new Network(reserveNetId()), new NetworkInfo(networkInfo), new LinkProperties( |
| 4141 | linkProperties), new NetworkCapabilities(networkCapabilities), currentScore, |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4142 | mContext, mTrackerHandler, new NetworkMisc(networkMisc), mDefaultRequest, this); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 4143 | synchronized (this) { |
| 4144 | nai.networkMonitor.systemReady = mSystemReady; |
| 4145 | } |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 4146 | addValidationLogs(nai.networkMonitor.getValidationLogs(), nai.network); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4147 | if (DBG) log("registerNetworkAgent " + nai); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4148 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, nai)); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4149 | return nai.network.netId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4150 | } |
| 4151 | |
| 4152 | private void handleRegisterNetworkAgent(NetworkAgentInfo na) { |
| 4153 | if (VDBG) log("Got NetworkAgent Messenger"); |
| 4154 | mNetworkAgentInfos.put(na.messenger, na); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 4155 | synchronized (mNetworkForNetId) { |
| 4156 | mNetworkForNetId.put(na.network.netId, na); |
| 4157 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4158 | na.asyncChannel.connect(mContext, mTrackerHandler, na.messenger); |
| 4159 | NetworkInfo networkInfo = na.networkInfo; |
| 4160 | na.networkInfo = null; |
| 4161 | updateNetworkInfo(na, networkInfo); |
| 4162 | } |
| 4163 | |
| 4164 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties oldLp) { |
| 4165 | LinkProperties newLp = networkAgent.linkProperties; |
| 4166 | int netId = networkAgent.network.netId; |
| 4167 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4168 | // The NetworkAgentInfo does not know whether clatd is running on its network or not. Before |
| 4169 | // we do anything else, make sure its LinkProperties are accurate. |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4170 | if (networkAgent.clatd != null) { |
| 4171 | networkAgent.clatd.fixupLinkProperties(oldLp); |
| 4172 | } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4173 | |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4174 | updateInterfaces(newLp, oldLp, netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4175 | updateMtu(newLp, oldLp); |
| 4176 | // TODO - figure out what to do for clat |
| 4177 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 4178 | // updateMtu(lp, null); |
| 4179 | // } |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4180 | updateTcpBufferSizes(networkAgent); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 4181 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4182 | updateRoutes(newLp, oldLp, netId); |
| 4183 | updateDnses(newLp, oldLp, netId); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 4184 | |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4185 | updateClat(newLp, oldLp, networkAgent); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4186 | if (isDefaultNetwork(networkAgent)) { |
| 4187 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
| 4188 | } else { |
| 4189 | updateProxy(newLp, oldLp, networkAgent); |
| 4190 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4191 | // TODO - move this check to cover the whole function |
| 4192 | if (!Objects.equals(newLp, oldLp)) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4193 | notifyIfacesChangedForNetworkStats(); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4194 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 4195 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 4196 | |
| 4197 | mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4198 | } |
| 4199 | |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4200 | private void updateClat(LinkProperties newLp, LinkProperties oldLp, NetworkAgentInfo nai) { |
| 4201 | final boolean wasRunningClat = nai.clatd != null && nai.clatd.isStarted(); |
| 4202 | final boolean shouldRunClat = Nat464Xlat.requiresClat(nai); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4203 | |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4204 | if (!wasRunningClat && shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4205 | nai.clatd = new Nat464Xlat(mContext, mNetd, mTrackerHandler, nai); |
| 4206 | nai.clatd.start(); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 4207 | } else if (wasRunningClat && !shouldRunClat) { |
Lorenzo Colitti | 9543946 | 2014-10-09 13:44:48 +0900 | [diff] [blame] | 4208 | nai.clatd.stop(); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 4209 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4210 | } |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4211 | |
| 4212 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 4213 | CompareResult<String> interfaceDiff = new CompareResult<String>(); |
| 4214 | if (oldLp != null) { |
| 4215 | interfaceDiff = oldLp.compareAllInterfaceNames(newLp); |
| 4216 | } else if (newLp != null) { |
| 4217 | interfaceDiff.added = newLp.getAllInterfaceNames(); |
| 4218 | } |
| 4219 | for (String iface : interfaceDiff.added) { |
| 4220 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4221 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4222 | mNetd.addInterfaceToNetwork(iface, netId); |
| 4223 | } catch (Exception e) { |
| 4224 | loge("Exception adding interface: " + e); |
| 4225 | } |
| 4226 | } |
| 4227 | for (String iface : interfaceDiff.removed) { |
| 4228 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4229 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 4230 | mNetd.removeInterfaceFromNetwork(iface, netId); |
| 4231 | } catch (Exception e) { |
| 4232 | loge("Exception removing interface: " + e); |
| 4233 | } |
| 4234 | } |
| 4235 | } |
| 4236 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 4237 | /** |
| 4238 | * Have netd update routes from oldLp to newLp. |
| 4239 | * @return true if routes changed between oldLp and newLp |
| 4240 | */ |
| 4241 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4242 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>(); |
| 4243 | if (oldLp != null) { |
| 4244 | routeDiff = oldLp.compareAllRoutes(newLp); |
| 4245 | } else if (newLp != null) { |
| 4246 | routeDiff.added = newLp.getAllRoutes(); |
| 4247 | } |
| 4248 | |
| 4249 | // add routes before removing old in case it helps with continuous connectivity |
| 4250 | |
| 4251 | // do this twice, adding non-nexthop routes first, then routes they are dependent on |
| 4252 | for (RouteInfo route : routeDiff.added) { |
| 4253 | if (route.hasGateway()) continue; |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4254 | if (VDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4255 | try { |
| 4256 | mNetd.addRoute(netId, route); |
| 4257 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4258 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 4259 | loge("Exception in addRoute for non-gateway: " + e); |
| 4260 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4261 | } |
| 4262 | } |
| 4263 | for (RouteInfo route : routeDiff.added) { |
| 4264 | if (route.hasGateway() == false) continue; |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4265 | if (VDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4266 | try { |
| 4267 | mNetd.addRoute(netId, route); |
| 4268 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4269 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 4270 | loge("Exception in addRoute for gateway: " + e); |
| 4271 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4272 | } |
| 4273 | } |
| 4274 | |
| 4275 | for (RouteInfo route : routeDiff.removed) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4276 | if (VDBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4277 | try { |
| 4278 | mNetd.removeRoute(netId, route); |
| 4279 | } catch (Exception e) { |
| 4280 | loge("Exception in removeRoute: " + e); |
| 4281 | } |
| 4282 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 4283 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4284 | } |
Erik Kline | 4136850 | 2015-06-17 13:19:54 +0900 | [diff] [blame] | 4285 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4286 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 4287 | if (oldLp != null && newLp.isIdenticalDnses(oldLp)) { |
| 4288 | return; // no updating necessary |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4289 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4290 | |
| 4291 | Collection<InetAddress> dnses = newLp.getDnsServers(); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4292 | if (DBG) log("Setting DNS servers for network " + netId + " to " + dnses); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 4293 | try { |
| 4294 | mNetd.setDnsServersForNetwork( |
| 4295 | netId, NetworkUtils.makeStrings(dnses), newLp.getDomains()); |
| 4296 | } catch (Exception e) { |
| 4297 | loge("Exception in setDnsServersForNetwork: " + e); |
| 4298 | } |
| 4299 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
| 4300 | if (defaultNai != null && defaultNai.network.netId == netId) { |
| 4301 | setDefaultDnsSystemProperties(dnses); |
| 4302 | } |
| 4303 | flushVmDnsCache(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4304 | } |
| 4305 | |
Robert Greenwalt | d5648dc | 2014-05-15 15:27:13 -0700 | [diff] [blame] | 4306 | private void setDefaultDnsSystemProperties(Collection<InetAddress> dnses) { |
| 4307 | int last = 0; |
| 4308 | for (InetAddress dns : dnses) { |
| 4309 | ++last; |
| 4310 | String key = "net.dns" + last; |
| 4311 | String value = dns.getHostAddress(); |
| 4312 | SystemProperties.set(key, value); |
| 4313 | } |
| 4314 | for (int i = last + 1; i <= mNumDnsEntries; ++i) { |
| 4315 | String key = "net.dns" + i; |
| 4316 | SystemProperties.set(key, ""); |
| 4317 | } |
| 4318 | mNumDnsEntries = last; |
| 4319 | } |
| 4320 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4321 | /** |
| 4322 | * Update the NetworkCapabilities for {@code networkAgent} to {@code networkCapabilities} |
| 4323 | * augmented with any stateful capabilities implied from {@code networkAgent} |
| 4324 | * (e.g., validated status and captive portal status). |
| 4325 | * |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4326 | * @param nai the network having its capabilities updated. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4327 | * @param networkCapabilities the new network capabilities. |
| 4328 | */ |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4329 | private void updateCapabilities(NetworkAgentInfo nai, NetworkCapabilities networkCapabilities) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4330 | // Don't modify caller's NetworkCapabilities. |
| 4331 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4332 | if (nai.lastValidated) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4333 | networkCapabilities.addCapability(NET_CAPABILITY_VALIDATED); |
| 4334 | } else { |
| 4335 | networkCapabilities.removeCapability(NET_CAPABILITY_VALIDATED); |
| 4336 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4337 | if (nai.lastCaptivePortalDetected) { |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4338 | networkCapabilities.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
| 4339 | } else { |
| 4340 | networkCapabilities.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
| 4341 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4342 | if (!Objects.equals(nai.networkCapabilities, networkCapabilities)) { |
| 4343 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 4344 | if (nai.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) != |
| 4345 | networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { |
| 4346 | try { |
| 4347 | mNetd.setNetworkPermission(nai.network.netId, |
| 4348 | networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) ? |
| 4349 | null : NetworkManagementService.PERMISSION_SYSTEM); |
| 4350 | } catch (RemoteException e) { |
| 4351 | loge("Exception in setNetworkPermission: " + e); |
| 4352 | } |
| 4353 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4354 | synchronized (nai) { |
| 4355 | nai.networkCapabilities = networkCapabilities; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4356 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4357 | rematchAllNetworksAndRequests(nai, oldScore); |
| 4358 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4359 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4360 | } |
| 4361 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4362 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
| 4363 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 4364 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 4365 | // Don't send listening requests to factories. b/17393458 |
| 4366 | if (!isRequest(nr)) continue; |
| 4367 | sendUpdatedScoreToFactories(nr, nai.getCurrentScore()); |
| 4368 | } |
| 4369 | } |
| 4370 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4371 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
| 4372 | if (VDBG) log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4373 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 4374 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 4375 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4376 | } |
| 4377 | } |
| 4378 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4379 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 4380 | int notificationType) { |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4381 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE && !nri.mPendingIntentSent) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4382 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 4383 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 4384 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4385 | nri.mPendingIntentSent = true; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4386 | sendIntent(nri.mPendingIntent, intent); |
| 4387 | } |
| 4388 | // else not handled |
| 4389 | } |
| 4390 | |
| 4391 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 4392 | mPendingIntentWakeLock.acquire(); |
| 4393 | try { |
| 4394 | if (DBG) log("Sending " + pendingIntent); |
| 4395 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 4396 | } catch (PendingIntent.CanceledException e) { |
| 4397 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 4398 | mPendingIntentWakeLock.release(); |
| 4399 | releasePendingNetworkRequest(pendingIntent); |
| 4400 | } |
| 4401 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 4402 | } |
| 4403 | |
| 4404 | @Override |
| 4405 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 4406 | String resultData, Bundle resultExtras) { |
| 4407 | if (DBG) log("Finished sending " + pendingIntent); |
| 4408 | mPendingIntentWakeLock.release(); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4409 | // Release with a delay so the receiving client has an opportunity to put in its |
| 4410 | // own request. |
| 4411 | releasePendingNetworkRequestWithDelay(pendingIntent); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4412 | } |
| 4413 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4414 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4415 | NetworkAgentInfo networkAgent, int notificationType) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4416 | if (nri.messenger == null) return; // Default request has no msgr |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4417 | Bundle bundle = new Bundle(); |
| 4418 | bundle.putParcelable(NetworkRequest.class.getSimpleName(), |
| 4419 | new NetworkRequest(nri.request)); |
| 4420 | Message msg = Message.obtain(); |
| 4421 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL && |
| 4422 | notificationType != ConnectivityManager.CALLBACK_RELEASED) { |
| 4423 | bundle.putParcelable(Network.class.getSimpleName(), networkAgent.network); |
| 4424 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4425 | switch (notificationType) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4426 | case ConnectivityManager.CALLBACK_LOSING: { |
| 4427 | msg.arg1 = 30 * 1000; // TODO - read this from NetworkMonitor |
| 4428 | break; |
| 4429 | } |
| 4430 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
| 4431 | bundle.putParcelable(NetworkCapabilities.class.getSimpleName(), |
| 4432 | new NetworkCapabilities(networkAgent.networkCapabilities)); |
| 4433 | break; |
| 4434 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4435 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4436 | bundle.putParcelable(LinkProperties.class.getSimpleName(), |
| 4437 | new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4438 | break; |
| 4439 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4440 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4441 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 4442 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4443 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4444 | if (VDBG) { |
| 4445 | log("sending notification " + notifyTypeToName(notificationType) + |
| 4446 | " for " + nri.request); |
| 4447 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4448 | nri.messenger.send(msg); |
| 4449 | } catch (RemoteException e) { |
| 4450 | // may occur naturally in the race of binder death. |
| 4451 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 4452 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4453 | } |
| 4454 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4455 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
| 4456 | for (int i = 0; i < nai.networkRequests.size(); i++) { |
| 4457 | NetworkRequest nr = nai.networkRequests.valueAt(i); |
| 4458 | // Ignore listening requests. |
| 4459 | if (!isRequest(nr)) continue; |
| 4460 | loge("Dead network still had at least " + nr); |
| 4461 | break; |
| 4462 | } |
| 4463 | nai.asyncChannel.disconnect(); |
| 4464 | } |
| 4465 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4466 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 4467 | if (oldNetwork == null) { |
| 4468 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 4469 | return; |
| 4470 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4471 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
| 4472 | teardownUnneededNetwork(oldNetwork); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4473 | } |
| 4474 | |
Pierre Imai | 6b4e15b | 2016-04-07 12:21:05 +0900 | [diff] [blame] | 4475 | private void makeDefault(NetworkAgentInfo newNetwork, NetworkAgentInfo prevNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4476 | if (DBG) log("Switching to new default network: " + newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4477 | setupDataActivityTracking(newNetwork); |
| 4478 | try { |
| 4479 | mNetd.setDefaultNetId(newNetwork.network.netId); |
| 4480 | } catch (Exception e) { |
| 4481 | loge("Exception setting default network :" + e); |
| 4482 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4483 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4484 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 4485 | updateTcpBufferSizes(newNetwork); |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4486 | setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 4487 | logDefaultNetworkEvent(newNetwork, prevNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 4488 | } |
| 4489 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4490 | // Handles a network appearing or improving its score. |
| 4491 | // |
| 4492 | // - Evaluates all current NetworkRequests that can be |
| 4493 | // satisfied by newNetwork, and reassigns to newNetwork |
| 4494 | // any such requests for which newNetwork is the best. |
| 4495 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4496 | // - Lingers any validated Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4497 | // needed. A network is needed if it is the best network for |
| 4498 | // one or more NetworkRequests, or if it is a VPN. |
| 4499 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4500 | // - Tears down newNetwork if it just became validated |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4501 | // but turns out to be unneeded. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4502 | // |
| 4503 | // - If reapUnvalidatedNetworks==REAP, tears down unvalidated |
| 4504 | // networks that have no chance (i.e. even if validated) |
| 4505 | // of becoming the highest scoring network. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4506 | // |
| 4507 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 4508 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 4509 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 4510 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 4511 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 4512 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4513 | // @param newNetwork is the network to be matched against NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4514 | // @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be |
| 4515 | // performed to tear down unvalidated networks that have no chance (i.e. even if |
| 4516 | // validated) of becoming the highest scoring network. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4517 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4518 | ReapUnvalidatedNetworks reapUnvalidatedNetworks) { |
Paul Jensen | 57a767f | 2014-11-19 13:01:46 -0500 | [diff] [blame] | 4519 | if (!newNetwork.created) return; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4520 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4521 | boolean isNewDefault = false; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4522 | NetworkAgentInfo oldDefaultNetwork = null; |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 4523 | if (VDBG) log("rematching " + newNetwork.name()); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4524 | // Find and migrate to this Network any NetworkRequests for |
| 4525 | // which this network is now the best. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4526 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<NetworkAgentInfo>(); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4527 | ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<NetworkRequestInfo>(); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4528 | if (VDBG) log(" network has: " + newNetwork.networkCapabilities); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4529 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4530 | final NetworkAgentInfo currentNetwork = mNetworkForRequestId.get(nri.request.requestId); |
| 4531 | final boolean satisfies = newNetwork.satisfies(nri.request); |
| 4532 | if (newNetwork == currentNetwork && satisfies) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4533 | if (VDBG) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 4534 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 4535 | " request " + nri.request.requestId + ". No change."); |
| 4536 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4537 | keep = true; |
| 4538 | continue; |
| 4539 | } |
| 4540 | |
| 4541 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4542 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4543 | if (satisfies) { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4544 | if (!nri.isRequest()) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4545 | // This is not a request, it's a callback listener. |
| 4546 | // Add it to newNetwork regardless of score. |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4547 | if (newNetwork.addRequest(nri.request)) addedRequests.add(nri); |
Paul Jensen | 0311b2b | 2014-08-19 10:31:40 -0400 | [diff] [blame] | 4548 | continue; |
| 4549 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4550 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4551 | // next check if it's better than any current network we're using for |
| 4552 | // this request |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4553 | if (VDBG) { |
| 4554 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4555 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
| 4556 | ", newScore = " + newNetwork.getCurrentScore()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4557 | } |
| 4558 | if (currentNetwork == null || |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4559 | currentNetwork.getCurrentScore() < newNetwork.getCurrentScore()) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4560 | if (VDBG) log("rematch for " + newNetwork.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4561 | if (currentNetwork != null) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4562 | if (VDBG) log(" accepting network in place of " + currentNetwork.name()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4563 | currentNetwork.networkRequests.remove(nri.request.requestId); |
| 4564 | currentNetwork.networkLingered.add(nri.request); |
| 4565 | affectedNetworks.add(currentNetwork); |
| 4566 | } else { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4567 | if (VDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4568 | } |
Paul Jensen | 573a035 | 2015-01-08 10:49:34 -0500 | [diff] [blame] | 4569 | unlinger(newNetwork); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4570 | mNetworkForRequestId.put(nri.request.requestId, newNetwork); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 4571 | if (!newNetwork.addRequest(nri.request)) { |
| 4572 | Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request); |
| 4573 | } |
| 4574 | addedRequests.add(nri); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4575 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4576 | // Tell NetworkFactories about the new score, so they can stop |
| 4577 | // trying to connect if they know they cannot match it. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4578 | // TODO - this could get expensive if we have alot of requests for this |
| 4579 | // network. Think about if there is a way to reduce this. Push |
| 4580 | // netid->request mapping to each factory? |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4581 | sendUpdatedScoreToFactories(nri.request, newNetwork.getCurrentScore()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4582 | if (mDefaultRequest.requestId == nri.request.requestId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4583 | isNewDefault = true; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 4584 | oldDefaultNetwork = currentNetwork; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4585 | } |
| 4586 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4587 | } else if (newNetwork.networkRequests.get(nri.request.requestId) != null) { |
| 4588 | // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri", |
| 4589 | // mark it as no longer satisfying "nri". Because networks are processed by |
| 4590 | // rematchAllNetworkAndRequests() in descending score order, "currentNetwork" will |
| 4591 | // match "newNetwork" before this loop will encounter a "currentNetwork" with higher |
| 4592 | // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri". |
| 4593 | // This means this code doesn't have to handle the case where "currentNetwork" no |
| 4594 | // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork". |
| 4595 | if (DBG) { |
| 4596 | log("Network " + newNetwork.name() + " stopped satisfying" + |
| 4597 | " request " + nri.request.requestId); |
| 4598 | } |
| 4599 | newNetwork.networkRequests.remove(nri.request.requestId); |
| 4600 | if (currentNetwork == newNetwork) { |
| 4601 | mNetworkForRequestId.remove(nri.request.requestId); |
| 4602 | sendUpdatedScoreToFactories(nri.request, 0); |
| 4603 | } else { |
Erik Kline | 371c7b7 | 2016-03-22 17:04:20 +0900 | [diff] [blame] | 4604 | if (nri.isRequest()) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4605 | Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + |
| 4606 | newNetwork.name() + |
| 4607 | " without updating mNetworkForRequestId or factories!"); |
| 4608 | } |
| 4609 | } |
| 4610 | // TODO: technically, sending CALLBACK_LOST here is |
| 4611 | // incorrect if nri is a request (not a listen) and there |
| 4612 | // is a replacement network currently connected that can |
| 4613 | // satisfy it. However, the only capability that can both |
| 4614 | // a) be requested and b) change is NET_CAPABILITY_TRUSTED, |
| 4615 | // so this code is only incorrect for a network that loses |
| 4616 | // the TRUSTED capability, which is a rare case. |
| 4617 | callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4618 | } |
| 4619 | } |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4620 | // Linger any networks that are no longer needed. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4621 | for (NetworkAgentInfo nai : affectedNetworks) { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4622 | if (nai.lingering) { |
| 4623 | // Already lingered. Nothing to do. This can only happen if "nai" is in |
| 4624 | // "affectedNetworks" twice. The reasoning being that to get added to |
| 4625 | // "affectedNetworks", "nai" must have been satisfying a NetworkRequest |
| 4626 | // (i.e. not lingered) so it could have only been lingered by this loop. |
| 4627 | // unneeded(nai) will be false and we'll call unlinger() below which would |
| 4628 | // be bad, so handle it here. |
| 4629 | } else if (unneeded(nai)) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4630 | linger(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4631 | } else { |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 4632 | // Clear nai.networkLingered we might have added above. |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 4633 | unlinger(nai); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4634 | } |
| 4635 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4636 | if (isNewDefault) { |
| 4637 | // Notify system services that this network is up. |
Pierre Imai | 6b4e15b | 2016-04-07 12:21:05 +0900 | [diff] [blame] | 4638 | makeDefault(newNetwork, oldDefaultNetwork); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 4639 | synchronized (ConnectivityService.this) { |
| 4640 | // have a new default network, release the transition wakelock in |
| 4641 | // a second if it's held. The second pause is to allow apps |
| 4642 | // to reconnect over the new network |
| 4643 | if (mNetTransitionWakeLock.isHeld()) { |
| 4644 | mHandler.sendMessageDelayed(mHandler.obtainMessage( |
| 4645 | EVENT_CLEAR_NET_TRANSITION_WAKELOCK, |
| 4646 | mNetTransitionWakeLockSerialNumber, 0), |
| 4647 | 1000); |
| 4648 | } |
| 4649 | } |
| 4650 | } |
| 4651 | |
| 4652 | // do this after the default net is switched, but |
| 4653 | // before LegacyTypeTracker sends legacy broadcasts |
| 4654 | for (NetworkRequestInfo nri : addedRequests) notifyNetworkCallback(newNetwork, nri); |
| 4655 | |
| 4656 | if (isNewDefault) { |
| 4657 | // Maintain the illusion: since the legacy API only |
| 4658 | // understands one network at a time, we must pretend |
| 4659 | // that the current default network disconnected before |
| 4660 | // the new one connected. |
| 4661 | if (oldDefaultNetwork != null) { |
| 4662 | mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), |
| 4663 | oldDefaultNetwork, true); |
| 4664 | } |
| 4665 | mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0; |
| 4666 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
| 4667 | notifyLockdownVpn(newNetwork); |
| 4668 | } |
| 4669 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4670 | if (keep) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4671 | // Notify battery stats service about this network, both the normal |
| 4672 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4673 | // 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] | 4674 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 4675 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 4676 | final int type = newNetwork.networkInfo.getType(); |
| 4677 | |
| 4678 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 4679 | bs.noteNetworkInterfaceType(baseIface, type); |
| 4680 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 4681 | final String stackedIface = stacked.getInterfaceName(); |
| 4682 | bs.noteNetworkInterfaceType(stackedIface, type); |
| 4683 | NetworkStatsFactory.noteStackedIface(stackedIface, baseIface); |
| 4684 | } |
| 4685 | } catch (RemoteException ignored) { |
| 4686 | } |
| 4687 | |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 4688 | // This has to happen after the notifyNetworkCallbacks as that tickles each |
| 4689 | // ConnectivityManager instance so that legacy requests correctly bind dns |
| 4690 | // requests to this network. The legacy users are listening for this bcast |
| 4691 | // and will generally do a dns request so they can ensureRouteToHost and if |
| 4692 | // they do that before the callbacks happen they'll use the default network. |
| 4693 | // |
| 4694 | // TODO: Is there still a race here? We send the broadcast |
| 4695 | // after sending the callback, but if the app can receive the |
| 4696 | // broadcast before the callback, it might still break. |
| 4697 | // |
| 4698 | // This *does* introduce a race where if the user uses the new api |
| 4699 | // (notification callbacks) and then uses the old api (getNetworkInfo(type)) |
| 4700 | // they may get old info. Reverse this after the old startUsing api is removed. |
| 4701 | // This is on top of the multiple intent sequencing referenced in the todo above. |
| 4702 | for (int i = 0; i < newNetwork.networkRequests.size(); i++) { |
| 4703 | NetworkRequest nr = newNetwork.networkRequests.valueAt(i); |
| 4704 | if (nr.legacyType != TYPE_NONE && isRequest(nr)) { |
| 4705 | // legacy type tracker filters out repeat adds |
| 4706 | mLegacyTypeTracker.add(nr.legacyType, newNetwork); |
| 4707 | } |
| 4708 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 4709 | |
| 4710 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 4711 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 4712 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 4713 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 4714 | if (newNetwork.isVPN()) { |
| 4715 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 4716 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4717 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4718 | if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) { |
| 4719 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4720 | if (unneeded(nai)) { |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4721 | if (DBG) log("Reaping " + nai.name()); |
| 4722 | teardownUnneededNetwork(nai); |
| 4723 | } |
| 4724 | } |
| 4725 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4726 | } |
| 4727 | |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4728 | /** |
| 4729 | * Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 4730 | * being disconnected. |
| 4731 | * @param changed If only one Network's score or capabilities have been modified since the last |
| 4732 | * time this function was called, pass this Network in this argument, otherwise pass |
| 4733 | * null. |
| 4734 | * @param oldScore If only one Network has been changed but its NetworkCapabilities have not |
| 4735 | * changed, pass in the Network's score (from getCurrentScore()) prior to the change via |
| 4736 | * this argument, otherwise pass {@code changed.getCurrentScore()} or 0 if |
| 4737 | * {@code changed} is {@code null}. This is because NetworkCapabilities influence a |
| 4738 | * network's score. |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 4739 | */ |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4740 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4741 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 4742 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 4743 | // example in the case where "changed"'s score decreases and another network should begin |
| 4744 | // satifying a NetworkRequest that "changed" currently satisfies. |
| 4745 | |
| 4746 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 4747 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 4748 | // rematchNetworkAndRequests() handles. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4749 | if (changed != null && oldScore < changed.getCurrentScore()) { |
| 4750 | rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4751 | } else { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4752 | final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray( |
| 4753 | new NetworkAgentInfo[mNetworkAgentInfos.size()]); |
| 4754 | // Rematch higher scoring networks first to prevent requests first matching a lower |
| 4755 | // scoring network and then a higher scoring network, which could produce multiple |
| 4756 | // callbacks and inadvertently unlinger networks. |
| 4757 | Arrays.sort(nais); |
| 4758 | for (NetworkAgentInfo nai : nais) { |
| 4759 | rematchNetworkAndRequests(nai, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4760 | // Only reap the last time through the loop. Reaping before all rematching |
| 4761 | // is complete could incorrectly teardown a network that hasn't yet been |
| 4762 | // rematched. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4763 | (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 4764 | : ReapUnvalidatedNetworks.REAP); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4768 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 4769 | private void updateInetCondition(NetworkAgentInfo nai) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4770 | // 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] | 4771 | if (!nai.everValidated) return; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4772 | // For now only update icons for default connection. |
| 4773 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 4774 | if (!isDefaultNetwork(nai)) return; |
| 4775 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 4776 | int newInetCondition = nai.lastValidated ? 100 : 0; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 4777 | // Don't repeat publish. |
| 4778 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 4779 | |
| 4780 | mDefaultInetConditionPublished = newInetCondition; |
| 4781 | sendInetConditionBroadcast(nai.networkInfo); |
| 4782 | } |
| 4783 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4784 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
| 4785 | if (mLockdownTracker != null) { |
| 4786 | if (nai != null && nai.isVPN()) { |
| 4787 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 4788 | } else { |
| 4789 | mLockdownTracker.onNetworkInfoChanged(); |
| 4790 | } |
| 4791 | } |
| 4792 | } |
| 4793 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4794 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
| 4795 | NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4796 | NetworkInfo oldInfo = null; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4797 | final int oldScore = networkAgent.getCurrentScore(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 4798 | synchronized (networkAgent) { |
| 4799 | oldInfo = networkAgent.networkInfo; |
| 4800 | networkAgent.networkInfo = newInfo; |
| 4801 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 4802 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4803 | |
| 4804 | if (oldInfo != null && oldInfo.getState() == state) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4805 | if (oldInfo.isRoaming() != newInfo.isRoaming()) { |
| 4806 | if (VDBG) log("roaming status changed, notifying NetworkStatsService"); |
| 4807 | notifyIfacesChangedForNetworkStats(); |
| 4808 | } else if (VDBG) log("ignoring duplicate network state non-change"); |
| 4809 | // In either case, no further work should be needed. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4810 | return; |
| 4811 | } |
| 4812 | if (DBG) { |
| 4813 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 4814 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 4815 | " to " + state); |
| 4816 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4817 | |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4818 | if (state == NetworkInfo.State.CONNECTED && !networkAgent.created) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4819 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4820 | // 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] | 4821 | if (networkAgent.isVPN()) { |
| 4822 | mNetd.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 4823 | !networkAgent.linkProperties.getDnsServers().isEmpty(), |
| 4824 | (networkAgent.networkMisc == null || |
| 4825 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4826 | } else { |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 4827 | mNetd.createPhysicalNetwork(networkAgent.network.netId, |
| 4828 | networkAgent.networkCapabilities.hasCapability( |
| 4829 | NET_CAPABILITY_NOT_RESTRICTED) ? |
| 4830 | null : NetworkManagementService.PERMISSION_SYSTEM); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4831 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4832 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 4833 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 4834 | + e.getMessage()); |
| 4835 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4836 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 4837 | networkAgent.created = true; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4838 | updateLinkProperties(networkAgent, null); |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 4839 | notifyIfacesChangedForNetworkStats(); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4840 | |
Paul Jensen | ca8f16a | 2014-05-09 12:47:55 -0400 | [diff] [blame] | 4841 | networkAgent.networkMonitor.sendMessage(NetworkMonitor.CMD_NETWORK_CONNECTED); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4842 | scheduleUnvalidatedPrompt(networkAgent); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4843 | |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4844 | if (networkAgent.isVPN()) { |
| 4845 | // Temporarily disable the default proxy (not global). |
| 4846 | synchronized (mProxyLock) { |
| 4847 | if (!mDefaultProxyDisabled) { |
| 4848 | mDefaultProxyDisabled = true; |
| 4849 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4850 | sendProxyBroadcast(null); |
| 4851 | } |
| 4852 | } |
| 4853 | } |
| 4854 | // TODO: support proxy per network. |
| 4855 | } |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4856 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4857 | // Whether a particular NetworkRequest listen should cause signal strength thresholds to |
| 4858 | // be communicated to a particular NetworkAgent depends only on the network's immutable, |
| 4859 | // capabilities, so it only needs to be done once on initial connect, not every time the |
| 4860 | // network's capabilities change. Note that we do this before rematching the network, |
| 4861 | // so we could decide to tear it down immediately afterwards. That's fine though - on |
| 4862 | // disconnection NetworkAgents should stop any signal strength monitoring they have been |
| 4863 | // doing. |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4864 | updateSignalStrengthThresholds(networkAgent, "CONNECT", null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4865 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4866 | // Consider network even though it is not yet validated. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4867 | rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 4868 | |
| 4869 | // This has to happen after matching the requests, because callbacks are just requests. |
| 4870 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4871 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4872 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 4873 | if (networkAgent.isVPN()) { |
| 4874 | synchronized (mProxyLock) { |
| 4875 | if (mDefaultProxyDisabled) { |
| 4876 | mDefaultProxyDisabled = false; |
| 4877 | if (mGlobalProxy == null && mDefaultProxy != null) { |
| 4878 | sendProxyBroadcast(mDefaultProxy); |
| 4879 | } |
| 4880 | } |
| 4881 | } |
| 4882 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4883 | } else if ((oldInfo != null && oldInfo.getState() == NetworkInfo.State.SUSPENDED) || |
| 4884 | state == NetworkInfo.State.SUSPENDED) { |
| 4885 | // going into or coming out of SUSPEND: rescore and notify |
| 4886 | if (networkAgent.getCurrentScore() != oldScore) { |
Paul Jensen | 3b9ce37 | 2015-07-10 12:19:38 -0400 | [diff] [blame] | 4887 | rematchAllNetworksAndRequests(networkAgent, oldScore); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4888 | } |
| 4889 | notifyNetworkCallbacks(networkAgent, (state == NetworkInfo.State.SUSPENDED ? |
| 4890 | ConnectivityManager.CALLBACK_SUSPENDED : |
| 4891 | ConnectivityManager.CALLBACK_RESUMED)); |
| 4892 | mLegacyTypeTracker.update(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4893 | } |
| 4894 | } |
| 4895 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4896 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4897 | if (VDBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 4898 | if (score < 0) { |
| 4899 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 4900 | "). Bumping score to min of 0"); |
| 4901 | score = 0; |
| 4902 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4903 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4904 | final int oldScore = nai.getCurrentScore(); |
| 4905 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 4906 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4907 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 4908 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 4909 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4912 | // notify only this one new request of the current state |
| 4913 | protected void notifyNetworkCallback(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
| 4914 | int notifyType = ConnectivityManager.CALLBACK_AVAILABLE; |
| 4915 | // TODO - read state from monitor to decide what to send. |
| 4916 | // if (nai.networkMonitor.isLingering()) { |
| 4917 | // notifyType = NetworkCallbacks.LOSING; |
| 4918 | // } else if (nai.networkMonitor.isEvaluating()) { |
| 4919 | // notifyType = NetworkCallbacks.callCallbackForRequest(request, nai, notifyType); |
| 4920 | // } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4921 | if (nri.mPendingIntent == null) { |
| 4922 | callCallbackForRequest(nri, nai, notifyType); |
| 4923 | } else { |
| 4924 | sendPendingIntentForRequest(nri, nai, notifyType); |
| 4925 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4926 | } |
| 4927 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4928 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, DetailedState state, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 4929 | // The NetworkInfo we actually send out has no bearing on the real |
| 4930 | // state of affairs. For example, if the default connection is mobile, |
| 4931 | // and a request for HIPRI has just gone away, we need to pretend that |
| 4932 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 4933 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 4934 | // and is still connected. |
| 4935 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 4936 | info.setType(type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4937 | if (state != DetailedState.DISCONNECTED) { |
| 4938 | info.setDetailedState(state, null, info.getExtraInfo()); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 4939 | sendConnectedBroadcast(info); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4940 | } else { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 4941 | info.setDetailedState(state, info.getReason(), info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4942 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 4943 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 4944 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 4945 | if (info.isFailover()) { |
| 4946 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 4947 | nai.networkInfo.setFailover(false); |
| 4948 | } |
| 4949 | if (info.getReason() != null) { |
| 4950 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 4951 | } |
| 4952 | if (info.getExtraInfo() != null) { |
| 4953 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 4954 | } |
| 4955 | NetworkAgentInfo newDefaultAgent = null; |
| 4956 | if (nai.networkRequests.get(mDefaultRequest.requestId) != null) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 4957 | newDefaultAgent = getDefaultNetwork(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4958 | if (newDefaultAgent != null) { |
| 4959 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 4960 | newDefaultAgent.networkInfo); |
| 4961 | } else { |
| 4962 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 4963 | } |
| 4964 | } |
| 4965 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 4966 | mDefaultInetConditionPublished); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 4967 | sendStickyBroadcast(intent); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4968 | if (newDefaultAgent != null) { |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 4969 | sendConnectedBroadcast(newDefaultAgent.networkInfo); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 4970 | } |
| 4971 | } |
| 4972 | } |
| 4973 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4974 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4975 | if (VDBG) log("notifyType " + notifyTypeToName(notifyType) + " for " + networkAgent.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4976 | for (int i = 0; i < networkAgent.networkRequests.size(); i++) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4977 | NetworkRequest nr = networkAgent.networkRequests.valueAt(i); |
| 4978 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 4979 | if (VDBG) log(" sending notification for " + nr); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4980 | if (nri.mPendingIntent == null) { |
| 4981 | callCallbackForRequest(nri, networkAgent, notifyType); |
| 4982 | } else { |
| 4983 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 4984 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4985 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 4986 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 4987 | |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 4988 | private String notifyTypeToName(int notifyType) { |
| 4989 | switch (notifyType) { |
| 4990 | case ConnectivityManager.CALLBACK_PRECHECK: return "PRECHECK"; |
| 4991 | case ConnectivityManager.CALLBACK_AVAILABLE: return "AVAILABLE"; |
| 4992 | case ConnectivityManager.CALLBACK_LOSING: return "LOSING"; |
| 4993 | case ConnectivityManager.CALLBACK_LOST: return "LOST"; |
| 4994 | case ConnectivityManager.CALLBACK_UNAVAIL: return "UNAVAILABLE"; |
| 4995 | case ConnectivityManager.CALLBACK_CAP_CHANGED: return "CAP_CHANGED"; |
| 4996 | case ConnectivityManager.CALLBACK_IP_CHANGED: return "IP_CHANGED"; |
| 4997 | case ConnectivityManager.CALLBACK_RELEASED: return "RELEASED"; |
| 4998 | } |
| 4999 | return "UNKNOWN"; |
| 5000 | } |
| 5001 | |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5002 | /** |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5003 | * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the |
| 5004 | * properties tracked by NetworkStatsService on an active iface has changed. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5005 | */ |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5006 | private void notifyIfacesChangedForNetworkStats() { |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 5007 | try { |
| 5008 | mStatsService.forceUpdateIfaces(); |
| 5009 | } catch (Exception ignored) { |
| 5010 | } |
| 5011 | } |
| 5012 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 5013 | @Override |
| 5014 | public boolean addVpnAddress(String address, int prefixLength) { |
| 5015 | throwIfLockdownEnabled(); |
| 5016 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 5017 | synchronized (mVpns) { |
| 5018 | return mVpns.get(user).addAddress(address, prefixLength); |
| 5019 | } |
| 5020 | } |
| 5021 | |
| 5022 | @Override |
| 5023 | public boolean removeVpnAddress(String address, int prefixLength) { |
| 5024 | throwIfLockdownEnabled(); |
| 5025 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 5026 | synchronized (mVpns) { |
| 5027 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 5028 | } |
| 5029 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5030 | |
| 5031 | @Override |
| 5032 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
| 5033 | throwIfLockdownEnabled(); |
| 5034 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5035 | boolean success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5036 | synchronized (mVpns) { |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5037 | success = mVpns.get(user).setUnderlyingNetworks(networks); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5038 | } |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5039 | if (success) { |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5040 | notifyIfacesChangedForNetworkStats(); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 5041 | } |
| 5042 | return success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 5043 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5044 | |
| 5045 | @Override |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 5046 | public String getCaptivePortalServerUrl() { |
| 5047 | return NetworkMonitor.getCaptivePortalServerUrl(mContext); |
| 5048 | } |
| 5049 | |
| 5050 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 5051 | public void startNattKeepalive(Network network, int intervalSeconds, Messenger messenger, |
| 5052 | IBinder binder, String srcAddr, int srcPort, String dstAddr) { |
| 5053 | enforceKeepalivePermission(); |
| 5054 | mKeepaliveTracker.startNattKeepalive( |
| 5055 | getNetworkAgentInfoForNetwork(network), |
| 5056 | intervalSeconds, messenger, binder, |
| 5057 | srcAddr, srcPort, dstAddr, ConnectivityManager.PacketKeepalive.NATT_PORT); |
| 5058 | } |
| 5059 | |
| 5060 | @Override |
| 5061 | public void stopKeepalive(Network network, int slot) { |
| 5062 | mHandler.sendMessage(mHandler.obtainMessage( |
| 5063 | NetworkAgent.CMD_STOP_PACKET_KEEPALIVE, slot, PacketKeepalive.SUCCESS, network)); |
| 5064 | } |
| 5065 | |
| 5066 | @Override |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5067 | public void factoryReset() { |
| 5068 | enforceConnectivityInternalPermission(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5069 | |
| 5070 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 5071 | return; |
| 5072 | } |
| 5073 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 5074 | final int userId = UserHandle.getCallingUserId(); |
| 5075 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5076 | // Turn airplane mode off |
| 5077 | setAirplaneMode(false); |
| 5078 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5079 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 5080 | // Untether |
| 5081 | for (String tether : getTetheredIfaces()) { |
| 5082 | untether(tether); |
| 5083 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5084 | } |
| 5085 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5086 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { |
| 5087 | // Turn VPN off |
| 5088 | VpnConfig vpnConfig = getVpnConfig(userId); |
| 5089 | if (vpnConfig != null) { |
| 5090 | if (vpnConfig.legacy) { |
| 5091 | prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, userId); |
| 5092 | } else { |
| 5093 | // Prevent this app (packagename = vpnConfig.user) from initiating VPN connections |
| 5094 | // in the future without user intervention. |
| 5095 | setVpnPackageAuthorization(vpnConfig.user, userId, false); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5096 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 5097 | prepareVpn(vpnConfig.user, VpnConfig.LEGACY_VPN, userId); |
| 5098 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 5099 | } |
| 5100 | } |
| 5101 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5102 | |
| 5103 | @VisibleForTesting |
| 5104 | public NetworkMonitor createNetworkMonitor(Context context, Handler handler, |
| 5105 | NetworkAgentInfo nai, NetworkRequest defaultRequest) { |
| 5106 | return new NetworkMonitor(context, handler, nai, defaultRequest); |
| 5107 | } |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5108 | |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 5109 | private static void logDefaultNetworkEvent(NetworkAgentInfo newNai, NetworkAgentInfo prevNai) { |
| 5110 | int newNetid = NETID_UNSET; |
| 5111 | int prevNetid = NETID_UNSET; |
| 5112 | int[] transports = new int[0]; |
| 5113 | boolean hadIPv4 = false; |
| 5114 | boolean hadIPv6 = false; |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5115 | |
Hugo Benichi | 5f16f76 | 2016-04-20 12:09:33 +0900 | [diff] [blame] | 5116 | if (newNai != null) { |
| 5117 | newNetid = newNai.network.netId; |
| 5118 | transports = newNai.networkCapabilities.getTransportTypes(); |
| 5119 | } |
| 5120 | if (prevNai != null) { |
| 5121 | prevNetid = prevNai.network.netId; |
| 5122 | final LinkProperties lp = prevNai.linkProperties; |
| 5123 | hadIPv4 = lp.hasIPv4Address() && lp.hasIPv4DefaultRoute(); |
| 5124 | hadIPv6 = lp.hasGlobalIPv6Address() && lp.hasIPv6DefaultRoute(); |
| 5125 | } |
| 5126 | |
| 5127 | DefaultNetworkEvent.logEvent(newNetid, transports, prevNetid, hadIPv4, hadIPv6); |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 5128 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5129 | } |