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; |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 20 | import static android.content.pm.PackageManager.PERMISSION_GRANTED; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.NETID_UNSET; |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_ETHERNET; |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 24 | import static android.net.ConnectivityManager.TYPE_NONE; |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 25 | import static android.net.ConnectivityManager.TYPE_VPN; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 26 | import static android.net.ConnectivityManager.getNetworkTypeName; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 27 | import static android.net.ConnectivityManager.isNetworkTypeValid; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 28 | import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 29 | import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 31 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 32 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 33 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 34 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 35 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED; |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 36 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 37 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 38 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 39 | import static android.net.NetworkPolicyManager.RULE_NONE; |
| 40 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 41 | import static android.net.shared.NetworkMonitorUtils.isValidationRequired; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 42 | import static android.net.shared.NetworkParcelableUtil.toStableParcelable; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 43 | import static android.os.Process.INVALID_UID; |
| 44 | import static android.system.OsConstants.IPPROTO_TCP; |
| 45 | import static android.system.OsConstants.IPPROTO_UDP; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 46 | |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 47 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 48 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 49 | import android.annotation.Nullable; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 50 | import android.app.BroadcastOptions; |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 51 | import android.app.NotificationManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 52 | import android.app.PendingIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 53 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.content.ContentResolver; |
| 55 | import android.content.Context; |
| 56 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 57 | import android.content.IntentFilter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 58 | import android.content.res.Configuration; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 59 | import android.database.ContentObserver; |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 60 | import android.net.CaptivePortal; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 61 | import android.net.ConnectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 63 | import android.net.ICaptivePortal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.net.IConnectivityManager; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 65 | import android.net.IIpConnectivityMetrics; |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 66 | import android.net.INetd; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 67 | import android.net.INetdEventCallback; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 68 | import android.net.INetworkManagementEventObserver; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 69 | import android.net.INetworkMonitor; |
| 70 | import android.net.INetworkMonitorCallbacks; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 71 | import android.net.INetworkPolicyListener; |
| 72 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 73 | import android.net.INetworkStatsService; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 74 | import android.net.InetAddresses; |
| 75 | import android.net.IpPrefix; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 76 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 77 | import android.net.LinkProperties.CompareResult; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 78 | import android.net.MatchAllNetworkSpecifier; |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 79 | import android.net.NattSocketKeepalive; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 80 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 81 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 82 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 83 | import android.net.NetworkConfig; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 85 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 86 | import android.net.NetworkMisc; |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 87 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 88 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 89 | import android.net.NetworkRequest; |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 90 | import android.net.NetworkSpecifier; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 91 | import android.net.NetworkStack; |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 92 | import android.net.NetworkStackClient; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 93 | import android.net.NetworkState; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 94 | import android.net.NetworkUtils; |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 95 | import android.net.NetworkWatchlistManager; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 96 | import android.net.PrivateDnsConfigParcel; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 97 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 98 | import android.net.RouteInfo; |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 99 | import android.net.SocketKeepalive; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 100 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 101 | import android.net.Uri; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 102 | import android.net.VpnService; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 103 | import android.net.metrics.IpConnectivityLog; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 104 | import android.net.metrics.NetworkEvent; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 105 | import android.net.netlink.InetDiagMessage; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 106 | import android.net.shared.NetworkMonitorUtils; |
| 107 | import android.net.shared.PrivateDnsConfig; |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 108 | import android.net.util.MultinetworkPolicyTracker; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 109 | import android.net.util.NetdService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | import android.os.Binder; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 111 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 112 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 114 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 115 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 116 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.os.Looper; |
| 118 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 119 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 120 | import android.os.ParcelFileDescriptor; |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 121 | import android.os.Parcelable; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 122 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 123 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 124 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 125 | import android.os.ResultReceiver; |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 126 | import android.os.ServiceManager; |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 127 | import android.os.ServiceSpecificException; |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 128 | import android.os.ShellCallback; |
| 129 | import android.os.ShellCommand; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 130 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 131 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 132 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 134 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 135 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 136 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 137 | import android.text.TextUtils; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 138 | import android.util.ArraySet; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 139 | import android.util.LocalLog; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 140 | import android.util.Log; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 141 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 142 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 143 | import android.util.SparseArray; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 144 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 145 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 146 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 148 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 149 | import com.android.internal.annotations.GuardedBy; |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 150 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 151 | import com.android.internal.app.IBatteryStats; |
Remi NGUYEN VAN | de60221 | 2019-01-30 15:22:01 +0900 | [diff] [blame] | 152 | import com.android.internal.logging.MetricsLogger; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 153 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 154 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 155 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 156 | import com.android.internal.net.VpnProfile; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 157 | import com.android.internal.util.ArrayUtils; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 158 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 159 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 160 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 161 | import com.android.internal.util.MessageUtils; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 162 | import com.android.internal.util.WakeupMessage; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 163 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 164 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 165 | import com.android.server.connectivity.DataConnectionStats; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 166 | import com.android.server.connectivity.DnsManager; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 167 | import com.android.server.connectivity.DnsManager.PrivateDnsValidationUpdate; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 168 | import com.android.server.connectivity.IpConnectivityMetrics; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 169 | import com.android.server.connectivity.KeepaliveTracker; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 170 | import com.android.server.connectivity.LingerMonitor; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 171 | import com.android.server.connectivity.MockableSystemProperties; |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 172 | import com.android.server.connectivity.MultipathPolicyTracker; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 173 | import com.android.server.connectivity.NetworkAgentInfo; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 174 | import com.android.server.connectivity.NetworkDiagnostics; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 175 | import com.android.server.connectivity.NetworkNotificationManager; |
| 176 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 177 | import com.android.server.connectivity.PermissionMonitor; |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 178 | import com.android.server.connectivity.ProxyTracker; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 179 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 180 | import com.android.server.connectivity.Vpn; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 181 | import com.android.server.connectivity.tethering.TetheringDependencies; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 182 | import com.android.server.net.BaseNetdEventCallback; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 183 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 184 | import com.android.server.net.LockdownVpnTracker; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 185 | import com.android.server.net.NetworkPolicyManagerInternal; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 186 | import com.android.server.utils.PriorityDump; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 187 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 188 | import com.google.android.collect.Lists; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 189 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 190 | import org.xmlpull.v1.XmlPullParser; |
| 191 | import org.xmlpull.v1.XmlPullParserException; |
| 192 | |
| 193 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 195 | import java.io.FileNotFoundException; |
| 196 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 197 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | import java.io.PrintWriter; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 199 | import java.net.Inet4Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 200 | import java.net.InetAddress; |
| 201 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 202 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 203 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 204 | import java.util.Collection; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 205 | import java.util.Comparator; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 206 | import java.util.ConcurrentModificationException; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 207 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 208 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 209 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 210 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 211 | import java.util.Objects; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 212 | import java.util.Set; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 213 | import java.util.SortedSet; |
| 214 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | |
| 216 | /** |
| 217 | * @hide |
| 218 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 219 | public class ConnectivityService extends IConnectivityManager.Stub |
| 220 | implements PendingIntent.OnFinished { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 221 | private static final String TAG = ConnectivityService.class.getSimpleName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 223 | private static final String DIAG_ARG = "--diag"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 224 | public static final String SHORT_ARG = "--short"; |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 225 | private static final String TETHERING_ARG = "tethering"; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 226 | private static final String NETWORK_ARG = "networks"; |
| 227 | private static final String REQUEST_ARG = "requests"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 228 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 229 | private static final boolean DBG = true; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 230 | private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG); |
| 231 | private static final boolean VDBG = Log.isLoggable(TAG, Log.VERBOSE); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 233 | private static final boolean LOGD_BLOCKED_NETWORKINFO = true; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 234 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 235 | // TODO: create better separation between radio types and network types |
| 236 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 237 | // how long to wait before switching back to a radio's default network |
| 238 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 239 | // system property that can override the above value |
| 240 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 241 | "android.telephony.apn-restore"; |
| 242 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 243 | // How long to wait before putting up a "This network doesn't have an Internet connection, |
| 244 | // connect anyway?" dialog after the user selects a network that doesn't validate. |
| 245 | private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000; |
| 246 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 247 | // How long to dismiss network notification. |
| 248 | private static final int TIMEOUT_NOTIFICATION_DELAY_MS = 20 * 1000; |
| 249 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 250 | // Default to 30s linger time-out. Modifiable only for testing. |
| 251 | private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger"; |
| 252 | private static final int DEFAULT_LINGER_DELAY_MS = 30_000; |
| 253 | @VisibleForTesting |
| 254 | protected int mLingerDelayMs; // Can't be final, or test subclass constructors can't change it. |
| 255 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 256 | // How long to delay to removal of a pending intent based request. |
| 257 | // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS |
| 258 | private final int mReleasePendingIntentDelayMs; |
| 259 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 260 | private MockableSystemProperties mSystemProperties; |
| 261 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 262 | private Tethering mTethering; |
| 263 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 264 | private final PermissionMonitor mPermissionMonitor; |
| 265 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 266 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 267 | |
Chalard Jean | fb0c87e | 2018-04-18 19:18:58 +0900 | [diff] [blame] | 268 | @VisibleForTesting |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 269 | @GuardedBy("mVpns") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 270 | protected final SparseArray<Vpn> mVpns = new SparseArray<>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 271 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 272 | // TODO: investigate if mLockdownEnabled can be removed and replaced everywhere by |
| 273 | // a direct call to LockdownVpnTracker.isEnabled(). |
| 274 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 275 | private boolean mLockdownEnabled; |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 276 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 277 | private LockdownVpnTracker mLockdownTracker; |
| 278 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 279 | /** |
| 280 | * Stale copy of uid rules provided by NPMS. As long as they are accessed only in internal |
| 281 | * handler thread, they don't need a lock. |
| 282 | */ |
| 283 | private SparseIntArray mUidRules = new SparseIntArray(); |
| 284 | /** Flag indicating if background data is restricted. */ |
| 285 | private boolean mRestrictBackground; |
| 286 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 287 | final private Context mContext; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 288 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 289 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 291 | private INetworkManagementService mNMS; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 292 | @VisibleForTesting |
| 293 | protected INetd mNetd; |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 294 | private INetworkStatsService mStatsService; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 295 | private INetworkPolicyManager mPolicyManager; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 296 | private NetworkPolicyManagerInternal mPolicyManagerInternal; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 297 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 298 | private String mCurrentTcpBufferSizes; |
| 299 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 300 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 301 | new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class, |
| 302 | NetworkAgentInfo.class }); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 303 | |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 304 | private enum ReapUnvalidatedNetworks { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 305 | // Tear down networks that have no chance (e.g. even if validated) of becoming |
| 306 | // the highest scoring network satisfying a NetworkRequest. This should be passed when |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 307 | // all networks have been rematched against all NetworkRequests. |
| 308 | REAP, |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 309 | // Don't reap networks. This should be passed when some networks have not yet been |
| 310 | // rematched against all NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 311 | DONT_REAP |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 312 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 313 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 314 | private enum UnneededFor { |
| 315 | LINGER, // Determine whether this network is unneeded and should be lingered. |
| 316 | TEARDOWN, // Determine whether this network is unneeded and should be torn down. |
| 317 | } |
| 318 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 319 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 320 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 321 | * from one net to another. Clear happens when we get a new |
| 322 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 323 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 324 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 325 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 326 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 327 | /** |
| 328 | * used internally to reload global proxy settings |
| 329 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 330 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 331 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 332 | /** |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 333 | * PAC manager has received new port. |
| 334 | */ |
| 335 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 336 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 337 | /** |
| 338 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 339 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 340 | */ |
| 341 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 342 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 343 | /** |
| 344 | * used internally when registering NetworkAgents |
| 345 | * obj = Messenger |
| 346 | */ |
| 347 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 348 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 349 | /** |
| 350 | * used to add a network request |
| 351 | * includes a NetworkRequestInfo |
| 352 | */ |
| 353 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 354 | |
| 355 | /** |
| 356 | * indicates a timeout period is over - check if we had a network yet or not |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 357 | * and if not, call the timeout callback (but leave the request live until they |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 358 | * cancel it. |
| 359 | * includes a NetworkRequestInfo |
| 360 | */ |
| 361 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 362 | |
| 363 | /** |
| 364 | * used to add a network listener - no request |
| 365 | * includes a NetworkRequestInfo |
| 366 | */ |
| 367 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 368 | |
| 369 | /** |
| 370 | * 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] | 371 | * arg1 = UID of caller |
| 372 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 373 | */ |
| 374 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 375 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 376 | /** |
| 377 | * used internally when registering NetworkFactories |
| 378 | * obj = Messenger |
| 379 | */ |
| 380 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 381 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 382 | /** |
| 383 | * used internally to expire a wakelock when transitioning |
| 384 | * from one net to another. Expire happens when we fail to find |
| 385 | * a new network (typically after 1 minute) - |
| 386 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 387 | * a replacement network. |
| 388 | */ |
| 389 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 390 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 391 | /** |
| 392 | * Used internally to indicate the system is ready. |
| 393 | */ |
| 394 | private static final int EVENT_SYSTEM_READY = 25; |
| 395 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 396 | /** |
| 397 | * used to add a network request with a pending intent |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 398 | * obj = NetworkRequestInfo |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 399 | */ |
| 400 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 401 | |
| 402 | /** |
| 403 | * used to remove a pending intent and its associated network request. |
| 404 | * arg1 = UID of caller |
| 405 | * obj = PendingIntent |
| 406 | */ |
| 407 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 408 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 409 | /** |
| 410 | * used to specify whether a network should be used even if unvalidated. |
| 411 | * arg1 = whether to accept the network if it's unvalidated (1 or 0) |
| 412 | * arg2 = whether to remember this choice in the future (1 or 0) |
| 413 | * obj = network |
| 414 | */ |
| 415 | private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28; |
| 416 | |
| 417 | /** |
| 418 | * used to ask the user to confirm a connection to an unvalidated network. |
| 419 | * obj = network |
| 420 | */ |
| 421 | private static final int EVENT_PROMPT_UNVALIDATED = 29; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 422 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 423 | /** |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 424 | * used internally to (re)configure always-on networks. |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 425 | */ |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 426 | private static final int EVENT_CONFIGURE_ALWAYS_ON_NETWORKS = 30; |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 427 | |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 428 | /** |
| 429 | * used to add a network listener with a pending intent |
| 430 | * obj = NetworkRequestInfo |
| 431 | */ |
| 432 | private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31; |
| 433 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 434 | /** |
| 435 | * used to specify whether a network should not be penalized when it becomes unvalidated. |
| 436 | */ |
| 437 | private static final int EVENT_SET_AVOID_UNVALIDATED = 35; |
| 438 | |
| 439 | /** |
| 440 | * used to trigger revalidation of a network. |
| 441 | */ |
| 442 | private static final int EVENT_REVALIDATE_NETWORK = 36; |
| 443 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 444 | // Handle changes in Private DNS settings. |
| 445 | private static final int EVENT_PRIVATE_DNS_SETTINGS_CHANGED = 37; |
| 446 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 447 | // Handle private DNS validation status updates. |
| 448 | private static final int EVENT_PRIVATE_DNS_VALIDATION_UPDATE = 38; |
| 449 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 450 | /** |
| 451 | * Used to handle onUidRulesChanged event from NetworkPolicyManagerService. |
| 452 | */ |
| 453 | private static final int EVENT_UID_RULES_CHANGED = 39; |
| 454 | |
| 455 | /** |
| 456 | * Used to handle onRestrictBackgroundChanged event from NetworkPolicyManagerService. |
| 457 | */ |
| 458 | private static final int EVENT_DATA_SAVER_CHANGED = 40; |
| 459 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 460 | /** |
| 461 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the network has |
| 462 | * been tested. |
| 463 | * obj = String representing URL that Internet probe was redirect to, if it was redirected. |
| 464 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
| 465 | * arg2 = NetID. |
| 466 | */ |
| 467 | public static final int EVENT_NETWORK_TESTED = 41; |
| 468 | |
| 469 | /** |
| 470 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the private DNS |
| 471 | * config was resolved. |
| 472 | * obj = PrivateDnsConfig |
| 473 | * arg2 = netid |
| 474 | */ |
| 475 | public static final int EVENT_PRIVATE_DNS_CONFIG_RESOLVED = 42; |
| 476 | |
| 477 | /** |
| 478 | * Request ConnectivityService display provisioning notification. |
| 479 | * arg1 = Whether to make the notification visible. |
| 480 | * arg2 = NetID. |
| 481 | * obj = Intent to be launched when notification selected by user, null if !arg1. |
| 482 | */ |
| 483 | public static final int EVENT_PROVISIONING_NOTIFICATION = 43; |
| 484 | |
| 485 | /** |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 486 | * This event can handle dismissing notification by given network id. |
| 487 | */ |
| 488 | public static final int EVENT_TIMEOUT_NOTIFICATION = 44; |
| 489 | |
| 490 | /** |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 491 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 492 | * should be shown. |
| 493 | */ |
| 494 | public static final int PROVISIONING_NOTIFICATION_SHOW = 1; |
| 495 | |
| 496 | /** |
| 497 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 498 | * should be hidden. |
| 499 | */ |
| 500 | public static final int PROVISIONING_NOTIFICATION_HIDE = 0; |
| 501 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 502 | private static String eventName(int what) { |
| 503 | return sMagicDecoderRing.get(what, Integer.toString(what)); |
| 504 | } |
| 505 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 506 | /** Handler thread used for both of the handlers below. */ |
| 507 | @VisibleForTesting |
| 508 | protected final HandlerThread mHandlerThread; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 509 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 510 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 511 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 512 | final private NetworkStateTrackerHandler mTrackerHandler; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 513 | private final DnsManager mDnsManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 514 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 515 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 516 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 517 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 518 | private PowerManager.WakeLock mNetTransitionWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 519 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 520 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 521 | |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 522 | // A helper object to track the current default HTTP proxy. ConnectivityService needs to tell |
| 523 | // the world when it changes. |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 524 | @VisibleForTesting |
| 525 | protected final ProxyTracker mProxyTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 526 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 527 | final private SettingsObserver mSettingsObserver; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 528 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 529 | private UserManager mUserManager; |
| 530 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 531 | private NetworkConfig[] mNetConfigs; |
| 532 | private int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 533 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 534 | // the set of network types that can only be enabled by system/sig apps |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 535 | private List mProtectedNetworks; |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 536 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 537 | private TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 538 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 539 | private KeepaliveTracker mKeepaliveTracker; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 540 | private NetworkNotificationManager mNotifier; |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 541 | private LingerMonitor mLingerMonitor; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 542 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 543 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 544 | private static final int MIN_NET_ID = 100; // some reserved marks |
| 545 | private static final int MAX_NET_ID = 65535 - 0x0400; // Top 1024 bits reserved by IpSecService |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 546 | private int mNextNetId = MIN_NET_ID; |
| 547 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 548 | // sequence number of NetworkRequests |
| 549 | private int mNextNetworkRequestId = 1; |
| 550 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 551 | // NetworkRequest activity String log entries. |
| 552 | private static final int MAX_NETWORK_REQUEST_LOGS = 20; |
| 553 | private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS); |
| 554 | |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 555 | // NetworkInfo blocked and unblocked String log entries |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 556 | private static final int MAX_NETWORK_INFO_LOGS = 40; |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 557 | private final LocalLog mNetworkInfoBlockingLogs = new LocalLog(MAX_NETWORK_INFO_LOGS); |
| 558 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 559 | private static final int MAX_WAKELOCK_LOGS = 20; |
| 560 | private final LocalLog mWakelockLogs = new LocalLog(MAX_WAKELOCK_LOGS); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 561 | private int mTotalWakelockAcquisitions = 0; |
| 562 | private int mTotalWakelockReleases = 0; |
| 563 | private long mTotalWakelockDurationMs = 0; |
| 564 | private long mMaxWakelockDurationMs = 0; |
| 565 | private long mLastWakeLockAcquireTimestamp = 0; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 566 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 567 | private final IpConnectivityLog mMetricsLog; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 568 | |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 569 | @GuardedBy("mBandwidthRequests") |
| 570 | private final SparseArray<Integer> mBandwidthRequests = new SparseArray(10); |
| 571 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 572 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 573 | final MultinetworkPolicyTracker mMultinetworkPolicyTracker; |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 574 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 575 | @VisibleForTesting |
| 576 | final MultipathPolicyTracker mMultipathPolicyTracker; |
| 577 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 578 | /** |
| 579 | * Implements support for the legacy "one network per network type" model. |
| 580 | * |
| 581 | * We used to have a static array of NetworkStateTrackers, one for each |
| 582 | * network type, but that doesn't work any more now that we can have, |
| 583 | * for example, more that one wifi network. This class stores all the |
| 584 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 585 | * API will only see the first one. |
| 586 | * |
| 587 | * It serves two main purposes: |
| 588 | * |
| 589 | * 1. Provide information about "the network for a given type" (since this |
| 590 | * API only supports one). |
| 591 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 592 | * the first network for a given type changes, or if the default network |
| 593 | * changes. |
| 594 | */ |
| 595 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 596 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 597 | private static final boolean DBG = true; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 598 | private static final boolean VDBG = false; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 599 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 600 | /** |
| 601 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 602 | * Each list holds references to all NetworkAgentInfos that are used to |
| 603 | * satisfy requests for that network type. |
| 604 | * |
| 605 | * This array is built out at startup such that an unsupported network |
| 606 | * doesn't get an ArrayList instance, making this a tristate: |
| 607 | * unsupported, supported but not active and active. |
| 608 | * |
| 609 | * The actual lists are populated when we scan the network types that |
| 610 | * are supported on this device. |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 611 | * |
| 612 | * Threading model: |
| 613 | * - addSupportedType() is only called in the constructor |
| 614 | * - add(), update(), remove() are only called from the ConnectivityService handler thread. |
| 615 | * They are therefore not thread-safe with respect to each other. |
| 616 | * - getNetworkForType() can be called at any time on binder threads. It is synchronized |
| 617 | * on mTypeLists to be thread-safe with respect to a concurrent remove call. |
| 618 | * - dump is thread-safe with respect to concurrent add and remove calls. |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 619 | */ |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 620 | private final ArrayList<NetworkAgentInfo> mTypeLists[]; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 621 | |
| 622 | public LegacyTypeTracker() { |
| 623 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 624 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 625 | } |
| 626 | |
| 627 | public void addSupportedType(int type) { |
| 628 | if (mTypeLists[type] != null) { |
| 629 | throw new IllegalStateException( |
| 630 | "legacy list for type " + type + "already initialized"); |
| 631 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 632 | mTypeLists[type] = new ArrayList<>(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 635 | public boolean isTypeSupported(int type) { |
| 636 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 637 | } |
| 638 | |
| 639 | public NetworkAgentInfo getNetworkForType(int type) { |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 640 | synchronized (mTypeLists) { |
| 641 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 642 | return mTypeLists[type].get(0); |
| 643 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 644 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 645 | return null; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 648 | private void maybeLogBroadcast(NetworkAgentInfo nai, DetailedState state, int type, |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 649 | boolean isDefaultNetwork) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 650 | if (DBG) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 651 | log("Sending " + state + |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 652 | " broadcast for type " + type + " " + nai.name() + |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 653 | " isDefaultNetwork=" + isDefaultNetwork); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 654 | } |
| 655 | } |
| 656 | |
| 657 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 658 | public void add(int type, NetworkAgentInfo nai) { |
| 659 | if (!isTypeSupported(type)) { |
| 660 | return; // Invalid network type. |
| 661 | } |
| 662 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 663 | |
| 664 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 665 | if (list.contains(nai)) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 666 | return; |
| 667 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 668 | synchronized (mTypeLists) { |
| 669 | list.add(nai); |
| 670 | } |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 671 | |
| 672 | // 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] | 673 | final boolean isDefaultNetwork = isDefaultNetwork(nai); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 674 | if ((list.size() == 1) || isDefaultNetwork) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 675 | maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork); |
| 676 | sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 677 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 678 | } |
| 679 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 680 | /** Removes the given network from the specified legacy type list. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 681 | public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 682 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 683 | if (list == null || list.isEmpty()) { |
| 684 | return; |
| 685 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 686 | final boolean wasFirstNetwork = list.get(0).equals(nai); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 687 | |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 688 | synchronized (mTypeLists) { |
| 689 | if (!list.remove(nai)) { |
| 690 | return; |
| 691 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 692 | } |
| 693 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 694 | final DetailedState state = DetailedState.DISCONNECTED; |
| 695 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 696 | if (wasFirstNetwork || wasDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 697 | maybeLogBroadcast(nai, state, type, wasDefault); |
| 698 | sendLegacyNetworkBroadcast(nai, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 699 | } |
| 700 | |
| 701 | if (!list.isEmpty() && wasFirstNetwork) { |
| 702 | if (DBG) log("Other network available for type " + type + |
| 703 | ", sending connected broadcast"); |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 704 | final NetworkAgentInfo replacement = list.get(0); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 705 | maybeLogBroadcast(replacement, state, type, isDefaultNetwork(replacement)); |
| 706 | sendLegacyNetworkBroadcast(replacement, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | |
| 710 | /** Removes the given network from all legacy type lists. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 711 | public void remove(NetworkAgentInfo nai, boolean wasDefault) { |
| 712 | if (VDBG) log("Removing agent " + nai + " wasDefault=" + wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 713 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 714 | remove(type, nai, wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 717 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 718 | // send out another legacy broadcast - currently only used for suspend/unsuspend |
| 719 | // toggle |
| 720 | public void update(NetworkAgentInfo nai) { |
| 721 | final boolean isDefault = isDefaultNetwork(nai); |
| 722 | final DetailedState state = nai.networkInfo.getDetailedState(); |
| 723 | for (int type = 0; type < mTypeLists.length; type++) { |
| 724 | final ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 725 | final boolean contains = (list != null && list.contains(nai)); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 726 | final boolean isFirst = contains && (nai == list.get(0)); |
| 727 | if (isFirst || contains && isDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 728 | maybeLogBroadcast(nai, state, type, isDefault); |
| 729 | sendLegacyNetworkBroadcast(nai, state, type); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 734 | private String naiToString(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 735 | String name = nai.name(); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 736 | String state = (nai.networkInfo != null) ? |
| 737 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 738 | "???/???"; |
| 739 | return name + " " + state; |
| 740 | } |
| 741 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 742 | public void dump(IndentingPrintWriter pw) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 743 | pw.println("mLegacyTypeTracker:"); |
| 744 | pw.increaseIndent(); |
| 745 | pw.print("Supported types:"); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 746 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 747 | if (mTypeLists[type] != null) pw.print(" " + type); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 748 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 749 | pw.println(); |
| 750 | pw.println("Current state:"); |
| 751 | pw.increaseIndent(); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 752 | synchronized (mTypeLists) { |
| 753 | for (int type = 0; type < mTypeLists.length; type++) { |
| 754 | if (mTypeLists[type] == null || mTypeLists[type].isEmpty()) continue; |
| 755 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
| 756 | pw.println(type + " " + naiToString(nai)); |
| 757 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | pw.decreaseIndent(); |
| 761 | pw.decreaseIndent(); |
| 762 | pw.println(); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 763 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 764 | } |
| 765 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 766 | |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 767 | /** |
| 768 | * Helper class which parses out priority arguments and dumps sections according to their |
| 769 | * priority. If priority arguments are omitted, function calls the legacy dump command. |
| 770 | */ |
| 771 | private final PriorityDump.PriorityDumper mPriorityDumper = new PriorityDump.PriorityDumper() { |
| 772 | @Override |
| 773 | public void dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 774 | doDump(fd, pw, new String[] {DIAG_ARG}, asProto); |
| 775 | doDump(fd, pw, new String[] {SHORT_ARG}, asProto); |
| 776 | } |
| 777 | |
| 778 | @Override |
| 779 | public void dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 780 | doDump(fd, pw, args, asProto); |
| 781 | } |
| 782 | |
| 783 | @Override |
| 784 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 785 | doDump(fd, pw, args, asProto); |
| 786 | } |
| 787 | }; |
| 788 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 789 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 790 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 791 | this(context, netManager, statsService, policyManager, new IpConnectivityLog()); |
| 792 | } |
| 793 | |
| 794 | @VisibleForTesting |
| 795 | protected ConnectivityService(Context context, INetworkManagementService netManager, |
| 796 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 797 | IpConnectivityLog logger) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 798 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 799 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 800 | mSystemProperties = getSystemProperties(); |
| 801 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 802 | mMetricsLog = logger; |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 803 | mDefaultRequest = createDefaultInternetRequestForTransport(-1, NetworkRequest.Type.REQUEST); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 804 | NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, new Binder()); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 805 | mNetworkRequests.put(mDefaultRequest, defaultNRI); |
| 806 | mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 807 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 808 | mDefaultMobileDataRequest = createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 809 | NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 810 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 811 | // The default WiFi request is a background request so that apps using WiFi are |
| 812 | // migrated to a better network (typically ethernet) when one comes up, instead |
| 813 | // of staying on WiFi forever. |
| 814 | mDefaultWifiRequest = createDefaultInternetRequestForTransport( |
| 815 | NetworkCapabilities.TRANSPORT_WIFI, NetworkRequest.Type.BACKGROUND_REQUEST); |
| 816 | |
Hugo Benichi | ad4db4e | 2016-10-17 15:54:51 +0900 | [diff] [blame] | 817 | mHandlerThread = new HandlerThread("ConnectivityServiceThread"); |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 818 | mHandlerThread.start(); |
| 819 | mHandler = new InternalHandler(mHandlerThread.getLooper()); |
| 820 | mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 821 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 822 | mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(), |
| 823 | Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000); |
| 824 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 825 | mLingerDelayMs = mSystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 826 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 827 | mContext = checkNotNull(context, "missing Context"); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 828 | mNMS = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 829 | mStatsService = checkNotNull(statsService, "missing INetworkStatsService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 830 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 831 | mPolicyManagerInternal = checkNotNull( |
| 832 | LocalServices.getService(NetworkPolicyManagerInternal.class), |
| 833 | "missing NetworkPolicyManagerInternal"); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 834 | mProxyTracker = makeProxyTracker(); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 835 | |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 836 | mNetd = NetdService.getInstance(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 837 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 838 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 839 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 840 | // To ensure uid rules are synchronized with Network Policy, register for |
| 841 | // NetworkPolicyManagerService events must happen prior to NetworkPolicyManagerService |
| 842 | // reading existing policy from disk. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 843 | try { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 844 | mPolicyManager.registerListener(mPolicyListener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 845 | } catch (RemoteException e) { |
| 846 | // ouch, no rules updates means some processes may never get network |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 847 | loge("unable to register INetworkPolicyListener" + e); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 851 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 852 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 853 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 854 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 855 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 856 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 857 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 858 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 859 | // TODO: What is the "correct" way to do determine if this is a wifi only device? |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 860 | boolean wifiOnly = mSystemProperties.getBoolean("ro.radio.noril", false); |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 861 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 862 | String[] naStrings = context.getResources().getStringArray( |
| 863 | com.android.internal.R.array.networkAttributes); |
| 864 | for (String naString : naStrings) { |
| 865 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 866 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 867 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 868 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 869 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 870 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 871 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 872 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 873 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 874 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 875 | n.type); |
| 876 | continue; |
| 877 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 878 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 879 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 880 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 881 | continue; |
| 882 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 883 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 884 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 885 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 886 | mNetworksDefined++; |
| 887 | } catch(Exception e) { |
| 888 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 889 | } |
| 890 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 891 | |
| 892 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 893 | if (mNetConfigs[TYPE_VPN] == null) { |
| 894 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 895 | // don't need to add TYPE_VPN to mNetConfigs. |
| 896 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 897 | mNetworksDefined++; // used only in the log() statement below. |
| 898 | } |
| 899 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 900 | // Do the same for Ethernet, since it's often not specified in the configs, although many |
| 901 | // devices can use it via USB host adapters. |
| 902 | if (mNetConfigs[TYPE_ETHERNET] == null && hasService(Context.ETHERNET_SERVICE)) { |
| 903 | mLegacyTypeTracker.addSupportedType(TYPE_ETHERNET); |
| 904 | mNetworksDefined++; |
| 905 | } |
| 906 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 907 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 908 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 909 | mProtectedNetworks = new ArrayList<Integer>(); |
| 910 | int[] protectedNetworks = context.getResources().getIntArray( |
| 911 | com.android.internal.R.array.config_protectedNetworks); |
| 912 | for (int p : protectedNetworks) { |
| 913 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 914 | mProtectedNetworks.add(p); |
| 915 | } else { |
| 916 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 917 | } |
| 918 | } |
| 919 | |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 920 | mTethering = makeTethering(); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 921 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 922 | mPermissionMonitor = new PermissionMonitor(mContext, mNMS); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 923 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 924 | // Set up the listener for user state for creating user VPNs. |
| 925 | // Should run on mHandler to avoid any races. |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 926 | IntentFilter intentFilter = new IntentFilter(); |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 927 | intentFilter.addAction(Intent.ACTION_USER_STARTED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 928 | intentFilter.addAction(Intent.ACTION_USER_STOPPED); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 929 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 930 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 931 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 932 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 933 | mIntentReceiver, |
| 934 | UserHandle.ALL, |
| 935 | intentFilter, |
| 936 | null /* broadcastPermission */, |
| 937 | mHandler); |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 938 | mContext.registerReceiverAsUser(mUserPresentReceiver, UserHandle.SYSTEM, |
| 939 | new IntentFilter(Intent.ACTION_USER_PRESENT), null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 940 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 941 | // Listen to package add and removal events for all users. |
| 942 | intentFilter = new IntentFilter(); |
| 943 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 944 | intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 945 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 946 | intentFilter.addDataScheme("package"); |
| 947 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 948 | mIntentReceiver, |
| 949 | UserHandle.ALL, |
| 950 | intentFilter, |
| 951 | null /* broadcastPermission */, |
| 952 | mHandler); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 953 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 954 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 955 | mNMS.registerObserver(mTethering); |
| 956 | mNMS.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 957 | } catch (RemoteException e) { |
| 958 | loge("Error registering observer :" + e); |
| 959 | } |
| 960 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 961 | mSettingsObserver = new SettingsObserver(mContext, mHandler); |
| 962 | registerSettingsCallbacks(); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 963 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 964 | final DataConnectionStats dataConnectionStats = new DataConnectionStats(mContext); |
| 965 | dataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 966 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 967 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 968 | |
| 969 | mKeepaliveTracker = new KeepaliveTracker(mHandler); |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 970 | mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager, |
| 971 | mContext.getSystemService(NotificationManager.class)); |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 972 | |
| 973 | final int dailyLimit = Settings.Global.getInt(mContext.getContentResolver(), |
| 974 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_DAILY_LIMIT, |
| 975 | LingerMonitor.DEFAULT_NOTIFICATION_DAILY_LIMIT); |
| 976 | final long rateLimit = Settings.Global.getLong(mContext.getContentResolver(), |
| 977 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_RATE_LIMIT_MILLIS, |
| 978 | LingerMonitor.DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS); |
| 979 | mLingerMonitor = new LingerMonitor(mContext, mNotifier, dailyLimit, rateLimit); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 980 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 981 | mMultinetworkPolicyTracker = createMultinetworkPolicyTracker( |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 982 | mContext, mHandler, () -> rematchForAvoidBadWifiUpdate()); |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 983 | mMultinetworkPolicyTracker.start(); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 984 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 985 | mMultipathPolicyTracker = new MultipathPolicyTracker(mContext, mHandler); |
| 986 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 987 | mDnsManager = new DnsManager(mContext, mNMS, mSystemProperties); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 988 | registerPrivateDnsSettingsCallbacks(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 990 | |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 991 | @VisibleForTesting |
| 992 | protected Tethering makeTethering() { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 993 | // TODO: Move other elements into @Overridden getters. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 994 | final TetheringDependencies deps = new TetheringDependencies() { |
| 995 | @Override |
| 996 | public boolean isTetheringSupported() { |
| 997 | return ConnectivityService.this.isTetheringSupported(); |
| 998 | } |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 999 | @Override |
| 1000 | public NetworkRequest getDefaultNetworkRequest() { |
| 1001 | return mDefaultRequest; |
| 1002 | } |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1003 | }; |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1004 | return new Tethering(mContext, mNMS, mStatsService, mPolicyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1005 | IoThread.get().getLooper(), new MockableSystemProperties(), |
| 1006 | deps); |
| 1007 | } |
| 1008 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 1009 | @VisibleForTesting |
| 1010 | protected ProxyTracker makeProxyTracker() { |
| 1011 | return new ProxyTracker(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
| 1012 | } |
| 1013 | |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1014 | private static NetworkCapabilities createDefaultNetworkCapabilitiesForUid(int uid) { |
| 1015 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1016 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1017 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
| 1018 | netCap.removeCapability(NET_CAPABILITY_NOT_VPN); |
| 1019 | netCap.setSingleUid(uid); |
| 1020 | return netCap; |
| 1021 | } |
| 1022 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 1023 | private NetworkRequest createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1024 | int transportType, NetworkRequest.Type type) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1025 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 1026 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1027 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1028 | if (transportType > -1) { |
| 1029 | netCap.addTransportType(transportType); |
| 1030 | } |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1031 | return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId(), type); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1032 | } |
| 1033 | |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1034 | // Used only for testing. |
| 1035 | // TODO: Delete this and either: |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1036 | // 1. Give FakeSettingsProvider the ability to send settings change notifications (requires |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1037 | // changing ContentResolver to make registerContentObserver non-final). |
| 1038 | // 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it |
| 1039 | // by subclassing SettingsObserver. |
| 1040 | @VisibleForTesting |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1041 | void updateAlwaysOnNetworks() { |
| 1042 | mHandler.sendEmptyMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1043 | } |
| 1044 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1045 | // See FakeSettingsProvider comment above. |
| 1046 | @VisibleForTesting |
| 1047 | void updatePrivateDnsSettings() { |
| 1048 | mHandler.sendEmptyMessage(EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
| 1049 | } |
| 1050 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1051 | private void handleAlwaysOnNetworkRequest( |
| 1052 | NetworkRequest networkRequest, String settingName, boolean defaultValue) { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 1053 | final boolean enable = toBool(Settings.Global.getInt( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1054 | mContext.getContentResolver(), settingName, encodeBool(defaultValue))); |
| 1055 | final boolean isEnabled = (mNetworkRequests.get(networkRequest) != null); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1056 | if (enable == isEnabled) { |
| 1057 | return; // Nothing to do. |
| 1058 | } |
| 1059 | |
| 1060 | if (enable) { |
| 1061 | handleRegisterNetworkRequest(new NetworkRequestInfo( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1062 | null, networkRequest, new Binder())); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1063 | } else { |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1064 | handleReleaseNetworkRequest(networkRequest, Process.SYSTEM_UID); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1065 | } |
| 1066 | } |
| 1067 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1068 | private void handleConfigureAlwaysOnNetworks() { |
| 1069 | handleAlwaysOnNetworkRequest( |
| 1070 | mDefaultMobileDataRequest,Settings.Global.MOBILE_DATA_ALWAYS_ON, true); |
| 1071 | handleAlwaysOnNetworkRequest(mDefaultWifiRequest, Settings.Global.WIFI_ALWAYS_REQUESTED, |
| 1072 | false); |
| 1073 | } |
| 1074 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1075 | private void registerSettingsCallbacks() { |
| 1076 | // Watch for global HTTP proxy changes. |
| 1077 | mSettingsObserver.observe( |
| 1078 | Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), |
| 1079 | EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 1080 | |
| 1081 | // Watch for whether or not to keep mobile data always on. |
| 1082 | mSettingsObserver.observe( |
| 1083 | Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1084 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
| 1085 | |
| 1086 | // Watch for whether or not to keep wifi always on. |
| 1087 | mSettingsObserver.observe( |
| 1088 | Settings.Global.getUriFor(Settings.Global.WIFI_ALWAYS_REQUESTED), |
| 1089 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1090 | } |
| 1091 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1092 | private void registerPrivateDnsSettingsCallbacks() { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1093 | for (Uri uri : DnsManager.getPrivateDnsSettingsUris()) { |
| 1094 | mSettingsObserver.observe(uri, EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1095 | } |
| 1096 | } |
| 1097 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 1098 | private synchronized int nextNetworkRequestId() { |
| 1099 | return mNextNetworkRequestId++; |
| 1100 | } |
| 1101 | |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 1102 | @VisibleForTesting |
| 1103 | protected int reserveNetId() { |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1104 | synchronized (mNetworkForNetId) { |
| 1105 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 1106 | int netId = mNextNetId; |
| 1107 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 1108 | // Make sure NetID unused. http://b/16815182 |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1109 | if (!mNetIdInUse.get(netId)) { |
| 1110 | mNetIdInUse.put(netId, true); |
| 1111 | return netId; |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1118 | private NetworkState getFilteredNetworkState(int networkType, int uid) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1119 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1120 | final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1121 | final NetworkState state; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1122 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1123 | state = nai.getNetworkState(); |
| 1124 | state.networkInfo.setType(networkType); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1125 | } else { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1126 | final NetworkInfo info = new NetworkInfo(networkType, 0, |
| 1127 | getNetworkTypeName(networkType), ""); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1128 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 1129 | info.setIsAvailable(true); |
Jeff Sharkey | 6226216 | 2017-12-04 15:52:01 -0700 | [diff] [blame] | 1130 | final NetworkCapabilities capabilities = new NetworkCapabilities(); |
| 1131 | capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, |
| 1132 | !info.isRoaming()); |
| 1133 | state = new NetworkState(info, new LinkProperties(), capabilities, |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1134 | null, null, null); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1135 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1136 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1137 | return state; |
| 1138 | } else { |
| 1139 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1140 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1141 | } |
| 1142 | |
junyulai | 4a192e2 | 2018-06-13 15:00:37 +0800 | [diff] [blame] | 1143 | @VisibleForTesting |
| 1144 | protected NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1145 | if (network == null) { |
| 1146 | return null; |
| 1147 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1148 | return getNetworkAgentInfoForNetId(network.netId); |
| 1149 | } |
| 1150 | |
| 1151 | private NetworkAgentInfo getNetworkAgentInfoForNetId(int netId) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1152 | synchronized (mNetworkForNetId) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1153 | return mNetworkForNetId.get(netId); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1154 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1155 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1156 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1157 | private Network[] getVpnUnderlyingNetworks(int uid) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1158 | synchronized (mVpns) { |
| 1159 | if (!mLockdownEnabled) { |
| 1160 | int user = UserHandle.getUserId(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1161 | Vpn vpn = mVpns.get(user); |
| 1162 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 1163 | return vpn.getUnderlyingNetworks(); |
| 1164 | } |
| 1165 | } |
| 1166 | } |
| 1167 | return null; |
| 1168 | } |
| 1169 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1170 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 1171 | NetworkAgentInfo nai = getDefaultNetwork(); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1172 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1173 | final Network[] networks = getVpnUnderlyingNetworks(uid); |
| 1174 | if (networks != null) { |
| 1175 | // getUnderlyingNetworks() returns: |
| 1176 | // null => there was no VPN, or the VPN didn't specify anything, so we use the default. |
| 1177 | // empty array => the VPN explicitly said "no default network". |
| 1178 | // non-empty array => the VPN specified one or more default networks; we use the |
| 1179 | // first one. |
| 1180 | if (networks.length > 0) { |
| 1181 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 1182 | } else { |
| 1183 | nai = null; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1184 | } |
| 1185 | } |
| 1186 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1187 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1188 | return nai.getNetworkState(); |
| 1189 | } else { |
| 1190 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1191 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 1196 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1197 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid, |
| 1198 | boolean ignoreBlocked) { |
| 1199 | // Networks aren't blocked when ignoring blocked status |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1200 | if (ignoreBlocked) { |
| 1201 | return false; |
| 1202 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1203 | synchronized (mVpns) { |
| 1204 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1205 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1206 | return true; |
| 1207 | } |
| 1208 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1209 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1210 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, iface); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1213 | private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) { |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 1214 | if (ni == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1215 | return; |
| 1216 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1217 | final boolean blocked; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1218 | synchronized (mBlockedAppUids) { |
| 1219 | if (ni.getDetailedState() == DetailedState.BLOCKED && mBlockedAppUids.add(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1220 | blocked = true; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1221 | } else if (ni.isConnected() && mBlockedAppUids.remove(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1222 | blocked = false; |
| 1223 | } else { |
| 1224 | return; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1225 | } |
| 1226 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1227 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1228 | log(String.format("Returning %s NetworkInfo to uid=%d", action, uid)); |
| 1229 | mNetworkInfoBlockingLogs.log(action + " " + uid); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1230 | } |
| 1231 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1232 | private void maybeLogBlockedStatusChanged(NetworkRequestInfo nri, Network net, |
| 1233 | boolean blocked) { |
| 1234 | if (nri == null || net == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1235 | return; |
| 1236 | } |
| 1237 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1238 | log(String.format("Blocked status changed to %s for %d(%d) on netId %d", blocked, |
| 1239 | nri.mUid, nri.request.requestId, net.netId)); |
| 1240 | mNetworkInfoBlockingLogs.log(action + " " + nri.mUid); |
| 1241 | } |
| 1242 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1243 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1244 | * Apply any relevant filters to {@link NetworkState} for the given UID. For |
| 1245 | * example, this may mark the network as {@link DetailedState#BLOCKED} based |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1246 | * on {@link #isNetworkWithLinkPropertiesBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1247 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1248 | private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1249 | if (state == null || state.networkInfo == null || state.linkProperties == null) return; |
| 1250 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1251 | if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, ignoreBlocked)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1252 | state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1253 | } |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1254 | synchronized (mVpns) { |
| 1255 | if (mLockdownTracker != null) { |
| 1256 | mLockdownTracker.augmentNetworkInfo(state.networkInfo); |
| 1257 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1258 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1262 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 1263 | * It is assumed that at most one network is active at a time. If more |
| 1264 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1265 | * @return the info for the active network, or {@code null} if none is |
| 1266 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1267 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1268 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1270 | enforceAccessPermission(); |
| 1271 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1272 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1273 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1274 | maybeLogBlockedNetworkInfo(state.networkInfo, uid); |
| 1275 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | } |
| 1277 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1278 | @Override |
| 1279 | public Network getActiveNetwork() { |
| 1280 | enforceAccessPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1281 | return getActiveNetworkForUidInternal(Binder.getCallingUid(), false); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1285 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1286 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1287 | return getActiveNetworkForUidInternal(uid, ignoreBlocked); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1288 | } |
| 1289 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1290 | private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1291 | final int user = UserHandle.getUserId(uid); |
| 1292 | int vpnNetId = NETID_UNSET; |
| 1293 | synchronized (mVpns) { |
| 1294 | final Vpn vpn = mVpns.get(user); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1295 | // TODO : now that capabilities contain the UID, the appliesToUid test should |
| 1296 | // be removed as the satisfying test below should be enough. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1297 | if (vpn != null && vpn.appliesToUid(uid)) vpnNetId = vpn.getNetId(); |
| 1298 | } |
| 1299 | NetworkAgentInfo nai; |
| 1300 | if (vpnNetId != NETID_UNSET) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1301 | nai = getNetworkAgentInfoForNetId(vpnNetId); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1302 | if (nai != null) { |
| 1303 | final NetworkCapabilities requiredCaps = |
| 1304 | createDefaultNetworkCapabilitiesForUid(uid); |
| 1305 | if (requiredCaps.satisfiedByNetworkCapabilities(nai.networkCapabilities)) { |
| 1306 | return nai.network; |
| 1307 | } |
| 1308 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1309 | } |
| 1310 | nai = getDefaultNetwork(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1311 | if (nai != null |
| 1312 | && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, ignoreBlocked)) { |
| 1313 | nai = null; |
| 1314 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1315 | return nai != null ? nai.network : null; |
| 1316 | } |
| 1317 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1318 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1319 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1320 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1321 | final int uid = Binder.getCallingUid(); |
| 1322 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1323 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1324 | } |
| 1325 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1326 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1327 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1328 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1329 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1330 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1331 | return state.networkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1335 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1336 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1337 | final int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1338 | if (getVpnUnderlyingNetworks(uid) != null) { |
| 1339 | // A VPN is active, so we may need to return one of its underlying networks. This |
| 1340 | // information is not available in LegacyTypeTracker, so we have to get it from |
| 1341 | // getUnfilteredActiveNetworkState. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1342 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1343 | if (state.networkInfo != null && state.networkInfo.getType() == networkType) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1344 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1345 | return state.networkInfo; |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1346 | } |
| 1347 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1348 | final NetworkState state = getFilteredNetworkState(networkType, uid); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1349 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | } |
| 1351 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1352 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1353 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1354 | enforceAccessPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1355 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1356 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1357 | final NetworkState state = nai.getNetworkState(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1358 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1359 | return state.networkInfo; |
| 1360 | } else { |
| 1361 | return null; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1362 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1366 | public NetworkInfo[] getAllNetworkInfo() { |
| 1367 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1368 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1369 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1370 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1371 | NetworkInfo info = getNetworkInfo(networkType); |
| 1372 | if (info != null) { |
| 1373 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1376 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1377 | } |
| 1378 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1379 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1380 | public Network getNetworkForType(int networkType) { |
| 1381 | enforceAccessPermission(); |
| 1382 | final int uid = Binder.getCallingUid(); |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1383 | NetworkState state = getFilteredNetworkState(networkType, uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1384 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, false)) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1385 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1386 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1387 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1388 | } |
| 1389 | |
| 1390 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1391 | public Network[] getAllNetworks() { |
| 1392 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1393 | synchronized (mNetworkForNetId) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1394 | final Network[] result = new Network[mNetworkForNetId.size()]; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1395 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1396 | result[i] = mNetworkForNetId.valueAt(i).network; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1397 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1398 | return result; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1399 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1400 | } |
| 1401 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1402 | @Override |
| 1403 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1404 | // The basic principle is: if an app's traffic could possibly go over a |
| 1405 | // network, without the app doing anything multinetwork-specific, |
| 1406 | // (hence, by "default"), then include that network's capabilities in |
| 1407 | // the array. |
| 1408 | // |
| 1409 | // In the normal case, app traffic only goes over the system's default |
| 1410 | // network connection, so that's the only network returned. |
| 1411 | // |
| 1412 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1413 | // over whatever underlying networks the VPN specifies, while other app |
| 1414 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1415 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1416 | // returned includes the VPN's underlying networks and the system |
| 1417 | // default. |
| 1418 | enforceAccessPermission(); |
| 1419 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1420 | HashMap<Network, NetworkCapabilities> result = new HashMap<>(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1421 | |
| 1422 | NetworkAgentInfo nai = getDefaultNetwork(); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1423 | NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1424 | if (nc != null) { |
| 1425 | result.put(nai.network, nc); |
| 1426 | } |
| 1427 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1428 | synchronized (mVpns) { |
| 1429 | if (!mLockdownEnabled) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1430 | Vpn vpn = mVpns.get(userId); |
| 1431 | if (vpn != null) { |
| 1432 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1433 | if (networks != null) { |
| 1434 | for (Network network : networks) { |
| 1435 | nai = getNetworkAgentInfoForNetwork(network); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1436 | nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1437 | if (nc != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1438 | result.put(network, nc); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | } |
| 1445 | |
| 1446 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1447 | out = result.values().toArray(out); |
| 1448 | return out; |
| 1449 | } |
| 1450 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1451 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1452 | public boolean isNetworkSupported(int networkType) { |
| 1453 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1454 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1455 | } |
| 1456 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1457 | /** |
| 1458 | * Return LinkProperties for the active (i.e., connected) default |
| 1459 | * network interface. It is assumed that at most one default network |
| 1460 | * is active at a time. If more than one is active, it is indeterminate |
| 1461 | * which will be returned. |
| 1462 | * @return the ip properties for the active network, or {@code null} if |
| 1463 | * none is active |
| 1464 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1465 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1466 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1467 | enforceAccessPermission(); |
| 1468 | final int uid = Binder.getCallingUid(); |
| 1469 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1470 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1473 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1474 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1475 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1476 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1477 | if (nai != null) { |
| 1478 | synchronized (nai) { |
| 1479 | return new LinkProperties(nai.linkProperties); |
| 1480 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1481 | } |
| 1482 | return null; |
| 1483 | } |
| 1484 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1485 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1486 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1487 | public LinkProperties getLinkProperties(Network network) { |
| 1488 | enforceAccessPermission(); |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1489 | return getLinkProperties(getNetworkAgentInfoForNetwork(network)); |
| 1490 | } |
| 1491 | |
| 1492 | private LinkProperties getLinkProperties(NetworkAgentInfo nai) { |
| 1493 | if (nai == null) { |
| 1494 | return null; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1495 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1496 | synchronized (nai) { |
| 1497 | return new LinkProperties(nai.linkProperties); |
| 1498 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1501 | private NetworkCapabilities getNetworkCapabilitiesInternal(NetworkAgentInfo nai) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1502 | if (nai != null) { |
| 1503 | synchronized (nai) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1504 | if (nai.networkCapabilities != null) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1505 | return networkCapabilitiesRestrictedForCallerPermissions( |
| 1506 | nai.networkCapabilities, |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1507 | Binder.getCallingPid(), Binder.getCallingUid()); |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1508 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1509 | } |
| 1510 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1511 | return null; |
| 1512 | } |
| 1513 | |
| 1514 | @Override |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1515 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1516 | enforceAccessPermission(); |
| 1517 | return getNetworkCapabilitiesInternal(getNetworkAgentInfoForNetwork(network)); |
| 1518 | } |
| 1519 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1520 | private NetworkCapabilities networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1521 | NetworkCapabilities nc, int callerPid, int callerUid) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1522 | final NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1523 | if (!checkSettingsPermission(callerPid, callerUid)) { |
| 1524 | newNc.setUids(null); |
| 1525 | newNc.setSSID(null); |
| 1526 | } |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 1527 | if (newNc.getNetworkSpecifier() != null) { |
| 1528 | newNc.setNetworkSpecifier(newNc.getNetworkSpecifier().redact()); |
| 1529 | } |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1530 | return newNc; |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1531 | } |
| 1532 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1533 | private void restrictRequestUidsForCaller(NetworkCapabilities nc) { |
| 1534 | if (!checkSettingsPermission()) { |
| 1535 | nc.setSingleUid(Binder.getCallingUid()); |
| 1536 | } |
| 1537 | } |
| 1538 | |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 1539 | private void restrictBackgroundRequestForCaller(NetworkCapabilities nc) { |
| 1540 | if (!mPermissionMonitor.hasUseBackgroundNetworksPermission(Binder.getCallingUid())) { |
| 1541 | nc.addCapability(NET_CAPABILITY_FOREGROUND); |
| 1542 | } |
| 1543 | } |
| 1544 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1545 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1546 | public NetworkState[] getAllNetworkState() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1547 | // Require internal since we're handing out IMSI details |
| 1548 | enforceConnectivityInternalPermission(); |
| 1549 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1550 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1551 | for (Network network : getAllNetworks()) { |
| 1552 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1553 | if (nai != null) { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1554 | // TODO (b/73321673) : NetworkState contains a copy of the |
| 1555 | // NetworkCapabilities, which may contain UIDs of apps to which the |
| 1556 | // network applies. Should the UIDs be cleared so as not to leak or |
| 1557 | // interfere ? |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1558 | result.add(nai.getNetworkState()); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1559 | } |
| 1560 | } |
| 1561 | return result.toArray(new NetworkState[result.size()]); |
| 1562 | } |
| 1563 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1564 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1565 | @Deprecated |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1566 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1567 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 1568 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 1569 | return new NetworkQuotaInfo(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1570 | } |
| 1571 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1572 | @Override |
| 1573 | public boolean isActiveNetworkMetered() { |
| 1574 | enforceAccessPermission(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1575 | |
Eric Enslen | c81ef19 | 2018-02-07 18:03:53 -0800 | [diff] [blame] | 1576 | final int uid = Binder.getCallingUid(); |
| 1577 | final NetworkCapabilities caps = getUnfilteredActiveNetworkState(uid).networkCapabilities; |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1578 | if (caps != null) { |
| 1579 | return !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 1580 | } else { |
| 1581 | // Always return the most conservative value |
| 1582 | return true; |
| 1583 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1584 | } |
| 1585 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1586 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1587 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1588 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1589 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1590 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1591 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1592 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1593 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1594 | /** |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1595 | * Ensures that the system cannot call a particular method. |
| 1596 | */ |
| 1597 | private boolean disallowedBecauseSystemCaller() { |
| 1598 | // TODO: start throwing a SecurityException when GnssLocationProvider stops calling |
| 1599 | // requestRouteToHost. |
| 1600 | if (isSystem(Binder.getCallingUid())) { |
| 1601 | log("This method exists only for app backwards compatibility" |
| 1602 | + " and must not be called by system services."); |
| 1603 | return true; |
| 1604 | } |
| 1605 | return false; |
| 1606 | } |
| 1607 | |
| 1608 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1609 | * Ensure that a network route exists to deliver traffic to the specified |
| 1610 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1611 | * @param networkType the type of the network over which traffic to the |
| 1612 | * specified host is to be routed |
| 1613 | * @param hostAddress the IP address of the host to which the route is |
| 1614 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1615 | * @return {@code true} on success, {@code false} on failure |
| 1616 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1617 | @Override |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1618 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1619 | if (disallowedBecauseSystemCaller()) { |
| 1620 | return false; |
| 1621 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1622 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1623 | if (mProtectedNetworks.contains(networkType)) { |
| 1624 | enforceConnectivityInternalPermission(); |
| 1625 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1626 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1627 | InetAddress addr; |
| 1628 | try { |
| 1629 | addr = InetAddress.getByAddress(hostAddress); |
| 1630 | } catch (UnknownHostException e) { |
| 1631 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1632 | return false; |
| 1633 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1634 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1635 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1636 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | return false; |
| 1638 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1639 | |
| 1640 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1641 | if (nai == null) { |
| 1642 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1643 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1644 | } else { |
| 1645 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1646 | } |
| 1647 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1648 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1649 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1650 | DetailedState netState; |
| 1651 | synchronized (nai) { |
| 1652 | netState = nai.networkInfo.getDetailedState(); |
| 1653 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1654 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1655 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1656 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1657 | log("requestRouteToHostAddress on down network " |
| 1658 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1659 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1660 | } |
| 1661 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1663 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1664 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1665 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1666 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1667 | LinkProperties lp; |
| 1668 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1669 | synchronized (nai) { |
| 1670 | lp = nai.linkProperties; |
| 1671 | netId = nai.network.netId; |
| 1672 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1673 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1674 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1675 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1676 | } finally { |
| 1677 | Binder.restoreCallingIdentity(token); |
| 1678 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1679 | } |
| 1680 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1681 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1682 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1683 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1684 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1685 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1686 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1687 | if (bestRoute.getGateway().equals(addr)) { |
| 1688 | // 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] | 1689 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1690 | } else { |
| 1691 | // if we will connect to this through another route, add a direct route |
| 1692 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1693 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1694 | } |
| 1695 | } |
Lorenzo Colitti | aa281e2 | 2015-09-04 13:12:42 +0900 | [diff] [blame] | 1696 | if (DBG) log("Adding legacy route " + bestRoute + |
| 1697 | " for UID/PID " + uid + "/" + Binder.getCallingPid()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1698 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1699 | mNMS.addLegacyRouteForNetId(netId, bestRoute, uid); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1700 | } catch (Exception e) { |
| 1701 | // never crash - catch them all |
| 1702 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1703 | return false; |
| 1704 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1705 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1706 | } |
| 1707 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1708 | @VisibleForTesting |
| 1709 | protected final INetdEventCallback mNetdEventCallback = new BaseNetdEventCallback() { |
| 1710 | @Override |
| 1711 | public void onPrivateDnsValidationEvent(int netId, String ipAddress, |
| 1712 | String hostname, boolean validated) { |
| 1713 | try { |
| 1714 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1715 | EVENT_PRIVATE_DNS_VALIDATION_UPDATE, |
| 1716 | new PrivateDnsValidationUpdate(netId, |
| 1717 | InetAddress.parseNumericAddress(ipAddress), |
| 1718 | hostname, validated))); |
| 1719 | } catch (IllegalArgumentException e) { |
| 1720 | loge("Error parsing ip address in validation event"); |
| 1721 | } |
| 1722 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1723 | |
| 1724 | @Override |
| 1725 | public void onDnsEvent(int netId, int eventType, int returnCode, String hostname, |
| 1726 | String[] ipAddresses, int ipAddressesCount, long timestamp, int uid) { |
| 1727 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
| 1728 | // Netd event only allow registrants from system. Each NetworkMonitor thread is under |
| 1729 | // the caller thread of registerNetworkAgent. Thus, it's not allowed to register netd |
| 1730 | // event callback for certain nai. e.g. cellular. Register here to pass to |
| 1731 | // NetworkMonitor instead. |
| 1732 | // TODO: Move the Dns Event to NetworkMonitor. Use Binder.clearCallingIdentity() in |
| 1733 | // registerNetworkAgent to have NetworkMonitor created with system process as design |
| 1734 | // expectation. Also, NetdEventListenerService only allow one callback from each |
| 1735 | // caller type. Need to re-factor NetdEventListenerService to allow multiple |
| 1736 | // NetworkMonitor registrants. |
| 1737 | if (nai != null && nai.satisfies(mDefaultRequest)) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1738 | try { |
| 1739 | nai.networkMonitor().notifyDnsResponse(returnCode); |
| 1740 | } catch (RemoteException e) { |
| 1741 | e.rethrowFromSystemServer(); |
| 1742 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1743 | } |
| 1744 | } |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 1745 | |
| 1746 | @Override |
| 1747 | public void onNat64PrefixEvent(int netId, boolean added, |
| 1748 | String prefixString, int prefixLength) { |
| 1749 | mHandler.post(() -> handleNat64PrefixEvent(netId, added, prefixString, prefixLength)); |
| 1750 | } |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1751 | }; |
| 1752 | |
| 1753 | @VisibleForTesting |
| 1754 | protected void registerNetdEventCallback() { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1755 | final IIpConnectivityMetrics ipConnectivityMetrics = |
| 1756 | IIpConnectivityMetrics.Stub.asInterface( |
| 1757 | ServiceManager.getService(IpConnectivityLog.SERVICE_NAME)); |
| 1758 | if (ipConnectivityMetrics == null) { |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1759 | Slog.wtf(TAG, "Missing IIpConnectivityMetrics"); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1760 | return; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1761 | } |
| 1762 | |
| 1763 | try { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1764 | ipConnectivityMetrics.addNetdEventCallback( |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1765 | INetdEventCallback.CALLBACK_CALLER_CONNECTIVITY_SERVICE, |
| 1766 | mNetdEventCallback); |
| 1767 | } catch (Exception e) { |
| 1768 | loge("Error registering netd callback: " + e); |
| 1769 | } |
| 1770 | } |
| 1771 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 1772 | private final INetworkPolicyListener mPolicyListener = new NetworkPolicyManager.Listener() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1773 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1774 | public void onUidRulesChanged(int uid, int uidRules) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1775 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UID_RULES_CHANGED, uid, uidRules)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1776 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1777 | @Override |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1778 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1779 | // caller is NPMS, since we only register with them |
| 1780 | if (LOGD_BLOCKED_NETWORKINFO) { |
| 1781 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1782 | } |
| 1783 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1784 | EVENT_DATA_SAVER_CHANGED, restrictBackground ? 1 : 0, 0)); |
| 1785 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1786 | // TODO: relocate this specific callback in Tethering. |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1787 | if (restrictBackground) { |
| 1788 | log("onRestrictBackgroundChanged(true): disabling tethering"); |
| 1789 | mTethering.untetherAll(); |
| 1790 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1791 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1792 | }; |
| 1793 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1794 | void handleUidRulesChanged(int uid, int newRules) { |
| 1795 | // skip update when we've already applied rules |
| 1796 | final int oldRules = mUidRules.get(uid, RULE_NONE); |
| 1797 | if (oldRules == newRules) return; |
| 1798 | |
| 1799 | maybeNotifyNetworkBlockedForNewUidRules(uid, newRules); |
| 1800 | |
| 1801 | if (newRules == RULE_NONE) { |
| 1802 | mUidRules.delete(uid); |
| 1803 | } else { |
| 1804 | mUidRules.put(uid, newRules); |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | void handleRestrictBackgroundChanged(boolean restrictBackground) { |
| 1809 | if (mRestrictBackground == restrictBackground) return; |
| 1810 | |
| 1811 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1812 | final boolean curMetered = nai.networkCapabilities.isMetered(); |
| 1813 | maybeNotifyNetworkBlocked(nai, curMetered, curMetered, mRestrictBackground, |
| 1814 | restrictBackground); |
| 1815 | } |
| 1816 | |
| 1817 | mRestrictBackground = restrictBackground; |
| 1818 | } |
| 1819 | |
| 1820 | private boolean isUidNetworkingWithVpnBlocked(int uid, int uidRules, boolean isNetworkMetered, |
| 1821 | boolean isBackgroundRestricted) { |
| 1822 | synchronized (mVpns) { |
| 1823 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
| 1824 | // Because the return value of this function depends on the list of UIDs the |
| 1825 | // always-on VPN blocks when in lockdown mode, when the always-on VPN changes that |
| 1826 | // list all state depending on the return value of this function has to be recomputed. |
| 1827 | // TODO: add a trigger when the always-on VPN sets its blocked UIDs to reevaluate and |
| 1828 | // send the necessary onBlockedStatusChanged callbacks. |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1829 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1830 | return true; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, uidRules, |
| 1835 | isNetworkMetered, isBackgroundRestricted); |
| 1836 | } |
| 1837 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 1838 | /** |
| 1839 | * Require that the caller is either in the same user or has appropriate permission to interact |
| 1840 | * across users. |
| 1841 | * |
| 1842 | * @param userId Target user for whatever operation the current IPC is supposed to perform. |
| 1843 | */ |
| 1844 | private void enforceCrossUserPermission(int userId) { |
| 1845 | if (userId == UserHandle.getCallingUserId()) { |
| 1846 | // Not a cross-user call. |
| 1847 | return; |
| 1848 | } |
| 1849 | mContext.enforceCallingOrSelfPermission( |
| 1850 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1851 | "ConnectivityService"); |
| 1852 | } |
| 1853 | |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1854 | private boolean checkAnyPermissionOf(String... permissions) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1855 | for (String permission : permissions) { |
| 1856 | if (mContext.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1857 | return true; |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1858 | } |
| 1859 | } |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1860 | return false; |
| 1861 | } |
| 1862 | |
| 1863 | private void enforceAnyPermissionOf(String... permissions) { |
| 1864 | if (!checkAnyPermissionOf(permissions)) { |
| 1865 | throw new SecurityException("Requires one of the following permissions: " |
| 1866 | + String.join(", ", permissions) + "."); |
| 1867 | } |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1868 | } |
| 1869 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1870 | private void enforceInternetPermission() { |
| 1871 | mContext.enforceCallingOrSelfPermission( |
| 1872 | android.Manifest.permission.INTERNET, |
| 1873 | "ConnectivityService"); |
| 1874 | } |
| 1875 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1876 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1877 | mContext.enforceCallingOrSelfPermission( |
| 1878 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1879 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1880 | } |
| 1881 | |
| 1882 | private void enforceChangePermission() { |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1883 | ConnectivityManager.enforceChangePermission(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1884 | } |
| 1885 | |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1886 | private void enforceSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1887 | enforceAnyPermissionOf( |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1888 | android.Manifest.permission.NETWORK_SETTINGS, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1889 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1890 | } |
| 1891 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1892 | private boolean checkSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1893 | return checkAnyPermissionOf( |
| 1894 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1895 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | private boolean checkSettingsPermission(int pid, int uid) { |
| 1899 | return PERMISSION_GRANTED == mContext.checkPermission( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1900 | android.Manifest.permission.NETWORK_SETTINGS, pid, uid) |
| 1901 | || PERMISSION_GRANTED == mContext.checkPermission( |
| 1902 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, pid, uid); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1903 | } |
| 1904 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1905 | private void enforceTetherAccessPermission() { |
| 1906 | mContext.enforceCallingOrSelfPermission( |
| 1907 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1908 | "ConnectivityService"); |
| 1909 | } |
| 1910 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1911 | private void enforceConnectivityInternalPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1912 | enforceAnyPermissionOf( |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1913 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1914 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1915 | } |
| 1916 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1917 | private void enforceControlAlwaysOnVpnPermission() { |
| 1918 | mContext.enforceCallingOrSelfPermission( |
| 1919 | android.Manifest.permission.CONTROL_ALWAYS_ON_VPN, |
| 1920 | "ConnectivityService"); |
| 1921 | } |
| 1922 | |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1923 | private void enforceNetworkStackSettingsOrSetup() { |
| 1924 | enforceAnyPermissionOf( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1925 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1926 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1927 | android.Manifest.permission.NETWORK_STACK, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1928 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1929 | } |
| 1930 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1931 | private boolean checkNetworkStackPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1932 | return checkAnyPermissionOf( |
| 1933 | android.Manifest.permission.NETWORK_STACK, |
| 1934 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1935 | } |
| 1936 | |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 1937 | private void enforceConnectivityRestrictedNetworksPermission() { |
| 1938 | try { |
| 1939 | mContext.enforceCallingOrSelfPermission( |
| 1940 | android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS, |
| 1941 | "ConnectivityService"); |
| 1942 | return; |
| 1943 | } catch (SecurityException e) { /* fallback to ConnectivityInternalPermission */ } |
| 1944 | enforceConnectivityInternalPermission(); |
| 1945 | } |
| 1946 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1947 | private void enforceKeepalivePermission() { |
Lorenzo Colitti | 7914ce5 | 2015-09-08 13:21:48 +0900 | [diff] [blame] | 1948 | mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1949 | } |
| 1950 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1951 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1952 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1953 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1954 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1958 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1959 | } |
| 1960 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1961 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1962 | synchronized (mVpns) { |
| 1963 | if (mLockdownTracker != null) { |
| 1964 | info = new NetworkInfo(info); |
| 1965 | mLockdownTracker.augmentNetworkInfo(info); |
| 1966 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1967 | } |
| 1968 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1969 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 1970 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 1971 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1972 | if (info.isFailover()) { |
| 1973 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 1974 | info.setFailover(false); |
| 1975 | } |
| 1976 | if (info.getReason() != null) { |
| 1977 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 1978 | } |
| 1979 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1980 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 1981 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1982 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1983 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1984 | return intent; |
| 1985 | } |
| 1986 | |
| 1987 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 1988 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 1989 | } |
| 1990 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1991 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1992 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 1993 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 1994 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1995 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 1996 | final long ident = Binder.clearCallingIdentity(); |
| 1997 | try { |
| 1998 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 1999 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2000 | } finally { |
| 2001 | Binder.restoreCallingIdentity(ident); |
| 2002 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2005 | private void sendStickyBroadcast(Intent intent) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2006 | synchronized (this) { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 2007 | if (!mSystemReady |
| 2008 | && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2009 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2010 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2011 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2012 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2013 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2016 | Bundle options = null; |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2017 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2018 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2019 | final NetworkInfo ni = intent.getParcelableExtra( |
| 2020 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 2021 | if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) { |
| 2022 | intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL); |
| 2023 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2024 | } else { |
| 2025 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 2026 | opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M); |
| 2027 | options = opts.toBundle(); |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2028 | } |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2029 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 2030 | try { |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2031 | bs.noteConnectivityChanged(intent.getIntExtra( |
| 2032 | ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE), |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 2033 | ni.getState().toString()); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2034 | } catch (RemoteException e) { |
| 2035 | } |
Chad Brubaker | ac92501 | 2018-03-08 10:37:09 -0800 | [diff] [blame] | 2036 | intent.addFlags(Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2037 | } |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2038 | try { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2039 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL, options); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2040 | } finally { |
| 2041 | Binder.restoreCallingIdentity(ident); |
| 2042 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2043 | } |
| 2044 | } |
| 2045 | |
| 2046 | void systemReady() { |
Chalard Jean | 655ad15 | 2018-06-08 12:47:42 +0900 | [diff] [blame] | 2047 | mProxyTracker.loadGlobalProxy(); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2048 | registerNetdEventCallback(); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 2049 | mTethering.systemReady(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2050 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2051 | synchronized (this) { |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2052 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2053 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2054 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2055 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2056 | } |
| 2057 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2058 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 2059 | // Try bringing up tracker, but KeyStore won't be ready yet for secondary users so wait |
| 2060 | // for user to unlock device too. |
| 2061 | updateLockdownVpn(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2062 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 2063 | // Create network requests for always-on networks. |
| 2064 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS)); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2065 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2066 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 2067 | |
| 2068 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2069 | } |
| 2070 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2071 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2072 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2073 | * |
| 2074 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2075 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2076 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2077 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2078 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2079 | |
| 2080 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2081 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2082 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2083 | if (networkAgent.networkCapabilities.hasTransport( |
| 2084 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2085 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2086 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Adam Lesinski | ed6160d | 2015-08-18 11:47:07 -0700 | [diff] [blame] | 2087 | 10); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2088 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2089 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 2090 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2091 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2092 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 2093 | 15); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2094 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2095 | } else { |
| 2096 | // do not track any other networks |
| 2097 | timeout = 0; |
| 2098 | } |
| 2099 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2100 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2101 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2102 | mNMS.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2103 | } catch (Exception e) { |
| 2104 | // You shall not crash! |
| 2105 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2106 | } |
| 2107 | } |
| 2108 | } |
| 2109 | |
| 2110 | /** |
| 2111 | * Remove data activity tracking when network disconnects. |
| 2112 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2113 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2114 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 2115 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2116 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2117 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 2118 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2119 | try { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2120 | // the call fails silently if no idle timer setup for this interface |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2121 | mNMS.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2122 | } catch (Exception e) { |
| 2123 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2124 | } |
| 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | /** |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 2129 | * Update data activity tracking when network state is updated. |
| 2130 | */ |
| 2131 | private void updateDataActivityTracking(NetworkAgentInfo newNetwork, |
| 2132 | NetworkAgentInfo oldNetwork) { |
| 2133 | if (newNetwork != null) { |
| 2134 | setupDataActivityTracking(newNetwork); |
| 2135 | } |
| 2136 | if (oldNetwork != null) { |
| 2137 | removeDataActivityTracking(oldNetwork); |
| 2138 | } |
| 2139 | } |
| 2140 | /** |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2141 | * Reads the network specific MTU size from resources. |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2142 | * and set it on it's iface. |
| 2143 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2144 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 2145 | final String iface = newLp.getInterfaceName(); |
| 2146 | final int mtu = newLp.getMtu(); |
Pierre Imai | 54f0d9e | 2016-02-08 16:01:40 +0900 | [diff] [blame] | 2147 | if (oldLp == null && mtu == 0) { |
| 2148 | // Silently ignore unset MTU value. |
| 2149 | return; |
| 2150 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2151 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 2152 | if (VDBG) log("identical MTU - not setting"); |
| 2153 | return; |
| 2154 | } |
Robert Greenwalt | 4307403 | 2014-08-15 17:53:05 -0700 | [diff] [blame] | 2155 | if (LinkProperties.isValidMtu(mtu, newLp.hasGlobalIPv6Address()) == false) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2156 | if (mtu != 0) loge("Unexpected mtu value: " + mtu + ", " + iface); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2157 | return; |
| 2158 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2159 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 2160 | // Cannot set MTU without interface name |
| 2161 | if (TextUtils.isEmpty(iface)) { |
| 2162 | loge("Setting MTU size with null iface."); |
| 2163 | return; |
| 2164 | } |
| 2165 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2166 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2167 | if (VDBG || DDBG) log("Setting MTU size: " + iface + ", " + mtu); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2168 | mNMS.setMtu(iface, mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2169 | } catch (Exception e) { |
| 2170 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2171 | } |
| 2172 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2173 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2174 | @VisibleForTesting |
| 2175 | protected 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] | 2176 | private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd"; |
| 2177 | |
| 2178 | // Overridden for testing purposes to avoid writing to SystemProperties. |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 2179 | @VisibleForTesting |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2180 | protected MockableSystemProperties getSystemProperties() { |
| 2181 | return new MockableSystemProperties(); |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 2182 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2183 | |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 2184 | private void updateTcpBufferSizes(String tcpBufferSizes) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2185 | String[] values = null; |
| 2186 | if (tcpBufferSizes != null) { |
| 2187 | values = tcpBufferSizes.split(","); |
| 2188 | } |
| 2189 | |
| 2190 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2191 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2192 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 2193 | values = tcpBufferSizes.split(","); |
| 2194 | } |
| 2195 | |
| 2196 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 2197 | |
| 2198 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2199 | if (VDBG || DDBG) Slog.d(TAG, "Setting tx/rx TCP buffers to " + tcpBufferSizes); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2200 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2201 | String rmemValues = String.join(" ", values[0], values[1], values[2]); |
| 2202 | String wmemValues = String.join(" ", values[3], values[4], values[5]); |
| 2203 | mNetd.setTcpRWmemorySize(rmemValues, wmemValues); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2204 | mCurrentTcpBufferSizes = tcpBufferSizes; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2205 | } catch (RemoteException | ServiceSpecificException e) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2206 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2207 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2208 | |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2209 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2210 | Settings.Global.TCP_DEFAULT_INIT_RWND, |
| 2211 | mSystemProperties.getInt("net.tcp.default_init_rwnd", 0)); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2212 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2213 | if (rwndValue != 0) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2214 | mSystemProperties.set(sysctlKey, rwndValue.toString()); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2215 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2216 | } |
| 2217 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2218 | @Override |
| 2219 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2220 | String restoreDefaultNetworkDelayStr = mSystemProperties.get( |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2221 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2222 | if(restoreDefaultNetworkDelayStr != null && |
| 2223 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2224 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 2225 | return Integer.parseInt(restoreDefaultNetworkDelayStr); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2226 | } catch (NumberFormatException e) { |
| 2227 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2228 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2229 | // if the system property isn't set, use the value for the apn type |
| 2230 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2231 | |
| 2232 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2233 | (mNetConfigs[networkType] != null)) { |
| 2234 | ret = mNetConfigs[networkType].restoreTime; |
| 2235 | } |
| 2236 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2237 | } |
| 2238 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2239 | private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { |
| 2240 | final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); |
| 2241 | final long DIAG_TIME_MS = 5000; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2242 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2243 | // Start gathering diagnostic information. |
| 2244 | netDiags.add(new NetworkDiagnostics( |
| 2245 | nai.network, |
| 2246 | new LinkProperties(nai.linkProperties), // Must be a copy. |
| 2247 | DIAG_TIME_MS)); |
| 2248 | } |
| 2249 | |
| 2250 | for (NetworkDiagnostics netDiag : netDiags) { |
| 2251 | pw.println(); |
| 2252 | netDiag.waitForMeasurements(); |
| 2253 | netDiag.dump(pw); |
| 2254 | } |
| 2255 | } |
| 2256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2257 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2258 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2259 | PriorityDump.dump(mPriorityDumper, fd, writer, args); |
| 2260 | } |
| 2261 | |
| 2262 | private void doDump(FileDescriptor fd, PrintWriter writer, String[] args, boolean asProto) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2263 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2264 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2265 | if (asProto) return; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2266 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2267 | if (ArrayUtils.contains(args, DIAG_ARG)) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2268 | dumpNetworkDiagnostics(pw); |
| 2269 | return; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2270 | } else if (ArrayUtils.contains(args, TETHERING_ARG)) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 2271 | mTethering.dump(fd, pw, args); |
| 2272 | return; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2273 | } else if (ArrayUtils.contains(args, NETWORK_ARG)) { |
| 2274 | dumpNetworks(pw); |
| 2275 | return; |
| 2276 | } else if (ArrayUtils.contains(args, REQUEST_ARG)) { |
| 2277 | dumpNetworkRequests(pw); |
| 2278 | return; |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2279 | } |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 2280 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2281 | pw.print("NetworkFactories for:"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2282 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2283 | pw.print(" " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2284 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2285 | pw.println(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2286 | pw.println(); |
| 2287 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2288 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2289 | pw.print("Active default network: "); |
| 2290 | if (defaultNai == null) { |
| 2291 | pw.println("none"); |
| 2292 | } else { |
| 2293 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2295 | pw.println(); |
| 2296 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2297 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2298 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2299 | dumpNetworks(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2300 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2301 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2302 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 2303 | pw.print("Restrict background: "); |
| 2304 | pw.println(mRestrictBackground); |
| 2305 | pw.println(); |
| 2306 | |
| 2307 | pw.println("Status for known UIDs:"); |
| 2308 | pw.increaseIndent(); |
| 2309 | final int size = mUidRules.size(); |
| 2310 | for (int i = 0; i < size; i++) { |
| 2311 | // Don't crash if the array is modified while dumping in bugreports. |
| 2312 | try { |
| 2313 | final int uid = mUidRules.keyAt(i); |
| 2314 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2315 | pw.println("UID=" + uid + " rules=" + uidRulesToString(uidRules)); |
| 2316 | } catch (ArrayIndexOutOfBoundsException e) { |
| 2317 | pw.println(" ArrayIndexOutOfBoundsException"); |
| 2318 | } catch (ConcurrentModificationException e) { |
| 2319 | pw.println(" ConcurrentModificationException"); |
| 2320 | } |
| 2321 | } |
| 2322 | pw.println(); |
| 2323 | pw.decreaseIndent(); |
| 2324 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2325 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2326 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2327 | dumpNetworkRequests(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2328 | pw.decreaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2329 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2330 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2331 | mLegacyTypeTracker.dump(pw); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2332 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2333 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2334 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2335 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2336 | pw.println(); |
| 2337 | mKeepaliveTracker.dump(pw); |
| 2338 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2339 | pw.println(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 2340 | dumpAvoidBadWifiSettings(pw); |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2341 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 2342 | pw.println(); |
| 2343 | mMultipathPolicyTracker.dump(pw); |
| 2344 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2345 | if (ArrayUtils.contains(args, SHORT_ARG) == false) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2346 | pw.println(); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2347 | pw.println("mNetworkRequestInfoLogs (most recent first):"); |
| 2348 | pw.increaseIndent(); |
| 2349 | mNetworkRequestInfoLogs.reverseDump(fd, pw, args); |
| 2350 | pw.decreaseIndent(); |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 2351 | |
| 2352 | pw.println(); |
| 2353 | pw.println("mNetworkInfoBlockingLogs (most recent first):"); |
| 2354 | pw.increaseIndent(); |
| 2355 | mNetworkInfoBlockingLogs.reverseDump(fd, pw, args); |
| 2356 | pw.decreaseIndent(); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2357 | |
| 2358 | pw.println(); |
| 2359 | pw.println("NetTransition WakeLock activity (most recent first):"); |
| 2360 | pw.increaseIndent(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 2361 | pw.println("total acquisitions: " + mTotalWakelockAcquisitions); |
| 2362 | pw.println("total releases: " + mTotalWakelockReleases); |
| 2363 | pw.println("cumulative duration: " + (mTotalWakelockDurationMs / 1000) + "s"); |
| 2364 | pw.println("longest duration: " + (mMaxWakelockDurationMs / 1000) + "s"); |
| 2365 | if (mTotalWakelockAcquisitions > mTotalWakelockReleases) { |
| 2366 | long duration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 2367 | pw.println("currently holding WakeLock for: " + (duration / 1000) + "s"); |
| 2368 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2369 | mWakelockLogs.reverseDump(fd, pw, args); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 2370 | |
| 2371 | pw.println(); |
| 2372 | pw.println("bandwidth update requests (by uid):"); |
| 2373 | pw.increaseIndent(); |
| 2374 | synchronized (mBandwidthRequests) { |
| 2375 | for (int i = 0; i < mBandwidthRequests.size(); i++) { |
| 2376 | pw.println("[" + mBandwidthRequests.keyAt(i) |
| 2377 | + "]: " + mBandwidthRequests.valueAt(i)); |
| 2378 | } |
| 2379 | } |
| 2380 | pw.decreaseIndent(); |
| 2381 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2382 | pw.decreaseIndent(); |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2383 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2384 | } |
| 2385 | |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2386 | private void dumpNetworks(IndentingPrintWriter pw) { |
| 2387 | for (NetworkAgentInfo nai : networksSortedById()) { |
| 2388 | pw.println(nai.toString()); |
| 2389 | pw.increaseIndent(); |
| 2390 | pw.println(String.format( |
| 2391 | "Requests: REQUEST:%d LISTEN:%d BACKGROUND_REQUEST:%d total:%d", |
| 2392 | nai.numForegroundNetworkRequests(), |
| 2393 | nai.numNetworkRequests() - nai.numRequestNetworkRequests(), |
| 2394 | nai.numBackgroundNetworkRequests(), |
| 2395 | nai.numNetworkRequests())); |
| 2396 | pw.increaseIndent(); |
| 2397 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 2398 | pw.println(nai.requestAt(i).toString()); |
| 2399 | } |
| 2400 | pw.decreaseIndent(); |
| 2401 | pw.println("Lingered:"); |
| 2402 | pw.increaseIndent(); |
| 2403 | nai.dumpLingerTimers(pw); |
| 2404 | pw.decreaseIndent(); |
| 2405 | pw.decreaseIndent(); |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | private void dumpNetworkRequests(IndentingPrintWriter pw) { |
| 2410 | for (NetworkRequestInfo nri : requestsSortedById()) { |
| 2411 | pw.println(nri.toString()); |
| 2412 | } |
| 2413 | } |
| 2414 | |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2415 | /** |
| 2416 | * Return an array of all current NetworkAgentInfos sorted by network id. |
| 2417 | */ |
| 2418 | private NetworkAgentInfo[] networksSortedById() { |
| 2419 | NetworkAgentInfo[] networks = new NetworkAgentInfo[0]; |
| 2420 | networks = mNetworkAgentInfos.values().toArray(networks); |
| 2421 | Arrays.sort(networks, Comparator.comparingInt(nai -> nai.network.netId)); |
| 2422 | return networks; |
| 2423 | } |
| 2424 | |
| 2425 | /** |
| 2426 | * Return an array of all current NetworkRequest sorted by request id. |
| 2427 | */ |
| 2428 | private NetworkRequestInfo[] requestsSortedById() { |
| 2429 | NetworkRequestInfo[] requests = new NetworkRequestInfo[0]; |
| 2430 | requests = mNetworkRequests.values().toArray(requests); |
| 2431 | Arrays.sort(requests, Comparator.comparingInt(nri -> nri.request.requestId)); |
| 2432 | return requests; |
| 2433 | } |
| 2434 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2435 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2436 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2437 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2438 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 2439 | if (officialNai != null || VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2440 | loge(eventName(what) + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2441 | " - " + nai); |
| 2442 | } |
| 2443 | return false; |
| 2444 | } |
| 2445 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2446 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2447 | private class NetworkStateTrackerHandler extends Handler { |
| 2448 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 2449 | super(looper); |
| 2450 | } |
| 2451 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2452 | private boolean maybeHandleAsyncChannelMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2453 | switch (msg.what) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2454 | default: |
| 2455 | return false; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2456 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2457 | handleAsyncChannelHalfConnect(msg); |
| 2458 | break; |
| 2459 | } |
| 2460 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 2461 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2462 | if (nai != null) nai.asyncChannel.disconnect(); |
| 2463 | break; |
| 2464 | } |
| 2465 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 2466 | handleAsyncChannelDisconnected(msg); |
| 2467 | break; |
| 2468 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2469 | } |
| 2470 | return true; |
| 2471 | } |
| 2472 | |
| 2473 | private void maybeHandleNetworkAgentMessage(Message msg) { |
| 2474 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2475 | if (nai == null) { |
| 2476 | if (VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2477 | log(String.format("%s from unknown NetworkAgent", eventName(msg.what))); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2478 | } |
| 2479 | return; |
| 2480 | } |
| 2481 | |
| 2482 | switch (msg.what) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2483 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2484 | final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj; |
| 2485 | if (networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL) || |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 2486 | networkCapabilities.hasCapability(NET_CAPABILITY_VALIDATED) || |
| 2487 | networkCapabilities.hasCapability(NET_CAPABILITY_FOREGROUND)) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2488 | Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2489 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2490 | updateCapabilities(nai.getCurrentScore(), nai, networkCapabilities); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2491 | break; |
| 2492 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2493 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 2494 | handleUpdateLinkProperties(nai, (LinkProperties) msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2495 | break; |
| 2496 | } |
| 2497 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2498 | NetworkInfo info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2499 | updateNetworkInfo(nai, info); |
| 2500 | break; |
| 2501 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2502 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
Roshan Pius | 64e99ef | 2018-08-02 10:25:02 -0700 | [diff] [blame] | 2503 | updateNetworkScore(nai, msg.arg1); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2504 | break; |
| 2505 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2506 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2507 | if (nai.everConnected && !nai.networkMisc.explicitlySelected) { |
| 2508 | loge("ERROR: already-connected network explicitly selected."); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2509 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2510 | nai.networkMisc.explicitlySelected = true; |
Roshan Pius | eaf8dee | 2018-08-10 07:36:39 -0700 | [diff] [blame] | 2511 | nai.networkMisc.acceptUnvalidated = msg.arg1 == 1; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2512 | break; |
| 2513 | } |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 2514 | case NetworkAgent.EVENT_SOCKET_KEEPALIVE: { |
| 2515 | mKeepaliveTracker.handleEventSocketKeepalive(nai, msg); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2516 | break; |
| 2517 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | private boolean maybeHandleNetworkMonitorMessage(Message msg) { |
| 2522 | switch (msg.what) { |
| 2523 | default: |
| 2524 | return false; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2525 | case EVENT_NETWORK_TESTED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2526 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2527 | if (nai == null) break; |
| 2528 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2529 | final boolean valid = (msg.arg1 == NETWORK_TEST_RESULT_VALID); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2530 | final boolean wasValidated = nai.lastValidated; |
| 2531 | final boolean wasDefault = isDefaultNetwork(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2532 | if (nai.everCaptivePortalDetected && !nai.captivePortalLoginNotified |
| 2533 | && valid) { |
| 2534 | nai.captivePortalLoginNotified = true; |
| 2535 | showNetworkNotification(nai, NotificationType.LOGGED_IN); |
| 2536 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2537 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2538 | final String redirectUrl = (msg.obj instanceof String) ? (String) msg.obj : ""; |
| 2539 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2540 | if (DBG) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2541 | final String logMsg = !TextUtils.isEmpty(redirectUrl) |
| 2542 | ? " with redirect to " + redirectUrl |
| 2543 | : ""; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2544 | log(nai.name() + " validation " + (valid ? "passed" : "failed") + logMsg); |
| 2545 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2546 | if (valid != nai.lastValidated) { |
| 2547 | if (wasDefault) { |
| 2548 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkValidity( |
| 2549 | SystemClock.elapsedRealtime(), valid); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2550 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2551 | final int oldScore = nai.getCurrentScore(); |
| 2552 | nai.lastValidated = valid; |
| 2553 | nai.everValidated |= valid; |
| 2554 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
| 2555 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2556 | if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2557 | if (valid) { |
| 2558 | handleFreshlyValidatedNetwork(nai); |
| 2559 | // Clear NO_INTERNET and LOST_INTERNET notifications if network becomes |
| 2560 | // valid. |
| 2561 | mNotifier.clearNotification(nai.network.netId, |
| 2562 | NotificationType.NO_INTERNET); |
| 2563 | mNotifier.clearNotification(nai.network.netId, |
| 2564 | NotificationType.LOST_INTERNET); |
| 2565 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2566 | } |
| 2567 | updateInetCondition(nai); |
| 2568 | // Let the NetworkAgent know the state of its network |
| 2569 | Bundle redirectUrlBundle = new Bundle(); |
| 2570 | redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, redirectUrl); |
| 2571 | nai.asyncChannel.sendMessage( |
| 2572 | NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
| 2573 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
| 2574 | 0, redirectUrlBundle); |
| 2575 | if (wasValidated && !nai.lastValidated) { |
| 2576 | handleNetworkUnvalidated(nai); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2577 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2578 | break; |
| 2579 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2580 | case EVENT_PROVISIONING_NOTIFICATION: { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2581 | final int netId = msg.arg2; |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 2582 | final boolean visible = toBool(msg.arg1); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2583 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2584 | // If captive portal status has changed, update capabilities or disconnect. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2585 | if (nai != null && (visible != nai.lastCaptivePortalDetected)) { |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2586 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2587 | nai.lastCaptivePortalDetected = visible; |
| 2588 | nai.everCaptivePortalDetected |= visible; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2589 | if (visible) { |
| 2590 | nai.captivePortalLoginNotified = false; |
| 2591 | } |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2592 | if (nai.lastCaptivePortalDetected && |
| 2593 | Settings.Global.CAPTIVE_PORTAL_MODE_AVOID == getCaptivePortalMode()) { |
| 2594 | if (DBG) log("Avoiding captive portal network: " + nai.name()); |
| 2595 | nai.asyncChannel.sendMessage( |
| 2596 | NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 2597 | teardownUnneededNetwork(nai); |
| 2598 | break; |
| 2599 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2600 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2601 | } |
| 2602 | if (!visible) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2603 | // Only clear SIGN_IN and NETWORK_SWITCH notifications here, or else other |
| 2604 | // notifications belong to the same network may be cleared unexpected. |
| 2605 | mNotifier.clearNotification(netId, NotificationType.SIGN_IN); |
| 2606 | mNotifier.clearNotification(netId, NotificationType.NETWORK_SWITCH); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2607 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2608 | if (nai == null) { |
| 2609 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2610 | break; |
| 2611 | } |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2612 | if (!nai.networkMisc.provisioningNotificationDisabled) { |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 2613 | mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai, null, |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 2614 | (PendingIntent) msg.obj, nai.networkMisc.explicitlySelected); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2615 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2616 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2617 | break; |
| 2618 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2619 | case EVENT_PRIVATE_DNS_CONFIG_RESOLVED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2620 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2621 | if (nai == null) break; |
| 2622 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2623 | updatePrivateDns(nai, (PrivateDnsConfig) msg.obj); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2624 | break; |
| 2625 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2626 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2627 | return true; |
| 2628 | } |
| 2629 | |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2630 | private int getCaptivePortalMode() { |
| 2631 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 2632 | Settings.Global.CAPTIVE_PORTAL_MODE, |
| 2633 | Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
| 2634 | } |
| 2635 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2636 | private boolean maybeHandleNetworkAgentInfoMessage(Message msg) { |
| 2637 | switch (msg.what) { |
| 2638 | default: |
| 2639 | return false; |
| 2640 | case NetworkAgentInfo.EVENT_NETWORK_LINGER_COMPLETE: { |
| 2641 | NetworkAgentInfo nai = (NetworkAgentInfo) msg.obj; |
| 2642 | if (nai != null && isLiveNetworkAgent(nai, msg.what)) { |
| 2643 | handleLingerComplete(nai); |
| 2644 | } |
| 2645 | break; |
| 2646 | } |
| 2647 | } |
| 2648 | return true; |
| 2649 | } |
| 2650 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2651 | @Override |
| 2652 | public void handleMessage(Message msg) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2653 | if (!maybeHandleAsyncChannelMessage(msg) && |
| 2654 | !maybeHandleNetworkMonitorMessage(msg) && |
| 2655 | !maybeHandleNetworkAgentInfoMessage(msg)) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2656 | maybeHandleNetworkAgentMessage(msg); |
| 2657 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2658 | } |
| 2659 | } |
| 2660 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2661 | private class NetworkMonitorCallbacks extends INetworkMonitorCallbacks.Stub { |
| 2662 | private final NetworkAgentInfo mNai; |
| 2663 | |
| 2664 | private NetworkMonitorCallbacks(NetworkAgentInfo nai) { |
| 2665 | mNai = nai; |
| 2666 | } |
| 2667 | |
| 2668 | @Override |
| 2669 | public void onNetworkMonitorCreated(INetworkMonitor networkMonitor) { |
| 2670 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, |
| 2671 | new Pair<>(mNai, networkMonitor))); |
| 2672 | } |
| 2673 | |
| 2674 | @Override |
| 2675 | public void notifyNetworkTested(int testResult, @Nullable String redirectUrl) { |
| 2676 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage(EVENT_NETWORK_TESTED, |
| 2677 | testResult, mNai.network.netId, redirectUrl)); |
| 2678 | } |
| 2679 | |
| 2680 | @Override |
| 2681 | public void notifyPrivateDnsConfigResolved(PrivateDnsConfigParcel config) { |
| 2682 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2683 | EVENT_PRIVATE_DNS_CONFIG_RESOLVED, |
| 2684 | 0, mNai.network.netId, PrivateDnsConfig.fromParcel(config))); |
| 2685 | } |
| 2686 | |
| 2687 | @Override |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2688 | public void showProvisioningNotification(String action, String packageName) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2689 | final Intent intent = new Intent(action); |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2690 | intent.setPackage(packageName); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2691 | |
| 2692 | final PendingIntent pendingIntent; |
| 2693 | // Only the system server can register notifications with package "android" |
| 2694 | final long token = Binder.clearCallingIdentity(); |
| 2695 | try { |
| 2696 | pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 2697 | } finally { |
| 2698 | Binder.restoreCallingIdentity(token); |
| 2699 | } |
| 2700 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2701 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_SHOW, |
| 2702 | mNai.network.netId, |
| 2703 | pendingIntent)); |
| 2704 | } |
| 2705 | |
| 2706 | @Override |
| 2707 | public void hideProvisioningNotification() { |
| 2708 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2709 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_HIDE, |
| 2710 | mNai.network.netId)); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2714 | private boolean networkRequiresValidation(NetworkAgentInfo nai) { |
Lorenzo Colitti | ed3168e | 2019-01-23 17:54:08 +0900 | [diff] [blame] | 2715 | return isValidationRequired(nai.networkCapabilities); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
Erik Kline | c6d0022 | 2018-06-26 18:53:43 +0900 | [diff] [blame] | 2718 | private void handleFreshlyValidatedNetwork(NetworkAgentInfo nai) { |
| 2719 | if (nai == null) return; |
| 2720 | // If the Private DNS mode is opportunistic, reprogram the DNS servers |
| 2721 | // in order to restart a validation pass from within netd. |
| 2722 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2723 | if (cfg.useTls && TextUtils.isEmpty(cfg.hostname)) { |
| 2724 | updateDnses(nai.linkProperties, null, nai.network.netId); |
| 2725 | } |
| 2726 | } |
| 2727 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2728 | private void handlePrivateDnsSettingsChanged() { |
| 2729 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2730 | |
| 2731 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2732 | handlePerNetworkPrivateDnsConfig(nai, cfg); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2733 | if (networkRequiresValidation(nai)) { |
| 2734 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2735 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2736 | } |
| 2737 | } |
| 2738 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2739 | private void handlePerNetworkPrivateDnsConfig(NetworkAgentInfo nai, PrivateDnsConfig cfg) { |
| 2740 | // Private DNS only ever applies to networks that might provide |
| 2741 | // Internet access and therefore also require validation. |
| 2742 | if (!networkRequiresValidation(nai)) return; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2743 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2744 | // Notify the NetworkAgentInfo/NetworkMonitor in case NetworkMonitor needs to cancel or |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2745 | // schedule DNS resolutions. If a DNS resolution is required the |
| 2746 | // result will be sent back to us. |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2747 | try { |
| 2748 | nai.networkMonitor().notifyPrivateDnsChanged(cfg.toParcel()); |
| 2749 | } catch (RemoteException e) { |
| 2750 | e.rethrowFromSystemServer(); |
| 2751 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2752 | |
| 2753 | // With Private DNS bypass support, we can proceed to update the |
| 2754 | // Private DNS config immediately, even if we're in strict mode |
| 2755 | // and have not yet resolved the provider name into a set of IPs. |
| 2756 | updatePrivateDns(nai, cfg); |
| 2757 | } |
| 2758 | |
| 2759 | private void updatePrivateDns(NetworkAgentInfo nai, PrivateDnsConfig newCfg) { |
| 2760 | mDnsManager.updatePrivateDns(nai.network, newCfg); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2761 | updateDnses(nai.linkProperties, null, nai.network.netId); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2762 | } |
| 2763 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2764 | private void handlePrivateDnsValidationUpdate(PrivateDnsValidationUpdate update) { |
| 2765 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(update.netId); |
| 2766 | if (nai == null) { |
| 2767 | return; |
| 2768 | } |
| 2769 | mDnsManager.updatePrivateDnsValidation(update); |
| 2770 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2771 | } |
| 2772 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 2773 | private void handleNat64PrefixEvent(int netId, boolean added, String prefixString, |
| 2774 | int prefixLength) { |
| 2775 | NetworkAgentInfo nai = mNetworkForNetId.get(netId); |
| 2776 | if (nai == null) return; |
| 2777 | |
| 2778 | log(String.format("NAT64 prefix %s on netId %d: %s/%d", |
| 2779 | (added ? "added" : "removed"), netId, prefixString, prefixLength)); |
| 2780 | |
| 2781 | IpPrefix prefix = null; |
| 2782 | if (added) { |
| 2783 | try { |
| 2784 | prefix = new IpPrefix(InetAddresses.parseNumericAddress(prefixString), |
| 2785 | prefixLength); |
| 2786 | } catch (IllegalArgumentException e) { |
| 2787 | loge("Invalid NAT64 prefix " + prefixString + "/" + prefixLength); |
| 2788 | return; |
| 2789 | } |
| 2790 | } |
| 2791 | |
| 2792 | nai.clatd.setNat64Prefix(prefix); |
| 2793 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2794 | } |
| 2795 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2796 | private void updateLingerState(NetworkAgentInfo nai, long now) { |
| 2797 | // 1. Update the linger timer. If it's changed, reschedule or cancel the alarm. |
| 2798 | // 2. If the network was lingering and there are now requests, unlinger it. |
| 2799 | // 3. If this network is unneeded (which implies it is not lingering), and there is at least |
| 2800 | // one lingered request, start lingering. |
| 2801 | nai.updateLingerTimer(); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2802 | if (nai.isLingering() && nai.numForegroundNetworkRequests() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2803 | if (DBG) log("Unlingering " + nai.name()); |
| 2804 | nai.unlinger(); |
| 2805 | logNetworkEvent(nai, NetworkEvent.NETWORK_UNLINGER); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2806 | } else if (unneeded(nai, UnneededFor.LINGER) && nai.getLingerExpiry() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2807 | int lingerTime = (int) (nai.getLingerExpiry() - now); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2808 | if (DBG) log("Lingering " + nai.name() + " for " + lingerTime + "ms"); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2809 | nai.linger(); |
| 2810 | logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER); |
| 2811 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING, lingerTime); |
| 2812 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2813 | } |
| 2814 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2815 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2816 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2817 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2818 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2819 | if (VDBG) log("NetworkFactory connected"); |
| 2820 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2821 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 2822 | if (nri.request.isListen()) continue; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 2823 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2824 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 2825 | (nai != null ? nai.getCurrentScore() : 0), 0, nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2826 | } |
| 2827 | } else { |
| 2828 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2829 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2830 | } |
| 2831 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2832 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2833 | if (VDBG) log("NetworkAgent connected"); |
| 2834 | // A network agent has requested a connection. Establish the connection. |
| 2835 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2836 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2837 | } else { |
| 2838 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2839 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2840 | if (nai != null) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2841 | final boolean wasDefault = isDefaultNetwork(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2842 | synchronized (mNetworkForNetId) { |
| 2843 | mNetworkForNetId.remove(nai.network.netId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2844 | mNetIdInUse.delete(nai.network.netId); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2845 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2846 | // Just in case. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2847 | mLegacyTypeTracker.remove(nai, wasDefault); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2848 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2849 | } |
| 2850 | } |
| 2851 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2852 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2853 | // This is a no-op if it's called with a message designating a network that has |
| 2854 | // already been destroyed, because its reference will not be found in the relevant |
| 2855 | // maps. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2856 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2857 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2858 | if (nai != null) { |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2859 | disconnectAndDestroyNetwork(nai); |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2860 | } else { |
| 2861 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(msg.replyTo); |
| 2862 | if (DBG && nfi != null) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2866 | // Destroys a network, remove references to it from the internal state managed by |
| 2867 | // ConnectivityService, free its interfaces and clean up. |
| 2868 | // Must be called on the Handler thread. |
| 2869 | private void disconnectAndDestroyNetwork(NetworkAgentInfo nai) { |
| 2870 | if (DBG) { |
| 2871 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.numNetworkRequests()); |
| 2872 | } |
lucaslin | f80b83b | 2019-02-12 15:30:13 +0800 | [diff] [blame] | 2873 | // Clear all notifications of this network. |
| 2874 | mNotifier.clearNotification(nai.network.netId); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2875 | // A network agent has disconnected. |
| 2876 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2877 | // because they lost all their requests or because their score isn't good) |
| 2878 | // then they would disconnect organically, report their new state and then |
| 2879 | // disconnect the channel. |
| 2880 | if (nai.networkInfo.isConnected()) { |
| 2881 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2882 | null, null); |
| 2883 | } |
| 2884 | final boolean wasDefault = isDefaultNetwork(nai); |
| 2885 | if (wasDefault) { |
| 2886 | mDefaultInetConditionPublished = 0; |
| 2887 | // Log default network disconnection before required book-keeping. |
| 2888 | // Let rematchAllNetworksAndRequests() below record a new default network event |
| 2889 | // if there is a fallback. Taken together, the two form a X -> 0, 0 -> Y sequence |
| 2890 | // whose timestamps tell how long it takes to recover a default network. |
| 2891 | long now = SystemClock.elapsedRealtime(); |
| 2892 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent(now, null, nai); |
| 2893 | } |
| 2894 | notifyIfacesChangedForNetworkStats(); |
| 2895 | // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied |
| 2896 | // by other networks that are already connected. Perhaps that can be done by |
| 2897 | // sending all CALLBACK_LOST messages (for requests, not listens) at the end |
| 2898 | // of rematchAllNetworksAndRequests |
| 2899 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 2900 | mKeepaliveTracker.handleStopAllKeepalives(nai, SocketKeepalive.ERROR_INVALID_NETWORK); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2901 | for (String iface : nai.linkProperties.getAllInterfaceNames()) { |
| 2902 | // Disable wakeup packet monitoring for each interface. |
| 2903 | wakeupModifyInterface(iface, nai.networkCapabilities, false); |
| 2904 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2905 | try { |
| 2906 | nai.networkMonitor().notifyNetworkDisconnected(); |
| 2907 | } catch (RemoteException e) { |
| 2908 | e.rethrowFromSystemServer(); |
| 2909 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2910 | mNetworkAgentInfos.remove(nai.messenger); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame^] | 2911 | nai.clatd.update(); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2912 | synchronized (mNetworkForNetId) { |
| 2913 | // Remove the NetworkAgent, but don't mark the netId as |
| 2914 | // available until we've told netd to delete it below. |
| 2915 | mNetworkForNetId.remove(nai.network.netId); |
| 2916 | } |
| 2917 | // Remove all previously satisfied requests. |
| 2918 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 2919 | NetworkRequest request = nai.requestAt(i); |
| 2920 | NetworkAgentInfo currentNetwork = getNetworkForRequest(request.requestId); |
| 2921 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 2922 | clearNetworkForRequest(request.requestId); |
| 2923 | sendUpdatedScoreToFactories(request, 0); |
| 2924 | } |
| 2925 | } |
| 2926 | nai.clearLingerState(); |
| 2927 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 2928 | updateDataActivityTracking(null /* newNetwork */, nai); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2929 | notifyLockdownVpn(nai); |
| 2930 | ensureNetworkTransitionWakelock(nai.name()); |
| 2931 | } |
| 2932 | mLegacyTypeTracker.remove(nai, wasDefault); |
| 2933 | if (!nai.networkCapabilities.hasTransport(TRANSPORT_VPN)) { |
| 2934 | updateAllVpnsCapabilities(); |
| 2935 | } |
| 2936 | rematchAllNetworksAndRequests(null, 0); |
| 2937 | mLingerMonitor.noteDisconnect(nai); |
| 2938 | if (nai.created) { |
| 2939 | // Tell netd to clean up the configuration for this network |
| 2940 | // (routing rules, DNS, etc). |
| 2941 | // This may be slow as it requires a lot of netd shelling out to ip and |
| 2942 | // ip[6]tables to flush routes and remove the incoming packet mark rule, so do it |
| 2943 | // after we've rematched networks with requests which should make a potential |
| 2944 | // fallback network the default or requested a new network from the |
| 2945 | // NetworkFactories, so network traffic isn't interrupted for an unnecessarily |
| 2946 | // long time. |
| 2947 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2948 | mNMS.removeNetwork(nai.network.netId); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2949 | } catch (Exception e) { |
| 2950 | loge("Exception removing network: " + e); |
| 2951 | } |
| 2952 | mDnsManager.removeNetwork(nai.network); |
| 2953 | } |
| 2954 | synchronized (mNetworkForNetId) { |
| 2955 | mNetIdInUse.delete(nai.network.netId); |
| 2956 | } |
| 2957 | } |
| 2958 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2959 | // If this method proves to be too slow then we can maintain a separate |
| 2960 | // pendingIntent => NetworkRequestInfo map. |
| 2961 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 2962 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 2963 | Intent intent = pendingIntent.getIntent(); |
| 2964 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 2965 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 2966 | if (existingPendingIntent != null && |
| 2967 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 2968 | return entry.getValue(); |
| 2969 | } |
| 2970 | } |
| 2971 | return null; |
| 2972 | } |
| 2973 | |
| 2974 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 2975 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 2976 | |
| 2977 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 2978 | if (existingRequest != null) { // remove the existing request. |
| 2979 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 2980 | + nri.request + " because their intents matched."); |
| 2981 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid()); |
| 2982 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2983 | handleRegisterNetworkRequest(nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2984 | } |
| 2985 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2986 | private void handleRegisterNetworkRequest(NetworkRequestInfo nri) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2987 | mNetworkRequests.put(nri.request, nri); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2988 | mNetworkRequestInfoLogs.log("REGISTER " + nri); |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 2989 | if (nri.request.isListen()) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2990 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 2991 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 2992 | network.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 2993 | updateSignalStrengthThresholds(network, "REGISTER", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2997 | rematchAllNetworksAndRequests(null, 0); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 2998 | if (nri.request.isRequest() && getNetworkForRequest(nri.request.requestId) == null) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2999 | sendUpdatedScoreToFactories(nri.request, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3000 | } |
| 3001 | } |
| 3002 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3003 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 3004 | int callingUid) { |
| 3005 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 3006 | if (nri != null) { |
| 3007 | handleReleaseNetworkRequest(nri.request, callingUid); |
| 3008 | } |
| 3009 | } |
| 3010 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3011 | // Determines whether the network is the best (or could become the best, if it validated), for |
| 3012 | // none of a particular type of NetworkRequests. The type of NetworkRequests considered depends |
| 3013 | // on the value of reason: |
| 3014 | // |
| 3015 | // - UnneededFor.TEARDOWN: non-listen NetworkRequests. If a network is unneeded for this reason, |
| 3016 | // then it should be torn down. |
| 3017 | // - UnneededFor.LINGER: foreground NetworkRequests. If a network is unneeded for this reason, |
| 3018 | // then it should be lingered. |
| 3019 | private boolean unneeded(NetworkAgentInfo nai, UnneededFor reason) { |
| 3020 | final int numRequests; |
| 3021 | switch (reason) { |
| 3022 | case TEARDOWN: |
| 3023 | numRequests = nai.numRequestNetworkRequests(); |
| 3024 | break; |
| 3025 | case LINGER: |
| 3026 | numRequests = nai.numForegroundNetworkRequests(); |
| 3027 | break; |
| 3028 | default: |
| 3029 | Slog.wtf(TAG, "Invalid reason. Cannot happen."); |
| 3030 | return true; |
| 3031 | } |
| 3032 | |
| 3033 | if (!nai.everConnected || nai.isVPN() || nai.isLingering() || numRequests > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 3034 | return false; |
| 3035 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3036 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3037 | if (reason == UnneededFor.LINGER && nri.request.isBackgroundRequest()) { |
| 3038 | // Background requests don't affect lingering. |
| 3039 | continue; |
| 3040 | } |
| 3041 | |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3042 | // If this Network is already the highest scoring Network for a request, or if |
| 3043 | // there is hope for it to become one if it validated, then it is needed. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 3044 | if (nri.request.isRequest() && nai.satisfies(nri.request) && |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 3045 | (nai.isSatisfyingRequest(nri.request.requestId) || |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3046 | // Note that this catches two important cases: |
| 3047 | // 1. Unvalidated cellular will not be reaped when unvalidated WiFi |
| 3048 | // is currently satisfying the request. This is desirable when |
| 3049 | // cellular ends up validating but WiFi does not. |
| 3050 | // 2. Unvalidated WiFi will not be reaped when validated cellular |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 3051 | // is currently satisfying the request. This is desirable when |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3052 | // WiFi ends up validating and out scoring cellular. |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3053 | getNetworkForRequest(nri.request.requestId).getCurrentScore() < |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3054 | nai.getCurrentScoreAsValidated())) { |
| 3055 | return false; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3056 | } |
| 3057 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3058 | return true; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3059 | } |
| 3060 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3061 | private NetworkRequestInfo getNriForAppRequest( |
| 3062 | NetworkRequest request, int callingUid, String requestedOperation) { |
| 3063 | final NetworkRequestInfo nri = mNetworkRequests.get(request); |
| 3064 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3065 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 3066 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3067 | log(String.format("UID %d attempted to %s for unowned request %s", |
| 3068 | callingUid, requestedOperation, nri)); |
| 3069 | return null; |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3070 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3071 | } |
| 3072 | |
| 3073 | return nri; |
| 3074 | } |
| 3075 | |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3076 | private void handleTimedOutNetworkRequest(final NetworkRequestInfo nri) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3077 | if (mNetworkRequests.get(nri.request) == null) { |
| 3078 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3079 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3080 | if (getNetworkForRequest(nri.request.requestId) != null) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3081 | return; |
| 3082 | } |
| 3083 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3084 | log("releasing " + nri.request + " (timeout)"); |
| 3085 | } |
| 3086 | handleRemoveNetworkRequest(nri); |
| 3087 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_UNAVAIL, 0); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3088 | } |
| 3089 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3090 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3091 | final NetworkRequestInfo nri = |
| 3092 | getNriForAppRequest(request, callingUid, "release NetworkRequest"); |
| 3093 | if (nri == null) { |
| 3094 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3095 | } |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3096 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3097 | log("releasing " + nri.request + " (release request)"); |
| 3098 | } |
| 3099 | handleRemoveNetworkRequest(nri); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3100 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3101 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3102 | private void handleRemoveNetworkRequest(final NetworkRequestInfo nri) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3103 | nri.unlinkDeathRecipient(); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3104 | mNetworkRequests.remove(nri.request); |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3105 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3106 | synchronized (mUidToNetworkRequestCount) { |
| 3107 | int requests = mUidToNetworkRequestCount.get(nri.mUid, 0); |
| 3108 | if (requests < 1) { |
| 3109 | Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + |
| 3110 | nri.mUid); |
| 3111 | } else if (requests == 1) { |
| 3112 | mUidToNetworkRequestCount.removeAt( |
| 3113 | mUidToNetworkRequestCount.indexOfKey(nri.mUid)); |
| 3114 | } else { |
| 3115 | mUidToNetworkRequestCount.put(nri.mUid, requests - 1); |
| 3116 | } |
| 3117 | } |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3118 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3119 | mNetworkRequestInfoLogs.log("RELEASE " + nri); |
| 3120 | if (nri.request.isRequest()) { |
| 3121 | boolean wasKept = false; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3122 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3123 | if (nai != null) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3124 | boolean wasBackgroundNetwork = nai.isBackgroundNetwork(); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3125 | nai.removeRequest(nri.request.requestId); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3126 | if (VDBG || DDBG) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3127 | log(" Removing from current network " + nai.name() + |
| 3128 | ", leaving " + nai.numNetworkRequests() + " requests."); |
| 3129 | } |
| 3130 | // If there are still lingered requests on this network, don't tear it down, |
| 3131 | // but resume lingering instead. |
| 3132 | updateLingerState(nai, SystemClock.elapsedRealtime()); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3133 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3134 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
| 3135 | teardownUnneededNetwork(nai); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3136 | } else { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3137 | wasKept = true; |
| 3138 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3139 | clearNetworkForRequest(nri.request.requestId); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3140 | if (!wasBackgroundNetwork && nai.isBackgroundNetwork()) { |
| 3141 | // Went from foreground to background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 3142 | updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3143 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3144 | } |
| 3145 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3146 | // Maintain the illusion. When this request arrived, we might have pretended |
| 3147 | // that a network connected to serve it, even though the network was already |
| 3148 | // connected. Now that this request has gone away, we might have to pretend |
| 3149 | // that the network disconnected. LegacyTypeTracker will generate that |
| 3150 | // phantom disconnect for this type. |
| 3151 | if (nri.request.legacyType != TYPE_NONE && nai != null) { |
| 3152 | boolean doRemove = true; |
| 3153 | if (wasKept) { |
| 3154 | // check if any of the remaining requests for this network are for the |
| 3155 | // same legacy type - if so, don't remove the nai |
| 3156 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 3157 | NetworkRequest otherRequest = nai.requestAt(i); |
| 3158 | if (otherRequest.legacyType == nri.request.legacyType && |
| 3159 | otherRequest.isRequest()) { |
| 3160 | if (DBG) log(" still have other legacy request - leaving"); |
| 3161 | doRemove = false; |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 3162 | } |
| 3163 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 3164 | } |
| 3165 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3166 | if (doRemove) { |
| 3167 | mLegacyTypeTracker.remove(nri.request.legacyType, nai, false); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3168 | } |
| 3169 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3170 | |
| 3171 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 3172 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 3173 | nri.request); |
| 3174 | } |
| 3175 | } else { |
| 3176 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 3177 | // if this listen request applies and remove it. |
| 3178 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 3179 | nai.removeRequest(nri.request.requestId); |
| 3180 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3181 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3182 | updateSignalStrengthThresholds(nai, "RELEASE", nri.request); |
| 3183 | } |
| 3184 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3185 | } |
| 3186 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3187 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3188 | @Override |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3189 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 3190 | enforceConnectivityInternalPermission(); |
| 3191 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED, |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3192 | encodeBool(accept), encodeBool(always), network)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3193 | } |
| 3194 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3195 | @Override |
| 3196 | public void setAvoidUnvalidated(Network network) { |
| 3197 | enforceConnectivityInternalPermission(); |
| 3198 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_AVOID_UNVALIDATED, network)); |
| 3199 | } |
| 3200 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3201 | private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 3202 | if (DBG) log("handleSetAcceptUnvalidated network=" + network + |
| 3203 | " accept=" + accept + " always=" + always); |
| 3204 | |
| 3205 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3206 | if (nai == null) { |
| 3207 | // Nothing to do. |
| 3208 | return; |
| 3209 | } |
| 3210 | |
| 3211 | if (nai.everValidated) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3212 | // The network validated while the dialog box was up. Take no action. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3213 | return; |
| 3214 | } |
| 3215 | |
| 3216 | if (!nai.networkMisc.explicitlySelected) { |
| 3217 | Slog.wtf(TAG, "BUG: setAcceptUnvalidated non non-explicitly selected network"); |
| 3218 | } |
| 3219 | |
| 3220 | if (accept != nai.networkMisc.acceptUnvalidated) { |
| 3221 | int oldScore = nai.getCurrentScore(); |
| 3222 | nai.networkMisc.acceptUnvalidated = accept; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 3223 | rematchAllNetworksAndRequests(nai, oldScore); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3224 | sendUpdatedScoreToFactories(nai); |
| 3225 | } |
| 3226 | |
| 3227 | if (always) { |
| 3228 | nai.asyncChannel.sendMessage( |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3229 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, encodeBool(accept)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3230 | } |
| 3231 | |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3232 | if (!accept) { |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3233 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 3234 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3235 | // Teardown the network. |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3236 | teardownUnneededNetwork(nai); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3237 | } |
| 3238 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3239 | } |
| 3240 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3241 | private void handleSetAvoidUnvalidated(Network network) { |
| 3242 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3243 | if (nai == null || nai.lastValidated) { |
| 3244 | // Nothing to do. The network either disconnected or revalidated. |
| 3245 | return; |
| 3246 | } |
| 3247 | if (!nai.avoidUnvalidated) { |
| 3248 | int oldScore = nai.getCurrentScore(); |
| 3249 | nai.avoidUnvalidated = true; |
| 3250 | rematchAllNetworksAndRequests(nai, oldScore); |
| 3251 | sendUpdatedScoreToFactories(nai); |
| 3252 | } |
| 3253 | } |
| 3254 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3255 | private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3256 | if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3257 | mHandler.sendMessageDelayed( |
| 3258 | mHandler.obtainMessage(EVENT_PROMPT_UNVALIDATED, nai.network), |
| 3259 | PROMPT_UNVALIDATED_DELAY_MS); |
| 3260 | } |
| 3261 | |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3262 | @Override |
| 3263 | public void startCaptivePortalApp(Network network) { |
| 3264 | enforceConnectivityInternalPermission(); |
| 3265 | mHandler.post(() -> { |
| 3266 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3267 | if (nai == null) return; |
| 3268 | if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3269 | try { |
| 3270 | nai.networkMonitor().launchCaptivePortalApp(); |
| 3271 | } catch (RemoteException e) { |
| 3272 | e.rethrowFromSystemServer(); |
| 3273 | } |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3274 | }); |
| 3275 | } |
| 3276 | |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3277 | /** |
| 3278 | * NetworkStack endpoint to start the captive portal app. The NetworkStack needs to use this |
| 3279 | * endpoint as it does not have INTERACT_ACROSS_USERS_FULL itself. |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3280 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3281 | * @param appExtras Bundle to use as intent extras for the captive portal application. |
| 3282 | * Must be treated as opaque to avoid preventing the captive portal app to |
| 3283 | * update its arguments. |
| 3284 | */ |
| 3285 | @Override |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3286 | public void startCaptivePortalAppInternal(Network network, Bundle appExtras) { |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3287 | mContext.checkCallingOrSelfPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
| 3288 | |
| 3289 | final Intent appIntent = new Intent(ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN); |
| 3290 | appIntent.putExtras(appExtras); |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3291 | appIntent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL, |
| 3292 | new CaptivePortal(new CaptivePortalImpl(network).asBinder())); |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3293 | appIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3294 | |
| 3295 | Binder.withCleanCallingIdentity(() -> |
| 3296 | mContext.startActivityAsUser(appIntent, UserHandle.CURRENT)); |
| 3297 | } |
| 3298 | |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3299 | private class CaptivePortalImpl extends ICaptivePortal.Stub { |
| 3300 | private final Network mNetwork; |
| 3301 | |
| 3302 | private CaptivePortalImpl(Network network) { |
| 3303 | mNetwork = network; |
| 3304 | } |
| 3305 | |
| 3306 | @Override |
| 3307 | public void appResponse(final int response) throws RemoteException { |
| 3308 | if (response == CaptivePortal.APP_RETURN_WANTED_AS_IS) { |
| 3309 | enforceSettingsPermission(); |
| 3310 | } |
| 3311 | |
| 3312 | // getNetworkAgentInfoForNetwork is thread-safe |
| 3313 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(mNetwork); |
| 3314 | if (nai == null) return; |
| 3315 | |
| 3316 | // nai.networkMonitor() is thread-safe |
| 3317 | final INetworkMonitor nm = nai.networkMonitor(); |
| 3318 | if (nm == null) return; |
| 3319 | |
| 3320 | final long token = Binder.clearCallingIdentity(); |
| 3321 | try { |
| 3322 | nm.notifyCaptivePortalAppFinished(response); |
| 3323 | } finally { |
| 3324 | // Not using Binder.withCleanCallingIdentity() to keep the checked RemoteException |
| 3325 | Binder.restoreCallingIdentity(token); |
| 3326 | } |
| 3327 | } |
| 3328 | |
| 3329 | @Override |
| 3330 | public void logEvent(int eventId, String packageName) { |
| 3331 | enforceSettingsPermission(); |
| 3332 | |
| 3333 | new MetricsLogger().action(eventId, packageName); |
| 3334 | } |
| 3335 | } |
| 3336 | |
Hugo Benichi | c8e9e12 | 2016-09-14 23:23:08 +0000 | [diff] [blame] | 3337 | public boolean avoidBadWifi() { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3338 | return mMultinetworkPolicyTracker.getAvoidBadWifi(); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 3339 | } |
| 3340 | |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 3341 | @Override |
| 3342 | public boolean getAvoidBadWifi() { |
| 3343 | if (!checkNetworkStackPermission()) { |
| 3344 | throw new SecurityException("avoidBadWifi requires NETWORK_STACK permission"); |
| 3345 | } |
| 3346 | return avoidBadWifi(); |
| 3347 | } |
| 3348 | |
| 3349 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3350 | private void rematchForAvoidBadWifiUpdate() { |
| 3351 | rematchAllNetworksAndRequests(null, 0); |
| 3352 | for (NetworkAgentInfo nai: mNetworkAgentInfos.values()) { |
| 3353 | if (nai.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 3354 | sendUpdatedScoreToFactories(nai); |
| 3355 | } |
| 3356 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3357 | } |
| 3358 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3359 | // TODO: Evaluate whether this is of interest to other consumers of |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3360 | // MultinetworkPolicyTracker and worth moving out of here. |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3361 | private void dumpAvoidBadWifiSettings(IndentingPrintWriter pw) { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3362 | final boolean configRestrict = mMultinetworkPolicyTracker.configRestrictsAvoidBadWifi(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3363 | if (!configRestrict) { |
| 3364 | pw.println("Bad Wi-Fi avoidance: unrestricted"); |
| 3365 | return; |
| 3366 | } |
| 3367 | |
| 3368 | pw.println("Bad Wi-Fi avoidance: " + avoidBadWifi()); |
| 3369 | pw.increaseIndent(); |
| 3370 | pw.println("Config restrict: " + configRestrict); |
| 3371 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3372 | final String value = mMultinetworkPolicyTracker.getAvoidBadWifiSetting(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3373 | String description; |
| 3374 | // Can't use a switch statement because strings are legal case labels, but null is not. |
| 3375 | if ("0".equals(value)) { |
| 3376 | description = "get stuck"; |
| 3377 | } else if (value == null) { |
| 3378 | description = "prompt"; |
| 3379 | } else if ("1".equals(value)) { |
| 3380 | description = "avoid"; |
| 3381 | } else { |
| 3382 | description = value + " (?)"; |
| 3383 | } |
| 3384 | pw.println("User setting: " + description); |
| 3385 | pw.println("Network overrides:"); |
| 3386 | pw.increaseIndent(); |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 3387 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3388 | if (nai.avoidUnvalidated) { |
| 3389 | pw.println(nai.name()); |
| 3390 | } |
| 3391 | } |
| 3392 | pw.decreaseIndent(); |
| 3393 | pw.decreaseIndent(); |
| 3394 | } |
| 3395 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3396 | private void showNetworkNotification(NetworkAgentInfo nai, NotificationType type) { |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3397 | final String action; |
| 3398 | switch (type) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3399 | case LOGGED_IN: |
| 3400 | action = Settings.ACTION_WIFI_SETTINGS; |
| 3401 | mHandler.removeMessages(EVENT_TIMEOUT_NOTIFICATION); |
| 3402 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NOTIFICATION, |
| 3403 | nai.network.netId, 0), TIMEOUT_NOTIFICATION_DELAY_MS); |
| 3404 | break; |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3405 | case NO_INTERNET: |
| 3406 | action = ConnectivityManager.ACTION_PROMPT_UNVALIDATED; |
| 3407 | break; |
| 3408 | case LOST_INTERNET: |
| 3409 | action = ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION; |
| 3410 | break; |
| 3411 | default: |
| 3412 | Slog.wtf(TAG, "Unknown notification type " + type); |
| 3413 | return; |
| 3414 | } |
| 3415 | |
| 3416 | Intent intent = new Intent(action); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3417 | if (type != NotificationType.LOGGED_IN) { |
| 3418 | intent.setData(Uri.fromParts("netId", Integer.toString(nai.network.netId), null)); |
| 3419 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3420 | intent.setClassName("com.android.settings", |
| 3421 | "com.android.settings.wifi.WifiNoInternetDialog"); |
| 3422 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3423 | |
| 3424 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 3425 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 3426 | mNotifier.showNotification(nai.network.netId, type, nai, null, pendingIntent, true); |
| 3427 | } |
| 3428 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3429 | private void handlePromptUnvalidated(Network network) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3430 | if (VDBG || DDBG) log("handlePromptUnvalidated " + network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3431 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3432 | |
| 3433 | // Only prompt if the network is unvalidated and was explicitly selected by the user, and if |
| 3434 | // 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] | 3435 | // 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] | 3436 | if (nai == null || nai.everValidated || nai.everCaptivePortalDetected || |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3437 | !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated) { |
| 3438 | return; |
| 3439 | } |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3440 | showNetworkNotification(nai, NotificationType.NO_INTERNET); |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3441 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3442 | |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3443 | private void handleNetworkUnvalidated(NetworkAgentInfo nai) { |
| 3444 | NetworkCapabilities nc = nai.networkCapabilities; |
| 3445 | if (DBG) log("handleNetworkUnvalidated " + nai.name() + " cap=" + nc); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 3446 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3447 | if (nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) && |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3448 | mMultinetworkPolicyTracker.shouldNotifyWifiUnvalidated()) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3449 | showNetworkNotification(nai, NotificationType.LOST_INTERNET); |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3450 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3451 | } |
| 3452 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3453 | @Override |
| 3454 | public int getMultipathPreference(Network network) { |
| 3455 | enforceAccessPermission(); |
| 3456 | |
| 3457 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3458 | if (nai != null && nai.networkCapabilities |
| 3459 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3460 | return ConnectivityManager.MULTIPATH_PREFERENCE_UNMETERED; |
| 3461 | } |
| 3462 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3463 | Integer networkPreference = mMultipathPolicyTracker.getMultipathPreference(network); |
| 3464 | if (networkPreference != null) { |
| 3465 | return networkPreference; |
| 3466 | } |
| 3467 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3468 | return mMultinetworkPolicyTracker.getMeteredMultipathPreference(); |
| 3469 | } |
| 3470 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3471 | @Override |
| 3472 | public NetworkRequest getDefaultRequest() { |
| 3473 | return mDefaultRequest; |
| 3474 | } |
| 3475 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3476 | private class InternalHandler extends Handler { |
| 3477 | public InternalHandler(Looper looper) { |
| 3478 | super(looper); |
| 3479 | } |
| 3480 | |
| 3481 | @Override |
| 3482 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3483 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3484 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3485 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3486 | handleReleaseNetworkTransitionWakelock(msg.what); |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3487 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3488 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3489 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 3490 | mProxyTracker.loadDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3491 | break; |
| 3492 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3493 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3494 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3495 | break; |
| 3496 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3497 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3498 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 3499 | break; |
| 3500 | } |
| 3501 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 3502 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3503 | break; |
| 3504 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3505 | case EVENT_REGISTER_NETWORK_AGENT: { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3506 | final Pair<NetworkAgentInfo, INetworkMonitor> arg = |
| 3507 | (Pair<NetworkAgentInfo, INetworkMonitor>) msg.obj; |
| 3508 | handleRegisterNetworkAgent(arg.first, arg.second); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3509 | break; |
| 3510 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3511 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 3512 | case EVENT_REGISTER_NETWORK_LISTENER: { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3513 | handleRegisterNetworkRequest((NetworkRequestInfo) msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3514 | break; |
| 3515 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3516 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: |
| 3517 | case EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT: { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3518 | handleRegisterNetworkRequestWithIntent(msg); |
| 3519 | break; |
| 3520 | } |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3521 | case EVENT_TIMEOUT_NETWORK_REQUEST: { |
| 3522 | NetworkRequestInfo nri = (NetworkRequestInfo) msg.obj; |
| 3523 | handleTimedOutNetworkRequest(nri); |
| 3524 | break; |
| 3525 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3526 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 3527 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 3528 | break; |
| 3529 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3530 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3531 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3532 | break; |
| 3533 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3534 | case EVENT_SET_ACCEPT_UNVALIDATED: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3535 | Network network = (Network) msg.obj; |
| 3536 | handleSetAcceptUnvalidated(network, toBool(msg.arg1), toBool(msg.arg2)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3537 | break; |
| 3538 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3539 | case EVENT_SET_AVOID_UNVALIDATED: { |
| 3540 | handleSetAvoidUnvalidated((Network) msg.obj); |
| 3541 | break; |
| 3542 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3543 | case EVENT_PROMPT_UNVALIDATED: { |
| 3544 | handlePromptUnvalidated((Network) msg.obj); |
| 3545 | break; |
| 3546 | } |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 3547 | case EVENT_CONFIGURE_ALWAYS_ON_NETWORKS: { |
| 3548 | handleConfigureAlwaysOnNetworks(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3549 | break; |
| 3550 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3551 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3552 | case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3553 | mKeepaliveTracker.handleStartKeepalive(msg); |
| 3554 | break; |
| 3555 | } |
| 3556 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3557 | case NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3558 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); |
| 3559 | int slot = msg.arg1; |
| 3560 | int reason = msg.arg2; |
| 3561 | mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); |
| 3562 | break; |
| 3563 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3564 | case EVENT_SYSTEM_READY: { |
| 3565 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3566 | // Might have been called already in handleRegisterNetworkAgent since |
| 3567 | // mSystemReady is set before sending EVENT_SYSTEM_READY, but calling |
| 3568 | // this several times is fine. |
| 3569 | try { |
| 3570 | nai.networkMonitor().notifySystemReady(); |
| 3571 | } catch (RemoteException e) { |
| 3572 | e.rethrowFromSystemServer(); |
| 3573 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3574 | } |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3575 | mMultipathPolicyTracker.start(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3576 | break; |
| 3577 | } |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3578 | case EVENT_REVALIDATE_NETWORK: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3579 | handleReportNetworkConnectivity((Network) msg.obj, msg.arg1, toBool(msg.arg2)); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3580 | break; |
| 3581 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 3582 | case EVENT_PRIVATE_DNS_SETTINGS_CHANGED: |
| 3583 | handlePrivateDnsSettingsChanged(); |
| 3584 | break; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 3585 | case EVENT_PRIVATE_DNS_VALIDATION_UPDATE: |
| 3586 | handlePrivateDnsValidationUpdate( |
| 3587 | (PrivateDnsValidationUpdate) msg.obj); |
| 3588 | break; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3589 | case EVENT_UID_RULES_CHANGED: |
| 3590 | handleUidRulesChanged(msg.arg1, msg.arg2); |
| 3591 | break; |
| 3592 | case EVENT_DATA_SAVER_CHANGED: |
| 3593 | handleRestrictBackgroundChanged(toBool(msg.arg1)); |
| 3594 | break; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3595 | case EVENT_TIMEOUT_NOTIFICATION: |
| 3596 | mNotifier.clearNotification(msg.arg1, NotificationType.LOGGED_IN); |
| 3597 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3598 | } |
| 3599 | } |
| 3600 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3601 | |
| 3602 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3603 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3604 | public int tether(String iface, String callerPkg) { |
| 3605 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3606 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3607 | return mTethering.tether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3608 | } else { |
| 3609 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3610 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3611 | } |
| 3612 | |
| 3613 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3614 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3615 | public int untether(String iface, String callerPkg) { |
| 3616 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3617 | |
| 3618 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3619 | return mTethering.untether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3620 | } else { |
| 3621 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3622 | } |
| 3623 | } |
| 3624 | |
| 3625 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3626 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3627 | public int getLastTetherError(String iface) { |
| 3628 | enforceTetherAccessPermission(); |
| 3629 | |
| 3630 | if (isTetheringSupported()) { |
| 3631 | return mTethering.getLastTetherError(iface); |
| 3632 | } else { |
| 3633 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3634 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | // TODO - proper iface API for selection by property, inspection, etc |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3638 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3639 | public String[] getTetherableUsbRegexs() { |
| 3640 | enforceTetherAccessPermission(); |
| 3641 | if (isTetheringSupported()) { |
| 3642 | return mTethering.getTetherableUsbRegexs(); |
| 3643 | } else { |
| 3644 | return new String[0]; |
| 3645 | } |
| 3646 | } |
| 3647 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3648 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3649 | public String[] getTetherableWifiRegexs() { |
| 3650 | enforceTetherAccessPermission(); |
| 3651 | if (isTetheringSupported()) { |
| 3652 | return mTethering.getTetherableWifiRegexs(); |
| 3653 | } else { |
| 3654 | return new String[0]; |
| 3655 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3656 | } |
| 3657 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3658 | @Override |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3659 | public String[] getTetherableBluetoothRegexs() { |
| 3660 | enforceTetherAccessPermission(); |
| 3661 | if (isTetheringSupported()) { |
| 3662 | return mTethering.getTetherableBluetoothRegexs(); |
| 3663 | } else { |
| 3664 | return new String[0]; |
| 3665 | } |
| 3666 | } |
| 3667 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3668 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3669 | public int setUsbTethering(boolean enable, String callerPkg) { |
| 3670 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3671 | if (isTetheringSupported()) { |
| 3672 | return mTethering.setUsbTethering(enable); |
| 3673 | } else { |
| 3674 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3675 | } |
| 3676 | } |
| 3677 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3678 | // TODO - move iface listing, queries, etc to new module |
| 3679 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3680 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3681 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3682 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3683 | return mTethering.getTetherableIfaces(); |
| 3684 | } |
| 3685 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3686 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3687 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3688 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3689 | return mTethering.getTetheredIfaces(); |
| 3690 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3691 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3692 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3693 | public String[] getTetheringErroredIfaces() { |
| 3694 | enforceTetherAccessPermission(); |
| 3695 | return mTethering.getErroredIfaces(); |
| 3696 | } |
| 3697 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3698 | @Override |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 3699 | public String[] getTetheredDhcpRanges() { |
| 3700 | enforceConnectivityInternalPermission(); |
| 3701 | return mTethering.getTetheredDhcpRanges(); |
| 3702 | } |
| 3703 | |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3704 | @Override |
| 3705 | public boolean isTetheringSupported(String callerPkg) { |
| 3706 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3707 | return isTetheringSupported(); |
| 3708 | } |
| 3709 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3710 | // if ro.tether.denied = true we default to no tethering |
| 3711 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3712 | // had previously been turned off. |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3713 | private boolean isTetheringSupported() { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3714 | int defaultVal = encodeBool(!mSystemProperties.get("ro.tether.denied").equals("true")); |
| 3715 | boolean tetherSupported = toBool(Settings.Global.getInt(mContext.getContentResolver(), |
| 3716 | Settings.Global.TETHER_SUPPORTED, defaultVal)); |
| 3717 | boolean tetherEnabledInSettings = tetherSupported |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 3718 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Jeremy Klein | 246a1fe | 2017-03-17 14:30:02 -0700 | [diff] [blame] | 3719 | |
| 3720 | // Elevate to system UID to avoid caller requiring MANAGE_USERS permission. |
| 3721 | boolean adminUser = false; |
| 3722 | final long token = Binder.clearCallingIdentity(); |
| 3723 | try { |
| 3724 | adminUser = mUserManager.isAdminUser(); |
| 3725 | } finally { |
| 3726 | Binder.restoreCallingIdentity(token); |
| 3727 | } |
| 3728 | |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3729 | return tetherEnabledInSettings && adminUser && mTethering.hasTetherableConfiguration(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3730 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3731 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3732 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3733 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi, |
| 3734 | String callerPkg) { |
| 3735 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3736 | if (!isTetheringSupported()) { |
| 3737 | receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null); |
| 3738 | return; |
| 3739 | } |
| 3740 | mTethering.startTethering(type, receiver, showProvisioningUi); |
| 3741 | } |
| 3742 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3743 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3744 | public void stopTethering(int type, String callerPkg) { |
| 3745 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3746 | mTethering.stopTethering(type); |
| 3747 | } |
| 3748 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3749 | /** |
| 3750 | * Get the latest value of the tethering entitlement check. |
| 3751 | * |
| 3752 | * Note: Allow privileged apps who have TETHER_PRIVILEGED permission to access. If it turns |
| 3753 | * out some such apps are observed to abuse this API, change to per-UID limits on this API |
| 3754 | * if it's really needed. |
| 3755 | */ |
| 3756 | @Override |
| 3757 | public void getLatestTetheringEntitlementValue(int type, ResultReceiver receiver, |
| 3758 | boolean showEntitlementUi, String callerPkg) { |
| 3759 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3760 | mTethering.getLatestTetheringEntitlementValue(type, receiver, showEntitlementUi); |
| 3761 | } |
| 3762 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 3763 | // Called when we lose the default network and have no replacement yet. |
| 3764 | // This will automatically be cleared after X seconds or a new default network |
| 3765 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 3766 | // first caller and not restarted by subsequent callers. |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3767 | private void ensureNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3768 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3769 | if (mNetTransitionWakeLock.isHeld()) { |
| 3770 | return; |
| 3771 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3772 | mNetTransitionWakeLock.acquire(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 3773 | mLastWakeLockAcquireTimestamp = SystemClock.elapsedRealtime(); |
| 3774 | mTotalWakelockAcquisitions++; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3775 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3776 | mWakelockLogs.log("ACQUIRE for " + forWhom); |
| 3777 | Message msg = mHandler.obtainMessage(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 3778 | mHandler.sendMessageDelayed(msg, mNetTransitionWakeLockTimeout); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3779 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3780 | |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3781 | // Called when we gain a new default network to release the network transition wakelock in a |
| 3782 | // second, to allow a grace period for apps to reconnect over the new network. Pending expiry |
| 3783 | // message is cancelled. |
| 3784 | private void scheduleReleaseNetworkTransitionWakelock() { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3785 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3786 | if (!mNetTransitionWakeLock.isHeld()) { |
| 3787 | return; // expiry message released the lock first. |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3788 | } |
| 3789 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3790 | // Cancel self timeout on wakelock hold. |
| 3791 | mHandler.removeMessages(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 3792 | Message msg = mHandler.obtainMessage(EVENT_CLEAR_NET_TRANSITION_WAKELOCK); |
| 3793 | mHandler.sendMessageDelayed(msg, 1000); |
| 3794 | } |
| 3795 | |
| 3796 | // Called when either message of ensureNetworkTransitionWakelock or |
| 3797 | // scheduleReleaseNetworkTransitionWakelock is processed. |
| 3798 | private void handleReleaseNetworkTransitionWakelock(int eventId) { |
| 3799 | String event = eventName(eventId); |
| 3800 | synchronized (this) { |
| 3801 | if (!mNetTransitionWakeLock.isHeld()) { |
| 3802 | mWakelockLogs.log(String.format("RELEASE: already released (%s)", event)); |
| 3803 | Slog.w(TAG, "expected Net Transition WakeLock to be held"); |
| 3804 | return; |
| 3805 | } |
| 3806 | mNetTransitionWakeLock.release(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 3807 | long lockDuration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 3808 | mTotalWakelockDurationMs += lockDuration; |
| 3809 | mMaxWakelockDurationMs = Math.max(mMaxWakelockDurationMs, lockDuration); |
| 3810 | mTotalWakelockReleases++; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3811 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3812 | mWakelockLogs.log(String.format("RELEASE (%s)", event)); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3813 | } |
| 3814 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3815 | // 100 percent is full good, 0 is full bad. |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3816 | @Override |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3817 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 3818 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Lorenzo Colitti | 0831f66 | 2015-02-11 07:39:20 +0900 | [diff] [blame] | 3819 | if (nai == null) return; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3820 | reportNetworkConnectivity(nai.network, percentage > 50); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 3821 | } |
| 3822 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3823 | @Override |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3824 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 3825 | enforceAccessPermission(); |
| 3826 | enforceInternetPermission(); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3827 | final int uid = Binder.getCallingUid(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3828 | final int connectivityInfo = encodeBool(hasConnectivity); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3829 | mHandler.sendMessage( |
| 3830 | mHandler.obtainMessage(EVENT_REVALIDATE_NETWORK, uid, connectivityInfo, network)); |
| 3831 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 3832 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3833 | private void handleReportNetworkConnectivity( |
| 3834 | Network network, int uid, boolean hasConnectivity) { |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 3835 | final NetworkAgentInfo nai; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3836 | if (network == null) { |
| 3837 | nai = getDefaultNetwork(); |
| 3838 | } else { |
| 3839 | nai = getNetworkAgentInfoForNetwork(network); |
| 3840 | } |
Paul Jensen | 4e8050e | 2015-06-25 10:28:34 -0400 | [diff] [blame] | 3841 | if (nai == null || nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTING || |
| 3842 | nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTED) { |
| 3843 | return; |
| 3844 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3845 | // Revalidate if the app report does not match our current validated state. |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 3846 | if (hasConnectivity == nai.lastValidated) { |
| 3847 | return; |
| 3848 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3849 | if (DBG) { |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3850 | int netid = nai.network.netId; |
| 3851 | log("reportNetworkConnectivity(" + netid + ", " + hasConnectivity + ") by " + uid); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3852 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 3853 | // Validating a network that has not yet connected could result in a call to |
| 3854 | // rematchNetworkAndRequests() which is not meant to work on such networks. |
| 3855 | if (!nai.everConnected) { |
| 3856 | return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 3857 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 3858 | LinkProperties lp = getLinkProperties(nai); |
| 3859 | if (isNetworkWithLinkPropertiesBlocked(lp, uid, false)) { |
| 3860 | return; |
| 3861 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3862 | try { |
| 3863 | nai.networkMonitor().forceReevaluation(uid); |
| 3864 | } catch (RemoteException e) { |
| 3865 | e.rethrowFromSystemServer(); |
| 3866 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3867 | } |
| 3868 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 3869 | /** |
| 3870 | * Returns information about the proxy a certain network is using. If given a null network, it |
| 3871 | * it will return the proxy for the bound network for the caller app or the default proxy if |
| 3872 | * none. |
| 3873 | * |
| 3874 | * @param network the network we want to get the proxy information for. |
| 3875 | * @return Proxy information if a network has a proxy configured, or otherwise null. |
| 3876 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3877 | @Override |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3878 | public ProxyInfo getProxyForNetwork(Network network) { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 3879 | final ProxyInfo globalProxy = mProxyTracker.getGlobalProxy(); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3880 | if (globalProxy != null) return globalProxy; |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 3881 | if (network == null) { |
| 3882 | // Get the network associated with the calling UID. |
| 3883 | final Network activeNetwork = getActiveNetworkForUidInternal(Binder.getCallingUid(), |
| 3884 | true); |
| 3885 | if (activeNetwork == null) { |
| 3886 | return null; |
| 3887 | } |
| 3888 | return getLinkPropertiesProxyInfo(activeNetwork); |
| 3889 | } else if (queryUserAccess(Binder.getCallingUid(), network.netId)) { |
| 3890 | // Don't call getLinkProperties() as it requires ACCESS_NETWORK_STATE permission, which |
| 3891 | // caller may not have. |
| 3892 | return getLinkPropertiesProxyInfo(network); |
| 3893 | } |
| 3894 | // No proxy info available if the calling UID does not have network access. |
| 3895 | return null; |
| 3896 | } |
| 3897 | |
| 3898 | @VisibleForTesting |
| 3899 | protected boolean queryUserAccess(int uid, int netId) { |
| 3900 | return NetworkUtils.queryUserAccess(uid, netId); |
| 3901 | } |
| 3902 | |
| 3903 | private ProxyInfo getLinkPropertiesProxyInfo(Network network) { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3904 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3905 | if (nai == null) return null; |
| 3906 | synchronized (nai) { |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 3907 | final ProxyInfo linkHttpProxy = nai.linkProperties.getHttpProxy(); |
| 3908 | return linkHttpProxy == null ? null : new ProxyInfo(linkHttpProxy); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3909 | } |
| 3910 | } |
| 3911 | |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 3912 | @Override |
| 3913 | public void setGlobalProxy(final ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 3914 | enforceConnectivityInternalPermission(); |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 3915 | mProxyTracker.setGlobalProxy(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 3918 | @Override |
| 3919 | @Nullable |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3920 | public ProxyInfo getGlobalProxy() { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 3921 | return mProxyTracker.getGlobalProxy(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3922 | } |
| 3923 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3924 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 3925 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 3926 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 3927 | proxy = null; |
| 3928 | } |
Chalard Jean | daab8f9 | 2018-06-08 12:20:15 +0900 | [diff] [blame] | 3929 | mProxyTracker.setDefaultProxy(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3930 | } |
| 3931 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 3932 | // If the proxy has changed from oldLp to newLp, resend proxy broadcast. This method gets called |
| 3933 | // when any network changes proxy. |
| 3934 | // TODO: Remove usage of broadcast extras as they are deprecated and not applicable in a |
| 3935 | // multi-network world where an app might be bound to a non-default network. |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3936 | private void updateProxy(LinkProperties newLp, LinkProperties oldLp) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3937 | ProxyInfo newProxyInfo = newLp == null ? null : newLp.getHttpProxy(); |
| 3938 | ProxyInfo oldProxyInfo = oldLp == null ? null : oldLp.getHttpProxy(); |
| 3939 | |
Chalard Jean | 4949dd7 | 2018-06-07 17:41:29 +0900 | [diff] [blame] | 3940 | if (!ProxyTracker.proxyInfoEqual(newProxyInfo, oldProxyInfo)) { |
Chalard Jean | b50a276 | 2018-06-08 19:46:44 +0900 | [diff] [blame] | 3941 | mProxyTracker.sendProxyBroadcast(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3942 | } |
| 3943 | } |
| 3944 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3945 | private static class SettingsObserver extends ContentObserver { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3946 | final private HashMap<Uri, Integer> mUriEventMap; |
| 3947 | final private Context mContext; |
| 3948 | final private Handler mHandler; |
| 3949 | |
| 3950 | SettingsObserver(Context context, Handler handler) { |
| 3951 | super(null); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3952 | mUriEventMap = new HashMap<>(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3953 | mContext = context; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3954 | mHandler = handler; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3955 | } |
| 3956 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3957 | void observe(Uri uri, int what) { |
| 3958 | mUriEventMap.put(uri, what); |
| 3959 | final ContentResolver resolver = mContext.getContentResolver(); |
| 3960 | resolver.registerContentObserver(uri, false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3961 | } |
| 3962 | |
| 3963 | @Override |
| 3964 | public void onChange(boolean selfChange) { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3965 | Slog.wtf(TAG, "Should never be reached."); |
| 3966 | } |
| 3967 | |
| 3968 | @Override |
| 3969 | public void onChange(boolean selfChange, Uri uri) { |
| 3970 | final Integer what = mUriEventMap.get(uri); |
| 3971 | if (what != null) { |
| 3972 | mHandler.obtainMessage(what.intValue()).sendToTarget(); |
| 3973 | } else { |
| 3974 | loge("No matching event to send for URI=" + uri); |
| 3975 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3976 | } |
| 3977 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3978 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3979 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3980 | Slog.d(TAG, s); |
| 3981 | } |
| 3982 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 3983 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 3984 | Slog.e(TAG, s); |
| 3985 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 3986 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 3987 | private static void loge(String s, Throwable t) { |
| 3988 | Slog.e(TAG, s, t); |
| 3989 | } |
| 3990 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 3991 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 3992 | * Prepare for a VPN application. |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 3993 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 3994 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 3995 | * |
| 3996 | * @param oldPackage Package name of the application which currently controls VPN, which will |
| 3997 | * be replaced. If there is no such application, this should should either be |
| 3998 | * {@code null} or {@link VpnConfig.LEGACY_VPN}. |
| 3999 | * @param newPackage Package name of the application which should gain control of VPN, or |
| 4000 | * {@code null} to disable. |
| 4001 | * @param userId User for whom to prepare the new VPN. |
| 4002 | * |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4003 | * @hide |
| 4004 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4005 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4006 | public boolean prepareVpn(@Nullable String oldPackage, @Nullable String newPackage, |
| 4007 | int userId) { |
| 4008 | enforceCrossUserPermission(userId); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4009 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4010 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4011 | throwIfLockdownEnabled(); |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4012 | Vpn vpn = mVpns.get(userId); |
| 4013 | if (vpn != null) { |
| 4014 | return vpn.prepare(oldPackage, newPackage); |
| 4015 | } else { |
| 4016 | return false; |
| 4017 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4018 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4019 | } |
| 4020 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4021 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4022 | * Set whether the VPN package has the ability to launch VPNs without user intervention. |
| 4023 | * This method is used by system-privileged apps. |
| 4024 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 4025 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 4026 | * |
| 4027 | * @param packageName The package for which authorization state should change. |
| 4028 | * @param userId User for whom {@code packageName} is installed. |
| 4029 | * @param authorized {@code true} if this app should be able to start a VPN connection without |
| 4030 | * explicit user approval, {@code false} if not. |
| 4031 | * |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4032 | * @hide |
| 4033 | */ |
| 4034 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4035 | public void setVpnPackageAuthorization(String packageName, int userId, boolean authorized) { |
| 4036 | enforceCrossUserPermission(userId); |
| 4037 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4038 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4039 | Vpn vpn = mVpns.get(userId); |
| 4040 | if (vpn != null) { |
| 4041 | vpn.setPackageAuthorization(packageName, authorized); |
| 4042 | } |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4047 | * Configure a TUN interface and return its file descriptor. Parameters |
| 4048 | * 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] | 4049 | * and not available in ConnectivityManager. Permissions are checked in |
| 4050 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4051 | * @hide |
| 4052 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4053 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4054 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4055 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4056 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4057 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4058 | return mVpns.get(user).establish(config); |
| 4059 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4062 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4063 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 4064 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4065 | */ |
| 4066 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4067 | public void startLegacyVpn(VpnProfile profile) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4068 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4069 | final LinkProperties egress = getActiveLinkProperties(); |
| 4070 | if (egress == null) { |
| 4071 | throw new IllegalStateException("Missing active network connection"); |
| 4072 | } |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4073 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4074 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4075 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 4076 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4077 | } |
| 4078 | |
| 4079 | /** |
| 4080 | * Return the information of the ongoing legacy VPN. This method is used |
| 4081 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 4082 | * are checked in Vpn class. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4083 | */ |
| 4084 | @Override |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4085 | public LegacyVpnInfo getLegacyVpnInfo(int userId) { |
| 4086 | enforceCrossUserPermission(userId); |
Hung-ying Tyan | 44c8c5c | 2015-07-29 12:39:21 +0800 | [diff] [blame] | 4087 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4088 | synchronized (mVpns) { |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4089 | return mVpns.get(userId).getLegacyVpnInfo(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4090 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4091 | } |
| 4092 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4093 | /** |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4094 | * Return the information of all ongoing VPNs. This method is used by NetworkStatsService |
| 4095 | * and not available in ConnectivityManager. |
| 4096 | */ |
| 4097 | @Override |
| 4098 | public VpnInfo[] getAllVpnInfo() { |
| 4099 | enforceConnectivityInternalPermission(); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4100 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4101 | if (mLockdownEnabled) { |
| 4102 | return new VpnInfo[0]; |
| 4103 | } |
| 4104 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4105 | List<VpnInfo> infoList = new ArrayList<>(); |
| 4106 | for (int i = 0; i < mVpns.size(); i++) { |
| 4107 | VpnInfo info = createVpnInfo(mVpns.valueAt(i)); |
| 4108 | if (info != null) { |
| 4109 | infoList.add(info); |
| 4110 | } |
| 4111 | } |
| 4112 | return infoList.toArray(new VpnInfo[infoList.size()]); |
| 4113 | } |
| 4114 | } |
| 4115 | |
| 4116 | /** |
| 4117 | * @return VPN information for accounting, or null if we can't retrieve all required |
| 4118 | * information, e.g primary underlying iface. |
| 4119 | */ |
| 4120 | @Nullable |
| 4121 | private VpnInfo createVpnInfo(Vpn vpn) { |
| 4122 | VpnInfo info = vpn.getVpnInfo(); |
| 4123 | if (info == null) { |
| 4124 | return null; |
| 4125 | } |
| 4126 | Network[] underlyingNetworks = vpn.getUnderlyingNetworks(); |
| 4127 | // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret |
| 4128 | // the underlyingNetworks list. |
| 4129 | if (underlyingNetworks == null) { |
| 4130 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 4131 | if (defaultNetwork != null && defaultNetwork.linkProperties != null) { |
| 4132 | info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName(); |
| 4133 | } |
| 4134 | } else if (underlyingNetworks.length > 0) { |
| 4135 | LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]); |
| 4136 | if (linkProperties != null) { |
| 4137 | info.primaryUnderlyingIface = linkProperties.getInterfaceName(); |
| 4138 | } |
| 4139 | } |
| 4140 | return info.primaryUnderlyingIface == null ? null : info; |
| 4141 | } |
| 4142 | |
| 4143 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4144 | * Returns the information of the ongoing VPN for {@code userId}. This method is used by |
| 4145 | * VpnDialogs and not available in ConnectivityManager. |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4146 | * Permissions are checked in Vpn class. |
| 4147 | * @hide |
| 4148 | */ |
| 4149 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4150 | public VpnConfig getVpnConfig(int userId) { |
| 4151 | enforceCrossUserPermission(userId); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4152 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4153 | Vpn vpn = mVpns.get(userId); |
| 4154 | if (vpn != null) { |
| 4155 | return vpn.getVpnConfig(); |
| 4156 | } else { |
| 4157 | return null; |
| 4158 | } |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4159 | } |
| 4160 | } |
| 4161 | |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4162 | /** |
| 4163 | * Ask all VPN objects to recompute and update their capabilities. |
| 4164 | * |
| 4165 | * When underlying networks change, VPNs may have to update capabilities to reflect things |
| 4166 | * like the metered bit, their transports, and so on. This asks the VPN objects to update |
| 4167 | * their capabilities, and as this will cause them to send messages to the ConnectivityService |
| 4168 | * handler thread through their agent, this is asynchronous. When the capabilities objects |
| 4169 | * are computed they will be up-to-date as they are computed synchronously from here and |
| 4170 | * this is running on the ConnectivityService thread. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4171 | */ |
| 4172 | private void updateAllVpnsCapabilities() { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4173 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4174 | synchronized (mVpns) { |
| 4175 | for (int i = 0; i < mVpns.size(); i++) { |
| 4176 | final Vpn vpn = mVpns.valueAt(i); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4177 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4178 | updateVpnCapabilities(vpn, nc); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4179 | } |
| 4180 | } |
| 4181 | } |
| 4182 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4183 | private void updateVpnCapabilities(Vpn vpn, @Nullable NetworkCapabilities nc) { |
| 4184 | ensureRunningOnConnectivityServiceThread(); |
| 4185 | NetworkAgentInfo vpnNai = getNetworkAgentInfoForNetId(vpn.getNetId()); |
| 4186 | if (vpnNai == null || nc == null) { |
| 4187 | return; |
| 4188 | } |
| 4189 | updateCapabilities(vpnNai.getCurrentScore(), vpnNai, nc); |
| 4190 | } |
| 4191 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4192 | @Override |
| 4193 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 4194 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4195 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 4196 | return false; |
| 4197 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4198 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4199 | synchronized (mVpns) { |
| 4200 | // Tear down existing lockdown if profile was removed |
| 4201 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 4202 | if (mLockdownEnabled) { |
| 4203 | byte[] profileTag = mKeyStore.get(Credentials.LOCKDOWN_VPN); |
| 4204 | if (profileTag == null) { |
| 4205 | Slog.e(TAG, "Lockdown VPN configured but cannot be read from keystore"); |
| 4206 | return false; |
| 4207 | } |
| 4208 | String profileName = new String(profileTag); |
| 4209 | final VpnProfile profile = VpnProfile.decode( |
| 4210 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
| 4211 | if (profile == null) { |
| 4212 | Slog.e(TAG, "Lockdown VPN configured invalid profile " + profileName); |
| 4213 | setLockdownTracker(null); |
| 4214 | return true; |
| 4215 | } |
| 4216 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robin Lee | 18566c1 | 2016-03-14 13:08:48 +0000 | [diff] [blame] | 4217 | Vpn vpn = mVpns.get(user); |
| 4218 | if (vpn == null) { |
| 4219 | Slog.w(TAG, "VPN for user " + user + " not ready yet. Skipping lockdown"); |
| 4220 | return false; |
| 4221 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4222 | setLockdownTracker(new LockdownVpnTracker(mContext, mNMS, this, vpn, profile)); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4223 | } else { |
| 4224 | setLockdownTracker(null); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4225 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | return true; |
| 4229 | } |
| 4230 | |
| 4231 | /** |
| 4232 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4233 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4234 | */ |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4235 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4236 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4237 | // Shutdown any existing tracker |
| 4238 | final LockdownVpnTracker existing = mLockdownTracker; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4239 | // TODO: Add a trigger when the always-on VPN enable/disable to reevaluate and send the |
| 4240 | // necessary onBlockedStatusChanged callbacks. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4241 | mLockdownTracker = null; |
| 4242 | if (existing != null) { |
| 4243 | existing.shutdown(); |
| 4244 | } |
| 4245 | |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 4246 | if (tracker != null) { |
| 4247 | mLockdownTracker = tracker; |
| 4248 | mLockdownTracker.init(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4249 | } |
| 4250 | } |
| 4251 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4252 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4253 | private void throwIfLockdownEnabled() { |
| 4254 | if (mLockdownEnabled) { |
| 4255 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4256 | } |
| 4257 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4258 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4259 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4260 | * Starts the always-on VPN {@link VpnService} for user {@param userId}, which should perform |
| 4261 | * some setup and then call {@code establish()} to connect. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4262 | * |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4263 | * @return {@code true} if the service was started, the service was already connected, or there |
| 4264 | * was no always-on VPN to start. {@code false} otherwise. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4265 | */ |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4266 | private boolean startAlwaysOnVpn(int userId) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4267 | synchronized (mVpns) { |
| 4268 | Vpn vpn = mVpns.get(userId); |
| 4269 | if (vpn == null) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4270 | // Shouldn't happen as all code paths that point here should have checked the Vpn |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4271 | // exists already. |
| 4272 | Slog.wtf(TAG, "User " + userId + " has no Vpn configuration"); |
| 4273 | return false; |
| 4274 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4275 | |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 4276 | return vpn.startAlwaysOnVpn(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4277 | } |
| 4278 | } |
| 4279 | |
| 4280 | @Override |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 4281 | public boolean isAlwaysOnVpnPackageSupported(int userId, String packageName) { |
| 4282 | enforceSettingsPermission(); |
| 4283 | enforceCrossUserPermission(userId); |
| 4284 | |
| 4285 | synchronized (mVpns) { |
| 4286 | Vpn vpn = mVpns.get(userId); |
| 4287 | if (vpn == null) { |
| 4288 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4289 | return false; |
| 4290 | } |
| 4291 | return vpn.isAlwaysOnPackageSupported(packageName); |
| 4292 | } |
| 4293 | } |
| 4294 | |
| 4295 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4296 | public boolean setAlwaysOnVpnPackage( |
| 4297 | int userId, String packageName, boolean lockdown, List<String> lockdownWhitelist) { |
| 4298 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4299 | enforceCrossUserPermission(userId); |
| 4300 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4301 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4302 | // Can't set always-on VPN if legacy VPN is already in lockdown mode. |
| 4303 | if (LockdownVpnTracker.isEnabled()) { |
| 4304 | return false; |
| 4305 | } |
| 4306 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4307 | Vpn vpn = mVpns.get(userId); |
| 4308 | if (vpn == null) { |
| 4309 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4310 | return false; |
| 4311 | } |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4312 | if (!vpn.setAlwaysOnPackage(packageName, lockdown, lockdownWhitelist)) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4313 | return false; |
| 4314 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4315 | if (!startAlwaysOnVpn(userId)) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4316 | vpn.setAlwaysOnPackage(null, false, null); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4317 | return false; |
| 4318 | } |
| 4319 | } |
| 4320 | return true; |
| 4321 | } |
| 4322 | |
| 4323 | @Override |
| 4324 | public String getAlwaysOnVpnPackage(int userId) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4325 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4326 | enforceCrossUserPermission(userId); |
| 4327 | |
| 4328 | synchronized (mVpns) { |
| 4329 | Vpn vpn = mVpns.get(userId); |
| 4330 | if (vpn == null) { |
| 4331 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4332 | return null; |
| 4333 | } |
| 4334 | return vpn.getAlwaysOnPackage(); |
| 4335 | } |
| 4336 | } |
| 4337 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4338 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4339 | public boolean isVpnLockdownEnabled(int userId) { |
| 4340 | enforceControlAlwaysOnVpnPermission(); |
| 4341 | enforceCrossUserPermission(userId); |
| 4342 | |
| 4343 | synchronized (mVpns) { |
| 4344 | Vpn vpn = mVpns.get(userId); |
| 4345 | if (vpn == null) { |
| 4346 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4347 | return false; |
| 4348 | } |
| 4349 | return vpn.getLockdown(); |
| 4350 | } |
| 4351 | } |
| 4352 | |
| 4353 | @Override |
| 4354 | public List<String> getVpnLockdownWhitelist(int userId) { |
| 4355 | enforceControlAlwaysOnVpnPermission(); |
| 4356 | enforceCrossUserPermission(userId); |
| 4357 | |
| 4358 | synchronized (mVpns) { |
| 4359 | Vpn vpn = mVpns.get(userId); |
| 4360 | if (vpn == null) { |
| 4361 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4362 | return null; |
| 4363 | } |
| 4364 | return vpn.getLockdownWhitelist(); |
| 4365 | } |
| 4366 | } |
| 4367 | |
| 4368 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4369 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4370 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 4371 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4372 | } |
| 4373 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4374 | /** Location to an updatable file listing carrier provisioning urls. |
| 4375 | * An example: |
| 4376 | * |
| 4377 | * <?xml version="1.0" encoding="utf-8"?> |
| 4378 | * <provisioningUrls> |
| 4379 | * <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] | 4380 | * </provisioningUrls> |
| 4381 | */ |
| 4382 | private static final String PROVISIONING_URL_PATH = |
| 4383 | "/data/misc/radio/provisioning_urls.xml"; |
| 4384 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4385 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4386 | /** XML tag for root element. */ |
| 4387 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 4388 | /** XML tag for individual url */ |
| 4389 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4390 | /** XML attribute for mcc */ |
| 4391 | private static final String ATTR_MCC = "mcc"; |
| 4392 | /** XML attribute for mnc */ |
| 4393 | private static final String ATTR_MNC = "mnc"; |
| 4394 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4395 | private String getProvisioningUrlBaseFromFile() { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4396 | FileReader fileReader = null; |
| 4397 | XmlPullParser parser = null; |
| 4398 | Configuration config = mContext.getResources().getConfiguration(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4399 | |
| 4400 | try { |
| 4401 | fileReader = new FileReader(mProvisioningUrlFile); |
| 4402 | parser = Xml.newPullParser(); |
| 4403 | parser.setInput(fileReader); |
| 4404 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 4405 | |
| 4406 | while (true) { |
| 4407 | XmlUtils.nextElement(parser); |
| 4408 | |
| 4409 | String element = parser.getName(); |
| 4410 | if (element == null) break; |
| 4411 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4412 | if (element.equals(TAG_PROVISIONING_URL)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4413 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 4414 | try { |
| 4415 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 4416 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 4417 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 4418 | parser.next(); |
| 4419 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 4420 | return parser.getText(); |
| 4421 | } |
| 4422 | } |
| 4423 | } |
| 4424 | } catch (NumberFormatException e) { |
| 4425 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 4426 | } |
| 4427 | } |
| 4428 | } |
| 4429 | return null; |
| 4430 | } catch (FileNotFoundException e) { |
| 4431 | loge("Carrier Provisioning Urls file not found"); |
| 4432 | } catch (XmlPullParserException e) { |
| 4433 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 4434 | } catch (IOException e) { |
| 4435 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 4436 | } finally { |
| 4437 | if (fileReader != null) { |
| 4438 | try { |
| 4439 | fileReader.close(); |
| 4440 | } catch (IOException e) {} |
| 4441 | } |
| 4442 | } |
| 4443 | return null; |
| 4444 | } |
| 4445 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4446 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4447 | public String getMobileProvisioningUrl() { |
| 4448 | enforceConnectivityInternalPermission(); |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4449 | String url = getProvisioningUrlBaseFromFile(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4450 | if (TextUtils.isEmpty(url)) { |
| 4451 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4452 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4453 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4454 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4455 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4456 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4457 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4458 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 4459 | if (TextUtils.isEmpty(phoneNumber)) { |
| 4460 | phoneNumber = "0000000000"; |
| 4461 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4462 | url = String.format(url, |
| 4463 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 4464 | mTelephonyManager.getDeviceId() /* IMEI */, |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4465 | phoneNumber /* Phone number */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4466 | } |
| 4467 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4468 | return url; |
| 4469 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4470 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4471 | @Override |
| 4472 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4473 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4474 | enforceConnectivityInternalPermission(); |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 4475 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 4476 | return; |
| 4477 | } |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4478 | final long ident = Binder.clearCallingIdentity(); |
| 4479 | try { |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 4480 | // Concatenate the range of types onto the range of NetIDs. |
| 4481 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
| 4482 | mNotifier.setProvNotificationVisible(visible, id, action); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4483 | } finally { |
| 4484 | Binder.restoreCallingIdentity(ident); |
| 4485 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4486 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 4487 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4488 | @Override |
| 4489 | public void setAirplaneMode(boolean enable) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 4490 | enforceNetworkStackSettingsOrSetup(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4491 | final long ident = Binder.clearCallingIdentity(); |
| 4492 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4493 | final ContentResolver cr = mContext.getContentResolver(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 4494 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, encodeBool(enable)); |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4495 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 4496 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 4497 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4498 | } finally { |
| 4499 | Binder.restoreCallingIdentity(ident); |
| 4500 | } |
| 4501 | } |
| 4502 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4503 | private void onUserStart(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4504 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4505 | Vpn userVpn = mVpns.get(userId); |
| 4506 | if (userVpn != null) { |
| 4507 | loge("Starting user already has a VPN"); |
| 4508 | return; |
| 4509 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4510 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNMS, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4511 | mVpns.put(userId, userVpn); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4512 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4513 | updateLockdownVpn(); |
| 4514 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4515 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | private void onUserStop(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4519 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4520 | Vpn userVpn = mVpns.get(userId); |
| 4521 | if (userVpn == null) { |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4522 | loge("Stopped user has no VPN"); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4523 | return; |
| 4524 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4525 | userVpn.onUserStopped(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4526 | mVpns.delete(userId); |
| 4527 | } |
| 4528 | } |
| 4529 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4530 | private void onUserAdded(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4531 | mPermissionMonitor.onUserAdded(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4532 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4533 | synchronized (mVpns) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4534 | final int vpnsSize = mVpns.size(); |
| 4535 | for (int i = 0; i < vpnsSize; i++) { |
| 4536 | Vpn vpn = mVpns.valueAt(i); |
| 4537 | vpn.onUserAdded(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4538 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4539 | updateVpnCapabilities(vpn, nc); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | |
| 4544 | private void onUserRemoved(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4545 | mPermissionMonitor.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4546 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4547 | synchronized (mVpns) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4548 | final int vpnsSize = mVpns.size(); |
| 4549 | for (int i = 0; i < vpnsSize; i++) { |
| 4550 | Vpn vpn = mVpns.valueAt(i); |
| 4551 | vpn.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4552 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4553 | updateVpnCapabilities(vpn, nc); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4554 | } |
| 4555 | } |
| 4556 | } |
| 4557 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4558 | private void onPackageAdded(String packageName, int uid) { |
| 4559 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4560 | Slog.wtf(TAG, "Invalid package in onPackageAdded: " + packageName + " | " + uid); |
| 4561 | return; |
| 4562 | } |
| 4563 | mPermissionMonitor.onPackageAdded(packageName, uid); |
| 4564 | } |
| 4565 | |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4566 | private void onPackageReplaced(String packageName, int uid) { |
| 4567 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4568 | Slog.wtf(TAG, "Invalid package in onPackageReplaced: " + packageName + " | " + uid); |
| 4569 | return; |
| 4570 | } |
| 4571 | final int userId = UserHandle.getUserId(uid); |
| 4572 | synchronized (mVpns) { |
| 4573 | final Vpn vpn = mVpns.get(userId); |
| 4574 | if (vpn == null) { |
| 4575 | return; |
| 4576 | } |
| 4577 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4578 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName)) { |
| 4579 | Slog.d(TAG, "Restarting always-on VPN package " + packageName + " for user " |
| 4580 | + userId); |
| 4581 | vpn.startAlwaysOnVpn(); |
| 4582 | } |
| 4583 | } |
| 4584 | } |
| 4585 | |
| 4586 | private void onPackageRemoved(String packageName, int uid, boolean isReplacing) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4587 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4588 | Slog.wtf(TAG, "Invalid package in onPackageRemoved: " + packageName + " | " + uid); |
| 4589 | return; |
| 4590 | } |
| 4591 | mPermissionMonitor.onPackageRemoved(uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4592 | |
| 4593 | final int userId = UserHandle.getUserId(uid); |
| 4594 | synchronized (mVpns) { |
| 4595 | final Vpn vpn = mVpns.get(userId); |
| 4596 | if (vpn == null) { |
| 4597 | return; |
| 4598 | } |
| 4599 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4600 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName) && !isReplacing) { |
| 4601 | Slog.d(TAG, "Removing always-on VPN package " + packageName + " for user " |
| 4602 | + userId); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4603 | vpn.setAlwaysOnPackage(null, false, null); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4604 | } |
| 4605 | } |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4606 | } |
| 4607 | |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4608 | private void onUserUnlocked(int userId) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4609 | synchronized (mVpns) { |
| 4610 | // User present may be sent because of an unlock, which might mean an unlocked keystore. |
| 4611 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4612 | updateLockdownVpn(); |
| 4613 | } else { |
| 4614 | startAlwaysOnVpn(userId); |
| 4615 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4616 | } |
| 4617 | } |
| 4618 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4619 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4620 | @Override |
| 4621 | public void onReceive(Context context, Intent intent) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4622 | ensureRunningOnConnectivityServiceThread(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4623 | final String action = intent.getAction(); |
| 4624 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4625 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 4626 | final Uri packageData = intent.getData(); |
| 4627 | final String packageName = |
| 4628 | packageData != null ? packageData.getSchemeSpecificPart() : null; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4629 | if (userId == UserHandle.USER_NULL) return; |
| 4630 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 4631 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4632 | onUserStart(userId); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4633 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4634 | onUserStop(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4635 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 4636 | onUserAdded(userId); |
| 4637 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 4638 | onUserRemoved(userId); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4639 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 4640 | onUserUnlocked(userId); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4641 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 4642 | onPackageAdded(packageName, uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4643 | } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 4644 | onPackageReplaced(packageName, uid); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4645 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4646 | final boolean isReplacing = intent.getBooleanExtra( |
| 4647 | Intent.EXTRA_REPLACING, false); |
| 4648 | onPackageRemoved(packageName, uid, isReplacing); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4649 | } |
| 4650 | } |
| 4651 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 4652 | |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 4653 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 4654 | @Override |
| 4655 | public void onReceive(Context context, Intent intent) { |
| 4656 | // Try creating lockdown tracker, since user present usually means |
| 4657 | // unlocked keystore. |
| 4658 | updateLockdownVpn(); |
| 4659 | mContext.unregisterReceiver(this); |
| 4660 | } |
| 4661 | }; |
| 4662 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4663 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = new HashMap<>(); |
| 4664 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = new HashMap<>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4665 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4666 | private static final int MAX_NETWORK_REQUESTS_PER_UID = 100; |
| 4667 | // Map from UID to number of NetworkRequests that UID has filed. |
| 4668 | @GuardedBy("mUidToNetworkRequestCount") |
| 4669 | private final SparseIntArray mUidToNetworkRequestCount = new SparseIntArray(); |
| 4670 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4671 | private static class NetworkFactoryInfo { |
| 4672 | public final String name; |
| 4673 | public final Messenger messenger; |
| 4674 | public final AsyncChannel asyncChannel; |
| 4675 | |
| 4676 | public NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel) { |
| 4677 | this.name = name; |
| 4678 | this.messenger = messenger; |
| 4679 | this.asyncChannel = asyncChannel; |
| 4680 | } |
| 4681 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4682 | |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4683 | private void ensureNetworkRequestHasType(NetworkRequest request) { |
| 4684 | if (request.type == NetworkRequest.Type.NONE) { |
| 4685 | throw new IllegalArgumentException( |
| 4686 | "All NetworkRequests in ConnectivityService must have a type"); |
| 4687 | } |
| 4688 | } |
| 4689 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4690 | /** |
| 4691 | * Tracks info about the requester. |
| 4692 | * Also used to notice when the calling process dies so we can self-expire |
| 4693 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4694 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4695 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4696 | final PendingIntent mPendingIntent; |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4697 | boolean mPendingIntentSent; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4698 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4699 | final int mPid; |
| 4700 | final int mUid; |
| 4701 | final Messenger messenger; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4702 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4703 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4704 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4705 | ensureNetworkRequestHasType(request); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4706 | mPendingIntent = pi; |
| 4707 | messenger = null; |
| 4708 | mBinder = null; |
| 4709 | mPid = getCallingPid(); |
| 4710 | mUid = getCallingUid(); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4711 | enforceRequestCountLimit(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4712 | } |
| 4713 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4714 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4715 | super(); |
| 4716 | messenger = m; |
| 4717 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4718 | ensureNetworkRequestHasType(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4719 | mBinder = binder; |
| 4720 | mPid = getCallingPid(); |
| 4721 | mUid = getCallingUid(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4722 | mPendingIntent = null; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4723 | enforceRequestCountLimit(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4724 | |
| 4725 | try { |
| 4726 | mBinder.linkToDeath(this, 0); |
| 4727 | } catch (RemoteException e) { |
| 4728 | binderDied(); |
| 4729 | } |
| 4730 | } |
| 4731 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4732 | private void enforceRequestCountLimit() { |
| 4733 | synchronized (mUidToNetworkRequestCount) { |
| 4734 | int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1; |
| 4735 | if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4736 | throw new ServiceSpecificException( |
| 4737 | ConnectivityManager.Errors.TOO_MANY_REQUESTS); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4738 | } |
| 4739 | mUidToNetworkRequestCount.put(mUid, networkRequests); |
| 4740 | } |
| 4741 | } |
| 4742 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4743 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4744 | if (mBinder != null) { |
| 4745 | mBinder.unlinkToDeath(this, 0); |
| 4746 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | public void binderDied() { |
| 4750 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 4751 | request + ", " + mBinder + ")"); |
| 4752 | releaseNetworkRequest(request); |
| 4753 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4754 | |
| 4755 | public String toString() { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4756 | return "uid/pid:" + mUid + "/" + mPid + " " + request + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4757 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4758 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4759 | } |
| 4760 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4761 | private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { |
| 4762 | final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); |
| 4763 | if (badCapability != null) { |
| 4764 | throw new IllegalArgumentException("Cannot request network with " + badCapability); |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4768 | // This checks that the passed capabilities either do not request a specific SSID, or the |
| 4769 | // calling app has permission to do so. |
| 4770 | private void ensureSufficientPermissionsForRequest(NetworkCapabilities nc, |
| 4771 | int callerPid, int callerUid) { |
Chalard Jean | b5120ca | 2018-04-16 12:25:22 +0900 | [diff] [blame] | 4772 | if (null != nc.getSSID() && !checkSettingsPermission(callerPid, callerUid)) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4773 | throw new SecurityException("Insufficient permissions to request a specific SSID"); |
| 4774 | } |
| 4775 | } |
| 4776 | |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 4777 | private ArrayList<Integer> getSignalStrengthThresholds(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4778 | final SortedSet<Integer> thresholds = new TreeSet<>(); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4779 | synchronized (nai) { |
| 4780 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 4781 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 4782 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 4783 | thresholds.add(nri.request.networkCapabilities.getSignalStrength()); |
| 4784 | } |
| 4785 | } |
| 4786 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4787 | return new ArrayList<>(thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4788 | } |
| 4789 | |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4790 | private void updateSignalStrengthThresholds( |
| 4791 | NetworkAgentInfo nai, String reason, NetworkRequest request) { |
| 4792 | ArrayList<Integer> thresholdsArray = getSignalStrengthThresholds(nai); |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 4793 | Bundle thresholds = new Bundle(); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4794 | thresholds.putIntegerArrayList("thresholds", thresholdsArray); |
| 4795 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 4796 | if (VDBG || (DBG && !"CONNECT".equals(reason))) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4797 | String detail; |
| 4798 | if (request != null && request.networkCapabilities.hasSignalStrength()) { |
| 4799 | detail = reason + " " + request.networkCapabilities.getSignalStrength(); |
| 4800 | } else { |
| 4801 | detail = reason; |
| 4802 | } |
| 4803 | log(String.format("updateSignalStrengthThresholds: %s, sending %s to %s", |
| 4804 | detail, Arrays.toString(thresholdsArray.toArray()), nai.name())); |
| 4805 | } |
| 4806 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4807 | nai.asyncChannel.sendMessage( |
| 4808 | android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 4809 | 0, 0, thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4810 | } |
| 4811 | |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 4812 | private void ensureValidNetworkSpecifier(NetworkCapabilities nc) { |
| 4813 | if (nc == null) { |
| 4814 | return; |
| 4815 | } |
| 4816 | NetworkSpecifier ns = nc.getNetworkSpecifier(); |
| 4817 | if (ns == null) { |
| 4818 | return; |
| 4819 | } |
| 4820 | MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(ns); |
| 4821 | ns.assertValidFromUid(Binder.getCallingUid()); |
| 4822 | } |
| 4823 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4824 | @Override |
| 4825 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4826 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4827 | final NetworkRequest.Type type = (networkCapabilities == null) |
| 4828 | ? NetworkRequest.Type.TRACK_DEFAULT |
| 4829 | : NetworkRequest.Type.REQUEST; |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4830 | // If the requested networkCapabilities is null, take them instead from |
| 4831 | // the default network request. This allows callers to keep track of |
| 4832 | // the system default network. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4833 | if (type == NetworkRequest.Type.TRACK_DEFAULT) { |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 4834 | networkCapabilities = createDefaultNetworkCapabilitiesForUid(Binder.getCallingUid()); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4835 | enforceAccessPermission(); |
| 4836 | } else { |
| 4837 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 4838 | enforceNetworkRequestPermissions(networkCapabilities); |
Lorenzo Colitti | b60570c | 2016-07-01 13:20:10 +0900 | [diff] [blame] | 4839 | // TODO: this is incorrect. We mark the request as metered or not depending on the state |
| 4840 | // of the app when the request is filed, but we never change the request if the app |
| 4841 | // changes network state. http://b/29964605 |
| 4842 | enforceMeteredApnPolicy(networkCapabilities); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4843 | } |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4844 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4845 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 4846 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 4847 | // Set the UID range for this request to the single UID of the requester, or to an empty |
| 4848 | // set of UIDs if the caller has the appropriate permission and UIDs have not been set. |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 4849 | // This will overwrite any allowed UIDs in the requested capabilities. Though there |
| 4850 | // are no visible methods to set the UIDs, an app could use reflection to try and get |
| 4851 | // networks for other apps so it's essential that the UIDs are overwritten. |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 4852 | restrictRequestUidsForCaller(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4853 | |
Etan Cohen | ba07c8c | 2017-02-05 10:42:27 -0800 | [diff] [blame] | 4854 | if (timeoutMs < 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4855 | throw new IllegalArgumentException("Bad timeout specified"); |
| 4856 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 4857 | ensureValidNetworkSpecifier(networkCapabilities); |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 4858 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4859 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4860 | nextNetworkRequestId(), type); |
| 4861 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4862 | if (DBG) log("requestNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4863 | |
| 4864 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4865 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4866 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4867 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4868 | } |
| 4869 | return networkRequest; |
| 4870 | } |
| 4871 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4872 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 4873 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) { |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 4874 | enforceConnectivityRestrictedNetworksPermission(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4875 | } else { |
| 4876 | enforceChangePermission(); |
| 4877 | } |
| 4878 | } |
| 4879 | |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4880 | @Override |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4881 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4882 | enforceAccessPermission(); |
| 4883 | NetworkAgentInfo nai = null; |
| 4884 | if (network == null) { |
| 4885 | return false; |
| 4886 | } |
| 4887 | synchronized (mNetworkForNetId) { |
| 4888 | nai = mNetworkForNetId.get(network.netId); |
| 4889 | } |
| 4890 | if (nai != null) { |
| 4891 | nai.asyncChannel.sendMessage(android.net.NetworkAgent.CMD_REQUEST_BANDWIDTH_UPDATE); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 4892 | synchronized (mBandwidthRequests) { |
| 4893 | final int uid = Binder.getCallingUid(); |
| 4894 | Integer uidReqs = mBandwidthRequests.get(uid); |
| 4895 | if (uidReqs == null) { |
| 4896 | uidReqs = new Integer(0); |
| 4897 | } |
| 4898 | mBandwidthRequests.put(uid, ++uidReqs); |
| 4899 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4900 | return true; |
| 4901 | } |
| 4902 | return false; |
| 4903 | } |
| 4904 | |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 4905 | private boolean isSystem(int uid) { |
| 4906 | return uid < Process.FIRST_APPLICATION_UID; |
| 4907 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4908 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4909 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 4910 | final int uid = Binder.getCallingUid(); |
| 4911 | if (isSystem(uid)) { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4912 | // Exemption for system uid. |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 4913 | return; |
| 4914 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4915 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 4916 | // Policy already enforced. |
| 4917 | return; |
| 4918 | } |
| 4919 | if (mPolicyManagerInternal.isUidRestrictedOnMeteredNetworks(uid)) { |
| 4920 | // If UID is restricted, don't allow them to bring up metered APNs. |
| 4921 | networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4922 | } |
| 4923 | } |
| 4924 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4925 | @Override |
| 4926 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 4927 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4928 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 4929 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 4930 | enforceNetworkRequestPermissions(networkCapabilities); |
| 4931 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4932 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4933 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 4934 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 4935 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 4936 | restrictRequestUidsForCaller(networkCapabilities); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 4937 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4938 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4939 | nextNetworkRequestId(), NetworkRequest.Type.REQUEST); |
| 4940 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 4941 | if (DBG) log("pendingRequest for " + nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4942 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 4943 | nri)); |
| 4944 | return networkRequest; |
| 4945 | } |
| 4946 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4947 | private void releasePendingNetworkRequestWithDelay(PendingIntent operation) { |
| 4948 | mHandler.sendMessageDelayed( |
| 4949 | mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 4950 | getCallingUid(), 0, operation), mReleasePendingIntentDelayMs); |
| 4951 | } |
| 4952 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4953 | @Override |
| 4954 | public void releasePendingNetworkRequest(PendingIntent operation) { |
Paul Jensen | 1a81c39 | 2015-05-21 08:15:08 -0400 | [diff] [blame] | 4955 | checkNotNull(operation, "PendingIntent cannot be null."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4956 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 4957 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4958 | } |
| 4959 | |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 4960 | // In order to implement the compatibility measure for pre-M apps that call |
| 4961 | // WifiManager.enableNetwork(..., true) without also binding to that network explicitly, |
| 4962 | // WifiManager registers a network listen for the purpose of calling setProcessDefaultNetwork. |
| 4963 | // This ensures it has permission to do so. |
| 4964 | private boolean hasWifiNetworkListenPermission(NetworkCapabilities nc) { |
| 4965 | if (nc == null) { |
| 4966 | return false; |
| 4967 | } |
| 4968 | int[] transportTypes = nc.getTransportTypes(); |
| 4969 | if (transportTypes.length != 1 || transportTypes[0] != NetworkCapabilities.TRANSPORT_WIFI) { |
| 4970 | return false; |
| 4971 | } |
| 4972 | try { |
| 4973 | mContext.enforceCallingOrSelfPermission( |
| 4974 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 4975 | "ConnectivityService"); |
| 4976 | } catch (SecurityException e) { |
| 4977 | return false; |
| 4978 | } |
| 4979 | return true; |
| 4980 | } |
| 4981 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4982 | @Override |
| 4983 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 4984 | Messenger messenger, IBinder binder) { |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 4985 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 4986 | enforceAccessPermission(); |
| 4987 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4988 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 4989 | NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4990 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 4991 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 4992 | restrictRequestUidsForCaller(nc); |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 4993 | // Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so |
| 4994 | // make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get |
| 4995 | // onLost and onAvailable callbacks when networks move in and out of the background. |
| 4996 | // There is no need to do this for requests because an app without CHANGE_NETWORK_STATE |
| 4997 | // can't request networks. |
| 4998 | restrictBackgroundRequestForCaller(nc); |
| 4999 | ensureValidNetworkSpecifier(nc); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5000 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5001 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5002 | NetworkRequest.Type.LISTEN); |
| 5003 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5004 | if (VDBG) log("listenForNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5005 | |
| 5006 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 5007 | return networkRequest; |
| 5008 | } |
| 5009 | |
| 5010 | @Override |
| 5011 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 5012 | PendingIntent operation) { |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5013 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 5014 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 5015 | enforceAccessPermission(); |
| 5016 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5017 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5018 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5019 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5020 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5021 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5022 | restrictRequestUidsForCaller(nc); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5023 | |
| 5024 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5025 | NetworkRequest.Type.LISTEN); |
| 5026 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5027 | if (VDBG) log("pendingListenForNetwork for " + nri); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5028 | |
| 5029 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5030 | } |
| 5031 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5032 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5033 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 5034 | ensureNetworkRequestHasType(networkRequest); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5035 | mHandler.sendMessage(mHandler.obtainMessage( |
| 5036 | EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5037 | } |
| 5038 | |
| 5039 | @Override |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5040 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5041 | enforceConnectivityInternalPermission(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5042 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel()); |
| 5043 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5044 | } |
| 5045 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5046 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5047 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5048 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 5049 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 5050 | } |
| 5051 | |
| 5052 | @Override |
| 5053 | public void unregisterNetworkFactory(Messenger messenger) { |
| 5054 | enforceConnectivityInternalPermission(); |
| 5055 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 5056 | } |
| 5057 | |
| 5058 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 5059 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 5060 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5061 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5062 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5063 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5064 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5065 | } |
| 5066 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5067 | /** |
| 5068 | * NetworkAgentInfo supporting a request by requestId. |
| 5069 | * These have already been vetted (their Capabilities satisfy the request) |
| 5070 | * and the are the highest scored network available. |
| 5071 | * the are keyed off the Requests requestId. |
| 5072 | */ |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5073 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5074 | @GuardedBy("mNetworkForRequestId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5075 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = new SparseArray<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5076 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5077 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5078 | @GuardedBy("mNetworkForNetId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5079 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = new SparseArray<>(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5080 | // NOTE: Accessed on multiple threads, synchronized with mNetworkForNetId. |
| 5081 | // An entry is first added to mNetIdInUse, prior to mNetworkForNetId, so |
| 5082 | // there may not be a strict 1:1 correlation between the two. |
| 5083 | @GuardedBy("mNetworkForNetId") |
| 5084 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5085 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5086 | // NetworkAgentInfo keyed off its connecting messenger |
| 5087 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5088 | // NOTE: Only should be accessed on ConnectivityServiceThread, except dump(). |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5089 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = new HashMap<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5090 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5091 | @GuardedBy("mBlockedAppUids") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5092 | private final HashSet<Integer> mBlockedAppUids = new HashSet<>(); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5093 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5094 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5095 | private final NetworkRequest mDefaultRequest; |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5096 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 5097 | // Request used to optionally keep mobile data active even when higher |
| 5098 | // priority networks like Wi-Fi are active. |
| 5099 | private final NetworkRequest mDefaultMobileDataRequest; |
| 5100 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 5101 | // Request used to optionally keep wifi data active even when higher |
| 5102 | // priority networks like ethernet are active. |
| 5103 | private final NetworkRequest mDefaultWifiRequest; |
| 5104 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5105 | private NetworkAgentInfo getNetworkForRequest(int requestId) { |
| 5106 | synchronized (mNetworkForRequestId) { |
| 5107 | return mNetworkForRequestId.get(requestId); |
| 5108 | } |
| 5109 | } |
| 5110 | |
| 5111 | private void clearNetworkForRequest(int requestId) { |
| 5112 | synchronized (mNetworkForRequestId) { |
| 5113 | mNetworkForRequestId.remove(requestId); |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | private void setNetworkForRequest(int requestId, NetworkAgentInfo nai) { |
| 5118 | synchronized (mNetworkForRequestId) { |
| 5119 | mNetworkForRequestId.put(requestId, nai); |
| 5120 | } |
| 5121 | } |
| 5122 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5123 | private NetworkAgentInfo getDefaultNetwork() { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5124 | return getNetworkForRequest(mDefaultRequest.requestId); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5125 | } |
| 5126 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5127 | @Nullable |
| 5128 | private Network getNetwork(@Nullable NetworkAgentInfo nai) { |
| 5129 | return nai != null ? nai.network : null; |
| 5130 | } |
| 5131 | |
| 5132 | private void ensureRunningOnConnectivityServiceThread() { |
| 5133 | if (mHandler.getLooper().getThread() != Thread.currentThread()) { |
| 5134 | throw new IllegalStateException( |
| 5135 | "Not running on ConnectivityService thread: " |
| 5136 | + Thread.currentThread().getName()); |
| 5137 | } |
| 5138 | } |
| 5139 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 5140 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5141 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 5142 | } |
| 5143 | |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 5144 | private boolean isDefaultRequest(NetworkRequestInfo nri) { |
| 5145 | return nri.request.requestId == mDefaultRequest.requestId; |
| 5146 | } |
| 5147 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5148 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5149 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 5150 | int currentScore, NetworkMisc networkMisc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5151 | enforceConnectivityInternalPermission(); |
| 5152 | |
Rubin Xu | 1bb5c08 | 2017-09-05 18:40:49 +0100 | [diff] [blame] | 5153 | LinkProperties lp = new LinkProperties(linkProperties); |
| 5154 | lp.ensureDirectlyConnectedRoutes(); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5155 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 5156 | // satisfies mDefaultRequest. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5157 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5158 | final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5159 | new Network(reserveNetId()), new NetworkInfo(networkInfo), lp, nc, currentScore, |
Lorenzo Colitti | 9307ca2 | 2019-01-12 01:54:23 +0900 | [diff] [blame] | 5160 | mContext, mTrackerHandler, new NetworkMisc(networkMisc), this, mNetd, mNMS); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5161 | // Make sure the network capabilities reflect what the agent info says. |
| 5162 | nai.networkCapabilities = mixInCapabilities(nai, nc); |
Chalard Jean | d771aa0 | 2018-04-26 16:16:10 +0900 | [diff] [blame] | 5163 | final String extraInfo = networkInfo.getExtraInfo(); |
| 5164 | final String name = TextUtils.isEmpty(extraInfo) |
| 5165 | ? nai.networkCapabilities.getSSID() : extraInfo; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5166 | if (DBG) log("registerNetworkAgent " + nai); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5167 | final long token = Binder.clearCallingIdentity(); |
| 5168 | try { |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5169 | getNetworkStack().makeNetworkMonitor( |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 5170 | toStableParcelable(nai.network), name, new NetworkMonitorCallbacks(nai)); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5171 | } finally { |
| 5172 | Binder.restoreCallingIdentity(token); |
| 5173 | } |
| 5174 | // NetworkAgentInfo registration will finish when the NetworkMonitor is created. |
| 5175 | // If the network disconnects or sends any other event before that, messages are deferred by |
| 5176 | // NetworkAgent until nai.asyncChannel.connect(), which will be called when finalizing the |
| 5177 | // registration. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5178 | return nai.network.netId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5179 | } |
| 5180 | |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5181 | @VisibleForTesting |
| 5182 | protected NetworkStackClient getNetworkStack() { |
| 5183 | return NetworkStackClient.getInstance(); |
| 5184 | } |
| 5185 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5186 | private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) { |
| 5187 | nai.onNetworkMonitorCreated(networkMonitor); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5188 | if (VDBG) log("Got NetworkAgent Messenger"); |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5189 | mNetworkAgentInfos.put(nai.messenger, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5190 | synchronized (mNetworkForNetId) { |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5191 | mNetworkForNetId.put(nai.network.netId, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5192 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5193 | synchronized (this) { |
| 5194 | if (mSystemReady) { |
| 5195 | try { |
| 5196 | networkMonitor.notifySystemReady(); |
| 5197 | } catch (RemoteException e) { |
| 5198 | e.rethrowFromSystemServer(); |
| 5199 | } |
| 5200 | } |
| 5201 | } |
| 5202 | |
| 5203 | try { |
| 5204 | networkMonitor.start(); |
| 5205 | } catch (RemoteException e) { |
| 5206 | e.rethrowFromSystemServer(); |
| 5207 | } |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5208 | nai.asyncChannel.connect(mContext, mTrackerHandler, nai.messenger); |
| 5209 | NetworkInfo networkInfo = nai.networkInfo; |
| 5210 | nai.networkInfo = null; |
| 5211 | updateNetworkInfo(nai, networkInfo); |
| 5212 | updateUids(nai, null, nai.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5213 | } |
| 5214 | |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5215 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties newLp, |
| 5216 | LinkProperties oldLp) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5217 | int netId = networkAgent.network.netId; |
| 5218 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 5219 | // The NetworkAgentInfo does not know whether clatd is running on its network or not, or |
| 5220 | // whether there is a NAT64 prefix. Before we do anything else, make sure its LinkProperties |
| 5221 | // are accurate. |
| 5222 | networkAgent.clatd.fixupLinkProperties(oldLp, newLp); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 5223 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5224 | updateInterfaces(newLp, oldLp, netId, networkAgent.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5225 | updateMtu(newLp, oldLp); |
| 5226 | // TODO - figure out what to do for clat |
| 5227 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 5228 | // updateMtu(lp, null); |
| 5229 | // } |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5230 | if (isDefaultNetwork(networkAgent)) { |
| 5231 | updateTcpBufferSizes(newLp.getTcpBufferSizes()); |
| 5232 | } |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5233 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5234 | updateRoutes(newLp, oldLp, netId); |
| 5235 | updateDnses(newLp, oldLp, netId); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 5236 | // Make sure LinkProperties represents the latest private DNS status. |
| 5237 | // This does not need to be done before updateDnses because the |
| 5238 | // LinkProperties are not the source of the private DNS configuration. |
| 5239 | // updateDnses will fetch the private DNS configuration from DnsManager. |
| 5240 | mDnsManager.updatePrivateDnsStatus(netId, newLp); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5241 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5242 | if (isDefaultNetwork(networkAgent)) { |
| 5243 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
| 5244 | } else { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5245 | updateProxy(newLp, oldLp); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5246 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5247 | // TODO - move this check to cover the whole function |
| 5248 | if (!Objects.equals(newLp, oldLp)) { |
Chalard Jean | c4f53ba | 2018-05-23 09:07:51 +0900 | [diff] [blame] | 5249 | synchronized (networkAgent) { |
| 5250 | networkAgent.linkProperties = newLp; |
| 5251 | } |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame^] | 5252 | // Start or stop DNS64 detection and 464xlat according to network state. |
| 5253 | networkAgent.clatd.update(); |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5254 | notifyIfacesChangedForNetworkStats(); |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5255 | if (networkAgent.everConnected) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5256 | try { |
| 5257 | networkAgent.networkMonitor().notifyLinkPropertiesChanged(); |
| 5258 | } catch (RemoteException e) { |
| 5259 | e.rethrowFromSystemServer(); |
| 5260 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5261 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 5262 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5263 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 5264 | |
| 5265 | mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5266 | } |
| 5267 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5268 | private void wakeupModifyInterface(String iface, NetworkCapabilities caps, boolean add) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5269 | // Marks are only available on WiFi interfaces. Checking for |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5270 | // marks on unsupported interfaces is harmless. |
| 5271 | if (!caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 5272 | return; |
| 5273 | } |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5274 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5275 | int mark = mContext.getResources().getInteger( |
| 5276 | com.android.internal.R.integer.config_networkWakeupPacketMark); |
| 5277 | int mask = mContext.getResources().getInteger( |
| 5278 | com.android.internal.R.integer.config_networkWakeupPacketMask); |
| 5279 | |
| 5280 | // Mask/mark of zero will not detect anything interesting. |
| 5281 | // Don't install rules unless both values are nonzero. |
| 5282 | if (mark == 0 || mask == 0) { |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5283 | return; |
| 5284 | } |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5285 | |
| 5286 | final String prefix = "iface:" + iface; |
| 5287 | try { |
| 5288 | if (add) { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5289 | mNetd.wakeupAddInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5290 | } else { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5291 | mNetd.wakeupDelInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5292 | } |
| 5293 | } catch (Exception e) { |
| 5294 | loge("Exception modifying wakeup packet monitoring: " + e); |
| 5295 | } |
| 5296 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5297 | } |
| 5298 | |
| 5299 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId, |
| 5300 | NetworkCapabilities caps) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5301 | CompareResult<String> interfaceDiff = new CompareResult<>( |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5302 | oldLp != null ? oldLp.getAllInterfaceNames() : null, |
| 5303 | newLp != null ? newLp.getAllInterfaceNames() : null); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5304 | for (String iface : interfaceDiff.added) { |
| 5305 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5306 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5307 | mNMS.addInterfaceToNetwork(iface, netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5308 | wakeupModifyInterface(iface, caps, true); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5309 | } catch (Exception e) { |
| 5310 | loge("Exception adding interface: " + e); |
| 5311 | } |
| 5312 | } |
| 5313 | for (String iface : interfaceDiff.removed) { |
| 5314 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5315 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5316 | wakeupModifyInterface(iface, caps, false); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5317 | mNMS.removeInterfaceFromNetwork(iface, netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5318 | } catch (Exception e) { |
| 5319 | loge("Exception removing interface: " + e); |
| 5320 | } |
| 5321 | } |
| 5322 | } |
| 5323 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5324 | /** |
| 5325 | * Have netd update routes from oldLp to newLp. |
| 5326 | * @return true if routes changed between oldLp and newLp |
| 5327 | */ |
| 5328 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5329 | // Compare the route diff to determine which routes should be added and removed. |
| 5330 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>( |
| 5331 | oldLp != null ? oldLp.getAllRoutes() : null, |
| 5332 | newLp != null ? newLp.getAllRoutes() : null); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5333 | |
| 5334 | // add routes before removing old in case it helps with continuous connectivity |
| 5335 | |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5336 | // do this twice, adding non-next-hop routes first, then routes they are dependent on |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5337 | for (RouteInfo route : routeDiff.added) { |
| 5338 | if (route.hasGateway()) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5339 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5340 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5341 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5342 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5343 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 5344 | loge("Exception in addRoute for non-gateway: " + e); |
| 5345 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5346 | } |
| 5347 | } |
| 5348 | for (RouteInfo route : routeDiff.added) { |
| 5349 | if (route.hasGateway() == false) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5350 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5351 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5352 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5353 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5354 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 5355 | loge("Exception in addRoute for gateway: " + e); |
| 5356 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5357 | } |
| 5358 | } |
| 5359 | |
| 5360 | for (RouteInfo route : routeDiff.removed) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5361 | if (VDBG || DDBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5362 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5363 | mNMS.removeRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5364 | } catch (Exception e) { |
| 5365 | loge("Exception in removeRoute: " + e); |
| 5366 | } |
| 5367 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5368 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5369 | } |
Erik Kline | 4136850 | 2015-06-17 13:19:54 +0900 | [diff] [blame] | 5370 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5371 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5372 | if (oldLp != null && newLp.isIdenticalDnses(oldLp)) { |
| 5373 | return; // no updating necessary |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5374 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5375 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5376 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
| 5377 | final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId); |
| 5378 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5379 | if (DBG) { |
| 5380 | final Collection<InetAddress> dnses = newLp.getDnsServers(); |
| 5381 | log("Setting DNS servers for network " + netId + " to " + dnses); |
| 5382 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5383 | try { |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5384 | mDnsManager.setDnsConfigurationForNetwork(netId, newLp, isDefaultNetwork); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5385 | } catch (Exception e) { |
Pierre Imai | bd8759b | 2016-04-28 17:00:04 +0900 | [diff] [blame] | 5386 | loge("Exception in setDnsConfigurationForNetwork: " + e); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5387 | } |
Erik Kline | 4edba01 | 2017-04-07 15:29:29 +0900 | [diff] [blame] | 5388 | } |
| 5389 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5390 | private int getNetworkPermission(NetworkCapabilities nc) { |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5391 | if (!nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5392 | return INetd.PERMISSION_SYSTEM; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5393 | } |
| 5394 | if (!nc.hasCapability(NET_CAPABILITY_FOREGROUND)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5395 | return INetd.PERMISSION_NETWORK; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5396 | } |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5397 | return INetd.PERMISSION_NONE; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5398 | } |
| 5399 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5400 | /** |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5401 | * Augments the NetworkCapabilities passed in by a NetworkAgent with capabilities that are |
| 5402 | * maintained here that the NetworkAgent is not aware of (e.g., validated, captive portal, |
| 5403 | * and foreground status). |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5404 | */ |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5405 | private NetworkCapabilities mixInCapabilities(NetworkAgentInfo nai, NetworkCapabilities nc) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5406 | // Once a NetworkAgent is connected, complain if some immutable capabilities are removed. |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5407 | // Don't complain for VPNs since they're not driven by requests and there is no risk of |
| 5408 | // causing a connect/teardown loop. |
| 5409 | // TODO: remove this altogether and make it the responsibility of the NetworkFactories to |
| 5410 | // avoid connect/teardown loops. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5411 | if (nai.everConnected && |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5412 | !nai.isVPN() && |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5413 | !nai.networkCapabilities.satisfiedByImmutableNetworkCapabilities(nc)) { |
| 5414 | // TODO: consider not complaining when a network agent degrades its capabilities if this |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5415 | // does not cause any request (that is not a listen) currently matching that agent to |
| 5416 | // stop being matched by the updated agent. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5417 | String diff = nai.networkCapabilities.describeImmutableDifferences(nc); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5418 | if (!TextUtils.isEmpty(diff)) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5419 | Slog.wtf(TAG, "BUG: " + nai + " lost immutable capabilities:" + diff); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5420 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5421 | } |
| 5422 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5423 | // Don't modify caller's NetworkCapabilities. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5424 | NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5425 | if (nai.lastValidated) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5426 | newNc.addCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5427 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5428 | newNc.removeCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5429 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5430 | if (nai.lastCaptivePortalDetected) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5431 | newNc.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5432 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5433 | newNc.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5434 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5435 | if (nai.isBackgroundNetwork()) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5436 | newNc.removeCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5437 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5438 | newNc.addCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5439 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5440 | if (nai.isSuspended()) { |
| 5441 | newNc.removeCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5442 | } else { |
| 5443 | newNc.addCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5444 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5445 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5446 | return newNc; |
| 5447 | } |
| 5448 | |
| 5449 | /** |
| 5450 | * Update the NetworkCapabilities for {@code nai} to {@code nc}. Specifically: |
| 5451 | * |
| 5452 | * 1. Calls mixInCapabilities to merge the passed-in NetworkCapabilities {@code nc} with the |
| 5453 | * capabilities we manage and store in {@code nai}, such as validated status and captive |
| 5454 | * portal status) |
| 5455 | * 2. Takes action on the result: changes network permissions, sends CAP_CHANGED callbacks, and |
| 5456 | * potentially triggers rematches. |
| 5457 | * 3. Directly informs other network stack components (NetworkStatsService, VPNs, etc. of the |
| 5458 | * change.) |
| 5459 | * |
| 5460 | * @param oldScore score of the network before any of the changes that prompted us |
| 5461 | * to call this function. |
| 5462 | * @param nai the network having its capabilities updated. |
| 5463 | * @param nc the new network capabilities. |
| 5464 | */ |
| 5465 | private void updateCapabilities(int oldScore, NetworkAgentInfo nai, NetworkCapabilities nc) { |
| 5466 | NetworkCapabilities newNc = mixInCapabilities(nai, nc); |
| 5467 | |
| 5468 | if (Objects.equals(nai.networkCapabilities, newNc)) return; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5469 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5470 | final int oldPermission = getNetworkPermission(nai.networkCapabilities); |
| 5471 | final int newPermission = getNetworkPermission(newNc); |
| 5472 | if (oldPermission != newPermission && nai.created && !nai.isVPN()) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5473 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5474 | mNMS.setNetworkPermission(nai.network.netId, newPermission); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5475 | } catch (RemoteException e) { |
| 5476 | loge("Exception in setNetworkPermission: " + e); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 5477 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5478 | } |
| 5479 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5480 | final NetworkCapabilities prevNc; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5481 | synchronized (nai) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5482 | prevNc = nai.networkCapabilities; |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5483 | nai.networkCapabilities = newNc; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5484 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5485 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5486 | updateUids(nai, prevNc, newNc); |
| 5487 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5488 | if (nai.getCurrentScore() == oldScore && newNc.equalRequestableCapabilities(prevNc)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5489 | // If the requestable capabilities haven't changed, and the score hasn't changed, then |
| 5490 | // the change we're processing can't affect any requests, it can only affect the listens |
| 5491 | // on this network. We might have been called by rematchNetworkAndRequests when a |
| 5492 | // network changed foreground state. |
| 5493 | processListenRequests(nai, true); |
| 5494 | } else { |
| 5495 | // If the requestable capabilities have changed or the score changed, we can't have been |
| 5496 | // called by rematchNetworkAndRequests, so it's safe to start a rematch. |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5497 | rematchAllNetworksAndRequests(nai, oldScore); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5498 | try { |
| 5499 | nai.networkMonitor().notifyNetworkCapabilitiesChanged(); |
| 5500 | } catch (RemoteException e) { |
| 5501 | e.rethrowFromSystemServer(); |
| 5502 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5503 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 5504 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5505 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5506 | if (prevNc != null) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5507 | final boolean oldMetered = prevNc.isMetered(); |
| 5508 | final boolean newMetered = newNc.isMetered(); |
| 5509 | final boolean meteredChanged = oldMetered != newMetered; |
| 5510 | |
| 5511 | if (meteredChanged) { |
| 5512 | maybeNotifyNetworkBlocked(nai, oldMetered, newMetered, mRestrictBackground, |
| 5513 | mRestrictBackground); |
| 5514 | } |
| 5515 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5516 | final boolean roamingChanged = prevNc.hasCapability(NET_CAPABILITY_NOT_ROAMING) != |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5517 | newNc.hasCapability(NET_CAPABILITY_NOT_ROAMING); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5518 | |
| 5519 | // Report changes that are interesting for network statistics tracking. |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5520 | if (meteredChanged || roamingChanged) { |
| 5521 | notifyIfacesChangedForNetworkStats(); |
| 5522 | } |
| 5523 | } |
| 5524 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5525 | if (!newNc.hasTransport(TRANSPORT_VPN)) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5526 | // Tell VPNs about updated capabilities, since they may need to |
| 5527 | // bubble those changes through. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5528 | updateAllVpnsCapabilities(); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5529 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5530 | } |
| 5531 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5532 | private void updateUids(NetworkAgentInfo nai, NetworkCapabilities prevNc, |
| 5533 | NetworkCapabilities newNc) { |
| 5534 | Set<UidRange> prevRanges = null == prevNc ? null : prevNc.getUids(); |
| 5535 | Set<UidRange> newRanges = null == newNc ? null : newNc.getUids(); |
| 5536 | if (null == prevRanges) prevRanges = new ArraySet<>(); |
| 5537 | if (null == newRanges) newRanges = new ArraySet<>(); |
| 5538 | final Set<UidRange> prevRangesCopy = new ArraySet<>(prevRanges); |
| 5539 | |
| 5540 | prevRanges.removeAll(newRanges); |
| 5541 | newRanges.removeAll(prevRangesCopy); |
| 5542 | |
| 5543 | try { |
| 5544 | if (!newRanges.isEmpty()) { |
| 5545 | final UidRange[] addedRangesArray = new UidRange[newRanges.size()]; |
| 5546 | newRanges.toArray(addedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5547 | mNMS.addVpnUidRanges(nai.network.netId, addedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5548 | } |
| 5549 | if (!prevRanges.isEmpty()) { |
| 5550 | final UidRange[] removedRangesArray = new UidRange[prevRanges.size()]; |
| 5551 | prevRanges.toArray(removedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5552 | mNMS.removeVpnUidRanges(nai.network.netId, removedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5553 | } |
| 5554 | } catch (Exception e) { |
| 5555 | // Never crash! |
| 5556 | loge("Exception in updateUids: " + e); |
| 5557 | } |
| 5558 | } |
| 5559 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5560 | public void handleUpdateLinkProperties(NetworkAgentInfo nai, LinkProperties newLp) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 5561 | if (getNetworkAgentInfoForNetId(nai.network.netId) != nai) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5562 | // Ignore updates for disconnected networks |
| 5563 | return; |
| 5564 | } |
Rubin Xu | 6c1f6fd | 2017-09-11 15:21:10 +0100 | [diff] [blame] | 5565 | // newLp is already a defensive copy. |
| 5566 | newLp.ensureDirectlyConnectedRoutes(); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5567 | if (VDBG || DDBG) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5568 | log("Update of LinkProperties for " + nai.name() + |
| 5569 | "; created=" + nai.created + |
| 5570 | "; everConnected=" + nai.everConnected); |
| 5571 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5572 | updateLinkProperties(nai, newLp, new LinkProperties(nai.linkProperties)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5575 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5576 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5577 | NetworkRequest nr = nai.requestAt(i); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5578 | // Don't send listening requests to factories. b/17393458 |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5579 | if (nr.isListen()) continue; |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5580 | sendUpdatedScoreToFactories(nr, nai.getCurrentScore()); |
| 5581 | } |
| 5582 | } |
| 5583 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5584 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, int score) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5585 | if (VDBG || DDBG){ |
| 5586 | log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
| 5587 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5588 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 5589 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, 0, |
| 5590 | networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5591 | } |
| 5592 | } |
| 5593 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5594 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 5595 | int notificationType) { |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5596 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE && !nri.mPendingIntentSent) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5597 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 5598 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 5599 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5600 | nri.mPendingIntentSent = true; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5601 | sendIntent(nri.mPendingIntent, intent); |
| 5602 | } |
| 5603 | // else not handled |
| 5604 | } |
| 5605 | |
| 5606 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 5607 | mPendingIntentWakeLock.acquire(); |
| 5608 | try { |
| 5609 | if (DBG) log("Sending " + pendingIntent); |
| 5610 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 5611 | } catch (PendingIntent.CanceledException e) { |
| 5612 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 5613 | mPendingIntentWakeLock.release(); |
| 5614 | releasePendingNetworkRequest(pendingIntent); |
| 5615 | } |
| 5616 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 5617 | } |
| 5618 | |
| 5619 | @Override |
| 5620 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 5621 | String resultData, Bundle resultExtras) { |
| 5622 | if (DBG) log("Finished sending " + pendingIntent); |
| 5623 | mPendingIntentWakeLock.release(); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5624 | // Release with a delay so the receiving client has an opportunity to put in its |
| 5625 | // own request. |
| 5626 | releasePendingNetworkRequestWithDelay(pendingIntent); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5627 | } |
| 5628 | |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5629 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5630 | NetworkAgentInfo networkAgent, int notificationType, int arg1) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5631 | if (nri.messenger == null) { |
| 5632 | return; // Default request has no msgr |
| 5633 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5634 | Bundle bundle = new Bundle(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5635 | // TODO: check if defensive copies of data is needed. |
| 5636 | putParcelable(bundle, new NetworkRequest(nri.request)); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5637 | Message msg = Message.obtain(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5638 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL) { |
| 5639 | putParcelable(bundle, networkAgent.network); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5640 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5641 | switch (notificationType) { |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5642 | case ConnectivityManager.CALLBACK_AVAILABLE: { |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 5643 | putParcelable(bundle, networkCapabilitiesRestrictedForCallerPermissions( |
| 5644 | networkAgent.networkCapabilities, nri.mPid, nri.mUid)); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5645 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5646 | // For this notification, arg1 contains the blocked status. |
| 5647 | msg.arg1 = arg1; |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5648 | break; |
| 5649 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5650 | case ConnectivityManager.CALLBACK_LOSING: { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5651 | msg.arg1 = arg1; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5652 | break; |
| 5653 | } |
| 5654 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5655 | // networkAgent can't be null as it has been accessed a few lines above. |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5656 | final NetworkCapabilities nc = networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5657 | networkAgent.networkCapabilities, nri.mPid, nri.mUid); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5658 | putParcelable(bundle, nc); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5659 | break; |
| 5660 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5661 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5662 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5663 | break; |
| 5664 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5665 | case ConnectivityManager.CALLBACK_BLK_CHANGED: { |
junyulai | e6b3651 | 2018-10-24 22:38:06 +0800 | [diff] [blame] | 5666 | maybeLogBlockedStatusChanged(nri, networkAgent.network, arg1 != 0); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5667 | msg.arg1 = arg1; |
| 5668 | break; |
| 5669 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5670 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5671 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5672 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5673 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5674 | if (VDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 5675 | String notification = ConnectivityManager.getCallbackName(notificationType); |
| 5676 | log("sending notification " + notification + " for " + nri.request); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5677 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5678 | nri.messenger.send(msg); |
| 5679 | } catch (RemoteException e) { |
| 5680 | // may occur naturally in the race of binder death. |
| 5681 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 5682 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5683 | } |
| 5684 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5685 | private static <T extends Parcelable> void putParcelable(Bundle bundle, T t) { |
| 5686 | bundle.putParcelable(t.getClass().getSimpleName(), t); |
| 5687 | } |
| 5688 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5689 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5690 | if (nai.numRequestNetworkRequests() != 0) { |
| 5691 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5692 | NetworkRequest nr = nai.requestAt(i); |
| 5693 | // Ignore listening requests. |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5694 | if (nr.isListen()) continue; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5695 | loge("Dead network still had at least " + nr); |
| 5696 | break; |
| 5697 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5698 | } |
| 5699 | nai.asyncChannel.disconnect(); |
| 5700 | } |
| 5701 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5702 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 5703 | if (oldNetwork == null) { |
| 5704 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 5705 | return; |
| 5706 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5707 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5708 | |
| 5709 | // If we get here it means that the last linger timeout for this network expired. So there |
| 5710 | // must be no other active linger timers, and we must stop lingering. |
| 5711 | oldNetwork.clearLingerState(); |
| 5712 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 5713 | if (unneeded(oldNetwork, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5714 | // Tear the network down. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5715 | teardownUnneededNetwork(oldNetwork); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5716 | } else { |
| 5717 | // Put the network in the background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 5718 | updateCapabilities(oldNetwork.getCurrentScore(), oldNetwork, |
| 5719 | oldNetwork.networkCapabilities); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5720 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5721 | } |
| 5722 | |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 5723 | private void makeDefault(NetworkAgentInfo newNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5724 | if (DBG) log("Switching to new default network: " + newNetwork); |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 5725 | |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5726 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5727 | mNMS.setDefaultNetId(newNetwork.network.netId); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5728 | } catch (Exception e) { |
| 5729 | loge("Exception setting default network :" + e); |
| 5730 | } |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5731 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 5732 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5733 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5734 | updateTcpBufferSizes(newNetwork.linkProperties.getTcpBufferSizes()); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5735 | mDnsManager.setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5736 | notifyIfacesChangedForNetworkStats(); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5737 | // Fix up the NetworkCapabilities of any VPNs that don't specify underlying networks. |
| 5738 | updateAllVpnsCapabilities(); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5739 | } |
| 5740 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5741 | private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5742 | // For consistency with previous behaviour, send onLost callbacks before onAvailable. |
| 5743 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5744 | NetworkRequest nr = nri.request; |
| 5745 | if (!nr.isListen()) continue; |
| 5746 | if (nai.isSatisfyingRequest(nr.requestId) && !nai.satisfies(nr)) { |
| 5747 | nai.removeRequest(nri.request.requestId); |
| 5748 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_LOST, 0); |
| 5749 | } |
| 5750 | } |
| 5751 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5752 | if (capabilitiesChanged) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5753 | try { |
| 5754 | nai.networkMonitor().notifyNetworkCapabilitiesChanged(); |
| 5755 | } catch (RemoteException e) { |
| 5756 | e.rethrowFromSystemServer(); |
| 5757 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5758 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
| 5759 | } |
| 5760 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5761 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5762 | NetworkRequest nr = nri.request; |
| 5763 | if (!nr.isListen()) continue; |
| 5764 | if (nai.satisfies(nr) && !nai.isSatisfyingRequest(nr.requestId)) { |
| 5765 | nai.addRequest(nr); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 5766 | notifyNetworkAvailable(nai, nri); |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | } |
| 5770 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5771 | // Handles a network appearing or improving its score. |
| 5772 | // |
| 5773 | // - Evaluates all current NetworkRequests that can be |
| 5774 | // satisfied by newNetwork, and reassigns to newNetwork |
| 5775 | // any such requests for which newNetwork is the best. |
| 5776 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 5777 | // - Lingers any validated Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5778 | // needed. A network is needed if it is the best network for |
| 5779 | // one or more NetworkRequests, or if it is a VPN. |
| 5780 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 5781 | // - Tears down newNetwork if it just became validated |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 5782 | // but turns out to be unneeded. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 5783 | // |
| 5784 | // - If reapUnvalidatedNetworks==REAP, tears down unvalidated |
| 5785 | // networks that have no chance (i.e. even if validated) |
| 5786 | // of becoming the highest scoring network. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5787 | // |
| 5788 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 5789 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 5790 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 5791 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 5792 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 5793 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 5794 | // @param newNetwork is the network to be matched against NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 5795 | // @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be |
| 5796 | // performed to tear down unvalidated networks that have no chance (i.e. even if |
| 5797 | // validated) of becoming the highest scoring network. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 5798 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5799 | ReapUnvalidatedNetworks reapUnvalidatedNetworks, long now) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 5800 | if (!newNetwork.everConnected) return; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 5801 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5802 | boolean isNewDefault = false; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 5803 | NetworkAgentInfo oldDefaultNetwork = null; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5804 | |
| 5805 | final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork(); |
| 5806 | final int score = newNetwork.getCurrentScore(); |
| 5807 | |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5808 | if (VDBG || DDBG) log("rematching " + newNetwork.name()); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5809 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5810 | // Find and migrate to this Network any NetworkRequests for |
| 5811 | // which this network is now the best. |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5812 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<>(); |
| 5813 | ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<>(); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5814 | NetworkCapabilities nc = newNetwork.networkCapabilities; |
| 5815 | if (VDBG) log(" network has: " + nc); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5816 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5817 | // Process requests in the first pass and listens in the second pass. This allows us to |
| 5818 | // change a network's capabilities depending on which requests it has. This is only |
| 5819 | // correct if the change in capabilities doesn't affect whether the network satisfies |
| 5820 | // requests or not, and doesn't affect the network's score. |
| 5821 | if (nri.request.isListen()) continue; |
| 5822 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5823 | final NetworkAgentInfo currentNetwork = getNetworkForRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5824 | final boolean satisfies = newNetwork.satisfies(nri.request); |
| 5825 | if (newNetwork == currentNetwork && satisfies) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 5826 | if (VDBG) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 5827 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 5828 | " request " + nri.request.requestId + ". No change."); |
| 5829 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 5830 | keep = true; |
| 5831 | continue; |
| 5832 | } |
| 5833 | |
| 5834 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5835 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5836 | if (satisfies) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5837 | // next check if it's better than any current network we're using for |
| 5838 | // this request |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5839 | if (VDBG || DDBG) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5840 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5841 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5842 | ", newScore = " + score); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5843 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5844 | if (currentNetwork == null || currentNetwork.getCurrentScore() < score) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5845 | if (VDBG) log("rematch for " + newNetwork.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5846 | if (currentNetwork != null) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5847 | if (VDBG || DDBG){ |
| 5848 | log(" accepting network in place of " + currentNetwork.name()); |
| 5849 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5850 | currentNetwork.removeRequest(nri.request.requestId); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5851 | currentNetwork.lingerRequest(nri.request, now, mLingerDelayMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5852 | affectedNetworks.add(currentNetwork); |
| 5853 | } else { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5854 | if (VDBG || DDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5855 | } |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5856 | newNetwork.unlingerRequest(nri.request); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5857 | setNetworkForRequest(nri.request.requestId, newNetwork); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 5858 | if (!newNetwork.addRequest(nri.request)) { |
| 5859 | Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request); |
| 5860 | } |
| 5861 | addedRequests.add(nri); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5862 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5863 | // Tell NetworkFactories about the new score, so they can stop |
| 5864 | // trying to connect if they know they cannot match it. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5865 | // TODO - this could get expensive if we have a lot of requests for this |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5866 | // network. Think about if there is a way to reduce this. Push |
| 5867 | // netid->request mapping to each factory? |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5868 | sendUpdatedScoreToFactories(nri.request, score); |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 5869 | if (isDefaultRequest(nri)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5870 | isNewDefault = true; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 5871 | oldDefaultNetwork = currentNetwork; |
Lorenzo Colitti | c2e10bb | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 5872 | if (currentNetwork != null) { |
| 5873 | mLingerMonitor.noteLingerDefaultNetwork(currentNetwork, newNetwork); |
| 5874 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5875 | } |
| 5876 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5877 | } else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5878 | // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri", |
| 5879 | // mark it as no longer satisfying "nri". Because networks are processed by |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 5880 | // rematchAllNetworksAndRequests() in descending score order, "currentNetwork" will |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5881 | // match "newNetwork" before this loop will encounter a "currentNetwork" with higher |
| 5882 | // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri". |
| 5883 | // This means this code doesn't have to handle the case where "currentNetwork" no |
| 5884 | // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork". |
| 5885 | if (DBG) { |
| 5886 | log("Network " + newNetwork.name() + " stopped satisfying" + |
| 5887 | " request " + nri.request.requestId); |
| 5888 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5889 | newNetwork.removeRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5890 | if (currentNetwork == newNetwork) { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5891 | clearNetworkForRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5892 | sendUpdatedScoreToFactories(nri.request, 0); |
| 5893 | } else { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5894 | Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + |
| 5895 | newNetwork.name() + |
| 5896 | " without updating mNetworkForRequestId or factories!"); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5897 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5898 | // TODO: Technically, sending CALLBACK_LOST here is |
| 5899 | // incorrect if there is a replacement network currently |
| 5900 | // connected that can satisfy nri, which is a request |
| 5901 | // (not a listen). However, the only capability that can both |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5902 | // a) be requested and b) change is NET_CAPABILITY_TRUSTED, |
| 5903 | // so this code is only incorrect for a network that loses |
| 5904 | // the TRUSTED capability, which is a rare case. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5905 | callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5906 | } |
| 5907 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5908 | if (isNewDefault) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 5909 | updateDataActivityTracking(newNetwork, oldDefaultNetwork); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5910 | // Notify system services that this network is up. |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 5911 | makeDefault(newNetwork); |
| 5912 | // Log 0 -> X and Y -> X default network transitions, where X is the new default. |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 5913 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent( |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 5914 | now, newNetwork, oldDefaultNetwork); |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 5915 | // Have a new default network, release the transition wakelock in |
| 5916 | scheduleReleaseNetworkTransitionWakelock(); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5917 | } |
| 5918 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5919 | if (!newNetwork.networkCapabilities.equalRequestableCapabilities(nc)) { |
| 5920 | Slog.wtf(TAG, String.format( |
| 5921 | "BUG: %s changed requestable capabilities during rematch: %s -> %s", |
Andreas Gampe | d4f64c4 | 2017-07-11 15:14:41 -0700 | [diff] [blame] | 5922 | newNetwork.name(), nc, newNetwork.networkCapabilities)); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5923 | } |
| 5924 | if (newNetwork.getCurrentScore() != score) { |
| 5925 | Slog.wtf(TAG, String.format( |
| 5926 | "BUG: %s changed score during rematch: %d -> %d", |
liangweikang@xiaomi.com | e9a7c26 | 2017-06-29 14:36:30 +0800 | [diff] [blame] | 5927 | newNetwork.name(), score, newNetwork.getCurrentScore())); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5928 | } |
| 5929 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5930 | // Second pass: process all listens. |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5931 | if (wasBackgroundNetwork != newNetwork.isBackgroundNetwork()) { |
| 5932 | // If the network went from background to foreground or vice versa, we need to update |
| 5933 | // its foreground state. It is safe to do this after rematching the requests because |
| 5934 | // NET_CAPABILITY_FOREGROUND does not affect requests, as is not a requestable |
| 5935 | // capability and does not affect the network's score (see the Slog.wtf call above). |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 5936 | updateCapabilities(score, newNetwork, newNetwork.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5937 | } else { |
| 5938 | processListenRequests(newNetwork, false); |
| 5939 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5940 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5941 | // do this after the default net is switched, but |
| 5942 | // before LegacyTypeTracker sends legacy broadcasts |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 5943 | for (NetworkRequestInfo nri : addedRequests) notifyNetworkAvailable(newNetwork, nri); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5944 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5945 | // Linger any networks that are no longer needed. This should be done after sending the |
| 5946 | // available callback for newNetwork. |
| 5947 | for (NetworkAgentInfo nai : affectedNetworks) { |
| 5948 | updateLingerState(nai, now); |
| 5949 | } |
| 5950 | // Possibly unlinger newNetwork. Unlingering a network does not send any callbacks so it |
| 5951 | // does not need to be done in any particular order. |
| 5952 | updateLingerState(newNetwork, now); |
| 5953 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5954 | if (isNewDefault) { |
| 5955 | // Maintain the illusion: since the legacy API only |
| 5956 | // understands one network at a time, we must pretend |
| 5957 | // that the current default network disconnected before |
| 5958 | // the new one connected. |
| 5959 | if (oldDefaultNetwork != null) { |
| 5960 | mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), |
| 5961 | oldDefaultNetwork, true); |
| 5962 | } |
| 5963 | mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0; |
| 5964 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
| 5965 | notifyLockdownVpn(newNetwork); |
| 5966 | } |
| 5967 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5968 | if (keep) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 5969 | // Notify battery stats service about this network, both the normal |
| 5970 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 5971 | // 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] | 5972 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 5973 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 5974 | final int type = newNetwork.networkInfo.getType(); |
| 5975 | |
| 5976 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 5977 | bs.noteNetworkInterfaceType(baseIface, type); |
| 5978 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 5979 | final String stackedIface = stacked.getInterfaceName(); |
| 5980 | bs.noteNetworkInterfaceType(stackedIface, type); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 5981 | } |
| 5982 | } catch (RemoteException ignored) { |
| 5983 | } |
| 5984 | |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 5985 | // This has to happen after the notifyNetworkCallbacks as that tickles each |
| 5986 | // ConnectivityManager instance so that legacy requests correctly bind dns |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5987 | // requests to this network. The legacy users are listening for this broadcast |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 5988 | // and will generally do a dns request so they can ensureRouteToHost and if |
| 5989 | // they do that before the callbacks happen they'll use the default network. |
| 5990 | // |
| 5991 | // TODO: Is there still a race here? We send the broadcast |
| 5992 | // after sending the callback, but if the app can receive the |
| 5993 | // broadcast before the callback, it might still break. |
| 5994 | // |
| 5995 | // This *does* introduce a race where if the user uses the new api |
| 5996 | // (notification callbacks) and then uses the old api (getNetworkInfo(type)) |
| 5997 | // they may get old info. Reverse this after the old startUsing api is removed. |
| 5998 | // This is on top of the multiple intent sequencing referenced in the todo above. |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5999 | for (int i = 0; i < newNetwork.numNetworkRequests(); i++) { |
| 6000 | NetworkRequest nr = newNetwork.requestAt(i); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 6001 | if (nr.legacyType != TYPE_NONE && nr.isRequest()) { |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6002 | // legacy type tracker filters out repeat adds |
| 6003 | mLegacyTypeTracker.add(nr.legacyType, newNetwork); |
| 6004 | } |
| 6005 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 6006 | |
| 6007 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 6008 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 6009 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 6010 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 6011 | if (newNetwork.isVPN()) { |
| 6012 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 6013 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6014 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6015 | if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) { |
| 6016 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 6017 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6018 | if (nai.getLingerExpiry() > 0) { |
| 6019 | // This network has active linger timers and no requests, but is not |
| 6020 | // lingering. Linger it. |
| 6021 | // |
| 6022 | // One way (the only way?) this can happen if this network is unvalidated |
| 6023 | // and became unneeded due to another network improving its score to the |
| 6024 | // point where this network will no longer be able to satisfy any requests |
| 6025 | // even if it validates. |
| 6026 | updateLingerState(nai, now); |
| 6027 | } else { |
| 6028 | if (DBG) log("Reaping " + nai.name()); |
| 6029 | teardownUnneededNetwork(nai); |
| 6030 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6031 | } |
| 6032 | } |
| 6033 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6034 | } |
| 6035 | |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6036 | /** |
| 6037 | * Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 6038 | * being disconnected. |
| 6039 | * @param changed If only one Network's score or capabilities have been modified since the last |
| 6040 | * time this function was called, pass this Network in this argument, otherwise pass |
| 6041 | * null. |
| 6042 | * @param oldScore If only one Network has been changed but its NetworkCapabilities have not |
| 6043 | * changed, pass in the Network's score (from getCurrentScore()) prior to the change via |
| 6044 | * this argument, otherwise pass {@code changed.getCurrentScore()} or 0 if |
| 6045 | * {@code changed} is {@code null}. This is because NetworkCapabilities influence a |
| 6046 | * network's score. |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6047 | */ |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6048 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6049 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 6050 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 6051 | // example in the case where "changed"'s score decreases and another network should begin |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6052 | // satisfying a NetworkRequest that "changed" currently satisfies. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6053 | |
| 6054 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 6055 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 6056 | // rematchNetworkAndRequests() handles. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6057 | final long now = SystemClock.elapsedRealtime(); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6058 | if (changed != null && oldScore < changed.getCurrentScore()) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6059 | rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP, now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6060 | } else { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6061 | final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray( |
| 6062 | new NetworkAgentInfo[mNetworkAgentInfos.size()]); |
| 6063 | // Rematch higher scoring networks first to prevent requests first matching a lower |
| 6064 | // scoring network and then a higher scoring network, which could produce multiple |
| 6065 | // callbacks and inadvertently unlinger networks. |
| 6066 | Arrays.sort(nais); |
| 6067 | for (NetworkAgentInfo nai : nais) { |
| 6068 | rematchNetworkAndRequests(nai, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6069 | // Only reap the last time through the loop. Reaping before all rematching |
| 6070 | // is complete could incorrectly teardown a network that hasn't yet been |
| 6071 | // rematched. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6072 | (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6073 | : ReapUnvalidatedNetworks.REAP, |
| 6074 | now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6075 | } |
| 6076 | } |
| 6077 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6078 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6079 | private void updateInetCondition(NetworkAgentInfo nai) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6080 | // 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] | 6081 | if (!nai.everValidated) return; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6082 | // For now only update icons for default connection. |
| 6083 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 6084 | if (!isDefaultNetwork(nai)) return; |
| 6085 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6086 | int newInetCondition = nai.lastValidated ? 100 : 0; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6087 | // Don't repeat publish. |
| 6088 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 6089 | |
| 6090 | mDefaultInetConditionPublished = newInetCondition; |
| 6091 | sendInetConditionBroadcast(nai.networkInfo); |
| 6092 | } |
| 6093 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6094 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6095 | synchronized (mVpns) { |
| 6096 | if (mLockdownTracker != null) { |
| 6097 | if (nai != null && nai.isVPN()) { |
| 6098 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 6099 | } else { |
| 6100 | mLockdownTracker.onNetworkInfoChanged(); |
| 6101 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6102 | } |
| 6103 | } |
| 6104 | } |
| 6105 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6106 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6107 | final NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6108 | NetworkInfo oldInfo = null; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6109 | final int oldScore = networkAgent.getCurrentScore(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6110 | synchronized (networkAgent) { |
| 6111 | oldInfo = networkAgent.networkInfo; |
| 6112 | networkAgent.networkInfo = newInfo; |
| 6113 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6114 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6115 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6116 | if (DBG) { |
| 6117 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 6118 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 6119 | " to " + state); |
| 6120 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6121 | |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6122 | if (!networkAgent.created |
| 6123 | && (state == NetworkInfo.State.CONNECTED |
| 6124 | || (state == NetworkInfo.State.CONNECTING && networkAgent.isVPN()))) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6125 | |
| 6126 | // A network that has just connected has zero requests and is thus a foreground network. |
| 6127 | networkAgent.networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND); |
| 6128 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6129 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6130 | // 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] | 6131 | if (networkAgent.isVPN()) { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6132 | mNMS.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 6133 | (networkAgent.networkMisc == null || |
| 6134 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6135 | } else { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6136 | mNMS.createPhysicalNetwork(networkAgent.network.netId, |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 6137 | getNetworkPermission(networkAgent.networkCapabilities)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6138 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6139 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 6140 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 6141 | + e.getMessage()); |
| 6142 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6143 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6144 | networkAgent.created = true; |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | if (!networkAgent.everConnected && state == NetworkInfo.State.CONNECTED) { |
| 6148 | networkAgent.everConnected = true; |
| 6149 | |
Lorenzo Colitti | 2df4c7d | 2018-02-27 22:47:01 +0900 | [diff] [blame] | 6150 | if (networkAgent.linkProperties == null) { |
| 6151 | Slog.wtf(TAG, networkAgent.name() + " connected with null LinkProperties"); |
| 6152 | } |
| 6153 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 6154 | handlePerNetworkPrivateDnsConfig(networkAgent, mDnsManager.getPrivateDnsConfig()); |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 6155 | updateLinkProperties(networkAgent, new LinkProperties(networkAgent.linkProperties), |
| 6156 | null); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6157 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6158 | // Until parceled LinkProperties are sent directly to NetworkMonitor, the connect |
| 6159 | // command must be sent after updating LinkProperties to maximize chances of |
| 6160 | // NetworkMonitor seeing the correct LinkProperties when starting. |
| 6161 | // TODO: pass LinkProperties to the NetworkMonitor in the notifyNetworkConnected call. |
| 6162 | try { |
| 6163 | networkAgent.networkMonitor().notifyNetworkConnected(); |
| 6164 | } catch (RemoteException e) { |
| 6165 | e.rethrowFromSystemServer(); |
| 6166 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 6167 | scheduleUnvalidatedPrompt(networkAgent); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6168 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6169 | // Whether a particular NetworkRequest listen should cause signal strength thresholds to |
| 6170 | // be communicated to a particular NetworkAgent depends only on the network's immutable, |
| 6171 | // capabilities, so it only needs to be done once on initial connect, not every time the |
| 6172 | // network's capabilities change. Note that we do this before rematching the network, |
| 6173 | // so we could decide to tear it down immediately afterwards. That's fine though - on |
| 6174 | // disconnection NetworkAgents should stop any signal strength monitoring they have been |
| 6175 | // doing. |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 6176 | updateSignalStrengthThresholds(networkAgent, "CONNECT", null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6177 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6178 | if (networkAgent.isVPN()) { |
| 6179 | updateAllVpnsCapabilities(); |
| 6180 | } |
| 6181 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6182 | // Consider network even though it is not yet validated. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6183 | final long now = SystemClock.elapsedRealtime(); |
| 6184 | rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP, now); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6185 | |
| 6186 | // This has to happen after matching the requests, because callbacks are just requests. |
| 6187 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6188 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6189 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6190 | if (networkAgent.isVPN()) { |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 6191 | updateUids(networkAgent, networkAgent.networkCapabilities, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6192 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 6193 | disconnectAndDestroyNetwork(networkAgent); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 6194 | if (networkAgent.isVPN()) { |
| 6195 | // As the active or bound network changes for apps, broadcast the default proxy, as |
| 6196 | // apps may need to update their proxy data. This is called after disconnecting from |
| 6197 | // VPN to make sure we do not broadcast the old proxy data. |
| 6198 | // TODO(b/122649188): send the broadcast only to VPN users. |
| 6199 | mProxyTracker.sendProxyBroadcast(); |
| 6200 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6201 | } else if ((oldInfo != null && oldInfo.getState() == NetworkInfo.State.SUSPENDED) || |
| 6202 | state == NetworkInfo.State.SUSPENDED) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6203 | // going into or coming out of SUSPEND: re-score and notify |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6204 | if (networkAgent.getCurrentScore() != oldScore) { |
Paul Jensen | 3b9ce37 | 2015-07-10 12:19:38 -0400 | [diff] [blame] | 6205 | rematchAllNetworksAndRequests(networkAgent, oldScore); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6206 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 6207 | updateCapabilities(networkAgent.getCurrentScore(), networkAgent, |
| 6208 | networkAgent.networkCapabilities); |
| 6209 | // TODO (b/73132094) : remove this call once the few users of onSuspended and |
| 6210 | // onResumed have been removed. |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6211 | notifyNetworkCallbacks(networkAgent, (state == NetworkInfo.State.SUSPENDED ? |
| 6212 | ConnectivityManager.CALLBACK_SUSPENDED : |
| 6213 | ConnectivityManager.CALLBACK_RESUMED)); |
| 6214 | mLegacyTypeTracker.update(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6215 | } |
| 6216 | } |
| 6217 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6218 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6219 | if (VDBG || DDBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 6220 | if (score < 0) { |
| 6221 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 6222 | "). Bumping score to min of 0"); |
| 6223 | score = 0; |
| 6224 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6225 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6226 | final int oldScore = nai.getCurrentScore(); |
| 6227 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6228 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6229 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6230 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 6231 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 6232 | } |
| 6233 | |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6234 | // Notify only this one new request of the current state. Transfer all the |
| 6235 | // current state by calling NetworkCapabilities and LinkProperties callbacks |
| 6236 | // so that callers can be guaranteed to have as close to atomicity in state |
| 6237 | // transfer as can be supported by this current API. |
| 6238 | protected void notifyNetworkAvailable(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
Etan Cohen | 681fcda | 2016-10-27 15:05:50 -0700 | [diff] [blame] | 6239 | mHandler.removeMessages(EVENT_TIMEOUT_NETWORK_REQUEST, nri); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6240 | if (nri.mPendingIntent != null) { |
| 6241 | sendPendingIntentForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE); |
| 6242 | // Attempt no subsequent state pushes where intents are involved. |
| 6243 | return; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6244 | } |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6245 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 6246 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6247 | final boolean blocked = isUidNetworkingWithVpnBlocked(nri.mUid, mUidRules.get(nri.mUid), |
| 6248 | metered, mRestrictBackground); |
| 6249 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE, blocked ? 1 : 0); |
| 6250 | } |
| 6251 | |
| 6252 | /** |
| 6253 | * Notify of the blocked state apps with a registered callback matching a given NAI. |
| 6254 | * |
| 6255 | * Unlike other callbacks, blocked status is different between each individual uid. So for |
| 6256 | * any given nai, all requests need to be considered according to the uid who filed it. |
| 6257 | * |
| 6258 | * @param nai The target NetworkAgentInfo. |
| 6259 | * @param oldMetered True if the previous network capabilities is metered. |
| 6260 | * @param newRestrictBackground True if data saver is enabled. |
| 6261 | */ |
| 6262 | private void maybeNotifyNetworkBlocked(NetworkAgentInfo nai, boolean oldMetered, |
| 6263 | boolean newMetered, boolean oldRestrictBackground, boolean newRestrictBackground) { |
| 6264 | |
| 6265 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6266 | NetworkRequest nr = nai.requestAt(i); |
| 6267 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6268 | final int uidRules = mUidRules.get(nri.mUid); |
| 6269 | final boolean oldBlocked, newBlocked; |
| 6270 | // mVpns lock needs to be hold here to ensure that the active VPN cannot be changed |
| 6271 | // between these two calls. |
| 6272 | synchronized (mVpns) { |
| 6273 | oldBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, oldMetered, |
| 6274 | oldRestrictBackground); |
| 6275 | newBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, newMetered, |
| 6276 | newRestrictBackground); |
| 6277 | } |
| 6278 | if (oldBlocked != newBlocked) { |
| 6279 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, |
| 6280 | encodeBool(newBlocked)); |
| 6281 | } |
| 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | /** |
| 6286 | * Notify apps with a given UID of the new blocked state according to new uid rules. |
| 6287 | * @param uid The uid for which the rules changed. |
| 6288 | * @param newRules The new rules to apply. |
| 6289 | */ |
| 6290 | private void maybeNotifyNetworkBlockedForNewUidRules(int uid, int newRules) { |
| 6291 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6292 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6293 | final boolean oldBlocked, newBlocked; |
| 6294 | // TODO: Consider that doze mode or turn on/off battery saver would deliver lots of uid |
| 6295 | // rules changed event. And this function actually loop through all connected nai and |
| 6296 | // its requests. It seems that mVpns lock will be grabbed frequently in this case. |
| 6297 | // Reduce the number of locking or optimize the use of lock are likely needed in future. |
| 6298 | synchronized (mVpns) { |
| 6299 | oldBlocked = isUidNetworkingWithVpnBlocked( |
| 6300 | uid, mUidRules.get(uid), metered, mRestrictBackground); |
| 6301 | newBlocked = isUidNetworkingWithVpnBlocked( |
| 6302 | uid, newRules, metered, mRestrictBackground); |
| 6303 | } |
| 6304 | if (oldBlocked == newBlocked) { |
| 6305 | return; |
| 6306 | } |
| 6307 | final int arg = encodeBool(newBlocked); |
| 6308 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6309 | NetworkRequest nr = nai.requestAt(i); |
| 6310 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6311 | if (nri != null && nri.mUid == uid) { |
| 6312 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, arg); |
| 6313 | } |
| 6314 | } |
| 6315 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6316 | } |
| 6317 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6318 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, DetailedState state, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 6319 | // The NetworkInfo we actually send out has no bearing on the real |
| 6320 | // state of affairs. For example, if the default connection is mobile, |
| 6321 | // and a request for HIPRI has just gone away, we need to pretend that |
| 6322 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 6323 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 6324 | // and is still connected. |
| 6325 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 6326 | info.setType(type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6327 | if (state != DetailedState.DISCONNECTED) { |
| 6328 | info.setDetailedState(state, null, info.getExtraInfo()); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6329 | sendConnectedBroadcast(info); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6330 | } else { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6331 | info.setDetailedState(state, info.getReason(), info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6332 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 6333 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 6334 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 6335 | if (info.isFailover()) { |
| 6336 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 6337 | nai.networkInfo.setFailover(false); |
| 6338 | } |
| 6339 | if (info.getReason() != null) { |
| 6340 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 6341 | } |
| 6342 | if (info.getExtraInfo() != null) { |
| 6343 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 6344 | } |
| 6345 | NetworkAgentInfo newDefaultAgent = null; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6346 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6347 | newDefaultAgent = getDefaultNetwork(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6348 | if (newDefaultAgent != null) { |
| 6349 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 6350 | newDefaultAgent.networkInfo); |
| 6351 | } else { |
| 6352 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 6353 | } |
| 6354 | } |
| 6355 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 6356 | mDefaultInetConditionPublished); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6357 | sendStickyBroadcast(intent); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6358 | if (newDefaultAgent != null) { |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6359 | sendConnectedBroadcast(newDefaultAgent.networkInfo); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6360 | } |
| 6361 | } |
| 6362 | } |
| 6363 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6364 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType, int arg1) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6365 | if (VDBG || DDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 6366 | String notification = ConnectivityManager.getCallbackName(notifyType); |
| 6367 | log("notifyType " + notification + " for " + networkAgent.name()); |
| 6368 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6369 | for (int i = 0; i < networkAgent.numNetworkRequests(); i++) { |
| 6370 | NetworkRequest nr = networkAgent.requestAt(i); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6371 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6372 | if (VDBG) log(" sending notification for " + nr); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6373 | // TODO: if we're in the middle of a rematch, can we send a CAP_CHANGED callback for |
| 6374 | // a network that no longer satisfies the listen? |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6375 | if (nri.mPendingIntent == null) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6376 | callCallbackForRequest(nri, networkAgent, notifyType, arg1); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6377 | } else { |
| 6378 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 6379 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6380 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6381 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6382 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6383 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
| 6384 | notifyNetworkCallbacks(networkAgent, notifyType, 0); |
| 6385 | } |
| 6386 | |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6387 | /** |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6388 | * Returns the list of all interfaces that could be used by network traffic that does not |
| 6389 | * explicitly specify a network. This includes the default network, but also all VPNs that are |
| 6390 | * currently connected. |
| 6391 | * |
| 6392 | * Must be called on the handler thread. |
| 6393 | */ |
| 6394 | private Network[] getDefaultNetworks() { |
| 6395 | ArrayList<Network> defaultNetworks = new ArrayList<>(); |
| 6396 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 6397 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6398 | if (nai.everConnected && (nai == defaultNetwork || nai.isVPN())) { |
| 6399 | defaultNetworks.add(nai.network); |
| 6400 | } |
| 6401 | } |
| 6402 | return defaultNetworks.toArray(new Network[0]); |
| 6403 | } |
| 6404 | |
| 6405 | /** |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6406 | * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the |
| 6407 | * properties tracked by NetworkStatsService on an active iface has changed. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6408 | */ |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6409 | private void notifyIfacesChangedForNetworkStats() { |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6410 | try { |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6411 | mStatsService.forceUpdateIfaces(getDefaultNetworks()); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6412 | } catch (Exception ignored) { |
| 6413 | } |
| 6414 | } |
| 6415 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6416 | @Override |
| 6417 | public boolean addVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6418 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6419 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6420 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6421 | return mVpns.get(user).addAddress(address, prefixLength); |
| 6422 | } |
| 6423 | } |
| 6424 | |
| 6425 | @Override |
| 6426 | public boolean removeVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6427 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6428 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6429 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6430 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 6431 | } |
| 6432 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6433 | |
| 6434 | @Override |
| 6435 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6436 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6437 | final boolean success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6438 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6439 | throwIfLockdownEnabled(); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6440 | success = mVpns.get(user).setUnderlyingNetworks(networks); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6441 | } |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6442 | if (success) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6443 | mHandler.post(() -> { |
| 6444 | // Update VPN's capabilities based on updated underlying network set. |
| 6445 | updateAllVpnsCapabilities(); |
| 6446 | notifyIfacesChangedForNetworkStats(); |
| 6447 | }); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6448 | } |
| 6449 | return success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6450 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6451 | |
| 6452 | @Override |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6453 | public String getCaptivePortalServerUrl() { |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 6454 | enforceConnectivityInternalPermission(); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6455 | return NetworkMonitorUtils.getCaptivePortalServerHttpUrl(mContext); |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6456 | } |
| 6457 | |
| 6458 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6459 | public void startNattKeepalive(Network network, int intervalSeconds, Messenger messenger, |
| 6460 | IBinder binder, String srcAddr, int srcPort, String dstAddr) { |
| 6461 | enforceKeepalivePermission(); |
| 6462 | mKeepaliveTracker.startNattKeepalive( |
| 6463 | getNetworkAgentInfoForNetwork(network), |
| 6464 | intervalSeconds, messenger, binder, |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6465 | srcAddr, srcPort, dstAddr, NattSocketKeepalive.NATT_PORT); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6466 | } |
| 6467 | |
| 6468 | @Override |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6469 | public void startNattKeepaliveWithFd(Network network, FileDescriptor fd, int resourceId, |
| 6470 | int intervalSeconds, Messenger messenger, IBinder binder, String srcAddr, |
| 6471 | String dstAddr) { |
| 6472 | enforceKeepalivePermission(); |
| 6473 | mKeepaliveTracker.startNattKeepalive( |
| 6474 | getNetworkAgentInfoForNetwork(network), fd, resourceId, |
| 6475 | intervalSeconds, messenger, binder, |
| 6476 | srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT); |
| 6477 | } |
| 6478 | |
| 6479 | @Override |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6480 | public void startTcpKeepalive(Network network, FileDescriptor fd, int intervalSeconds, |
| 6481 | Messenger messenger, IBinder binder) { |
| 6482 | enforceKeepalivePermission(); |
| 6483 | mKeepaliveTracker.startTcpKeepalive( |
| 6484 | getNetworkAgentInfoForNetwork(network), fd, intervalSeconds, messenger, binder); |
| 6485 | } |
| 6486 | |
| 6487 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6488 | public void stopKeepalive(Network network, int slot) { |
| 6489 | mHandler.sendMessage(mHandler.obtainMessage( |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6490 | NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE, slot, SocketKeepalive.SUCCESS, network)); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6491 | } |
| 6492 | |
| 6493 | @Override |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6494 | public void factoryReset() { |
| 6495 | enforceConnectivityInternalPermission(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6496 | |
| 6497 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 6498 | return; |
| 6499 | } |
| 6500 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 6501 | final int userId = UserHandle.getCallingUserId(); |
| 6502 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6503 | // Turn airplane mode off |
| 6504 | setAirplaneMode(false); |
| 6505 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6506 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 6507 | // Untether |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6508 | String pkgName = mContext.getOpPackageName(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6509 | for (String tether : getTetheredIfaces()) { |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6510 | untether(tether, pkgName); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6511 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6512 | } |
| 6513 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6514 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6515 | // Remove always-on package |
| 6516 | synchronized (mVpns) { |
| 6517 | final String alwaysOnPackage = getAlwaysOnVpnPackage(userId); |
| 6518 | if (alwaysOnPackage != null) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 6519 | setAlwaysOnVpnPackage(userId, null, false, null); |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6520 | setVpnPackageAuthorization(alwaysOnPackage, userId, false); |
| 6521 | } |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6522 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6523 | // Turn Always-on VPN off |
| 6524 | if (mLockdownEnabled && userId == UserHandle.USER_SYSTEM) { |
| 6525 | final long ident = Binder.clearCallingIdentity(); |
| 6526 | try { |
| 6527 | mKeyStore.delete(Credentials.LOCKDOWN_VPN); |
| 6528 | mLockdownEnabled = false; |
| 6529 | setLockdownTracker(null); |
| 6530 | } finally { |
| 6531 | Binder.restoreCallingIdentity(ident); |
| 6532 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6533 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6534 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6535 | // Turn VPN off |
| 6536 | VpnConfig vpnConfig = getVpnConfig(userId); |
| 6537 | if (vpnConfig != null) { |
| 6538 | if (vpnConfig.legacy) { |
| 6539 | prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, userId); |
| 6540 | } else { |
| 6541 | // Prevent this app (packagename = vpnConfig.user) from initiating |
| 6542 | // VPN connections in the future without user intervention. |
| 6543 | setVpnPackageAuthorization(vpnConfig.user, userId, false); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6544 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6545 | prepareVpn(null, VpnConfig.LEGACY_VPN, userId); |
| 6546 | } |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6547 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6548 | } |
| 6549 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 6550 | |
| 6551 | Settings.Global.putString(mContext.getContentResolver(), |
| 6552 | Settings.Global.NETWORK_AVOID_BAD_WIFI, null); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6553 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6554 | |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 6555 | @Override |
| 6556 | public byte[] getNetworkWatchlistConfigHash() { |
| 6557 | NetworkWatchlistManager nwm = mContext.getSystemService(NetworkWatchlistManager.class); |
| 6558 | if (nwm == null) { |
| 6559 | loge("Unable to get NetworkWatchlistManager"); |
| 6560 | return null; |
| 6561 | } |
| 6562 | // Redirect it to network watchlist service to access watchlist file and calculate hash. |
| 6563 | return nwm.getWatchlistConfigHash(); |
| 6564 | } |
| 6565 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6566 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 6567 | MultinetworkPolicyTracker createMultinetworkPolicyTracker(Context c, Handler h, Runnable r) { |
| 6568 | return new MultinetworkPolicyTracker(c, h, r); |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | @VisibleForTesting |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6572 | public WakeupMessage makeWakeupMessage(Context c, Handler h, String s, int cmd, Object obj) { |
| 6573 | return new WakeupMessage(c, h, s, cmd, 0, 0, obj); |
| 6574 | } |
| 6575 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 6576 | @VisibleForTesting |
| 6577 | public boolean hasService(String name) { |
| 6578 | return ServiceManager.checkService(name) != null; |
| 6579 | } |
| 6580 | |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 6581 | @VisibleForTesting |
| 6582 | protected IpConnectivityMetrics.Logger metricsLogger() { |
| 6583 | return checkNotNull(LocalServices.getService(IpConnectivityMetrics.Logger.class), |
| 6584 | "no IpConnectivityMetrics service"); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | private void logNetworkEvent(NetworkAgentInfo nai, int evtype) { |
Hugo Benichi | edf5c24 | 2017-11-11 08:06:43 +0900 | [diff] [blame] | 6588 | int[] transports = nai.networkCapabilities.getTransportTypes(); |
| 6589 | mMetricsLog.log(nai.network.netId, transports, new NetworkEvent(evtype)); |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 6590 | } |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 6591 | |
| 6592 | private static boolean toBool(int encodedBoolean) { |
| 6593 | return encodedBoolean != 0; // Only 0 means false. |
| 6594 | } |
| 6595 | |
| 6596 | private static int encodeBool(boolean b) { |
| 6597 | return b ? 1 : 0; |
| 6598 | } |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 6599 | |
| 6600 | @Override |
| 6601 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 6602 | FileDescriptor err, String[] args, ShellCallback callback, |
| 6603 | ResultReceiver resultReceiver) { |
| 6604 | (new ShellCmd()).exec(this, in, out, err, args, callback, resultReceiver); |
| 6605 | } |
| 6606 | |
| 6607 | private class ShellCmd extends ShellCommand { |
| 6608 | |
| 6609 | @Override |
| 6610 | public int onCommand(String cmd) { |
| 6611 | if (cmd == null) { |
| 6612 | return handleDefaultCommands(cmd); |
| 6613 | } |
| 6614 | final PrintWriter pw = getOutPrintWriter(); |
| 6615 | try { |
| 6616 | switch (cmd) { |
| 6617 | case "airplane-mode": |
| 6618 | final String action = getNextArg(); |
| 6619 | if ("enable".equals(action)) { |
| 6620 | setAirplaneMode(true); |
| 6621 | return 0; |
| 6622 | } else if ("disable".equals(action)) { |
| 6623 | setAirplaneMode(false); |
| 6624 | return 0; |
| 6625 | } else if (action == null) { |
| 6626 | final ContentResolver cr = mContext.getContentResolver(); |
| 6627 | final int enabled = Settings.Global.getInt(cr, |
| 6628 | Settings.Global.AIRPLANE_MODE_ON); |
| 6629 | pw.println(enabled == 0 ? "disabled" : "enabled"); |
| 6630 | return 0; |
| 6631 | } else { |
| 6632 | onHelp(); |
| 6633 | return -1; |
| 6634 | } |
| 6635 | default: |
| 6636 | return handleDefaultCommands(cmd); |
| 6637 | } |
| 6638 | } catch (Exception e) { |
| 6639 | pw.println(e); |
| 6640 | } |
| 6641 | return -1; |
| 6642 | } |
| 6643 | |
| 6644 | @Override |
| 6645 | public void onHelp() { |
| 6646 | PrintWriter pw = getOutPrintWriter(); |
| 6647 | pw.println("Connectivity service commands:"); |
| 6648 | pw.println(" help"); |
| 6649 | pw.println(" Print this help text."); |
| 6650 | pw.println(" airplane-mode [enable|disable]"); |
| 6651 | pw.println(" Turn airplane mode on or off."); |
| 6652 | pw.println(" airplane-mode"); |
| 6653 | pw.println(" Get airplane mode."); |
| 6654 | } |
| 6655 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6656 | |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6657 | @GuardedBy("mVpns") |
| 6658 | private Vpn getVpnIfOwner() { |
| 6659 | final int uid = Binder.getCallingUid(); |
| 6660 | final int user = UserHandle.getUserId(uid); |
| 6661 | |
| 6662 | final Vpn vpn = mVpns.get(user); |
| 6663 | if (vpn == null) { |
| 6664 | return null; |
| 6665 | } else { |
| 6666 | final VpnInfo info = vpn.getVpnInfo(); |
| 6667 | return (info == null || info.ownerUid != uid) ? null : vpn; |
| 6668 | } |
| 6669 | } |
| 6670 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6671 | /** |
| 6672 | * Caller either needs to be an active VPN, or hold the NETWORK_STACK permission |
| 6673 | * for testing. |
| 6674 | */ |
| 6675 | private Vpn enforceActiveVpnOrNetworkStackPermission() { |
| 6676 | if (checkNetworkStackPermission()) { |
| 6677 | return null; |
| 6678 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6679 | synchronized (mVpns) { |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6680 | Vpn vpn = getVpnIfOwner(); |
| 6681 | if (vpn != null) { |
| 6682 | return vpn; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6683 | } |
| 6684 | } |
| 6685 | throw new SecurityException("App must either be an active VPN or have the NETWORK_STACK " |
| 6686 | + "permission"); |
| 6687 | } |
| 6688 | |
| 6689 | /** |
| 6690 | * @param connectionInfo the connection to resolve. |
| 6691 | * @return {@code uid} if the connection is found and the app has permission to observe it |
| 6692 | * (e.g., if it is associated with the calling VPN app's tunnel) or {@code INVALID_UID} if the |
| 6693 | * connection is not found. |
| 6694 | */ |
| 6695 | public int getConnectionOwnerUid(ConnectionInfo connectionInfo) { |
| 6696 | final Vpn vpn = enforceActiveVpnOrNetworkStackPermission(); |
| 6697 | if (connectionInfo.protocol != IPPROTO_TCP && connectionInfo.protocol != IPPROTO_UDP) { |
| 6698 | throw new IllegalArgumentException("Unsupported protocol " + connectionInfo.protocol); |
| 6699 | } |
| 6700 | |
| 6701 | final int uid = InetDiagMessage.getConnectionOwnerUid(connectionInfo.protocol, |
| 6702 | connectionInfo.local, connectionInfo.remote); |
| 6703 | |
| 6704 | /* Filter out Uids not associated with the VPN. */ |
| 6705 | if (vpn != null && !vpn.appliesToUid(uid)) { |
| 6706 | return INVALID_UID; |
| 6707 | } |
| 6708 | |
| 6709 | return uid; |
| 6710 | } |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6711 | |
| 6712 | @Override |
| 6713 | public boolean isCallerCurrentAlwaysOnVpnApp() { |
| 6714 | synchronized (mVpns) { |
| 6715 | Vpn vpn = getVpnIfOwner(); |
| 6716 | return vpn != null && vpn.getAlwaysOn(); |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | @Override |
| 6721 | public boolean isCallerCurrentAlwaysOnVpnLockdownApp() { |
| 6722 | synchronized (mVpns) { |
| 6723 | Vpn vpn = getVpnIfOwner(); |
| 6724 | return vpn != null && vpn.getLockdown(); |
| 6725 | } |
| 6726 | } |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 6727 | } |