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; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 28 | import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 29 | import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID; |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 30 | import static android.net.NetworkCapabilities.NET_CAPABILITY_CAPTIVE_PORTAL; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 31 | import static android.net.NetworkCapabilities.NET_CAPABILITY_FOREGROUND; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 32 | import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 33 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; |
| 34 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 35 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 36 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED; |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 37 | import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_VPN; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 38 | import static android.net.NetworkCapabilities.NET_CAPABILITY_PARTIAL_CONNECTIVITY; |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 39 | import static android.net.NetworkCapabilities.NET_CAPABILITY_VALIDATED; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 40 | import static android.net.NetworkCapabilities.TRANSPORT_VPN; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 41 | import static android.net.NetworkPolicyManager.RULE_NONE; |
| 42 | import static android.net.NetworkPolicyManager.uidRulesToString; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 43 | import static android.net.shared.NetworkMonitorUtils.isValidationRequired; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 44 | import static android.os.Process.INVALID_UID; |
| 45 | import static android.system.OsConstants.IPPROTO_TCP; |
| 46 | import static android.system.OsConstants.IPPROTO_UDP; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 47 | |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 48 | import static com.android.internal.util.Preconditions.checkNotNull; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 49 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 50 | import android.annotation.Nullable; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 51 | import android.app.BroadcastOptions; |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 52 | import android.app.NotificationManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 53 | import android.app.PendingIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 54 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | import android.content.ContentResolver; |
| 56 | import android.content.Context; |
| 57 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 58 | import android.content.IntentFilter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 59 | import android.content.res.Configuration; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 60 | import android.database.ContentObserver; |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 61 | import android.net.CaptivePortal; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 62 | import android.net.ConnectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 64 | import android.net.ICaptivePortal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.net.IConnectivityManager; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 66 | import android.net.IIpConnectivityMetrics; |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 67 | import android.net.INetd; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 68 | import android.net.INetdEventCallback; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 69 | import android.net.INetworkManagementEventObserver; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 70 | import android.net.INetworkMonitor; |
| 71 | import android.net.INetworkMonitorCallbacks; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 72 | import android.net.INetworkPolicyListener; |
| 73 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 74 | import android.net.INetworkStatsService; |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 75 | import android.net.ISocketKeepaliveCallback; |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 76 | import android.net.ITetheringEventCallback; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 77 | import android.net.InetAddresses; |
| 78 | import android.net.IpPrefix; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 79 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 80 | import android.net.LinkProperties.CompareResult; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 81 | import android.net.MatchAllNetworkSpecifier; |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 82 | import android.net.NattSocketKeepalive; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 83 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 84 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 85 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 86 | import android.net.NetworkConfig; |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 87 | import android.net.NetworkFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 89 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 90 | import android.net.NetworkMisc; |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 91 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 92 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 93 | import android.net.NetworkRequest; |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 94 | import android.net.NetworkSpecifier; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 95 | import android.net.NetworkStack; |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 96 | import android.net.NetworkStackClient; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 97 | import android.net.NetworkState; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 98 | import android.net.NetworkUtils; |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 99 | import android.net.NetworkWatchlistManager; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 100 | import android.net.PrivateDnsConfigParcel; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 101 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 102 | import android.net.RouteInfo; |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 103 | import android.net.SocketKeepalive; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 104 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 105 | import android.net.Uri; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 106 | import android.net.VpnService; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 107 | import android.net.metrics.IpConnectivityLog; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 108 | import android.net.metrics.NetworkEvent; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 109 | import android.net.netlink.InetDiagMessage; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 110 | import android.net.shared.NetworkMonitorUtils; |
| 111 | import android.net.shared.PrivateDnsConfig; |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 112 | import android.net.util.MultinetworkPolicyTracker; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 113 | import android.net.util.NetdService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | import android.os.Binder; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 115 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 116 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 118 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 119 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 120 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | import android.os.Looper; |
| 122 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 123 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 124 | import android.os.ParcelFileDescriptor; |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 125 | import android.os.Parcelable; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 126 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 127 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 128 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 129 | import android.os.ResultReceiver; |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 130 | import android.os.ServiceManager; |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 131 | import android.os.ServiceSpecificException; |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 132 | import android.os.ShellCallback; |
| 133 | import android.os.ShellCommand; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 134 | import android.os.SystemClock; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 135 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 136 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 138 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 139 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 140 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 141 | import android.text.TextUtils; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 142 | import android.util.ArraySet; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 143 | import android.util.LocalLog; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 144 | import android.util.Log; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 145 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 146 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 147 | import android.util.SparseArray; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 148 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 149 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 150 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 152 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 153 | import com.android.internal.annotations.GuardedBy; |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 154 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 155 | import com.android.internal.app.IBatteryStats; |
Remi NGUYEN VAN | de60221 | 2019-01-30 15:22:01 +0900 | [diff] [blame] | 156 | import com.android.internal.logging.MetricsLogger; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 157 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 158 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 159 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 160 | import com.android.internal.net.VpnProfile; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 161 | import com.android.internal.util.ArrayUtils; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 162 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 163 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 164 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 165 | import com.android.internal.util.MessageUtils; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 166 | import com.android.internal.util.WakeupMessage; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 167 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 168 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 169 | import com.android.server.connectivity.DataConnectionStats; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 170 | import com.android.server.connectivity.DnsManager; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 171 | import com.android.server.connectivity.DnsManager.PrivateDnsValidationUpdate; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 172 | import com.android.server.connectivity.IpConnectivityMetrics; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 173 | import com.android.server.connectivity.KeepaliveTracker; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 174 | import com.android.server.connectivity.LingerMonitor; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 175 | import com.android.server.connectivity.MockableSystemProperties; |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 176 | import com.android.server.connectivity.MultipathPolicyTracker; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 177 | import com.android.server.connectivity.NetworkAgentInfo; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 178 | import com.android.server.connectivity.NetworkDiagnostics; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 179 | import com.android.server.connectivity.NetworkNotificationManager; |
| 180 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 181 | import com.android.server.connectivity.PermissionMonitor; |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 182 | import com.android.server.connectivity.ProxyTracker; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 183 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 184 | import com.android.server.connectivity.Vpn; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 185 | import com.android.server.connectivity.tethering.TetheringDependencies; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 186 | import com.android.server.net.BaseNetdEventCallback; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 187 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 188 | import com.android.server.net.LockdownVpnTracker; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 189 | import com.android.server.net.NetworkPolicyManagerInternal; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 190 | import com.android.server.utils.PriorityDump; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 191 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 192 | import com.google.android.collect.Lists; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 193 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 194 | import org.xmlpull.v1.XmlPullParser; |
| 195 | import org.xmlpull.v1.XmlPullParserException; |
| 196 | |
| 197 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 199 | import java.io.FileNotFoundException; |
| 200 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 201 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | import java.io.PrintWriter; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 203 | import java.net.Inet4Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 204 | import java.net.InetAddress; |
| 205 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 206 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 207 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 208 | import java.util.Collection; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 209 | import java.util.Comparator; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 210 | import java.util.ConcurrentModificationException; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 211 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 212 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 213 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 214 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 215 | import java.util.Objects; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 216 | import java.util.Set; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 217 | import java.util.SortedSet; |
| 218 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | |
| 220 | /** |
| 221 | * @hide |
| 222 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 223 | public class ConnectivityService extends IConnectivityManager.Stub |
| 224 | implements PendingIntent.OnFinished { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 225 | private static final String TAG = ConnectivityService.class.getSimpleName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 227 | private static final String DIAG_ARG = "--diag"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 228 | public static final String SHORT_ARG = "--short"; |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 229 | private static final String TETHERING_ARG = "tethering"; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 230 | private static final String NETWORK_ARG = "networks"; |
| 231 | private static final String REQUEST_ARG = "requests"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 232 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 233 | private static final boolean DBG = true; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 234 | private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG); |
| 235 | 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] | 236 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 237 | private static final boolean LOGD_BLOCKED_NETWORKINFO = true; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 238 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 239 | // TODO: create better separation between radio types and network types |
| 240 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 241 | // how long to wait before switching back to a radio's default network |
| 242 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 243 | // system property that can override the above value |
| 244 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 245 | "android.telephony.apn-restore"; |
| 246 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 247 | // How long to wait before putting up a "This network doesn't have an Internet connection, |
| 248 | // connect anyway?" dialog after the user selects a network that doesn't validate. |
| 249 | private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000; |
| 250 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 251 | // How long to dismiss network notification. |
| 252 | private static final int TIMEOUT_NOTIFICATION_DELAY_MS = 20 * 1000; |
| 253 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 254 | // Default to 30s linger time-out. Modifiable only for testing. |
| 255 | private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger"; |
| 256 | private static final int DEFAULT_LINGER_DELAY_MS = 30_000; |
| 257 | @VisibleForTesting |
| 258 | protected int mLingerDelayMs; // Can't be final, or test subclass constructors can't change it. |
| 259 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 260 | // How long to delay to removal of a pending intent based request. |
| 261 | // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS |
| 262 | private final int mReleasePendingIntentDelayMs; |
| 263 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 264 | private MockableSystemProperties mSystemProperties; |
| 265 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 266 | private Tethering mTethering; |
| 267 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 268 | private final PermissionMonitor mPermissionMonitor; |
| 269 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 270 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 271 | |
Chalard Jean | fb0c87e | 2018-04-18 19:18:58 +0900 | [diff] [blame] | 272 | @VisibleForTesting |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 273 | @GuardedBy("mVpns") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 274 | protected final SparseArray<Vpn> mVpns = new SparseArray<>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 275 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 276 | // TODO: investigate if mLockdownEnabled can be removed and replaced everywhere by |
| 277 | // a direct call to LockdownVpnTracker.isEnabled(). |
| 278 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 279 | private boolean mLockdownEnabled; |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 280 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 281 | private LockdownVpnTracker mLockdownTracker; |
| 282 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 283 | /** |
| 284 | * Stale copy of uid rules provided by NPMS. As long as they are accessed only in internal |
| 285 | * handler thread, they don't need a lock. |
| 286 | */ |
| 287 | private SparseIntArray mUidRules = new SparseIntArray(); |
| 288 | /** Flag indicating if background data is restricted. */ |
| 289 | private boolean mRestrictBackground; |
| 290 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 291 | final private Context mContext; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 292 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 293 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 295 | private INetworkManagementService mNMS; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 296 | @VisibleForTesting |
| 297 | protected INetd mNetd; |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 298 | private INetworkStatsService mStatsService; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 299 | private INetworkPolicyManager mPolicyManager; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 300 | private NetworkPolicyManagerInternal mPolicyManagerInternal; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 301 | |
Benedict Wong | a341fbc | 2018-11-09 14:45:34 -0800 | [diff] [blame] | 302 | /** |
| 303 | * TestNetworkService (lazily) created upon first usage. Locked to prevent creation of multiple |
| 304 | * instances. |
| 305 | */ |
| 306 | @GuardedBy("mTNSLock") |
| 307 | private TestNetworkService mTNS; |
| 308 | |
| 309 | private final Object mTNSLock = new Object(); |
| 310 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 311 | private String mCurrentTcpBufferSizes; |
| 312 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 313 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 314 | new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class, |
| 315 | NetworkAgentInfo.class }); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 316 | |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 317 | private enum ReapUnvalidatedNetworks { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 318 | // Tear down networks that have no chance (e.g. even if validated) of becoming |
| 319 | // the highest scoring network satisfying a NetworkRequest. This should be passed when |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 320 | // all networks have been rematched against all NetworkRequests. |
| 321 | REAP, |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 322 | // Don't reap networks. This should be passed when some networks have not yet been |
| 323 | // rematched against all NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 324 | DONT_REAP |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 325 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 326 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 327 | private enum UnneededFor { |
| 328 | LINGER, // Determine whether this network is unneeded and should be lingered. |
| 329 | TEARDOWN, // Determine whether this network is unneeded and should be torn down. |
| 330 | } |
| 331 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 332 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 333 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 334 | * from one net to another. Clear happens when we get a new |
| 335 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 336 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 337 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 338 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 339 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 340 | /** |
| 341 | * used internally to reload global proxy settings |
| 342 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 343 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 344 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 345 | /** |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 346 | * PAC manager has received new port. |
| 347 | */ |
| 348 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 349 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 350 | /** |
| 351 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 352 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 353 | */ |
| 354 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 355 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 356 | /** |
| 357 | * used internally when registering NetworkAgents |
| 358 | * obj = Messenger |
| 359 | */ |
| 360 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 361 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 362 | /** |
| 363 | * used to add a network request |
| 364 | * includes a NetworkRequestInfo |
| 365 | */ |
| 366 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 367 | |
| 368 | /** |
| 369 | * 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] | 370 | * 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] | 371 | * cancel it. |
| 372 | * includes a NetworkRequestInfo |
| 373 | */ |
| 374 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 375 | |
| 376 | /** |
| 377 | * used to add a network listener - no request |
| 378 | * includes a NetworkRequestInfo |
| 379 | */ |
| 380 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 381 | |
| 382 | /** |
| 383 | * 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] | 384 | * arg1 = UID of caller |
| 385 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 386 | */ |
| 387 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 388 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 389 | /** |
| 390 | * used internally when registering NetworkFactories |
| 391 | * obj = Messenger |
| 392 | */ |
| 393 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 394 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 395 | /** |
| 396 | * used internally to expire a wakelock when transitioning |
| 397 | * from one net to another. Expire happens when we fail to find |
| 398 | * a new network (typically after 1 minute) - |
| 399 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 400 | * a replacement network. |
| 401 | */ |
| 402 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 403 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Used internally to indicate the system is ready. |
| 406 | */ |
| 407 | private static final int EVENT_SYSTEM_READY = 25; |
| 408 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 409 | /** |
| 410 | * used to add a network request with a pending intent |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 411 | * obj = NetworkRequestInfo |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 412 | */ |
| 413 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 414 | |
| 415 | /** |
| 416 | * used to remove a pending intent and its associated network request. |
| 417 | * arg1 = UID of caller |
| 418 | * obj = PendingIntent |
| 419 | */ |
| 420 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 421 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 422 | /** |
| 423 | * used to specify whether a network should be used even if unvalidated. |
| 424 | * arg1 = whether to accept the network if it's unvalidated (1 or 0) |
| 425 | * arg2 = whether to remember this choice in the future (1 or 0) |
| 426 | * obj = network |
| 427 | */ |
| 428 | private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28; |
| 429 | |
| 430 | /** |
| 431 | * used to ask the user to confirm a connection to an unvalidated network. |
| 432 | * obj = network |
| 433 | */ |
| 434 | private static final int EVENT_PROMPT_UNVALIDATED = 29; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 435 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 436 | /** |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 437 | * used internally to (re)configure always-on networks. |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 438 | */ |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 439 | private static final int EVENT_CONFIGURE_ALWAYS_ON_NETWORKS = 30; |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 440 | |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 441 | /** |
| 442 | * used to add a network listener with a pending intent |
| 443 | * obj = NetworkRequestInfo |
| 444 | */ |
| 445 | private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31; |
| 446 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 447 | /** |
| 448 | * used to specify whether a network should not be penalized when it becomes unvalidated. |
| 449 | */ |
| 450 | private static final int EVENT_SET_AVOID_UNVALIDATED = 35; |
| 451 | |
| 452 | /** |
| 453 | * used to trigger revalidation of a network. |
| 454 | */ |
| 455 | private static final int EVENT_REVALIDATE_NETWORK = 36; |
| 456 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 457 | // Handle changes in Private DNS settings. |
| 458 | private static final int EVENT_PRIVATE_DNS_SETTINGS_CHANGED = 37; |
| 459 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 460 | // Handle private DNS validation status updates. |
| 461 | private static final int EVENT_PRIVATE_DNS_VALIDATION_UPDATE = 38; |
| 462 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 463 | /** |
| 464 | * Used to handle onUidRulesChanged event from NetworkPolicyManagerService. |
| 465 | */ |
| 466 | private static final int EVENT_UID_RULES_CHANGED = 39; |
| 467 | |
| 468 | /** |
| 469 | * Used to handle onRestrictBackgroundChanged event from NetworkPolicyManagerService. |
| 470 | */ |
| 471 | private static final int EVENT_DATA_SAVER_CHANGED = 40; |
| 472 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 473 | /** |
| 474 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the network has |
| 475 | * been tested. |
| 476 | * obj = String representing URL that Internet probe was redirect to, if it was redirected. |
| 477 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
| 478 | * arg2 = NetID. |
| 479 | */ |
| 480 | public static final int EVENT_NETWORK_TESTED = 41; |
| 481 | |
| 482 | /** |
| 483 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the private DNS |
| 484 | * config was resolved. |
| 485 | * obj = PrivateDnsConfig |
| 486 | * arg2 = netid |
| 487 | */ |
| 488 | public static final int EVENT_PRIVATE_DNS_CONFIG_RESOLVED = 42; |
| 489 | |
| 490 | /** |
| 491 | * Request ConnectivityService display provisioning notification. |
| 492 | * arg1 = Whether to make the notification visible. |
| 493 | * arg2 = NetID. |
| 494 | * obj = Intent to be launched when notification selected by user, null if !arg1. |
| 495 | */ |
| 496 | public static final int EVENT_PROVISIONING_NOTIFICATION = 43; |
| 497 | |
| 498 | /** |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 499 | * This event can handle dismissing notification by given network id. |
| 500 | */ |
| 501 | public static final int EVENT_TIMEOUT_NOTIFICATION = 44; |
| 502 | |
| 503 | /** |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 504 | * Used to specify whether a network should be used even if connectivity is partial. |
| 505 | * arg1 = whether to accept the network if its connectivity is partial (1 for true or 0 for |
| 506 | * false) |
| 507 | * arg2 = whether to remember this choice in the future (1 for true or 0 for false) |
| 508 | * obj = network |
| 509 | */ |
| 510 | private static final int EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY = 45; |
| 511 | |
| 512 | /** |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 513 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 514 | * should be shown. |
| 515 | */ |
| 516 | public static final int PROVISIONING_NOTIFICATION_SHOW = 1; |
| 517 | |
| 518 | /** |
| 519 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 520 | * should be hidden. |
| 521 | */ |
| 522 | public static final int PROVISIONING_NOTIFICATION_HIDE = 0; |
| 523 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 524 | private static String eventName(int what) { |
| 525 | return sMagicDecoderRing.get(what, Integer.toString(what)); |
| 526 | } |
| 527 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 528 | /** Handler thread used for both of the handlers below. */ |
| 529 | @VisibleForTesting |
| 530 | protected final HandlerThread mHandlerThread; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 531 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 532 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 533 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 534 | final private NetworkStateTrackerHandler mTrackerHandler; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 535 | private final DnsManager mDnsManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 536 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 537 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 538 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 539 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 540 | private PowerManager.WakeLock mNetTransitionWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 541 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 542 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 543 | |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 544 | // A helper object to track the current default HTTP proxy. ConnectivityService needs to tell |
| 545 | // the world when it changes. |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 546 | @VisibleForTesting |
| 547 | protected final ProxyTracker mProxyTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 548 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 549 | final private SettingsObserver mSettingsObserver; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 550 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 551 | private UserManager mUserManager; |
| 552 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 553 | private NetworkConfig[] mNetConfigs; |
| 554 | private int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 555 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 556 | // 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] | 557 | private List mProtectedNetworks; |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 558 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 559 | private TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 560 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 561 | private KeepaliveTracker mKeepaliveTracker; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 562 | private NetworkNotificationManager mNotifier; |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 563 | private LingerMonitor mLingerMonitor; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 564 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 565 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 566 | private static final int MIN_NET_ID = 100; // some reserved marks |
| 567 | 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] | 568 | private int mNextNetId = MIN_NET_ID; |
| 569 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 570 | // sequence number of NetworkRequests |
| 571 | private int mNextNetworkRequestId = 1; |
| 572 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 573 | // NetworkRequest activity String log entries. |
| 574 | private static final int MAX_NETWORK_REQUEST_LOGS = 20; |
| 575 | private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS); |
| 576 | |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 577 | // NetworkInfo blocked and unblocked String log entries |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 578 | private static final int MAX_NETWORK_INFO_LOGS = 40; |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 579 | private final LocalLog mNetworkInfoBlockingLogs = new LocalLog(MAX_NETWORK_INFO_LOGS); |
| 580 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 581 | private static final int MAX_WAKELOCK_LOGS = 20; |
| 582 | private final LocalLog mWakelockLogs = new LocalLog(MAX_WAKELOCK_LOGS); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 583 | private int mTotalWakelockAcquisitions = 0; |
| 584 | private int mTotalWakelockReleases = 0; |
| 585 | private long mTotalWakelockDurationMs = 0; |
| 586 | private long mMaxWakelockDurationMs = 0; |
| 587 | private long mLastWakeLockAcquireTimestamp = 0; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 588 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 589 | private final IpConnectivityLog mMetricsLog; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 590 | |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 591 | @GuardedBy("mBandwidthRequests") |
| 592 | private final SparseArray<Integer> mBandwidthRequests = new SparseArray(10); |
| 593 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 594 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 595 | final MultinetworkPolicyTracker mMultinetworkPolicyTracker; |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 596 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 597 | @VisibleForTesting |
| 598 | final MultipathPolicyTracker mMultipathPolicyTracker; |
| 599 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 600 | /** |
| 601 | * Implements support for the legacy "one network per network type" model. |
| 602 | * |
| 603 | * We used to have a static array of NetworkStateTrackers, one for each |
| 604 | * network type, but that doesn't work any more now that we can have, |
| 605 | * for example, more that one wifi network. This class stores all the |
| 606 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 607 | * API will only see the first one. |
| 608 | * |
| 609 | * It serves two main purposes: |
| 610 | * |
| 611 | * 1. Provide information about "the network for a given type" (since this |
| 612 | * API only supports one). |
| 613 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 614 | * the first network for a given type changes, or if the default network |
| 615 | * changes. |
| 616 | */ |
| 617 | private class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 618 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 619 | private static final boolean DBG = true; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 620 | private static final boolean VDBG = false; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 621 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 622 | /** |
| 623 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 624 | * Each list holds references to all NetworkAgentInfos that are used to |
| 625 | * satisfy requests for that network type. |
| 626 | * |
| 627 | * This array is built out at startup such that an unsupported network |
| 628 | * doesn't get an ArrayList instance, making this a tristate: |
| 629 | * unsupported, supported but not active and active. |
| 630 | * |
| 631 | * The actual lists are populated when we scan the network types that |
| 632 | * are supported on this device. |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 633 | * |
| 634 | * Threading model: |
| 635 | * - addSupportedType() is only called in the constructor |
| 636 | * - add(), update(), remove() are only called from the ConnectivityService handler thread. |
| 637 | * They are therefore not thread-safe with respect to each other. |
| 638 | * - getNetworkForType() can be called at any time on binder threads. It is synchronized |
| 639 | * on mTypeLists to be thread-safe with respect to a concurrent remove call. |
| 640 | * - dump is thread-safe with respect to concurrent add and remove calls. |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 641 | */ |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 642 | private final ArrayList<NetworkAgentInfo> mTypeLists[]; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 643 | |
| 644 | public LegacyTypeTracker() { |
| 645 | mTypeLists = (ArrayList<NetworkAgentInfo>[]) |
| 646 | new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
| 647 | } |
| 648 | |
| 649 | public void addSupportedType(int type) { |
| 650 | if (mTypeLists[type] != null) { |
| 651 | throw new IllegalStateException( |
| 652 | "legacy list for type " + type + "already initialized"); |
| 653 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 654 | mTypeLists[type] = new ArrayList<>(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 657 | public boolean isTypeSupported(int type) { |
| 658 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 659 | } |
| 660 | |
| 661 | public NetworkAgentInfo getNetworkForType(int type) { |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 662 | synchronized (mTypeLists) { |
| 663 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 664 | return mTypeLists[type].get(0); |
| 665 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 666 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 667 | return null; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 670 | private void maybeLogBroadcast(NetworkAgentInfo nai, DetailedState state, int type, |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 671 | boolean isDefaultNetwork) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 672 | if (DBG) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 673 | log("Sending " + state + |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 674 | " broadcast for type " + type + " " + nai.name() + |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 675 | " isDefaultNetwork=" + isDefaultNetwork); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 676 | } |
| 677 | } |
| 678 | |
| 679 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 680 | public void add(int type, NetworkAgentInfo nai) { |
| 681 | if (!isTypeSupported(type)) { |
| 682 | return; // Invalid network type. |
| 683 | } |
| 684 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 685 | |
| 686 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 687 | if (list.contains(nai)) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 688 | return; |
| 689 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 690 | synchronized (mTypeLists) { |
| 691 | list.add(nai); |
| 692 | } |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 693 | |
| 694 | // 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] | 695 | final boolean isDefaultNetwork = isDefaultNetwork(nai); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 696 | if ((list.size() == 1) || isDefaultNetwork) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 697 | maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork); |
| 698 | sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 699 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 702 | /** Removes the given network from the specified legacy type list. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 703 | public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 704 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 705 | if (list == null || list.isEmpty()) { |
| 706 | return; |
| 707 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 708 | final boolean wasFirstNetwork = list.get(0).equals(nai); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 709 | |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 710 | synchronized (mTypeLists) { |
| 711 | if (!list.remove(nai)) { |
| 712 | return; |
| 713 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 714 | } |
| 715 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 716 | final DetailedState state = DetailedState.DISCONNECTED; |
| 717 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 718 | if (wasFirstNetwork || wasDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 719 | maybeLogBroadcast(nai, state, type, wasDefault); |
| 720 | sendLegacyNetworkBroadcast(nai, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | if (!list.isEmpty() && wasFirstNetwork) { |
| 724 | if (DBG) log("Other network available for type " + type + |
| 725 | ", sending connected broadcast"); |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 726 | final NetworkAgentInfo replacement = list.get(0); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 727 | maybeLogBroadcast(replacement, state, type, isDefaultNetwork(replacement)); |
| 728 | sendLegacyNetworkBroadcast(replacement, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 729 | } |
| 730 | } |
| 731 | |
| 732 | /** Removes the given network from all legacy type lists. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 733 | public void remove(NetworkAgentInfo nai, boolean wasDefault) { |
| 734 | if (VDBG) log("Removing agent " + nai + " wasDefault=" + wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 735 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 736 | remove(type, nai, wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 737 | } |
| 738 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 739 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 740 | // send out another legacy broadcast - currently only used for suspend/unsuspend |
| 741 | // toggle |
| 742 | public void update(NetworkAgentInfo nai) { |
| 743 | final boolean isDefault = isDefaultNetwork(nai); |
| 744 | final DetailedState state = nai.networkInfo.getDetailedState(); |
| 745 | for (int type = 0; type < mTypeLists.length; type++) { |
| 746 | final ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 747 | final boolean contains = (list != null && list.contains(nai)); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 748 | final boolean isFirst = contains && (nai == list.get(0)); |
| 749 | if (isFirst || contains && isDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 750 | maybeLogBroadcast(nai, state, type, isDefault); |
| 751 | sendLegacyNetworkBroadcast(nai, state, type); |
| 752 | } |
| 753 | } |
| 754 | } |
| 755 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 756 | private String naiToString(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 757 | String name = nai.name(); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 758 | String state = (nai.networkInfo != null) ? |
| 759 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 760 | "???/???"; |
| 761 | return name + " " + state; |
| 762 | } |
| 763 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 764 | public void dump(IndentingPrintWriter pw) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 765 | pw.println("mLegacyTypeTracker:"); |
| 766 | pw.increaseIndent(); |
| 767 | pw.print("Supported types:"); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 768 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 769 | if (mTypeLists[type] != null) pw.print(" " + type); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 770 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 771 | pw.println(); |
| 772 | pw.println("Current state:"); |
| 773 | pw.increaseIndent(); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 774 | synchronized (mTypeLists) { |
| 775 | for (int type = 0; type < mTypeLists.length; type++) { |
| 776 | if (mTypeLists[type] == null || mTypeLists[type].isEmpty()) continue; |
| 777 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
| 778 | pw.println(type + " " + naiToString(nai)); |
| 779 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 780 | } |
| 781 | } |
| 782 | pw.decreaseIndent(); |
| 783 | pw.decreaseIndent(); |
| 784 | pw.println(); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 785 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 786 | } |
| 787 | private LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(); |
| 788 | |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 789 | /** |
| 790 | * Helper class which parses out priority arguments and dumps sections according to their |
| 791 | * priority. If priority arguments are omitted, function calls the legacy dump command. |
| 792 | */ |
| 793 | private final PriorityDump.PriorityDumper mPriorityDumper = new PriorityDump.PriorityDumper() { |
| 794 | @Override |
| 795 | public void dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 796 | doDump(fd, pw, new String[] {DIAG_ARG}, asProto); |
| 797 | doDump(fd, pw, new String[] {SHORT_ARG}, asProto); |
| 798 | } |
| 799 | |
| 800 | @Override |
| 801 | public void dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 802 | doDump(fd, pw, args, asProto); |
| 803 | } |
| 804 | |
| 805 | @Override |
| 806 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 807 | doDump(fd, pw, args, asProto); |
| 808 | } |
| 809 | }; |
| 810 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 811 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 812 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 813 | this(context, netManager, statsService, policyManager, new IpConnectivityLog()); |
| 814 | } |
| 815 | |
| 816 | @VisibleForTesting |
| 817 | protected ConnectivityService(Context context, INetworkManagementService netManager, |
| 818 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
| 819 | IpConnectivityLog logger) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 820 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 821 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 822 | mSystemProperties = getSystemProperties(); |
| 823 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 824 | mMetricsLog = logger; |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 825 | mDefaultRequest = createDefaultInternetRequestForTransport(-1, NetworkRequest.Type.REQUEST); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 826 | NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, new Binder()); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 827 | mNetworkRequests.put(mDefaultRequest, defaultNRI); |
| 828 | mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 829 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 830 | mDefaultMobileDataRequest = createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 831 | NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 832 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 833 | // The default WiFi request is a background request so that apps using WiFi are |
| 834 | // migrated to a better network (typically ethernet) when one comes up, instead |
| 835 | // of staying on WiFi forever. |
| 836 | mDefaultWifiRequest = createDefaultInternetRequestForTransport( |
| 837 | NetworkCapabilities.TRANSPORT_WIFI, NetworkRequest.Type.BACKGROUND_REQUEST); |
| 838 | |
Hugo Benichi | ad4db4e | 2016-10-17 15:54:51 +0900 | [diff] [blame] | 839 | mHandlerThread = new HandlerThread("ConnectivityServiceThread"); |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 840 | mHandlerThread.start(); |
| 841 | mHandler = new InternalHandler(mHandlerThread.getLooper()); |
| 842 | mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 843 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 844 | mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(), |
| 845 | Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000); |
| 846 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 847 | mLingerDelayMs = mSystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 848 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 849 | mContext = checkNotNull(context, "missing Context"); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 850 | mNMS = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 851 | mStatsService = checkNotNull(statsService, "missing INetworkStatsService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 852 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 853 | mPolicyManagerInternal = checkNotNull( |
| 854 | LocalServices.getService(NetworkPolicyManagerInternal.class), |
| 855 | "missing NetworkPolicyManagerInternal"); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 856 | mProxyTracker = makeProxyTracker(); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 857 | |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 858 | mNetd = NetdService.getInstance(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 859 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 860 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 861 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 862 | // To ensure uid rules are synchronized with Network Policy, register for |
| 863 | // NetworkPolicyManagerService events must happen prior to NetworkPolicyManagerService |
| 864 | // reading existing policy from disk. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 865 | try { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 866 | mPolicyManager.registerListener(mPolicyListener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 867 | } catch (RemoteException e) { |
| 868 | // ouch, no rules updates means some processes may never get network |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 869 | loge("unable to register INetworkPolicyListener" + e); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 873 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 874 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 875 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 876 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 877 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 878 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 879 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 880 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 881 | // 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] | 882 | boolean wifiOnly = mSystemProperties.getBoolean("ro.radio.noril", false); |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 883 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 884 | String[] naStrings = context.getResources().getStringArray( |
| 885 | com.android.internal.R.array.networkAttributes); |
| 886 | for (String naString : naStrings) { |
| 887 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 888 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 889 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 890 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 891 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 892 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 893 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 894 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 895 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 896 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 897 | n.type); |
| 898 | continue; |
| 899 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 900 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 901 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 902 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 903 | continue; |
| 904 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 905 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 906 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 907 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 908 | mNetworksDefined++; |
| 909 | } catch(Exception e) { |
| 910 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 911 | } |
| 912 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 913 | |
| 914 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 915 | if (mNetConfigs[TYPE_VPN] == null) { |
| 916 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 917 | // don't need to add TYPE_VPN to mNetConfigs. |
| 918 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 919 | mNetworksDefined++; // used only in the log() statement below. |
| 920 | } |
| 921 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 922 | // Do the same for Ethernet, since it's often not specified in the configs, although many |
| 923 | // devices can use it via USB host adapters. |
| 924 | if (mNetConfigs[TYPE_ETHERNET] == null && hasService(Context.ETHERNET_SERVICE)) { |
| 925 | mLegacyTypeTracker.addSupportedType(TYPE_ETHERNET); |
| 926 | mNetworksDefined++; |
| 927 | } |
| 928 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 929 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 930 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 931 | mProtectedNetworks = new ArrayList<Integer>(); |
| 932 | int[] protectedNetworks = context.getResources().getIntArray( |
| 933 | com.android.internal.R.array.config_protectedNetworks); |
| 934 | for (int p : protectedNetworks) { |
| 935 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 936 | mProtectedNetworks.add(p); |
| 937 | } else { |
| 938 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 939 | } |
| 940 | } |
| 941 | |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 942 | mTethering = makeTethering(); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 943 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 944 | mPermissionMonitor = new PermissionMonitor(mContext, mNMS); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 945 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 946 | // Set up the listener for user state for creating user VPNs. |
| 947 | // Should run on mHandler to avoid any races. |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 948 | IntentFilter intentFilter = new IntentFilter(); |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 949 | intentFilter.addAction(Intent.ACTION_USER_STARTED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 950 | intentFilter.addAction(Intent.ACTION_USER_STOPPED); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 951 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 952 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 953 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 954 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 955 | mIntentReceiver, |
| 956 | UserHandle.ALL, |
| 957 | intentFilter, |
| 958 | null /* broadcastPermission */, |
| 959 | mHandler); |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 960 | mContext.registerReceiverAsUser(mUserPresentReceiver, UserHandle.SYSTEM, |
| 961 | new IntentFilter(Intent.ACTION_USER_PRESENT), null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 962 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 963 | // Listen to package add and removal events for all users. |
| 964 | intentFilter = new IntentFilter(); |
| 965 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 966 | intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 967 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 968 | intentFilter.addDataScheme("package"); |
| 969 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 970 | mIntentReceiver, |
| 971 | UserHandle.ALL, |
| 972 | intentFilter, |
| 973 | null /* broadcastPermission */, |
| 974 | mHandler); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 975 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 976 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 977 | mNMS.registerObserver(mTethering); |
| 978 | mNMS.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 979 | } catch (RemoteException e) { |
| 980 | loge("Error registering observer :" + e); |
| 981 | } |
| 982 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 983 | mSettingsObserver = new SettingsObserver(mContext, mHandler); |
| 984 | registerSettingsCallbacks(); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 985 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 986 | final DataConnectionStats dataConnectionStats = new DataConnectionStats(mContext); |
| 987 | dataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 988 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 989 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 990 | |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 991 | mKeepaliveTracker = new KeepaliveTracker(mContext, mHandler); |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 992 | mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager, |
| 993 | mContext.getSystemService(NotificationManager.class)); |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 994 | |
| 995 | final int dailyLimit = Settings.Global.getInt(mContext.getContentResolver(), |
| 996 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_DAILY_LIMIT, |
| 997 | LingerMonitor.DEFAULT_NOTIFICATION_DAILY_LIMIT); |
| 998 | final long rateLimit = Settings.Global.getLong(mContext.getContentResolver(), |
| 999 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_RATE_LIMIT_MILLIS, |
| 1000 | LingerMonitor.DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS); |
| 1001 | mLingerMonitor = new LingerMonitor(mContext, mNotifier, dailyLimit, rateLimit); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 1002 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 1003 | mMultinetworkPolicyTracker = createMultinetworkPolicyTracker( |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 1004 | mContext, mHandler, () -> rematchForAvoidBadWifiUpdate()); |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 1005 | mMultinetworkPolicyTracker.start(); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 1006 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 1007 | mMultipathPolicyTracker = new MultipathPolicyTracker(mContext, mHandler); |
| 1008 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1009 | mDnsManager = new DnsManager(mContext, mNMS, mSystemProperties); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1010 | registerPrivateDnsSettingsCallbacks(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1012 | |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 1013 | @VisibleForTesting |
| 1014 | protected Tethering makeTethering() { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1015 | // TODO: Move other elements into @Overridden getters. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1016 | final TetheringDependencies deps = new TetheringDependencies() { |
| 1017 | @Override |
| 1018 | public boolean isTetheringSupported() { |
| 1019 | return ConnectivityService.this.isTetheringSupported(); |
| 1020 | } |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1021 | @Override |
| 1022 | public NetworkRequest getDefaultNetworkRequest() { |
| 1023 | return mDefaultRequest; |
| 1024 | } |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1025 | }; |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1026 | return new Tethering(mContext, mNMS, mStatsService, mPolicyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1027 | IoThread.get().getLooper(), new MockableSystemProperties(), |
| 1028 | deps); |
| 1029 | } |
| 1030 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 1031 | @VisibleForTesting |
| 1032 | protected ProxyTracker makeProxyTracker() { |
| 1033 | return new ProxyTracker(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
| 1034 | } |
| 1035 | |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1036 | private static NetworkCapabilities createDefaultNetworkCapabilitiesForUid(int uid) { |
| 1037 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1038 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1039 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
| 1040 | netCap.removeCapability(NET_CAPABILITY_NOT_VPN); |
| 1041 | netCap.setSingleUid(uid); |
| 1042 | return netCap; |
| 1043 | } |
| 1044 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 1045 | private NetworkRequest createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1046 | int transportType, NetworkRequest.Type type) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1047 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 1048 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1049 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1050 | if (transportType > -1) { |
| 1051 | netCap.addTransportType(transportType); |
| 1052 | } |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1053 | return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId(), type); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1054 | } |
| 1055 | |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1056 | // Used only for testing. |
| 1057 | // TODO: Delete this and either: |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1058 | // 1. Give FakeSettingsProvider the ability to send settings change notifications (requires |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1059 | // changing ContentResolver to make registerContentObserver non-final). |
| 1060 | // 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it |
| 1061 | // by subclassing SettingsObserver. |
| 1062 | @VisibleForTesting |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1063 | void updateAlwaysOnNetworks() { |
| 1064 | mHandler.sendEmptyMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1065 | } |
| 1066 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1067 | // See FakeSettingsProvider comment above. |
| 1068 | @VisibleForTesting |
| 1069 | void updatePrivateDnsSettings() { |
| 1070 | mHandler.sendEmptyMessage(EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
| 1071 | } |
| 1072 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1073 | private void handleAlwaysOnNetworkRequest( |
| 1074 | NetworkRequest networkRequest, String settingName, boolean defaultValue) { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 1075 | final boolean enable = toBool(Settings.Global.getInt( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1076 | mContext.getContentResolver(), settingName, encodeBool(defaultValue))); |
| 1077 | final boolean isEnabled = (mNetworkRequests.get(networkRequest) != null); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1078 | if (enable == isEnabled) { |
| 1079 | return; // Nothing to do. |
| 1080 | } |
| 1081 | |
| 1082 | if (enable) { |
| 1083 | handleRegisterNetworkRequest(new NetworkRequestInfo( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1084 | null, networkRequest, new Binder())); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1085 | } else { |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 1086 | handleReleaseNetworkRequest(networkRequest, Process.SYSTEM_UID, |
| 1087 | /* callOnUnavailable */ false); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1088 | } |
| 1089 | } |
| 1090 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1091 | private void handleConfigureAlwaysOnNetworks() { |
| 1092 | handleAlwaysOnNetworkRequest( |
| 1093 | mDefaultMobileDataRequest,Settings.Global.MOBILE_DATA_ALWAYS_ON, true); |
| 1094 | handleAlwaysOnNetworkRequest(mDefaultWifiRequest, Settings.Global.WIFI_ALWAYS_REQUESTED, |
| 1095 | false); |
| 1096 | } |
| 1097 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1098 | private void registerSettingsCallbacks() { |
| 1099 | // Watch for global HTTP proxy changes. |
| 1100 | mSettingsObserver.observe( |
| 1101 | Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), |
| 1102 | EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 1103 | |
| 1104 | // Watch for whether or not to keep mobile data always on. |
| 1105 | mSettingsObserver.observe( |
| 1106 | Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1107 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
| 1108 | |
| 1109 | // Watch for whether or not to keep wifi always on. |
| 1110 | mSettingsObserver.observe( |
| 1111 | Settings.Global.getUriFor(Settings.Global.WIFI_ALWAYS_REQUESTED), |
| 1112 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1113 | } |
| 1114 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1115 | private void registerPrivateDnsSettingsCallbacks() { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1116 | for (Uri uri : DnsManager.getPrivateDnsSettingsUris()) { |
| 1117 | mSettingsObserver.observe(uri, EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 1121 | private synchronized int nextNetworkRequestId() { |
| 1122 | return mNextNetworkRequestId++; |
| 1123 | } |
| 1124 | |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 1125 | @VisibleForTesting |
| 1126 | protected int reserveNetId() { |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1127 | synchronized (mNetworkForNetId) { |
| 1128 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 1129 | int netId = mNextNetId; |
| 1130 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 1131 | // Make sure NetID unused. http://b/16815182 |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1132 | if (!mNetIdInUse.get(netId)) { |
| 1133 | mNetIdInUse.put(netId, true); |
| 1134 | return netId; |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1141 | private NetworkState getFilteredNetworkState(int networkType, int uid) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1142 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1143 | final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1144 | final NetworkState state; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1145 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1146 | state = nai.getNetworkState(); |
| 1147 | state.networkInfo.setType(networkType); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1148 | } else { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1149 | final NetworkInfo info = new NetworkInfo(networkType, 0, |
| 1150 | getNetworkTypeName(networkType), ""); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1151 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 1152 | info.setIsAvailable(true); |
Jeff Sharkey | 6226216 | 2017-12-04 15:52:01 -0700 | [diff] [blame] | 1153 | final NetworkCapabilities capabilities = new NetworkCapabilities(); |
| 1154 | capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, |
| 1155 | !info.isRoaming()); |
| 1156 | state = new NetworkState(info, new LinkProperties(), capabilities, |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1157 | null, null, null); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1158 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1159 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1160 | return state; |
| 1161 | } else { |
| 1162 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1163 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1164 | } |
| 1165 | |
junyulai | 4a192e2 | 2018-06-13 15:00:37 +0800 | [diff] [blame] | 1166 | @VisibleForTesting |
| 1167 | protected NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1168 | if (network == null) { |
| 1169 | return null; |
| 1170 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1171 | return getNetworkAgentInfoForNetId(network.netId); |
| 1172 | } |
| 1173 | |
| 1174 | private NetworkAgentInfo getNetworkAgentInfoForNetId(int netId) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1175 | synchronized (mNetworkForNetId) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1176 | return mNetworkForNetId.get(netId); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1177 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1178 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1179 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1180 | private Network[] getVpnUnderlyingNetworks(int uid) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1181 | synchronized (mVpns) { |
| 1182 | if (!mLockdownEnabled) { |
| 1183 | int user = UserHandle.getUserId(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1184 | Vpn vpn = mVpns.get(user); |
| 1185 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 1186 | return vpn.getUnderlyingNetworks(); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | return null; |
| 1191 | } |
| 1192 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1193 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 1194 | NetworkAgentInfo nai = getDefaultNetwork(); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1195 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1196 | final Network[] networks = getVpnUnderlyingNetworks(uid); |
| 1197 | if (networks != null) { |
| 1198 | // getUnderlyingNetworks() returns: |
| 1199 | // null => there was no VPN, or the VPN didn't specify anything, so we use the default. |
| 1200 | // empty array => the VPN explicitly said "no default network". |
| 1201 | // non-empty array => the VPN specified one or more default networks; we use the |
| 1202 | // first one. |
| 1203 | if (networks.length > 0) { |
| 1204 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 1205 | } else { |
| 1206 | nai = null; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1207 | } |
| 1208 | } |
| 1209 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1210 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1211 | return nai.getNetworkState(); |
| 1212 | } else { |
| 1213 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1214 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1215 | } |
| 1216 | |
| 1217 | /** |
| 1218 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 1219 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1220 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid, |
| 1221 | boolean ignoreBlocked) { |
| 1222 | // Networks aren't blocked when ignoring blocked status |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1223 | if (ignoreBlocked) { |
| 1224 | return false; |
| 1225 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1226 | synchronized (mVpns) { |
| 1227 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1228 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1229 | return true; |
| 1230 | } |
| 1231 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1232 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1233 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, iface); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1234 | } |
| 1235 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1236 | private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) { |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 1237 | if (ni == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1238 | return; |
| 1239 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1240 | final boolean blocked; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1241 | synchronized (mBlockedAppUids) { |
| 1242 | if (ni.getDetailedState() == DetailedState.BLOCKED && mBlockedAppUids.add(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1243 | blocked = true; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1244 | } else if (ni.isConnected() && mBlockedAppUids.remove(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1245 | blocked = false; |
| 1246 | } else { |
| 1247 | return; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1248 | } |
| 1249 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1250 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1251 | log(String.format("Returning %s NetworkInfo to uid=%d", action, uid)); |
| 1252 | mNetworkInfoBlockingLogs.log(action + " " + uid); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1253 | } |
| 1254 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1255 | private void maybeLogBlockedStatusChanged(NetworkRequestInfo nri, Network net, |
| 1256 | boolean blocked) { |
| 1257 | if (nri == null || net == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1258 | return; |
| 1259 | } |
| 1260 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1261 | log(String.format("Blocked status changed to %s for %d(%d) on netId %d", blocked, |
| 1262 | nri.mUid, nri.request.requestId, net.netId)); |
| 1263 | mNetworkInfoBlockingLogs.log(action + " " + nri.mUid); |
| 1264 | } |
| 1265 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1266 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1267 | * Apply any relevant filters to {@link NetworkState} for the given UID. For |
| 1268 | * example, this may mark the network as {@link DetailedState#BLOCKED} based |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1269 | * on {@link #isNetworkWithLinkPropertiesBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1270 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1271 | private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1272 | if (state == null || state.networkInfo == null || state.linkProperties == null) return; |
| 1273 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1274 | if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, ignoreBlocked)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1275 | state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1276 | } |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1277 | synchronized (mVpns) { |
| 1278 | if (mLockdownTracker != null) { |
| 1279 | mLockdownTracker.augmentNetworkInfo(state.networkInfo); |
| 1280 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1281 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 1286 | * It is assumed that at most one network is active at a time. If more |
| 1287 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1288 | * @return the info for the active network, or {@code null} if none is |
| 1289 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1290 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1291 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1292 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1293 | enforceAccessPermission(); |
| 1294 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1295 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1296 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1297 | maybeLogBlockedNetworkInfo(state.networkInfo, uid); |
| 1298 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | } |
| 1300 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1301 | @Override |
| 1302 | public Network getActiveNetwork() { |
| 1303 | enforceAccessPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1304 | return getActiveNetworkForUidInternal(Binder.getCallingUid(), false); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1305 | } |
| 1306 | |
| 1307 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1308 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1309 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1310 | return getActiveNetworkForUidInternal(uid, ignoreBlocked); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1311 | } |
| 1312 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1313 | private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1314 | final int user = UserHandle.getUserId(uid); |
| 1315 | int vpnNetId = NETID_UNSET; |
| 1316 | synchronized (mVpns) { |
| 1317 | final Vpn vpn = mVpns.get(user); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1318 | // TODO : now that capabilities contain the UID, the appliesToUid test should |
| 1319 | // be removed as the satisfying test below should be enough. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1320 | if (vpn != null && vpn.appliesToUid(uid)) vpnNetId = vpn.getNetId(); |
| 1321 | } |
| 1322 | NetworkAgentInfo nai; |
| 1323 | if (vpnNetId != NETID_UNSET) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1324 | nai = getNetworkAgentInfoForNetId(vpnNetId); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1325 | if (nai != null) { |
| 1326 | final NetworkCapabilities requiredCaps = |
| 1327 | createDefaultNetworkCapabilitiesForUid(uid); |
| 1328 | if (requiredCaps.satisfiedByNetworkCapabilities(nai.networkCapabilities)) { |
| 1329 | return nai.network; |
| 1330 | } |
| 1331 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1332 | } |
| 1333 | nai = getDefaultNetwork(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1334 | if (nai != null |
| 1335 | && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, ignoreBlocked)) { |
| 1336 | nai = null; |
| 1337 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1338 | return nai != null ? nai.network : null; |
| 1339 | } |
| 1340 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1341 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1342 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1343 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1344 | final int uid = Binder.getCallingUid(); |
| 1345 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1346 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1347 | } |
| 1348 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1349 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1350 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1351 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1352 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1353 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1354 | return state.networkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1359 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1360 | final int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1361 | if (getVpnUnderlyingNetworks(uid) != null) { |
| 1362 | // A VPN is active, so we may need to return one of its underlying networks. This |
| 1363 | // information is not available in LegacyTypeTracker, so we have to get it from |
| 1364 | // getUnfilteredActiveNetworkState. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1365 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1366 | if (state.networkInfo != null && state.networkInfo.getType() == networkType) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1367 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1368 | return state.networkInfo; |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1369 | } |
| 1370 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1371 | final NetworkState state = getFilteredNetworkState(networkType, uid); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1372 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1373 | } |
| 1374 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1375 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1376 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1377 | enforceAccessPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1378 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1379 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1380 | final NetworkState state = nai.getNetworkState(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1381 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1382 | return state.networkInfo; |
| 1383 | } else { |
| 1384 | return null; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1385 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1386 | } |
| 1387 | |
| 1388 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1389 | public NetworkInfo[] getAllNetworkInfo() { |
| 1390 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1391 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1392 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1393 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1394 | NetworkInfo info = getNetworkInfo(networkType); |
| 1395 | if (info != null) { |
| 1396 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1398 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1399 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1402 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1403 | public Network getNetworkForType(int networkType) { |
| 1404 | enforceAccessPermission(); |
| 1405 | final int uid = Binder.getCallingUid(); |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1406 | NetworkState state = getFilteredNetworkState(networkType, uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1407 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, false)) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1408 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1409 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1410 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1414 | public Network[] getAllNetworks() { |
| 1415 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1416 | synchronized (mNetworkForNetId) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1417 | final Network[] result = new Network[mNetworkForNetId.size()]; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1418 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1419 | result[i] = mNetworkForNetId.valueAt(i).network; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1420 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1421 | return result; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1422 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1423 | } |
| 1424 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1425 | @Override |
| 1426 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1427 | // The basic principle is: if an app's traffic could possibly go over a |
| 1428 | // network, without the app doing anything multinetwork-specific, |
| 1429 | // (hence, by "default"), then include that network's capabilities in |
| 1430 | // the array. |
| 1431 | // |
| 1432 | // In the normal case, app traffic only goes over the system's default |
| 1433 | // network connection, so that's the only network returned. |
| 1434 | // |
| 1435 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1436 | // over whatever underlying networks the VPN specifies, while other app |
| 1437 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1438 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1439 | // returned includes the VPN's underlying networks and the system |
| 1440 | // default. |
| 1441 | enforceAccessPermission(); |
| 1442 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1443 | HashMap<Network, NetworkCapabilities> result = new HashMap<>(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1444 | |
| 1445 | NetworkAgentInfo nai = getDefaultNetwork(); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1446 | NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1447 | if (nc != null) { |
| 1448 | result.put(nai.network, nc); |
| 1449 | } |
| 1450 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1451 | synchronized (mVpns) { |
| 1452 | if (!mLockdownEnabled) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1453 | Vpn vpn = mVpns.get(userId); |
| 1454 | if (vpn != null) { |
| 1455 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1456 | if (networks != null) { |
| 1457 | for (Network network : networks) { |
| 1458 | nai = getNetworkAgentInfoForNetwork(network); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1459 | nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1460 | if (nc != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1461 | result.put(network, nc); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1462 | } |
| 1463 | } |
| 1464 | } |
| 1465 | } |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1470 | out = result.values().toArray(out); |
| 1471 | return out; |
| 1472 | } |
| 1473 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1474 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1475 | public boolean isNetworkSupported(int networkType) { |
| 1476 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1477 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1478 | } |
| 1479 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1480 | /** |
| 1481 | * Return LinkProperties for the active (i.e., connected) default |
| 1482 | * network interface. It is assumed that at most one default network |
| 1483 | * is active at a time. If more than one is active, it is indeterminate |
| 1484 | * which will be returned. |
| 1485 | * @return the ip properties for the active network, or {@code null} if |
| 1486 | * none is active |
| 1487 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1488 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1489 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1490 | enforceAccessPermission(); |
| 1491 | final int uid = Binder.getCallingUid(); |
| 1492 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1493 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1494 | } |
| 1495 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1496 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1497 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1498 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1499 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1500 | if (nai != null) { |
| 1501 | synchronized (nai) { |
| 1502 | return new LinkProperties(nai.linkProperties); |
| 1503 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1504 | } |
| 1505 | return null; |
| 1506 | } |
| 1507 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1508 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1509 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1510 | public LinkProperties getLinkProperties(Network network) { |
| 1511 | enforceAccessPermission(); |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1512 | return getLinkProperties(getNetworkAgentInfoForNetwork(network)); |
| 1513 | } |
| 1514 | |
| 1515 | private LinkProperties getLinkProperties(NetworkAgentInfo nai) { |
| 1516 | if (nai == null) { |
| 1517 | return null; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1518 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1519 | synchronized (nai) { |
| 1520 | return new LinkProperties(nai.linkProperties); |
| 1521 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1522 | } |
| 1523 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1524 | private NetworkCapabilities getNetworkCapabilitiesInternal(NetworkAgentInfo nai) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1525 | if (nai != null) { |
| 1526 | synchronized (nai) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1527 | if (nai.networkCapabilities != null) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1528 | return networkCapabilitiesRestrictedForCallerPermissions( |
| 1529 | nai.networkCapabilities, |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1530 | Binder.getCallingPid(), Binder.getCallingUid()); |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1531 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1532 | } |
| 1533 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1534 | return null; |
| 1535 | } |
| 1536 | |
| 1537 | @Override |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1538 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1539 | enforceAccessPermission(); |
| 1540 | return getNetworkCapabilitiesInternal(getNetworkAgentInfoForNetwork(network)); |
| 1541 | } |
| 1542 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1543 | private NetworkCapabilities networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1544 | NetworkCapabilities nc, int callerPid, int callerUid) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1545 | final NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1546 | if (!checkSettingsPermission(callerPid, callerUid)) { |
| 1547 | newNc.setUids(null); |
| 1548 | newNc.setSSID(null); |
| 1549 | } |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 1550 | if (newNc.getNetworkSpecifier() != null) { |
| 1551 | newNc.setNetworkSpecifier(newNc.getNetworkSpecifier().redact()); |
| 1552 | } |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1553 | return newNc; |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1554 | } |
| 1555 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1556 | private void restrictRequestUidsForCaller(NetworkCapabilities nc) { |
| 1557 | if (!checkSettingsPermission()) { |
| 1558 | nc.setSingleUid(Binder.getCallingUid()); |
| 1559 | } |
| 1560 | } |
| 1561 | |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 1562 | private void restrictBackgroundRequestForCaller(NetworkCapabilities nc) { |
| 1563 | if (!mPermissionMonitor.hasUseBackgroundNetworksPermission(Binder.getCallingUid())) { |
| 1564 | nc.addCapability(NET_CAPABILITY_FOREGROUND); |
| 1565 | } |
| 1566 | } |
| 1567 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1568 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1569 | public NetworkState[] getAllNetworkState() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1570 | // Require internal since we're handing out IMSI details |
| 1571 | enforceConnectivityInternalPermission(); |
| 1572 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1573 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1574 | for (Network network : getAllNetworks()) { |
| 1575 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1576 | if (nai != null) { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1577 | // TODO (b/73321673) : NetworkState contains a copy of the |
| 1578 | // NetworkCapabilities, which may contain UIDs of apps to which the |
| 1579 | // network applies. Should the UIDs be cleared so as not to leak or |
| 1580 | // interfere ? |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1581 | result.add(nai.getNetworkState()); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | return result.toArray(new NetworkState[result.size()]); |
| 1585 | } |
| 1586 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1587 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1588 | @Deprecated |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1589 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1590 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 1591 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 1592 | return new NetworkQuotaInfo(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1595 | @Override |
| 1596 | public boolean isActiveNetworkMetered() { |
| 1597 | enforceAccessPermission(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1598 | |
Varun Anand | c51b06d | 2019-02-25 17:22:02 -0800 | [diff] [blame] | 1599 | final NetworkCapabilities caps = getNetworkCapabilities(getActiveNetwork()); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1600 | if (caps != null) { |
| 1601 | return !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 1602 | } else { |
| 1603 | // Always return the most conservative value |
| 1604 | return true; |
| 1605 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1606 | } |
| 1607 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1608 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1609 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1610 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1611 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1612 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1613 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1614 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1615 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1616 | /** |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1617 | * Ensures that the system cannot call a particular method. |
| 1618 | */ |
| 1619 | private boolean disallowedBecauseSystemCaller() { |
| 1620 | // TODO: start throwing a SecurityException when GnssLocationProvider stops calling |
| 1621 | // requestRouteToHost. |
| 1622 | if (isSystem(Binder.getCallingUid())) { |
| 1623 | log("This method exists only for app backwards compatibility" |
| 1624 | + " and must not be called by system services."); |
| 1625 | return true; |
| 1626 | } |
| 1627 | return false; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1631 | * Ensure that a network route exists to deliver traffic to the specified |
| 1632 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1633 | * @param networkType the type of the network over which traffic to the |
| 1634 | * specified host is to be routed |
| 1635 | * @param hostAddress the IP address of the host to which the route is |
| 1636 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | * @return {@code true} on success, {@code false} on failure |
| 1638 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1639 | @Override |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1640 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1641 | if (disallowedBecauseSystemCaller()) { |
| 1642 | return false; |
| 1643 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1645 | if (mProtectedNetworks.contains(networkType)) { |
| 1646 | enforceConnectivityInternalPermission(); |
| 1647 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1648 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1649 | InetAddress addr; |
| 1650 | try { |
| 1651 | addr = InetAddress.getByAddress(hostAddress); |
| 1652 | } catch (UnknownHostException e) { |
| 1653 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1654 | return false; |
| 1655 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1656 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1658 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1659 | return false; |
| 1660 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1661 | |
| 1662 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1663 | if (nai == null) { |
| 1664 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1665 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1666 | } else { |
| 1667 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1668 | } |
| 1669 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1670 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1671 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1672 | DetailedState netState; |
| 1673 | synchronized (nai) { |
| 1674 | netState = nai.networkInfo.getDetailedState(); |
| 1675 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1676 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1677 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1678 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1679 | log("requestRouteToHostAddress on down network " |
| 1680 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1681 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1682 | } |
| 1683 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1685 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1686 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1687 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1688 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1689 | LinkProperties lp; |
| 1690 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1691 | synchronized (nai) { |
| 1692 | lp = nai.linkProperties; |
| 1693 | netId = nai.network.netId; |
| 1694 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1695 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1696 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1697 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1698 | } finally { |
| 1699 | Binder.restoreCallingIdentity(token); |
| 1700 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1703 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1704 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1705 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1706 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1707 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1708 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1709 | if (bestRoute.getGateway().equals(addr)) { |
| 1710 | // 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] | 1711 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1712 | } else { |
| 1713 | // if we will connect to this through another route, add a direct route |
| 1714 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1715 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1716 | } |
| 1717 | } |
Lorenzo Colitti | aa281e2 | 2015-09-04 13:12:42 +0900 | [diff] [blame] | 1718 | if (DBG) log("Adding legacy route " + bestRoute + |
| 1719 | " for UID/PID " + uid + "/" + Binder.getCallingPid()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1720 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1721 | mNMS.addLegacyRouteForNetId(netId, bestRoute, uid); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1722 | } catch (Exception e) { |
| 1723 | // never crash - catch them all |
| 1724 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1725 | return false; |
| 1726 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1727 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1728 | } |
| 1729 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1730 | @VisibleForTesting |
| 1731 | protected final INetdEventCallback mNetdEventCallback = new BaseNetdEventCallback() { |
| 1732 | @Override |
| 1733 | public void onPrivateDnsValidationEvent(int netId, String ipAddress, |
| 1734 | String hostname, boolean validated) { |
| 1735 | try { |
| 1736 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1737 | EVENT_PRIVATE_DNS_VALIDATION_UPDATE, |
| 1738 | new PrivateDnsValidationUpdate(netId, |
| 1739 | InetAddress.parseNumericAddress(ipAddress), |
| 1740 | hostname, validated))); |
| 1741 | } catch (IllegalArgumentException e) { |
| 1742 | loge("Error parsing ip address in validation event"); |
| 1743 | } |
| 1744 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1745 | |
| 1746 | @Override |
| 1747 | public void onDnsEvent(int netId, int eventType, int returnCode, String hostname, |
| 1748 | String[] ipAddresses, int ipAddressesCount, long timestamp, int uid) { |
| 1749 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
| 1750 | // Netd event only allow registrants from system. Each NetworkMonitor thread is under |
| 1751 | // the caller thread of registerNetworkAgent. Thus, it's not allowed to register netd |
| 1752 | // event callback for certain nai. e.g. cellular. Register here to pass to |
| 1753 | // NetworkMonitor instead. |
| 1754 | // TODO: Move the Dns Event to NetworkMonitor. Use Binder.clearCallingIdentity() in |
| 1755 | // registerNetworkAgent to have NetworkMonitor created with system process as design |
| 1756 | // expectation. Also, NetdEventListenerService only allow one callback from each |
| 1757 | // caller type. Need to re-factor NetdEventListenerService to allow multiple |
| 1758 | // NetworkMonitor registrants. |
| 1759 | if (nai != null && nai.satisfies(mDefaultRequest)) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 1760 | try { |
| 1761 | nai.networkMonitor().notifyDnsResponse(returnCode); |
| 1762 | } catch (RemoteException e) { |
| 1763 | e.rethrowFromSystemServer(); |
| 1764 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1765 | } |
| 1766 | } |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 1767 | |
| 1768 | @Override |
| 1769 | public void onNat64PrefixEvent(int netId, boolean added, |
| 1770 | String prefixString, int prefixLength) { |
| 1771 | mHandler.post(() -> handleNat64PrefixEvent(netId, added, prefixString, prefixLength)); |
| 1772 | } |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1773 | }; |
| 1774 | |
| 1775 | @VisibleForTesting |
| 1776 | protected void registerNetdEventCallback() { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1777 | final IIpConnectivityMetrics ipConnectivityMetrics = |
| 1778 | IIpConnectivityMetrics.Stub.asInterface( |
| 1779 | ServiceManager.getService(IpConnectivityLog.SERVICE_NAME)); |
| 1780 | if (ipConnectivityMetrics == null) { |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1781 | Slog.wtf(TAG, "Missing IIpConnectivityMetrics"); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1782 | return; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1783 | } |
| 1784 | |
| 1785 | try { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1786 | ipConnectivityMetrics.addNetdEventCallback( |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1787 | INetdEventCallback.CALLBACK_CALLER_CONNECTIVITY_SERVICE, |
| 1788 | mNetdEventCallback); |
| 1789 | } catch (Exception e) { |
| 1790 | loge("Error registering netd callback: " + e); |
| 1791 | } |
| 1792 | } |
| 1793 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 1794 | private final INetworkPolicyListener mPolicyListener = new NetworkPolicyManager.Listener() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1795 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1796 | public void onUidRulesChanged(int uid, int uidRules) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1797 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UID_RULES_CHANGED, uid, uidRules)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1798 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1799 | @Override |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1800 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1801 | // caller is NPMS, since we only register with them |
| 1802 | if (LOGD_BLOCKED_NETWORKINFO) { |
| 1803 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1804 | } |
| 1805 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1806 | EVENT_DATA_SAVER_CHANGED, restrictBackground ? 1 : 0, 0)); |
| 1807 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1808 | // TODO: relocate this specific callback in Tethering. |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1809 | if (restrictBackground) { |
| 1810 | log("onRestrictBackgroundChanged(true): disabling tethering"); |
| 1811 | mTethering.untetherAll(); |
| 1812 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1813 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1814 | }; |
| 1815 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1816 | void handleUidRulesChanged(int uid, int newRules) { |
| 1817 | // skip update when we've already applied rules |
| 1818 | final int oldRules = mUidRules.get(uid, RULE_NONE); |
| 1819 | if (oldRules == newRules) return; |
| 1820 | |
| 1821 | maybeNotifyNetworkBlockedForNewUidRules(uid, newRules); |
| 1822 | |
| 1823 | if (newRules == RULE_NONE) { |
| 1824 | mUidRules.delete(uid); |
| 1825 | } else { |
| 1826 | mUidRules.put(uid, newRules); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | void handleRestrictBackgroundChanged(boolean restrictBackground) { |
| 1831 | if (mRestrictBackground == restrictBackground) return; |
| 1832 | |
| 1833 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1834 | final boolean curMetered = nai.networkCapabilities.isMetered(); |
| 1835 | maybeNotifyNetworkBlocked(nai, curMetered, curMetered, mRestrictBackground, |
| 1836 | restrictBackground); |
| 1837 | } |
| 1838 | |
| 1839 | mRestrictBackground = restrictBackground; |
| 1840 | } |
| 1841 | |
| 1842 | private boolean isUidNetworkingWithVpnBlocked(int uid, int uidRules, boolean isNetworkMetered, |
| 1843 | boolean isBackgroundRestricted) { |
| 1844 | synchronized (mVpns) { |
| 1845 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
| 1846 | // Because the return value of this function depends on the list of UIDs the |
| 1847 | // always-on VPN blocks when in lockdown mode, when the always-on VPN changes that |
| 1848 | // list all state depending on the return value of this function has to be recomputed. |
| 1849 | // TODO: add a trigger when the always-on VPN sets its blocked UIDs to reevaluate and |
| 1850 | // send the necessary onBlockedStatusChanged callbacks. |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1851 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1852 | return true; |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, uidRules, |
| 1857 | isNetworkMetered, isBackgroundRestricted); |
| 1858 | } |
| 1859 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 1860 | /** |
| 1861 | * Require that the caller is either in the same user or has appropriate permission to interact |
| 1862 | * across users. |
| 1863 | * |
| 1864 | * @param userId Target user for whatever operation the current IPC is supposed to perform. |
| 1865 | */ |
| 1866 | private void enforceCrossUserPermission(int userId) { |
| 1867 | if (userId == UserHandle.getCallingUserId()) { |
| 1868 | // Not a cross-user call. |
| 1869 | return; |
| 1870 | } |
| 1871 | mContext.enforceCallingOrSelfPermission( |
| 1872 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1873 | "ConnectivityService"); |
| 1874 | } |
| 1875 | |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1876 | private boolean checkAnyPermissionOf(String... permissions) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1877 | for (String permission : permissions) { |
| 1878 | if (mContext.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1879 | return true; |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1880 | } |
| 1881 | } |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1882 | return false; |
| 1883 | } |
| 1884 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame^] | 1885 | private boolean checkAnyPermissionOf(int pid, int uid, String... permissions) { |
| 1886 | for (String permission : permissions) { |
| 1887 | if (mContext.checkPermission(permission, pid, uid) == PERMISSION_GRANTED) { |
| 1888 | return true; |
| 1889 | } |
| 1890 | } |
| 1891 | return false; |
| 1892 | } |
| 1893 | |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1894 | private void enforceAnyPermissionOf(String... permissions) { |
| 1895 | if (!checkAnyPermissionOf(permissions)) { |
| 1896 | throw new SecurityException("Requires one of the following permissions: " |
| 1897 | + String.join(", ", permissions) + "."); |
| 1898 | } |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1899 | } |
| 1900 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1901 | private void enforceInternetPermission() { |
| 1902 | mContext.enforceCallingOrSelfPermission( |
| 1903 | android.Manifest.permission.INTERNET, |
| 1904 | "ConnectivityService"); |
| 1905 | } |
| 1906 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1907 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1908 | mContext.enforceCallingOrSelfPermission( |
| 1909 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1910 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | private void enforceChangePermission() { |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1914 | ConnectivityManager.enforceChangePermission(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1915 | } |
| 1916 | |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1917 | private void enforceSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1918 | enforceAnyPermissionOf( |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1919 | android.Manifest.permission.NETWORK_SETTINGS, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1920 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1921 | } |
| 1922 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1923 | private boolean checkSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1924 | return checkAnyPermissionOf( |
| 1925 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1926 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1927 | } |
| 1928 | |
| 1929 | private boolean checkSettingsPermission(int pid, int uid) { |
| 1930 | return PERMISSION_GRANTED == mContext.checkPermission( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1931 | android.Manifest.permission.NETWORK_SETTINGS, pid, uid) |
| 1932 | || PERMISSION_GRANTED == mContext.checkPermission( |
| 1933 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, pid, uid); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1934 | } |
| 1935 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1936 | private void enforceTetherAccessPermission() { |
| 1937 | mContext.enforceCallingOrSelfPermission( |
| 1938 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1939 | "ConnectivityService"); |
| 1940 | } |
| 1941 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1942 | private void enforceConnectivityInternalPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1943 | enforceAnyPermissionOf( |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1944 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1945 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1946 | } |
| 1947 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1948 | private void enforceControlAlwaysOnVpnPermission() { |
| 1949 | mContext.enforceCallingOrSelfPermission( |
| 1950 | android.Manifest.permission.CONTROL_ALWAYS_ON_VPN, |
| 1951 | "ConnectivityService"); |
| 1952 | } |
| 1953 | |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1954 | private void enforceNetworkStackSettingsOrSetup() { |
| 1955 | enforceAnyPermissionOf( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1956 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1957 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1958 | android.Manifest.permission.NETWORK_STACK, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1959 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1962 | private boolean checkNetworkStackPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1963 | return checkAnyPermissionOf( |
| 1964 | android.Manifest.permission.NETWORK_STACK, |
| 1965 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1966 | } |
| 1967 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame^] | 1968 | private boolean checkNetworkSignalStrengthWakeupPermission(int pid, int uid) { |
| 1969 | return checkAnyPermissionOf(pid, uid, |
| 1970 | android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP, |
| 1971 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
| 1972 | } |
| 1973 | |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 1974 | private void enforceConnectivityRestrictedNetworksPermission() { |
| 1975 | try { |
| 1976 | mContext.enforceCallingOrSelfPermission( |
| 1977 | android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS, |
| 1978 | "ConnectivityService"); |
| 1979 | return; |
| 1980 | } catch (SecurityException e) { /* fallback to ConnectivityInternalPermission */ } |
| 1981 | enforceConnectivityInternalPermission(); |
| 1982 | } |
| 1983 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1984 | private void enforceKeepalivePermission() { |
Lorenzo Colitti | 7914ce5 | 2015-09-08 13:21:48 +0900 | [diff] [blame] | 1985 | mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1986 | } |
| 1987 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1988 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1989 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1990 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 1991 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 1995 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 1996 | } |
| 1997 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 1998 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1999 | synchronized (mVpns) { |
| 2000 | if (mLockdownTracker != null) { |
| 2001 | info = new NetworkInfo(info); |
| 2002 | mLockdownTracker.augmentNetworkInfo(info); |
| 2003 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2006 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2007 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2008 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2009 | if (info.isFailover()) { |
| 2010 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2011 | info.setFailover(false); |
| 2012 | } |
| 2013 | if (info.getReason() != null) { |
| 2014 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2015 | } |
| 2016 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2017 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2018 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2020 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2021 | return intent; |
| 2022 | } |
| 2023 | |
| 2024 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 2025 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 2026 | } |
| 2027 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2028 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2029 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 2030 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 2031 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2032 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2033 | final long ident = Binder.clearCallingIdentity(); |
| 2034 | try { |
| 2035 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 2036 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2037 | } finally { |
| 2038 | Binder.restoreCallingIdentity(ident); |
| 2039 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2040 | } |
| 2041 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2042 | private void sendStickyBroadcast(Intent intent) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2043 | synchronized (this) { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 2044 | if (!mSystemReady |
| 2045 | && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2046 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2047 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2048 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2049 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2050 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2051 | } |
| 2052 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2053 | Bundle options = null; |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2054 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2055 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2056 | final NetworkInfo ni = intent.getParcelableExtra( |
| 2057 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 2058 | if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) { |
| 2059 | intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL); |
| 2060 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2061 | } else { |
| 2062 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 2063 | opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M); |
| 2064 | options = opts.toBundle(); |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2065 | } |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2066 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 2067 | try { |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2068 | bs.noteConnectivityChanged(intent.getIntExtra( |
| 2069 | ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE), |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 2070 | ni.getState().toString()); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2071 | } catch (RemoteException e) { |
| 2072 | } |
Chad Brubaker | ac92501 | 2018-03-08 10:37:09 -0800 | [diff] [blame] | 2073 | intent.addFlags(Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2074 | } |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2075 | try { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2076 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL, options); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2077 | } finally { |
| 2078 | Binder.restoreCallingIdentity(ident); |
| 2079 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2080 | } |
| 2081 | } |
| 2082 | |
| 2083 | void systemReady() { |
Chalard Jean | 655ad15 | 2018-06-08 12:47:42 +0900 | [diff] [blame] | 2084 | mProxyTracker.loadGlobalProxy(); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2085 | registerNetdEventCallback(); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 2086 | mTethering.systemReady(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2087 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2088 | synchronized (this) { |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2089 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2090 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2091 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2092 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2093 | } |
| 2094 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2095 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 2096 | // Try bringing up tracker, but KeyStore won't be ready yet for secondary users so wait |
| 2097 | // for user to unlock device too. |
| 2098 | updateLockdownVpn(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2099 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 2100 | // Create network requests for always-on networks. |
| 2101 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS)); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2102 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2103 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 2104 | |
| 2105 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2106 | } |
| 2107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2108 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2109 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2110 | * |
| 2111 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2112 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2113 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2114 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2115 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2116 | |
| 2117 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2118 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2119 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2120 | if (networkAgent.networkCapabilities.hasTransport( |
| 2121 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2122 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2123 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Adam Lesinski | ed6160d | 2015-08-18 11:47:07 -0700 | [diff] [blame] | 2124 | 10); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2125 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2126 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 2127 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2128 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2129 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 2130 | 15); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2131 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2132 | } else { |
| 2133 | // do not track any other networks |
| 2134 | timeout = 0; |
| 2135 | } |
| 2136 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2137 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2138 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2139 | mNMS.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2140 | } catch (Exception e) { |
| 2141 | // You shall not crash! |
| 2142 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2143 | } |
| 2144 | } |
| 2145 | } |
| 2146 | |
| 2147 | /** |
| 2148 | * Remove data activity tracking when network disconnects. |
| 2149 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2150 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2151 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 2152 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2153 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2154 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 2155 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2156 | try { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2157 | // the call fails silently if no idle timer setup for this interface |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2158 | mNMS.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2159 | } catch (Exception e) { |
| 2160 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2161 | } |
| 2162 | } |
| 2163 | } |
| 2164 | |
| 2165 | /** |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 2166 | * Update data activity tracking when network state is updated. |
| 2167 | */ |
| 2168 | private void updateDataActivityTracking(NetworkAgentInfo newNetwork, |
| 2169 | NetworkAgentInfo oldNetwork) { |
| 2170 | if (newNetwork != null) { |
| 2171 | setupDataActivityTracking(newNetwork); |
| 2172 | } |
| 2173 | if (oldNetwork != null) { |
| 2174 | removeDataActivityTracking(oldNetwork); |
| 2175 | } |
| 2176 | } |
| 2177 | /** |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2178 | * Reads the network specific MTU size from resources. |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2179 | * and set it on it's iface. |
| 2180 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2181 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 2182 | final String iface = newLp.getInterfaceName(); |
| 2183 | final int mtu = newLp.getMtu(); |
Pierre Imai | 54f0d9e | 2016-02-08 16:01:40 +0900 | [diff] [blame] | 2184 | if (oldLp == null && mtu == 0) { |
| 2185 | // Silently ignore unset MTU value. |
| 2186 | return; |
| 2187 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2188 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 2189 | if (VDBG) log("identical MTU - not setting"); |
| 2190 | return; |
| 2191 | } |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 2192 | if (!LinkProperties.isValidMtu(mtu, newLp.hasGlobalIpv6Address())) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2193 | if (mtu != 0) loge("Unexpected mtu value: " + mtu + ", " + iface); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2194 | return; |
| 2195 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2196 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 2197 | // Cannot set MTU without interface name |
| 2198 | if (TextUtils.isEmpty(iface)) { |
| 2199 | loge("Setting MTU size with null iface."); |
| 2200 | return; |
| 2201 | } |
| 2202 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2203 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2204 | if (VDBG || DDBG) log("Setting MTU size: " + iface + ", " + mtu); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2205 | mNMS.setMtu(iface, mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2206 | } catch (Exception e) { |
| 2207 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2208 | } |
| 2209 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2210 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2211 | @VisibleForTesting |
| 2212 | 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] | 2213 | private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd"; |
| 2214 | |
| 2215 | // Overridden for testing purposes to avoid writing to SystemProperties. |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 2216 | @VisibleForTesting |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2217 | protected MockableSystemProperties getSystemProperties() { |
| 2218 | return new MockableSystemProperties(); |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 2219 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2220 | |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 2221 | private void updateTcpBufferSizes(String tcpBufferSizes) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2222 | String[] values = null; |
| 2223 | if (tcpBufferSizes != null) { |
| 2224 | values = tcpBufferSizes.split(","); |
| 2225 | } |
| 2226 | |
| 2227 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2228 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2229 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 2230 | values = tcpBufferSizes.split(","); |
| 2231 | } |
| 2232 | |
| 2233 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 2234 | |
| 2235 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2236 | 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] | 2237 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2238 | String rmemValues = String.join(" ", values[0], values[1], values[2]); |
| 2239 | String wmemValues = String.join(" ", values[3], values[4], values[5]); |
| 2240 | mNetd.setTcpRWmemorySize(rmemValues, wmemValues); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2241 | mCurrentTcpBufferSizes = tcpBufferSizes; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2242 | } catch (RemoteException | ServiceSpecificException e) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2243 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2244 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2245 | |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2246 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2247 | Settings.Global.TCP_DEFAULT_INIT_RWND, |
| 2248 | mSystemProperties.getInt("net.tcp.default_init_rwnd", 0)); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2249 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2250 | if (rwndValue != 0) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2251 | mSystemProperties.set(sysctlKey, rwndValue.toString()); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2252 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2253 | } |
| 2254 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2255 | @Override |
| 2256 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2257 | String restoreDefaultNetworkDelayStr = mSystemProperties.get( |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2258 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2259 | if(restoreDefaultNetworkDelayStr != null && |
| 2260 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2261 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 2262 | return Integer.parseInt(restoreDefaultNetworkDelayStr); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2263 | } catch (NumberFormatException e) { |
| 2264 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2265 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2266 | // if the system property isn't set, use the value for the apn type |
| 2267 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2268 | |
| 2269 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2270 | (mNetConfigs[networkType] != null)) { |
| 2271 | ret = mNetConfigs[networkType].restoreTime; |
| 2272 | } |
| 2273 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2274 | } |
| 2275 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2276 | private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { |
| 2277 | final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); |
| 2278 | final long DIAG_TIME_MS = 5000; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2279 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2280 | // Start gathering diagnostic information. |
| 2281 | netDiags.add(new NetworkDiagnostics( |
| 2282 | nai.network, |
| 2283 | new LinkProperties(nai.linkProperties), // Must be a copy. |
| 2284 | DIAG_TIME_MS)); |
| 2285 | } |
| 2286 | |
| 2287 | for (NetworkDiagnostics netDiag : netDiags) { |
| 2288 | pw.println(); |
| 2289 | netDiag.waitForMeasurements(); |
| 2290 | netDiag.dump(pw); |
| 2291 | } |
| 2292 | } |
| 2293 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2294 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2295 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2296 | PriorityDump.dump(mPriorityDumper, fd, writer, args); |
| 2297 | } |
| 2298 | |
| 2299 | private void doDump(FileDescriptor fd, PrintWriter writer, String[] args, boolean asProto) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2300 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2301 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2302 | if (asProto) return; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2303 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2304 | if (ArrayUtils.contains(args, DIAG_ARG)) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2305 | dumpNetworkDiagnostics(pw); |
| 2306 | return; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2307 | } else if (ArrayUtils.contains(args, TETHERING_ARG)) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 2308 | mTethering.dump(fd, pw, args); |
| 2309 | return; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2310 | } else if (ArrayUtils.contains(args, NETWORK_ARG)) { |
| 2311 | dumpNetworks(pw); |
| 2312 | return; |
| 2313 | } else if (ArrayUtils.contains(args, REQUEST_ARG)) { |
| 2314 | dumpNetworkRequests(pw); |
| 2315 | return; |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2316 | } |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 2317 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2318 | pw.print("NetworkFactories for:"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2319 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2320 | pw.print(" " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2321 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2322 | pw.println(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2323 | pw.println(); |
| 2324 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2325 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2326 | pw.print("Active default network: "); |
| 2327 | if (defaultNai == null) { |
| 2328 | pw.println("none"); |
| 2329 | } else { |
| 2330 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2331 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2332 | pw.println(); |
| 2333 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2334 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2335 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2336 | dumpNetworks(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2337 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2338 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2339 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 2340 | pw.print("Restrict background: "); |
| 2341 | pw.println(mRestrictBackground); |
| 2342 | pw.println(); |
| 2343 | |
| 2344 | pw.println("Status for known UIDs:"); |
| 2345 | pw.increaseIndent(); |
| 2346 | final int size = mUidRules.size(); |
| 2347 | for (int i = 0; i < size; i++) { |
| 2348 | // Don't crash if the array is modified while dumping in bugreports. |
| 2349 | try { |
| 2350 | final int uid = mUidRules.keyAt(i); |
| 2351 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2352 | pw.println("UID=" + uid + " rules=" + uidRulesToString(uidRules)); |
| 2353 | } catch (ArrayIndexOutOfBoundsException e) { |
| 2354 | pw.println(" ArrayIndexOutOfBoundsException"); |
| 2355 | } catch (ConcurrentModificationException e) { |
| 2356 | pw.println(" ConcurrentModificationException"); |
| 2357 | } |
| 2358 | } |
| 2359 | pw.println(); |
| 2360 | pw.decreaseIndent(); |
| 2361 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2362 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2363 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2364 | dumpNetworkRequests(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2365 | pw.decreaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2366 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2367 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2368 | mLegacyTypeTracker.dump(pw); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2369 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2370 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2371 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2372 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2373 | pw.println(); |
| 2374 | mKeepaliveTracker.dump(pw); |
| 2375 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2376 | pw.println(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 2377 | dumpAvoidBadWifiSettings(pw); |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2378 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 2379 | pw.println(); |
| 2380 | mMultipathPolicyTracker.dump(pw); |
| 2381 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2382 | if (ArrayUtils.contains(args, SHORT_ARG) == false) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2383 | pw.println(); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2384 | pw.println("mNetworkRequestInfoLogs (most recent first):"); |
| 2385 | pw.increaseIndent(); |
| 2386 | mNetworkRequestInfoLogs.reverseDump(fd, pw, args); |
| 2387 | pw.decreaseIndent(); |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 2388 | |
| 2389 | pw.println(); |
| 2390 | pw.println("mNetworkInfoBlockingLogs (most recent first):"); |
| 2391 | pw.increaseIndent(); |
| 2392 | mNetworkInfoBlockingLogs.reverseDump(fd, pw, args); |
| 2393 | pw.decreaseIndent(); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2394 | |
| 2395 | pw.println(); |
| 2396 | pw.println("NetTransition WakeLock activity (most recent first):"); |
| 2397 | pw.increaseIndent(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 2398 | pw.println("total acquisitions: " + mTotalWakelockAcquisitions); |
| 2399 | pw.println("total releases: " + mTotalWakelockReleases); |
| 2400 | pw.println("cumulative duration: " + (mTotalWakelockDurationMs / 1000) + "s"); |
| 2401 | pw.println("longest duration: " + (mMaxWakelockDurationMs / 1000) + "s"); |
| 2402 | if (mTotalWakelockAcquisitions > mTotalWakelockReleases) { |
| 2403 | long duration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 2404 | pw.println("currently holding WakeLock for: " + (duration / 1000) + "s"); |
| 2405 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2406 | mWakelockLogs.reverseDump(fd, pw, args); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 2407 | |
| 2408 | pw.println(); |
| 2409 | pw.println("bandwidth update requests (by uid):"); |
| 2410 | pw.increaseIndent(); |
| 2411 | synchronized (mBandwidthRequests) { |
| 2412 | for (int i = 0; i < mBandwidthRequests.size(); i++) { |
| 2413 | pw.println("[" + mBandwidthRequests.keyAt(i) |
| 2414 | + "]: " + mBandwidthRequests.valueAt(i)); |
| 2415 | } |
| 2416 | } |
| 2417 | pw.decreaseIndent(); |
| 2418 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2419 | pw.decreaseIndent(); |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2420 | } |
Remi NGUYEN VAN | a25379f | 2019-02-18 11:20:28 +0900 | [diff] [blame] | 2421 | |
| 2422 | pw.println(); |
| 2423 | pw.println("NetworkStackClient logs:"); |
| 2424 | pw.increaseIndent(); |
| 2425 | NetworkStackClient.getInstance().dump(pw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2426 | } |
| 2427 | |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2428 | private void dumpNetworks(IndentingPrintWriter pw) { |
| 2429 | for (NetworkAgentInfo nai : networksSortedById()) { |
| 2430 | pw.println(nai.toString()); |
| 2431 | pw.increaseIndent(); |
| 2432 | pw.println(String.format( |
| 2433 | "Requests: REQUEST:%d LISTEN:%d BACKGROUND_REQUEST:%d total:%d", |
| 2434 | nai.numForegroundNetworkRequests(), |
| 2435 | nai.numNetworkRequests() - nai.numRequestNetworkRequests(), |
| 2436 | nai.numBackgroundNetworkRequests(), |
| 2437 | nai.numNetworkRequests())); |
| 2438 | pw.increaseIndent(); |
| 2439 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 2440 | pw.println(nai.requestAt(i).toString()); |
| 2441 | } |
| 2442 | pw.decreaseIndent(); |
| 2443 | pw.println("Lingered:"); |
| 2444 | pw.increaseIndent(); |
| 2445 | nai.dumpLingerTimers(pw); |
| 2446 | pw.decreaseIndent(); |
| 2447 | pw.decreaseIndent(); |
| 2448 | } |
| 2449 | } |
| 2450 | |
| 2451 | private void dumpNetworkRequests(IndentingPrintWriter pw) { |
| 2452 | for (NetworkRequestInfo nri : requestsSortedById()) { |
| 2453 | pw.println(nri.toString()); |
| 2454 | } |
| 2455 | } |
| 2456 | |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2457 | /** |
| 2458 | * Return an array of all current NetworkAgentInfos sorted by network id. |
| 2459 | */ |
| 2460 | private NetworkAgentInfo[] networksSortedById() { |
| 2461 | NetworkAgentInfo[] networks = new NetworkAgentInfo[0]; |
| 2462 | networks = mNetworkAgentInfos.values().toArray(networks); |
| 2463 | Arrays.sort(networks, Comparator.comparingInt(nai -> nai.network.netId)); |
| 2464 | return networks; |
| 2465 | } |
| 2466 | |
| 2467 | /** |
| 2468 | * Return an array of all current NetworkRequest sorted by request id. |
| 2469 | */ |
| 2470 | private NetworkRequestInfo[] requestsSortedById() { |
| 2471 | NetworkRequestInfo[] requests = new NetworkRequestInfo[0]; |
| 2472 | requests = mNetworkRequests.values().toArray(requests); |
| 2473 | Arrays.sort(requests, Comparator.comparingInt(nri -> nri.request.requestId)); |
| 2474 | return requests; |
| 2475 | } |
| 2476 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2477 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2478 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2479 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2480 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 2481 | if (officialNai != null || VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2482 | loge(eventName(what) + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2483 | " - " + nai); |
| 2484 | } |
| 2485 | return false; |
| 2486 | } |
| 2487 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2488 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2489 | private class NetworkStateTrackerHandler extends Handler { |
| 2490 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 2491 | super(looper); |
| 2492 | } |
| 2493 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2494 | private boolean maybeHandleAsyncChannelMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2495 | switch (msg.what) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2496 | default: |
| 2497 | return false; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2498 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2499 | handleAsyncChannelHalfConnect(msg); |
| 2500 | break; |
| 2501 | } |
| 2502 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 2503 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2504 | if (nai != null) nai.asyncChannel.disconnect(); |
| 2505 | break; |
| 2506 | } |
| 2507 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 2508 | handleAsyncChannelDisconnected(msg); |
| 2509 | break; |
| 2510 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2511 | } |
| 2512 | return true; |
| 2513 | } |
| 2514 | |
| 2515 | private void maybeHandleNetworkAgentMessage(Message msg) { |
| 2516 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2517 | if (nai == null) { |
| 2518 | if (VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2519 | log(String.format("%s from unknown NetworkAgent", eventName(msg.what))); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2520 | } |
| 2521 | return; |
| 2522 | } |
| 2523 | |
| 2524 | switch (msg.what) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2525 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2526 | final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2527 | if (networkCapabilities.hasConnectivityManagedCapability()) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2528 | Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2529 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2530 | updateCapabilities(nai.getCurrentScore(), nai, networkCapabilities); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2531 | break; |
| 2532 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2533 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 2534 | handleUpdateLinkProperties(nai, (LinkProperties) msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2535 | break; |
| 2536 | } |
| 2537 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2538 | NetworkInfo info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2539 | updateNetworkInfo(nai, info); |
| 2540 | break; |
| 2541 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2542 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
Roshan Pius | 64e99ef | 2018-08-02 10:25:02 -0700 | [diff] [blame] | 2543 | updateNetworkScore(nai, msg.arg1); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2544 | break; |
| 2545 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2546 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2547 | if (nai.everConnected && !nai.networkMisc.explicitlySelected) { |
| 2548 | loge("ERROR: already-connected network explicitly selected."); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2549 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2550 | nai.networkMisc.explicitlySelected = true; |
Roshan Pius | eaf8dee | 2018-08-10 07:36:39 -0700 | [diff] [blame] | 2551 | nai.networkMisc.acceptUnvalidated = msg.arg1 == 1; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2552 | // Mark the network as temporarily accepting partial connectivity so that it |
| 2553 | // will be validated (and possibly become default) even if it only provides |
| 2554 | // partial internet access. Note that if user connects to partial connectivity |
| 2555 | // and choose "don't ask again", then wifi disconnected by some reasons(maybe |
| 2556 | // out of wifi coverage) and if the same wifi is available again, the device |
| 2557 | // will auto connect to this wifi even though the wifi has "no internet". |
| 2558 | // TODO: Evaluate using a separate setting in IpMemoryStore. |
| 2559 | nai.networkMisc.acceptPartialConnectivity = msg.arg1 == 1; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2560 | break; |
| 2561 | } |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 2562 | case NetworkAgent.EVENT_SOCKET_KEEPALIVE: { |
| 2563 | mKeepaliveTracker.handleEventSocketKeepalive(nai, msg); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2564 | break; |
| 2565 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | private boolean maybeHandleNetworkMonitorMessage(Message msg) { |
| 2570 | switch (msg.what) { |
| 2571 | default: |
| 2572 | return false; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2573 | case EVENT_NETWORK_TESTED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2574 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2575 | if (nai == null) break; |
| 2576 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2577 | final boolean partialConnectivity = |
| 2578 | (msg.arg1 == NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY) |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2579 | || (nai.networkMisc.acceptPartialConnectivity |
| 2580 | && nai.partialConnectivity); |
| 2581 | // Once a network is determined to have partial connectivity, it cannot |
lucaslin | 975b7d3 | 2019-03-21 11:59:22 +0800 | [diff] [blame] | 2582 | // go back to full connectivity without a disconnect. This is because |
| 2583 | // NetworkMonitor can only communicate either PARTIAL_CONNECTIVITY or VALID, |
| 2584 | // but not both. |
| 2585 | // TODO: Provide multi-testResult to improve the communication between |
| 2586 | // ConnectivityService and NetworkMonitor, so that ConnectivityService could |
| 2587 | // know the real status of network. |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 2588 | final boolean partialConnectivityChanged = |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2589 | (partialConnectivity && !nai.partialConnectivity); |
| 2590 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2591 | final boolean valid = (msg.arg1 == NETWORK_TEST_RESULT_VALID); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2592 | final boolean wasValidated = nai.lastValidated; |
| 2593 | final boolean wasDefault = isDefaultNetwork(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2594 | if (nai.everCaptivePortalDetected && !nai.captivePortalLoginNotified |
| 2595 | && valid) { |
| 2596 | nai.captivePortalLoginNotified = true; |
| 2597 | showNetworkNotification(nai, NotificationType.LOGGED_IN); |
| 2598 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2599 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2600 | final String redirectUrl = (msg.obj instanceof String) ? (String) msg.obj : ""; |
| 2601 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2602 | if (DBG) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2603 | final String logMsg = !TextUtils.isEmpty(redirectUrl) |
| 2604 | ? " with redirect to " + redirectUrl |
| 2605 | : ""; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2606 | log(nai.name() + " validation " + (valid ? "passed" : "failed") + logMsg); |
| 2607 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2608 | if (valid != nai.lastValidated) { |
| 2609 | if (wasDefault) { |
| 2610 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkValidity( |
| 2611 | SystemClock.elapsedRealtime(), valid); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2612 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2613 | final int oldScore = nai.getCurrentScore(); |
| 2614 | nai.lastValidated = valid; |
| 2615 | nai.everValidated |= valid; |
| 2616 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
| 2617 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2618 | if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2619 | if (valid) { |
| 2620 | handleFreshlyValidatedNetwork(nai); |
| 2621 | // Clear NO_INTERNET and LOST_INTERNET notifications if network becomes |
| 2622 | // valid. |
| 2623 | mNotifier.clearNotification(nai.network.netId, |
| 2624 | NotificationType.NO_INTERNET); |
| 2625 | mNotifier.clearNotification(nai.network.netId, |
| 2626 | NotificationType.LOST_INTERNET); |
| 2627 | } |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 2628 | } else if (partialConnectivityChanged) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2629 | nai.partialConnectivity = partialConnectivity; |
| 2630 | updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2631 | } |
| 2632 | updateInetCondition(nai); |
| 2633 | // Let the NetworkAgent know the state of its network |
| 2634 | Bundle redirectUrlBundle = new Bundle(); |
| 2635 | redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, redirectUrl); |
| 2636 | nai.asyncChannel.sendMessage( |
| 2637 | NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
| 2638 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
| 2639 | 0, redirectUrlBundle); |
| 2640 | if (wasValidated && !nai.lastValidated) { |
| 2641 | handleNetworkUnvalidated(nai); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2642 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2643 | break; |
| 2644 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2645 | case EVENT_PROVISIONING_NOTIFICATION: { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2646 | final int netId = msg.arg2; |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 2647 | final boolean visible = toBool(msg.arg1); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2648 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2649 | // If captive portal status has changed, update capabilities or disconnect. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2650 | if (nai != null && (visible != nai.lastCaptivePortalDetected)) { |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2651 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2652 | nai.lastCaptivePortalDetected = visible; |
| 2653 | nai.everCaptivePortalDetected |= visible; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2654 | if (visible) { |
| 2655 | nai.captivePortalLoginNotified = false; |
| 2656 | } |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2657 | if (nai.lastCaptivePortalDetected && |
| 2658 | Settings.Global.CAPTIVE_PORTAL_MODE_AVOID == getCaptivePortalMode()) { |
| 2659 | if (DBG) log("Avoiding captive portal network: " + nai.name()); |
| 2660 | nai.asyncChannel.sendMessage( |
| 2661 | NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 2662 | teardownUnneededNetwork(nai); |
| 2663 | break; |
| 2664 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2665 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2666 | } |
| 2667 | if (!visible) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2668 | // Only clear SIGN_IN and NETWORK_SWITCH notifications here, or else other |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2669 | // notifications belong to the same network may be cleared unexpectedly. |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2670 | mNotifier.clearNotification(netId, NotificationType.SIGN_IN); |
| 2671 | mNotifier.clearNotification(netId, NotificationType.NETWORK_SWITCH); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2672 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2673 | if (nai == null) { |
| 2674 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2675 | break; |
| 2676 | } |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2677 | if (!nai.networkMisc.provisioningNotificationDisabled) { |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 2678 | mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai, null, |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 2679 | (PendingIntent) msg.obj, nai.networkMisc.explicitlySelected); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2680 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2681 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2682 | break; |
| 2683 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2684 | case EVENT_PRIVATE_DNS_CONFIG_RESOLVED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2685 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2686 | if (nai == null) break; |
| 2687 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2688 | updatePrivateDns(nai, (PrivateDnsConfig) msg.obj); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2689 | break; |
| 2690 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2691 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2692 | return true; |
| 2693 | } |
| 2694 | |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2695 | private int getCaptivePortalMode() { |
| 2696 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 2697 | Settings.Global.CAPTIVE_PORTAL_MODE, |
| 2698 | Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
| 2699 | } |
| 2700 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2701 | private boolean maybeHandleNetworkAgentInfoMessage(Message msg) { |
| 2702 | switch (msg.what) { |
| 2703 | default: |
| 2704 | return false; |
| 2705 | case NetworkAgentInfo.EVENT_NETWORK_LINGER_COMPLETE: { |
| 2706 | NetworkAgentInfo nai = (NetworkAgentInfo) msg.obj; |
| 2707 | if (nai != null && isLiveNetworkAgent(nai, msg.what)) { |
| 2708 | handleLingerComplete(nai); |
| 2709 | } |
| 2710 | break; |
| 2711 | } |
| 2712 | } |
| 2713 | return true; |
| 2714 | } |
| 2715 | |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2716 | private boolean maybeHandleNetworkFactoryMessage(Message msg) { |
| 2717 | switch (msg.what) { |
| 2718 | default: |
| 2719 | return false; |
| 2720 | case android.net.NetworkFactory.EVENT_UNFULFILLABLE_REQUEST: { |
| 2721 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.sendingUid, |
| 2722 | /* callOnUnavailable */ true); |
| 2723 | break; |
| 2724 | } |
| 2725 | } |
| 2726 | return true; |
| 2727 | } |
| 2728 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2729 | @Override |
| 2730 | public void handleMessage(Message msg) { |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2731 | if (!maybeHandleAsyncChannelMessage(msg) |
| 2732 | && !maybeHandleNetworkMonitorMessage(msg) |
| 2733 | && !maybeHandleNetworkAgentInfoMessage(msg) |
| 2734 | && !maybeHandleNetworkFactoryMessage(msg)) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2735 | maybeHandleNetworkAgentMessage(msg); |
| 2736 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2740 | private class NetworkMonitorCallbacks extends INetworkMonitorCallbacks.Stub { |
| 2741 | private final NetworkAgentInfo mNai; |
| 2742 | |
| 2743 | private NetworkMonitorCallbacks(NetworkAgentInfo nai) { |
| 2744 | mNai = nai; |
| 2745 | } |
| 2746 | |
| 2747 | @Override |
| 2748 | public void onNetworkMonitorCreated(INetworkMonitor networkMonitor) { |
| 2749 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, |
| 2750 | new Pair<>(mNai, networkMonitor))); |
| 2751 | } |
| 2752 | |
| 2753 | @Override |
| 2754 | public void notifyNetworkTested(int testResult, @Nullable String redirectUrl) { |
| 2755 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage(EVENT_NETWORK_TESTED, |
| 2756 | testResult, mNai.network.netId, redirectUrl)); |
| 2757 | } |
| 2758 | |
| 2759 | @Override |
| 2760 | public void notifyPrivateDnsConfigResolved(PrivateDnsConfigParcel config) { |
| 2761 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2762 | EVENT_PRIVATE_DNS_CONFIG_RESOLVED, |
| 2763 | 0, mNai.network.netId, PrivateDnsConfig.fromParcel(config))); |
| 2764 | } |
| 2765 | |
| 2766 | @Override |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2767 | public void showProvisioningNotification(String action, String packageName) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2768 | final Intent intent = new Intent(action); |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2769 | intent.setPackage(packageName); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2770 | |
| 2771 | final PendingIntent pendingIntent; |
| 2772 | // Only the system server can register notifications with package "android" |
| 2773 | final long token = Binder.clearCallingIdentity(); |
| 2774 | try { |
| 2775 | pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 2776 | } finally { |
| 2777 | Binder.restoreCallingIdentity(token); |
| 2778 | } |
| 2779 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2780 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_SHOW, |
| 2781 | mNai.network.netId, |
| 2782 | pendingIntent)); |
| 2783 | } |
| 2784 | |
| 2785 | @Override |
| 2786 | public void hideProvisioningNotification() { |
| 2787 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2788 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_HIDE, |
| 2789 | mNai.network.netId)); |
| 2790 | } |
| 2791 | } |
| 2792 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2793 | private boolean networkRequiresValidation(NetworkAgentInfo nai) { |
Lorenzo Colitti | ed3168e | 2019-01-23 17:54:08 +0900 | [diff] [blame] | 2794 | return isValidationRequired(nai.networkCapabilities); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
Erik Kline | c6d0022 | 2018-06-26 18:53:43 +0900 | [diff] [blame] | 2797 | private void handleFreshlyValidatedNetwork(NetworkAgentInfo nai) { |
| 2798 | if (nai == null) return; |
| 2799 | // If the Private DNS mode is opportunistic, reprogram the DNS servers |
| 2800 | // in order to restart a validation pass from within netd. |
| 2801 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2802 | if (cfg.useTls && TextUtils.isEmpty(cfg.hostname)) { |
| 2803 | updateDnses(nai.linkProperties, null, nai.network.netId); |
| 2804 | } |
| 2805 | } |
| 2806 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2807 | private void handlePrivateDnsSettingsChanged() { |
| 2808 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2809 | |
| 2810 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2811 | handlePerNetworkPrivateDnsConfig(nai, cfg); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2812 | if (networkRequiresValidation(nai)) { |
| 2813 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2814 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2815 | } |
| 2816 | } |
| 2817 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2818 | private void handlePerNetworkPrivateDnsConfig(NetworkAgentInfo nai, PrivateDnsConfig cfg) { |
| 2819 | // Private DNS only ever applies to networks that might provide |
| 2820 | // Internet access and therefore also require validation. |
| 2821 | if (!networkRequiresValidation(nai)) return; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2822 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2823 | // Notify the NetworkAgentInfo/NetworkMonitor in case NetworkMonitor needs to cancel or |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2824 | // schedule DNS resolutions. If a DNS resolution is required the |
| 2825 | // result will be sent back to us. |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2826 | try { |
| 2827 | nai.networkMonitor().notifyPrivateDnsChanged(cfg.toParcel()); |
| 2828 | } catch (RemoteException e) { |
| 2829 | e.rethrowFromSystemServer(); |
| 2830 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2831 | |
| 2832 | // With Private DNS bypass support, we can proceed to update the |
| 2833 | // Private DNS config immediately, even if we're in strict mode |
| 2834 | // and have not yet resolved the provider name into a set of IPs. |
| 2835 | updatePrivateDns(nai, cfg); |
| 2836 | } |
| 2837 | |
| 2838 | private void updatePrivateDns(NetworkAgentInfo nai, PrivateDnsConfig newCfg) { |
| 2839 | mDnsManager.updatePrivateDns(nai.network, newCfg); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2840 | updateDnses(nai.linkProperties, null, nai.network.netId); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2841 | } |
| 2842 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2843 | private void handlePrivateDnsValidationUpdate(PrivateDnsValidationUpdate update) { |
| 2844 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(update.netId); |
| 2845 | if (nai == null) { |
| 2846 | return; |
| 2847 | } |
| 2848 | mDnsManager.updatePrivateDnsValidation(update); |
| 2849 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2850 | } |
| 2851 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 2852 | private void handleNat64PrefixEvent(int netId, boolean added, String prefixString, |
| 2853 | int prefixLength) { |
| 2854 | NetworkAgentInfo nai = mNetworkForNetId.get(netId); |
| 2855 | if (nai == null) return; |
| 2856 | |
| 2857 | log(String.format("NAT64 prefix %s on netId %d: %s/%d", |
| 2858 | (added ? "added" : "removed"), netId, prefixString, prefixLength)); |
| 2859 | |
| 2860 | IpPrefix prefix = null; |
| 2861 | if (added) { |
| 2862 | try { |
| 2863 | prefix = new IpPrefix(InetAddresses.parseNumericAddress(prefixString), |
| 2864 | prefixLength); |
| 2865 | } catch (IllegalArgumentException e) { |
| 2866 | loge("Invalid NAT64 prefix " + prefixString + "/" + prefixLength); |
| 2867 | return; |
| 2868 | } |
| 2869 | } |
| 2870 | |
| 2871 | nai.clatd.setNat64Prefix(prefix); |
| 2872 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2873 | } |
| 2874 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2875 | private void updateLingerState(NetworkAgentInfo nai, long now) { |
| 2876 | // 1. Update the linger timer. If it's changed, reschedule or cancel the alarm. |
| 2877 | // 2. If the network was lingering and there are now requests, unlinger it. |
| 2878 | // 3. If this network is unneeded (which implies it is not lingering), and there is at least |
| 2879 | // one lingered request, start lingering. |
| 2880 | nai.updateLingerTimer(); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2881 | if (nai.isLingering() && nai.numForegroundNetworkRequests() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2882 | if (DBG) log("Unlingering " + nai.name()); |
| 2883 | nai.unlinger(); |
| 2884 | logNetworkEvent(nai, NetworkEvent.NETWORK_UNLINGER); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2885 | } else if (unneeded(nai, UnneededFor.LINGER) && nai.getLingerExpiry() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2886 | int lingerTime = (int) (nai.getLingerExpiry() - now); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2887 | if (DBG) log("Lingering " + nai.name() + " for " + lingerTime + "ms"); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2888 | nai.linger(); |
| 2889 | logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER); |
| 2890 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING, lingerTime); |
| 2891 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2892 | } |
| 2893 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2894 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2895 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2896 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2897 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2898 | if (VDBG) log("NetworkFactory connected"); |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2899 | // Finish setting up the full connection |
| 2900 | mNetworkFactoryInfos.get(msg.replyTo).asyncChannel.sendMessage( |
| 2901 | AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2902 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2903 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 2904 | if (nri.request.isListen()) continue; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 2905 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 2906 | final int score; |
| 2907 | final int serial; |
| 2908 | if (nai != null) { |
| 2909 | score = nai.getCurrentScore(); |
| 2910 | serial = nai.factorySerialNumber; |
| 2911 | } else { |
| 2912 | score = 0; |
| 2913 | serial = NetworkFactory.SerialNumber.NONE; |
| 2914 | } |
| 2915 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, serial, |
| 2916 | nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2917 | } |
| 2918 | } else { |
| 2919 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2920 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2921 | } |
| 2922 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2923 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2924 | if (VDBG) log("NetworkAgent connected"); |
| 2925 | // A network agent has requested a connection. Establish the connection. |
| 2926 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2927 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2928 | } else { |
| 2929 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2930 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2931 | if (nai != null) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2932 | final boolean wasDefault = isDefaultNetwork(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2933 | synchronized (mNetworkForNetId) { |
| 2934 | mNetworkForNetId.remove(nai.network.netId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2935 | mNetIdInUse.delete(nai.network.netId); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2936 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2937 | // Just in case. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2938 | mLegacyTypeTracker.remove(nai, wasDefault); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2939 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2940 | } |
| 2941 | } |
| 2942 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2943 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2944 | // This is a no-op if it's called with a message designating a network that has |
| 2945 | // already been destroyed, because its reference will not be found in the relevant |
| 2946 | // maps. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2947 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2948 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2949 | if (nai != null) { |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2950 | disconnectAndDestroyNetwork(nai); |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2951 | } else { |
| 2952 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(msg.replyTo); |
| 2953 | if (DBG && nfi != null) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2954 | } |
| 2955 | } |
| 2956 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2957 | // Destroys a network, remove references to it from the internal state managed by |
| 2958 | // ConnectivityService, free its interfaces and clean up. |
| 2959 | // Must be called on the Handler thread. |
| 2960 | private void disconnectAndDestroyNetwork(NetworkAgentInfo nai) { |
| 2961 | if (DBG) { |
| 2962 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.numNetworkRequests()); |
| 2963 | } |
lucaslin | f80b83b | 2019-02-12 15:30:13 +0800 | [diff] [blame] | 2964 | // Clear all notifications of this network. |
| 2965 | mNotifier.clearNotification(nai.network.netId); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2966 | // A network agent has disconnected. |
| 2967 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2968 | // because they lost all their requests or because their score isn't good) |
| 2969 | // then they would disconnect organically, report their new state and then |
| 2970 | // disconnect the channel. |
| 2971 | if (nai.networkInfo.isConnected()) { |
| 2972 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2973 | null, null); |
| 2974 | } |
| 2975 | final boolean wasDefault = isDefaultNetwork(nai); |
| 2976 | if (wasDefault) { |
| 2977 | mDefaultInetConditionPublished = 0; |
| 2978 | // Log default network disconnection before required book-keeping. |
| 2979 | // Let rematchAllNetworksAndRequests() below record a new default network event |
| 2980 | // if there is a fallback. Taken together, the two form a X -> 0, 0 -> Y sequence |
| 2981 | // whose timestamps tell how long it takes to recover a default network. |
| 2982 | long now = SystemClock.elapsedRealtime(); |
| 2983 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent(now, null, nai); |
| 2984 | } |
| 2985 | notifyIfacesChangedForNetworkStats(); |
| 2986 | // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied |
| 2987 | // by other networks that are already connected. Perhaps that can be done by |
| 2988 | // sending all CALLBACK_LOST messages (for requests, not listens) at the end |
| 2989 | // of rematchAllNetworksAndRequests |
| 2990 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 2991 | mKeepaliveTracker.handleStopAllKeepalives(nai, SocketKeepalive.ERROR_INVALID_NETWORK); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2992 | for (String iface : nai.linkProperties.getAllInterfaceNames()) { |
| 2993 | // Disable wakeup packet monitoring for each interface. |
| 2994 | wakeupModifyInterface(iface, nai.networkCapabilities, false); |
| 2995 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2996 | try { |
| 2997 | nai.networkMonitor().notifyNetworkDisconnected(); |
| 2998 | } catch (RemoteException e) { |
| 2999 | e.rethrowFromSystemServer(); |
| 3000 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3001 | mNetworkAgentInfos.remove(nai.messenger); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 3002 | nai.clatd.update(); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3003 | synchronized (mNetworkForNetId) { |
| 3004 | // Remove the NetworkAgent, but don't mark the netId as |
| 3005 | // available until we've told netd to delete it below. |
| 3006 | mNetworkForNetId.remove(nai.network.netId); |
| 3007 | } |
| 3008 | // Remove all previously satisfied requests. |
| 3009 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 3010 | NetworkRequest request = nai.requestAt(i); |
| 3011 | NetworkAgentInfo currentNetwork = getNetworkForRequest(request.requestId); |
| 3012 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 3013 | clearNetworkForRequest(request.requestId); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3014 | sendUpdatedScoreToFactories(request, null); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | nai.clearLingerState(); |
| 3018 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 3019 | updateDataActivityTracking(null /* newNetwork */, nai); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3020 | notifyLockdownVpn(nai); |
| 3021 | ensureNetworkTransitionWakelock(nai.name()); |
| 3022 | } |
| 3023 | mLegacyTypeTracker.remove(nai, wasDefault); |
| 3024 | if (!nai.networkCapabilities.hasTransport(TRANSPORT_VPN)) { |
| 3025 | updateAllVpnsCapabilities(); |
| 3026 | } |
| 3027 | rematchAllNetworksAndRequests(null, 0); |
| 3028 | mLingerMonitor.noteDisconnect(nai); |
| 3029 | if (nai.created) { |
| 3030 | // Tell netd to clean up the configuration for this network |
| 3031 | // (routing rules, DNS, etc). |
| 3032 | // This may be slow as it requires a lot of netd shelling out to ip and |
| 3033 | // ip[6]tables to flush routes and remove the incoming packet mark rule, so do it |
| 3034 | // after we've rematched networks with requests which should make a potential |
| 3035 | // fallback network the default or requested a new network from the |
| 3036 | // NetworkFactories, so network traffic isn't interrupted for an unnecessarily |
| 3037 | // long time. |
| 3038 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 3039 | mNMS.removeNetwork(nai.network.netId); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3040 | } catch (Exception e) { |
| 3041 | loge("Exception removing network: " + e); |
| 3042 | } |
| 3043 | mDnsManager.removeNetwork(nai.network); |
| 3044 | } |
| 3045 | synchronized (mNetworkForNetId) { |
| 3046 | mNetIdInUse.delete(nai.network.netId); |
| 3047 | } |
| 3048 | } |
| 3049 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3050 | // If this method proves to be too slow then we can maintain a separate |
| 3051 | // pendingIntent => NetworkRequestInfo map. |
| 3052 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 3053 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 3054 | Intent intent = pendingIntent.getIntent(); |
| 3055 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 3056 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 3057 | if (existingPendingIntent != null && |
| 3058 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 3059 | return entry.getValue(); |
| 3060 | } |
| 3061 | } |
| 3062 | return null; |
| 3063 | } |
| 3064 | |
| 3065 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 3066 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 3067 | |
| 3068 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 3069 | if (existingRequest != null) { // remove the existing request. |
| 3070 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 3071 | + nri.request + " because their intents matched."); |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3072 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid(), |
| 3073 | /* callOnUnavailable */ false); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3074 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3075 | handleRegisterNetworkRequest(nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3076 | } |
| 3077 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3078 | private void handleRegisterNetworkRequest(NetworkRequestInfo nri) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3079 | mNetworkRequests.put(nri.request, nri); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3080 | mNetworkRequestInfoLogs.log("REGISTER " + nri); |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 3081 | if (nri.request.isListen()) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3082 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3083 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3084 | network.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3085 | updateSignalStrengthThresholds(network, "REGISTER", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3086 | } |
| 3087 | } |
| 3088 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 3089 | rematchAllNetworksAndRequests(null, 0); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3090 | if (nri.request.isRequest() && getNetworkForRequest(nri.request.requestId) == null) { |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3091 | sendUpdatedScoreToFactories(nri.request, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3092 | } |
| 3093 | } |
| 3094 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3095 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 3096 | int callingUid) { |
| 3097 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 3098 | if (nri != null) { |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3099 | handleReleaseNetworkRequest(nri.request, callingUid, /* callOnUnavailable */ false); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3100 | } |
| 3101 | } |
| 3102 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3103 | // Determines whether the network is the best (or could become the best, if it validated), for |
| 3104 | // none of a particular type of NetworkRequests. The type of NetworkRequests considered depends |
| 3105 | // on the value of reason: |
| 3106 | // |
| 3107 | // - UnneededFor.TEARDOWN: non-listen NetworkRequests. If a network is unneeded for this reason, |
| 3108 | // then it should be torn down. |
| 3109 | // - UnneededFor.LINGER: foreground NetworkRequests. If a network is unneeded for this reason, |
| 3110 | // then it should be lingered. |
| 3111 | private boolean unneeded(NetworkAgentInfo nai, UnneededFor reason) { |
| 3112 | final int numRequests; |
| 3113 | switch (reason) { |
| 3114 | case TEARDOWN: |
| 3115 | numRequests = nai.numRequestNetworkRequests(); |
| 3116 | break; |
| 3117 | case LINGER: |
| 3118 | numRequests = nai.numForegroundNetworkRequests(); |
| 3119 | break; |
| 3120 | default: |
| 3121 | Slog.wtf(TAG, "Invalid reason. Cannot happen."); |
| 3122 | return true; |
| 3123 | } |
| 3124 | |
| 3125 | if (!nai.everConnected || nai.isVPN() || nai.isLingering() || numRequests > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 3126 | return false; |
| 3127 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3128 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3129 | if (reason == UnneededFor.LINGER && nri.request.isBackgroundRequest()) { |
| 3130 | // Background requests don't affect lingering. |
| 3131 | continue; |
| 3132 | } |
| 3133 | |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3134 | // If this Network is already the highest scoring Network for a request, or if |
| 3135 | // 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] | 3136 | if (nri.request.isRequest() && nai.satisfies(nri.request) && |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 3137 | (nai.isSatisfyingRequest(nri.request.requestId) || |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3138 | // Note that this catches two important cases: |
| 3139 | // 1. Unvalidated cellular will not be reaped when unvalidated WiFi |
| 3140 | // is currently satisfying the request. This is desirable when |
| 3141 | // cellular ends up validating but WiFi does not. |
| 3142 | // 2. Unvalidated WiFi will not be reaped when validated cellular |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 3143 | // is currently satisfying the request. This is desirable when |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3144 | // WiFi ends up validating and out scoring cellular. |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3145 | getNetworkForRequest(nri.request.requestId).getCurrentScore() < |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3146 | nai.getCurrentScoreAsValidated())) { |
| 3147 | return false; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3148 | } |
| 3149 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3150 | return true; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3151 | } |
| 3152 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3153 | private NetworkRequestInfo getNriForAppRequest( |
| 3154 | NetworkRequest request, int callingUid, String requestedOperation) { |
| 3155 | final NetworkRequestInfo nri = mNetworkRequests.get(request); |
| 3156 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3157 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 3158 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3159 | log(String.format("UID %d attempted to %s for unowned request %s", |
| 3160 | callingUid, requestedOperation, nri)); |
| 3161 | return null; |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3162 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3163 | } |
| 3164 | |
| 3165 | return nri; |
| 3166 | } |
| 3167 | |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3168 | private void handleTimedOutNetworkRequest(final NetworkRequestInfo nri) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3169 | if (mNetworkRequests.get(nri.request) == null) { |
| 3170 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3171 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3172 | if (getNetworkForRequest(nri.request.requestId) != null) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3173 | return; |
| 3174 | } |
| 3175 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3176 | log("releasing " + nri.request + " (timeout)"); |
| 3177 | } |
| 3178 | handleRemoveNetworkRequest(nri); |
| 3179 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_UNAVAIL, 0); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3180 | } |
| 3181 | |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3182 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid, |
| 3183 | boolean callOnUnavailable) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3184 | final NetworkRequestInfo nri = |
| 3185 | getNriForAppRequest(request, callingUid, "release NetworkRequest"); |
| 3186 | if (nri == null) { |
| 3187 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3188 | } |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3189 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3190 | log("releasing " + nri.request + " (release request)"); |
| 3191 | } |
| 3192 | handleRemoveNetworkRequest(nri); |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3193 | if (callOnUnavailable) { |
| 3194 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_UNAVAIL, 0); |
| 3195 | } |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3196 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3197 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3198 | private void handleRemoveNetworkRequest(final NetworkRequestInfo nri) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3199 | nri.unlinkDeathRecipient(); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3200 | mNetworkRequests.remove(nri.request); |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3201 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3202 | synchronized (mUidToNetworkRequestCount) { |
| 3203 | int requests = mUidToNetworkRequestCount.get(nri.mUid, 0); |
| 3204 | if (requests < 1) { |
| 3205 | Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + |
| 3206 | nri.mUid); |
| 3207 | } else if (requests == 1) { |
| 3208 | mUidToNetworkRequestCount.removeAt( |
| 3209 | mUidToNetworkRequestCount.indexOfKey(nri.mUid)); |
| 3210 | } else { |
| 3211 | mUidToNetworkRequestCount.put(nri.mUid, requests - 1); |
| 3212 | } |
| 3213 | } |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3214 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3215 | mNetworkRequestInfoLogs.log("RELEASE " + nri); |
| 3216 | if (nri.request.isRequest()) { |
| 3217 | boolean wasKept = false; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3218 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3219 | if (nai != null) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3220 | boolean wasBackgroundNetwork = nai.isBackgroundNetwork(); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3221 | nai.removeRequest(nri.request.requestId); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3222 | if (VDBG || DDBG) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3223 | log(" Removing from current network " + nai.name() + |
| 3224 | ", leaving " + nai.numNetworkRequests() + " requests."); |
| 3225 | } |
| 3226 | // If there are still lingered requests on this network, don't tear it down, |
| 3227 | // but resume lingering instead. |
| 3228 | updateLingerState(nai, SystemClock.elapsedRealtime()); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3229 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3230 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
| 3231 | teardownUnneededNetwork(nai); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3232 | } else { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3233 | wasKept = true; |
| 3234 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3235 | clearNetworkForRequest(nri.request.requestId); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3236 | if (!wasBackgroundNetwork && nai.isBackgroundNetwork()) { |
| 3237 | // Went from foreground to background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 3238 | updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3239 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3240 | } |
| 3241 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3242 | // Maintain the illusion. When this request arrived, we might have pretended |
| 3243 | // that a network connected to serve it, even though the network was already |
| 3244 | // connected. Now that this request has gone away, we might have to pretend |
| 3245 | // that the network disconnected. LegacyTypeTracker will generate that |
| 3246 | // phantom disconnect for this type. |
| 3247 | if (nri.request.legacyType != TYPE_NONE && nai != null) { |
| 3248 | boolean doRemove = true; |
| 3249 | if (wasKept) { |
| 3250 | // check if any of the remaining requests for this network are for the |
| 3251 | // same legacy type - if so, don't remove the nai |
| 3252 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 3253 | NetworkRequest otherRequest = nai.requestAt(i); |
| 3254 | if (otherRequest.legacyType == nri.request.legacyType && |
| 3255 | otherRequest.isRequest()) { |
| 3256 | if (DBG) log(" still have other legacy request - leaving"); |
| 3257 | doRemove = false; |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 3258 | } |
| 3259 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 3260 | } |
| 3261 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3262 | if (doRemove) { |
| 3263 | mLegacyTypeTracker.remove(nri.request.legacyType, nai, false); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3264 | } |
| 3265 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3266 | |
| 3267 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 3268 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 3269 | nri.request); |
| 3270 | } |
| 3271 | } else { |
| 3272 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 3273 | // if this listen request applies and remove it. |
| 3274 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 3275 | nai.removeRequest(nri.request.requestId); |
| 3276 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3277 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3278 | updateSignalStrengthThresholds(nai, "RELEASE", nri.request); |
| 3279 | } |
| 3280 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3281 | } |
| 3282 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3283 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3284 | @Override |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3285 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3286 | enforceNetworkStackSettingsOrSetup(); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3287 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED, |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3288 | encodeBool(accept), encodeBool(always), network)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3289 | } |
| 3290 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3291 | @Override |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3292 | public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) { |
| 3293 | enforceNetworkStackSettingsOrSetup(); |
| 3294 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY, |
| 3295 | encodeBool(accept), encodeBool(always), network)); |
| 3296 | } |
| 3297 | |
| 3298 | @Override |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3299 | public void setAvoidUnvalidated(Network network) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3300 | enforceNetworkStackSettingsOrSetup(); |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3301 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_AVOID_UNVALIDATED, network)); |
| 3302 | } |
| 3303 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3304 | private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 3305 | if (DBG) log("handleSetAcceptUnvalidated network=" + network + |
| 3306 | " accept=" + accept + " always=" + always); |
| 3307 | |
| 3308 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3309 | if (nai == null) { |
| 3310 | // Nothing to do. |
| 3311 | return; |
| 3312 | } |
| 3313 | |
| 3314 | if (nai.everValidated) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3315 | // The network validated while the dialog box was up. Take no action. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3316 | return; |
| 3317 | } |
| 3318 | |
| 3319 | if (!nai.networkMisc.explicitlySelected) { |
| 3320 | Slog.wtf(TAG, "BUG: setAcceptUnvalidated non non-explicitly selected network"); |
| 3321 | } |
| 3322 | |
| 3323 | if (accept != nai.networkMisc.acceptUnvalidated) { |
| 3324 | int oldScore = nai.getCurrentScore(); |
| 3325 | nai.networkMisc.acceptUnvalidated = accept; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3326 | // If network becomes partial connectivity and user already accepted to use this |
| 3327 | // network, we should respect the user's option and don't need to popup the |
| 3328 | // PARTIAL_CONNECTIVITY notification to user again. |
| 3329 | nai.networkMisc.acceptPartialConnectivity = accept; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 3330 | rematchAllNetworksAndRequests(nai, oldScore); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3331 | sendUpdatedScoreToFactories(nai); |
| 3332 | } |
| 3333 | |
| 3334 | if (always) { |
| 3335 | nai.asyncChannel.sendMessage( |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3336 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, encodeBool(accept)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3337 | } |
| 3338 | |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3339 | if (!accept) { |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3340 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 3341 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3342 | // Teardown the network. |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3343 | teardownUnneededNetwork(nai); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3344 | } |
| 3345 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3346 | } |
| 3347 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3348 | private void handleSetAcceptPartialConnectivity(Network network, boolean accept, |
| 3349 | boolean always) { |
| 3350 | if (DBG) { |
| 3351 | log("handleSetAcceptPartialConnectivity network=" + network + " accept=" + accept |
| 3352 | + " always=" + always); |
| 3353 | } |
| 3354 | |
| 3355 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3356 | if (nai == null) { |
| 3357 | // Nothing to do. |
| 3358 | return; |
| 3359 | } |
| 3360 | |
| 3361 | if (nai.lastValidated) { |
| 3362 | // The network validated while the dialog box was up. Take no action. |
| 3363 | return; |
| 3364 | } |
| 3365 | |
| 3366 | if (accept != nai.networkMisc.acceptPartialConnectivity) { |
| 3367 | nai.networkMisc.acceptPartialConnectivity = accept; |
| 3368 | } |
| 3369 | |
| 3370 | // TODO: Use the current design or save the user choice into IpMemoryStore. |
| 3371 | if (always) { |
| 3372 | nai.asyncChannel.sendMessage( |
| 3373 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, encodeBool(accept)); |
| 3374 | } |
| 3375 | |
| 3376 | if (!accept) { |
| 3377 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 3378 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 3379 | // Tear down the network. |
| 3380 | teardownUnneededNetwork(nai); |
| 3381 | } else { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3382 | // Inform NetworkMonitor that partial connectivity is acceptable. This will likely |
| 3383 | // result in a partial connectivity result which will be processed by |
| 3384 | // maybeHandleNetworkMonitorMessage. |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3385 | try { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3386 | nai.networkMonitor().setAcceptPartialConnectivity(); |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3387 | } catch (RemoteException e) { |
| 3388 | e.rethrowFromSystemServer(); |
| 3389 | } |
| 3390 | } |
| 3391 | } |
| 3392 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3393 | private void handleSetAvoidUnvalidated(Network network) { |
| 3394 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3395 | if (nai == null || nai.lastValidated) { |
| 3396 | // Nothing to do. The network either disconnected or revalidated. |
| 3397 | return; |
| 3398 | } |
| 3399 | if (!nai.avoidUnvalidated) { |
| 3400 | int oldScore = nai.getCurrentScore(); |
| 3401 | nai.avoidUnvalidated = true; |
| 3402 | rematchAllNetworksAndRequests(nai, oldScore); |
| 3403 | sendUpdatedScoreToFactories(nai); |
| 3404 | } |
| 3405 | } |
| 3406 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3407 | private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3408 | if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3409 | mHandler.sendMessageDelayed( |
| 3410 | mHandler.obtainMessage(EVENT_PROMPT_UNVALIDATED, nai.network), |
| 3411 | PROMPT_UNVALIDATED_DELAY_MS); |
| 3412 | } |
| 3413 | |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3414 | @Override |
| 3415 | public void startCaptivePortalApp(Network network) { |
| 3416 | enforceConnectivityInternalPermission(); |
| 3417 | mHandler.post(() -> { |
| 3418 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3419 | if (nai == null) return; |
| 3420 | if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3421 | try { |
| 3422 | nai.networkMonitor().launchCaptivePortalApp(); |
| 3423 | } catch (RemoteException e) { |
| 3424 | e.rethrowFromSystemServer(); |
| 3425 | } |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3426 | }); |
| 3427 | } |
| 3428 | |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3429 | /** |
| 3430 | * NetworkStack endpoint to start the captive portal app. The NetworkStack needs to use this |
| 3431 | * endpoint as it does not have INTERACT_ACROSS_USERS_FULL itself. |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3432 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3433 | * @param appExtras Bundle to use as intent extras for the captive portal application. |
| 3434 | * Must be treated as opaque to avoid preventing the captive portal app to |
| 3435 | * update its arguments. |
| 3436 | */ |
| 3437 | @Override |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3438 | public void startCaptivePortalAppInternal(Network network, Bundle appExtras) { |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3439 | mContext.checkCallingOrSelfPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
| 3440 | |
| 3441 | final Intent appIntent = new Intent(ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN); |
| 3442 | appIntent.putExtras(appExtras); |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3443 | appIntent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL, |
| 3444 | new CaptivePortal(new CaptivePortalImpl(network).asBinder())); |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3445 | appIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3446 | |
| 3447 | Binder.withCleanCallingIdentity(() -> |
| 3448 | mContext.startActivityAsUser(appIntent, UserHandle.CURRENT)); |
| 3449 | } |
| 3450 | |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3451 | private class CaptivePortalImpl extends ICaptivePortal.Stub { |
| 3452 | private final Network mNetwork; |
| 3453 | |
| 3454 | private CaptivePortalImpl(Network network) { |
| 3455 | mNetwork = network; |
| 3456 | } |
| 3457 | |
| 3458 | @Override |
| 3459 | public void appResponse(final int response) throws RemoteException { |
| 3460 | if (response == CaptivePortal.APP_RETURN_WANTED_AS_IS) { |
| 3461 | enforceSettingsPermission(); |
| 3462 | } |
| 3463 | |
| 3464 | // getNetworkAgentInfoForNetwork is thread-safe |
| 3465 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(mNetwork); |
| 3466 | if (nai == null) return; |
| 3467 | |
| 3468 | // nai.networkMonitor() is thread-safe |
| 3469 | final INetworkMonitor nm = nai.networkMonitor(); |
| 3470 | if (nm == null) return; |
| 3471 | |
| 3472 | final long token = Binder.clearCallingIdentity(); |
| 3473 | try { |
| 3474 | nm.notifyCaptivePortalAppFinished(response); |
| 3475 | } finally { |
| 3476 | // Not using Binder.withCleanCallingIdentity() to keep the checked RemoteException |
| 3477 | Binder.restoreCallingIdentity(token); |
| 3478 | } |
| 3479 | } |
| 3480 | |
| 3481 | @Override |
| 3482 | public void logEvent(int eventId, String packageName) { |
| 3483 | enforceSettingsPermission(); |
| 3484 | |
| 3485 | new MetricsLogger().action(eventId, packageName); |
| 3486 | } |
| 3487 | } |
| 3488 | |
Hugo Benichi | c8e9e12 | 2016-09-14 23:23:08 +0000 | [diff] [blame] | 3489 | public boolean avoidBadWifi() { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3490 | return mMultinetworkPolicyTracker.getAvoidBadWifi(); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 3491 | } |
| 3492 | |
Remi NGUYEN VAN | e2365d6 | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 3493 | /** |
| 3494 | * Return whether the device should maintain continuous, working connectivity by switching away |
| 3495 | * from WiFi networks having no connectivity. |
| 3496 | * @see MultinetworkPolicyTracker#getAvoidBadWifi() |
| 3497 | */ |
| 3498 | public boolean shouldAvoidBadWifi() { |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 3499 | if (!checkNetworkStackPermission()) { |
| 3500 | throw new SecurityException("avoidBadWifi requires NETWORK_STACK permission"); |
| 3501 | } |
| 3502 | return avoidBadWifi(); |
| 3503 | } |
| 3504 | |
| 3505 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3506 | private void rematchForAvoidBadWifiUpdate() { |
| 3507 | rematchAllNetworksAndRequests(null, 0); |
| 3508 | for (NetworkAgentInfo nai: mNetworkAgentInfos.values()) { |
| 3509 | if (nai.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 3510 | sendUpdatedScoreToFactories(nai); |
| 3511 | } |
| 3512 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3513 | } |
| 3514 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3515 | // TODO: Evaluate whether this is of interest to other consumers of |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3516 | // MultinetworkPolicyTracker and worth moving out of here. |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3517 | private void dumpAvoidBadWifiSettings(IndentingPrintWriter pw) { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3518 | final boolean configRestrict = mMultinetworkPolicyTracker.configRestrictsAvoidBadWifi(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3519 | if (!configRestrict) { |
| 3520 | pw.println("Bad Wi-Fi avoidance: unrestricted"); |
| 3521 | return; |
| 3522 | } |
| 3523 | |
| 3524 | pw.println("Bad Wi-Fi avoidance: " + avoidBadWifi()); |
| 3525 | pw.increaseIndent(); |
| 3526 | pw.println("Config restrict: " + configRestrict); |
| 3527 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3528 | final String value = mMultinetworkPolicyTracker.getAvoidBadWifiSetting(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3529 | String description; |
| 3530 | // Can't use a switch statement because strings are legal case labels, but null is not. |
| 3531 | if ("0".equals(value)) { |
| 3532 | description = "get stuck"; |
| 3533 | } else if (value == null) { |
| 3534 | description = "prompt"; |
| 3535 | } else if ("1".equals(value)) { |
| 3536 | description = "avoid"; |
| 3537 | } else { |
| 3538 | description = value + " (?)"; |
| 3539 | } |
| 3540 | pw.println("User setting: " + description); |
| 3541 | pw.println("Network overrides:"); |
| 3542 | pw.increaseIndent(); |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 3543 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3544 | if (nai.avoidUnvalidated) { |
| 3545 | pw.println(nai.name()); |
| 3546 | } |
| 3547 | } |
| 3548 | pw.decreaseIndent(); |
| 3549 | pw.decreaseIndent(); |
| 3550 | } |
| 3551 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3552 | private void showNetworkNotification(NetworkAgentInfo nai, NotificationType type) { |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3553 | final String action; |
| 3554 | switch (type) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3555 | case LOGGED_IN: |
| 3556 | action = Settings.ACTION_WIFI_SETTINGS; |
| 3557 | mHandler.removeMessages(EVENT_TIMEOUT_NOTIFICATION); |
| 3558 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NOTIFICATION, |
| 3559 | nai.network.netId, 0), TIMEOUT_NOTIFICATION_DELAY_MS); |
| 3560 | break; |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3561 | case NO_INTERNET: |
| 3562 | action = ConnectivityManager.ACTION_PROMPT_UNVALIDATED; |
| 3563 | break; |
| 3564 | case LOST_INTERNET: |
| 3565 | action = ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION; |
| 3566 | break; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3567 | case PARTIAL_CONNECTIVITY: |
| 3568 | action = ConnectivityManager.ACTION_PROMPT_PARTIAL_CONNECTIVITY; |
| 3569 | break; |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3570 | default: |
| 3571 | Slog.wtf(TAG, "Unknown notification type " + type); |
| 3572 | return; |
| 3573 | } |
| 3574 | |
| 3575 | Intent intent = new Intent(action); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3576 | if (type != NotificationType.LOGGED_IN) { |
| 3577 | intent.setData(Uri.fromParts("netId", Integer.toString(nai.network.netId), null)); |
| 3578 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3579 | intent.setClassName("com.android.settings", |
| 3580 | "com.android.settings.wifi.WifiNoInternetDialog"); |
| 3581 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3582 | |
| 3583 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 3584 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 3585 | mNotifier.showNotification(nai.network.netId, type, nai, null, pendingIntent, true); |
| 3586 | } |
| 3587 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3588 | private void handlePromptUnvalidated(Network network) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3589 | if (VDBG || DDBG) log("handlePromptUnvalidated " + network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3590 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3591 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3592 | // Only prompt if the network is unvalidated or network has partial internet connectivity |
| 3593 | // and was explicitly selected by the user, and if we haven't already been told to switch |
| 3594 | // to it regardless of whether it validated or not. Also don't prompt on captive portals |
| 3595 | // because we're already prompting the user to sign in. |
| 3596 | if (nai == null || nai.everValidated || nai.everCaptivePortalDetected |
| 3597 | || !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3598 | // TODO: Once the value of acceptPartialConnectivity is moved to IpMemoryStore, |
| 3599 | // we should reevaluate how to handle acceptPartialConnectivity when network just |
| 3600 | // connected. |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3601 | || nai.networkMisc.acceptPartialConnectivity) { |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3602 | return; |
| 3603 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3604 | // TODO: Evaluate if it's needed to wait 8 seconds for triggering notification when |
| 3605 | // NetworkMonitor detects the network is partial connectivity. Need to change the design to |
| 3606 | // popup the notification immediately when the network is partial connectivity. |
| 3607 | if (nai.partialConnectivity) { |
lucaslin | 975b7d3 | 2019-03-21 11:59:22 +0800 | [diff] [blame] | 3608 | showNetworkNotification(nai, NotificationType.PARTIAL_CONNECTIVITY); |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3609 | } else { |
| 3610 | showNetworkNotification(nai, NotificationType.NO_INTERNET); |
| 3611 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3612 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3613 | |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3614 | private void handleNetworkUnvalidated(NetworkAgentInfo nai) { |
| 3615 | NetworkCapabilities nc = nai.networkCapabilities; |
| 3616 | if (DBG) log("handleNetworkUnvalidated " + nai.name() + " cap=" + nc); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 3617 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3618 | if (!nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 3619 | return; |
| 3620 | } |
| 3621 | |
| 3622 | if (mMultinetworkPolicyTracker.shouldNotifyWifiUnvalidated()) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3623 | showNetworkNotification(nai, NotificationType.LOST_INTERNET); |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3624 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3625 | } |
| 3626 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3627 | @Override |
| 3628 | public int getMultipathPreference(Network network) { |
| 3629 | enforceAccessPermission(); |
| 3630 | |
| 3631 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3632 | if (nai != null && nai.networkCapabilities |
| 3633 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3634 | return ConnectivityManager.MULTIPATH_PREFERENCE_UNMETERED; |
| 3635 | } |
| 3636 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3637 | Integer networkPreference = mMultipathPolicyTracker.getMultipathPreference(network); |
| 3638 | if (networkPreference != null) { |
| 3639 | return networkPreference; |
| 3640 | } |
| 3641 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3642 | return mMultinetworkPolicyTracker.getMeteredMultipathPreference(); |
| 3643 | } |
| 3644 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3645 | @Override |
| 3646 | public NetworkRequest getDefaultRequest() { |
| 3647 | return mDefaultRequest; |
| 3648 | } |
| 3649 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3650 | private class InternalHandler extends Handler { |
| 3651 | public InternalHandler(Looper looper) { |
| 3652 | super(looper); |
| 3653 | } |
| 3654 | |
| 3655 | @Override |
| 3656 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3657 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3658 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3659 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3660 | handleReleaseNetworkTransitionWakelock(msg.what); |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3661 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3662 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3663 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 3664 | mProxyTracker.loadDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3665 | break; |
| 3666 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3667 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3668 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3669 | break; |
| 3670 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3671 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3672 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 3673 | break; |
| 3674 | } |
| 3675 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 3676 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3677 | break; |
| 3678 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3679 | case EVENT_REGISTER_NETWORK_AGENT: { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3680 | final Pair<NetworkAgentInfo, INetworkMonitor> arg = |
| 3681 | (Pair<NetworkAgentInfo, INetworkMonitor>) msg.obj; |
| 3682 | handleRegisterNetworkAgent(arg.first, arg.second); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3683 | break; |
| 3684 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3685 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 3686 | case EVENT_REGISTER_NETWORK_LISTENER: { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3687 | handleRegisterNetworkRequest((NetworkRequestInfo) msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3688 | break; |
| 3689 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3690 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: |
| 3691 | case EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT: { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3692 | handleRegisterNetworkRequestWithIntent(msg); |
| 3693 | break; |
| 3694 | } |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3695 | case EVENT_TIMEOUT_NETWORK_REQUEST: { |
| 3696 | NetworkRequestInfo nri = (NetworkRequestInfo) msg.obj; |
| 3697 | handleTimedOutNetworkRequest(nri); |
| 3698 | break; |
| 3699 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3700 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 3701 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 3702 | break; |
| 3703 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3704 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3705 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1, |
| 3706 | /* callOnUnavailable */ false); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3707 | break; |
| 3708 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3709 | case EVENT_SET_ACCEPT_UNVALIDATED: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3710 | Network network = (Network) msg.obj; |
| 3711 | handleSetAcceptUnvalidated(network, toBool(msg.arg1), toBool(msg.arg2)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3712 | break; |
| 3713 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3714 | case EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY: { |
| 3715 | Network network = (Network) msg.obj; |
| 3716 | handleSetAcceptPartialConnectivity(network, toBool(msg.arg1), |
| 3717 | toBool(msg.arg2)); |
| 3718 | break; |
| 3719 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3720 | case EVENT_SET_AVOID_UNVALIDATED: { |
| 3721 | handleSetAvoidUnvalidated((Network) msg.obj); |
| 3722 | break; |
| 3723 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3724 | case EVENT_PROMPT_UNVALIDATED: { |
| 3725 | handlePromptUnvalidated((Network) msg.obj); |
| 3726 | break; |
| 3727 | } |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 3728 | case EVENT_CONFIGURE_ALWAYS_ON_NETWORKS: { |
| 3729 | handleConfigureAlwaysOnNetworks(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3730 | break; |
| 3731 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3732 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3733 | case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3734 | mKeepaliveTracker.handleStartKeepalive(msg); |
| 3735 | break; |
| 3736 | } |
| 3737 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3738 | case NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3739 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); |
| 3740 | int slot = msg.arg1; |
| 3741 | int reason = msg.arg2; |
| 3742 | mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); |
| 3743 | break; |
| 3744 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3745 | case EVENT_SYSTEM_READY: { |
| 3746 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3747 | // Might have been called already in handleRegisterNetworkAgent since |
| 3748 | // mSystemReady is set before sending EVENT_SYSTEM_READY, but calling |
| 3749 | // this several times is fine. |
| 3750 | try { |
| 3751 | nai.networkMonitor().notifySystemReady(); |
| 3752 | } catch (RemoteException e) { |
| 3753 | e.rethrowFromSystemServer(); |
| 3754 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3755 | } |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3756 | mMultipathPolicyTracker.start(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3757 | break; |
| 3758 | } |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3759 | case EVENT_REVALIDATE_NETWORK: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3760 | handleReportNetworkConnectivity((Network) msg.obj, msg.arg1, toBool(msg.arg2)); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3761 | break; |
| 3762 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 3763 | case EVENT_PRIVATE_DNS_SETTINGS_CHANGED: |
| 3764 | handlePrivateDnsSettingsChanged(); |
| 3765 | break; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 3766 | case EVENT_PRIVATE_DNS_VALIDATION_UPDATE: |
| 3767 | handlePrivateDnsValidationUpdate( |
| 3768 | (PrivateDnsValidationUpdate) msg.obj); |
| 3769 | break; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3770 | case EVENT_UID_RULES_CHANGED: |
| 3771 | handleUidRulesChanged(msg.arg1, msg.arg2); |
| 3772 | break; |
| 3773 | case EVENT_DATA_SAVER_CHANGED: |
| 3774 | handleRestrictBackgroundChanged(toBool(msg.arg1)); |
| 3775 | break; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3776 | case EVENT_TIMEOUT_NOTIFICATION: |
| 3777 | mNotifier.clearNotification(msg.arg1, NotificationType.LOGGED_IN); |
| 3778 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3779 | } |
| 3780 | } |
| 3781 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3782 | |
| 3783 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3784 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3785 | public int tether(String iface, String callerPkg) { |
| 3786 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3787 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3788 | return mTethering.tether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3789 | } else { |
| 3790 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3791 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3792 | } |
| 3793 | |
| 3794 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3795 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3796 | public int untether(String iface, String callerPkg) { |
| 3797 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3798 | |
| 3799 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3800 | return mTethering.untether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3801 | } else { |
| 3802 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3803 | } |
| 3804 | } |
| 3805 | |
| 3806 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3807 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3808 | public int getLastTetherError(String iface) { |
| 3809 | enforceTetherAccessPermission(); |
| 3810 | |
| 3811 | if (isTetheringSupported()) { |
| 3812 | return mTethering.getLastTetherError(iface); |
| 3813 | } else { |
| 3814 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3815 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3816 | } |
| 3817 | |
| 3818 | // TODO - proper iface API for selection by property, inspection, etc |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3819 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3820 | public String[] getTetherableUsbRegexs() { |
| 3821 | enforceTetherAccessPermission(); |
| 3822 | if (isTetheringSupported()) { |
| 3823 | return mTethering.getTetherableUsbRegexs(); |
| 3824 | } else { |
| 3825 | return new String[0]; |
| 3826 | } |
| 3827 | } |
| 3828 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3829 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3830 | public String[] getTetherableWifiRegexs() { |
| 3831 | enforceTetherAccessPermission(); |
| 3832 | if (isTetheringSupported()) { |
| 3833 | return mTethering.getTetherableWifiRegexs(); |
| 3834 | } else { |
| 3835 | return new String[0]; |
| 3836 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3837 | } |
| 3838 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3839 | @Override |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3840 | public String[] getTetherableBluetoothRegexs() { |
| 3841 | enforceTetherAccessPermission(); |
| 3842 | if (isTetheringSupported()) { |
| 3843 | return mTethering.getTetherableBluetoothRegexs(); |
| 3844 | } else { |
| 3845 | return new String[0]; |
| 3846 | } |
| 3847 | } |
| 3848 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3849 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3850 | public int setUsbTethering(boolean enable, String callerPkg) { |
| 3851 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3852 | if (isTetheringSupported()) { |
| 3853 | return mTethering.setUsbTethering(enable); |
| 3854 | } else { |
| 3855 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3856 | } |
| 3857 | } |
| 3858 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3859 | // TODO - move iface listing, queries, etc to new module |
| 3860 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3861 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3862 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3863 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3864 | return mTethering.getTetherableIfaces(); |
| 3865 | } |
| 3866 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3867 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3868 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3869 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3870 | return mTethering.getTetheredIfaces(); |
| 3871 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3872 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3873 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3874 | public String[] getTetheringErroredIfaces() { |
| 3875 | enforceTetherAccessPermission(); |
| 3876 | return mTethering.getErroredIfaces(); |
| 3877 | } |
| 3878 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3879 | @Override |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 3880 | public String[] getTetheredDhcpRanges() { |
| 3881 | enforceConnectivityInternalPermission(); |
| 3882 | return mTethering.getTetheredDhcpRanges(); |
| 3883 | } |
| 3884 | |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3885 | @Override |
| 3886 | public boolean isTetheringSupported(String callerPkg) { |
| 3887 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3888 | return isTetheringSupported(); |
| 3889 | } |
| 3890 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3891 | // if ro.tether.denied = true we default to no tethering |
| 3892 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3893 | // had previously been turned off. |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3894 | private boolean isTetheringSupported() { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3895 | int defaultVal = encodeBool(!mSystemProperties.get("ro.tether.denied").equals("true")); |
| 3896 | boolean tetherSupported = toBool(Settings.Global.getInt(mContext.getContentResolver(), |
| 3897 | Settings.Global.TETHER_SUPPORTED, defaultVal)); |
| 3898 | boolean tetherEnabledInSettings = tetherSupported |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 3899 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Jeremy Klein | 246a1fe | 2017-03-17 14:30:02 -0700 | [diff] [blame] | 3900 | |
| 3901 | // Elevate to system UID to avoid caller requiring MANAGE_USERS permission. |
| 3902 | boolean adminUser = false; |
| 3903 | final long token = Binder.clearCallingIdentity(); |
| 3904 | try { |
| 3905 | adminUser = mUserManager.isAdminUser(); |
| 3906 | } finally { |
| 3907 | Binder.restoreCallingIdentity(token); |
| 3908 | } |
| 3909 | |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3910 | return tetherEnabledInSettings && adminUser && mTethering.hasTetherableConfiguration(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3911 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3912 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3913 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3914 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi, |
| 3915 | String callerPkg) { |
| 3916 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3917 | if (!isTetheringSupported()) { |
| 3918 | receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null); |
| 3919 | return; |
| 3920 | } |
| 3921 | mTethering.startTethering(type, receiver, showProvisioningUi); |
| 3922 | } |
| 3923 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3924 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3925 | public void stopTethering(int type, String callerPkg) { |
| 3926 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3927 | mTethering.stopTethering(type); |
| 3928 | } |
| 3929 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3930 | /** |
| 3931 | * Get the latest value of the tethering entitlement check. |
| 3932 | * |
| 3933 | * Note: Allow privileged apps who have TETHER_PRIVILEGED permission to access. If it turns |
| 3934 | * out some such apps are observed to abuse this API, change to per-UID limits on this API |
| 3935 | * if it's really needed. |
| 3936 | */ |
| 3937 | @Override |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3938 | public void getLatestTetheringEntitlementResult(int type, ResultReceiver receiver, |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3939 | boolean showEntitlementUi, String callerPkg) { |
| 3940 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3941 | mTethering.getLatestTetheringEntitlementResult(type, receiver, showEntitlementUi); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3942 | } |
| 3943 | |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 3944 | /** Register tethering event callback. */ |
| 3945 | @Override |
| 3946 | public void registerTetheringEventCallback(ITetheringEventCallback callback, |
| 3947 | String callerPkg) { |
| 3948 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3949 | mTethering.registerTetheringEventCallback(callback); |
| 3950 | } |
| 3951 | |
| 3952 | /** Unregister tethering event callback. */ |
| 3953 | @Override |
| 3954 | public void unregisterTetheringEventCallback(ITetheringEventCallback callback, |
| 3955 | String callerPkg) { |
| 3956 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3957 | mTethering.unregisterTetheringEventCallback(callback); |
| 3958 | } |
| 3959 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 3960 | // Called when we lose the default network and have no replacement yet. |
| 3961 | // This will automatically be cleared after X seconds or a new default network |
| 3962 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 3963 | // first caller and not restarted by subsequent callers. |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3964 | private void ensureNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3965 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3966 | if (mNetTransitionWakeLock.isHeld()) { |
| 3967 | return; |
| 3968 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3969 | mNetTransitionWakeLock.acquire(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 3970 | mLastWakeLockAcquireTimestamp = SystemClock.elapsedRealtime(); |
| 3971 | mTotalWakelockAcquisitions++; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3972 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3973 | mWakelockLogs.log("ACQUIRE for " + forWhom); |
| 3974 | Message msg = mHandler.obtainMessage(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 3975 | mHandler.sendMessageDelayed(msg, mNetTransitionWakeLockTimeout); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3976 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3977 | |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3978 | // Called when we gain a new default network to release the network transition wakelock in a |
| 3979 | // second, to allow a grace period for apps to reconnect over the new network. Pending expiry |
| 3980 | // message is cancelled. |
| 3981 | private void scheduleReleaseNetworkTransitionWakelock() { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3982 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3983 | if (!mNetTransitionWakeLock.isHeld()) { |
| 3984 | return; // expiry message released the lock first. |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3985 | } |
| 3986 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3987 | // Cancel self timeout on wakelock hold. |
| 3988 | mHandler.removeMessages(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 3989 | Message msg = mHandler.obtainMessage(EVENT_CLEAR_NET_TRANSITION_WAKELOCK); |
| 3990 | mHandler.sendMessageDelayed(msg, 1000); |
| 3991 | } |
| 3992 | |
| 3993 | // Called when either message of ensureNetworkTransitionWakelock or |
| 3994 | // scheduleReleaseNetworkTransitionWakelock is processed. |
| 3995 | private void handleReleaseNetworkTransitionWakelock(int eventId) { |
| 3996 | String event = eventName(eventId); |
| 3997 | synchronized (this) { |
| 3998 | if (!mNetTransitionWakeLock.isHeld()) { |
| 3999 | mWakelockLogs.log(String.format("RELEASE: already released (%s)", event)); |
| 4000 | Slog.w(TAG, "expected Net Transition WakeLock to be held"); |
| 4001 | return; |
| 4002 | } |
| 4003 | mNetTransitionWakeLock.release(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 4004 | long lockDuration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 4005 | mTotalWakelockDurationMs += lockDuration; |
| 4006 | mMaxWakelockDurationMs = Math.max(mMaxWakelockDurationMs, lockDuration); |
| 4007 | mTotalWakelockReleases++; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 4008 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 4009 | mWakelockLogs.log(String.format("RELEASE (%s)", event)); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 4010 | } |
| 4011 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 4012 | // 100 percent is full good, 0 is full bad. |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4013 | @Override |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 4014 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4015 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Lorenzo Colitti | 0831f66 | 2015-02-11 07:39:20 +0900 | [diff] [blame] | 4016 | if (nai == null) return; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4017 | reportNetworkConnectivity(nai.network, percentage > 50); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 4018 | } |
| 4019 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4020 | @Override |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4021 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4022 | enforceAccessPermission(); |
| 4023 | enforceInternetPermission(); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4024 | final int uid = Binder.getCallingUid(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 4025 | final int connectivityInfo = encodeBool(hasConnectivity); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4026 | mHandler.sendMessage( |
| 4027 | mHandler.obtainMessage(EVENT_REVALIDATE_NETWORK, uid, connectivityInfo, network)); |
| 4028 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4029 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4030 | private void handleReportNetworkConnectivity( |
| 4031 | Network network, int uid, boolean hasConnectivity) { |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4032 | final NetworkAgentInfo nai; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4033 | if (network == null) { |
| 4034 | nai = getDefaultNetwork(); |
| 4035 | } else { |
| 4036 | nai = getNetworkAgentInfoForNetwork(network); |
| 4037 | } |
Paul Jensen | 4e8050e | 2015-06-25 10:28:34 -0400 | [diff] [blame] | 4038 | if (nai == null || nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTING || |
| 4039 | nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTED) { |
| 4040 | return; |
| 4041 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4042 | // Revalidate if the app report does not match our current validated state. |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4043 | if (hasConnectivity == nai.lastValidated) { |
| 4044 | return; |
| 4045 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4046 | if (DBG) { |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4047 | int netid = nai.network.netId; |
| 4048 | log("reportNetworkConnectivity(" + netid + ", " + hasConnectivity + ") by " + uid); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4049 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4050 | // Validating a network that has not yet connected could result in a call to |
| 4051 | // rematchNetworkAndRequests() which is not meant to work on such networks. |
| 4052 | if (!nai.everConnected) { |
| 4053 | return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4054 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4055 | LinkProperties lp = getLinkProperties(nai); |
| 4056 | if (isNetworkWithLinkPropertiesBlocked(lp, uid, false)) { |
| 4057 | return; |
| 4058 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 4059 | try { |
| 4060 | nai.networkMonitor().forceReevaluation(uid); |
| 4061 | } catch (RemoteException e) { |
| 4062 | e.rethrowFromSystemServer(); |
| 4063 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4064 | } |
| 4065 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4066 | /** |
| 4067 | * Returns information about the proxy a certain network is using. If given a null network, it |
| 4068 | * it will return the proxy for the bound network for the caller app or the default proxy if |
| 4069 | * none. |
| 4070 | * |
| 4071 | * @param network the network we want to get the proxy information for. |
| 4072 | * @return Proxy information if a network has a proxy configured, or otherwise null. |
| 4073 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4074 | @Override |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4075 | public ProxyInfo getProxyForNetwork(Network network) { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4076 | final ProxyInfo globalProxy = mProxyTracker.getGlobalProxy(); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4077 | if (globalProxy != null) return globalProxy; |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4078 | if (network == null) { |
| 4079 | // Get the network associated with the calling UID. |
| 4080 | final Network activeNetwork = getActiveNetworkForUidInternal(Binder.getCallingUid(), |
| 4081 | true); |
| 4082 | if (activeNetwork == null) { |
| 4083 | return null; |
| 4084 | } |
| 4085 | return getLinkPropertiesProxyInfo(activeNetwork); |
| 4086 | } else if (queryUserAccess(Binder.getCallingUid(), network.netId)) { |
| 4087 | // Don't call getLinkProperties() as it requires ACCESS_NETWORK_STATE permission, which |
| 4088 | // caller may not have. |
| 4089 | return getLinkPropertiesProxyInfo(network); |
| 4090 | } |
| 4091 | // No proxy info available if the calling UID does not have network access. |
| 4092 | return null; |
| 4093 | } |
| 4094 | |
| 4095 | @VisibleForTesting |
| 4096 | protected boolean queryUserAccess(int uid, int netId) { |
| 4097 | return NetworkUtils.queryUserAccess(uid, netId); |
| 4098 | } |
| 4099 | |
| 4100 | private ProxyInfo getLinkPropertiesProxyInfo(Network network) { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4101 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 4102 | if (nai == null) return null; |
| 4103 | synchronized (nai) { |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4104 | final ProxyInfo linkHttpProxy = nai.linkProperties.getHttpProxy(); |
| 4105 | return linkHttpProxy == null ? null : new ProxyInfo(linkHttpProxy); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4106 | } |
| 4107 | } |
| 4108 | |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 4109 | @Override |
| 4110 | public void setGlobalProxy(final ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 4111 | enforceConnectivityInternalPermission(); |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 4112 | mProxyTracker.setGlobalProxy(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4113 | } |
| 4114 | |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4115 | @Override |
| 4116 | @Nullable |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 4117 | public ProxyInfo getGlobalProxy() { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4118 | return mProxyTracker.getGlobalProxy(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4119 | } |
| 4120 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 4121 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 4122 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 4123 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4124 | proxy = null; |
| 4125 | } |
Chalard Jean | daab8f9 | 2018-06-08 12:20:15 +0900 | [diff] [blame] | 4126 | mProxyTracker.setDefaultProxy(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4129 | // If the proxy has changed from oldLp to newLp, resend proxy broadcast. This method gets called |
| 4130 | // when any network changes proxy. |
| 4131 | // TODO: Remove usage of broadcast extras as they are deprecated and not applicable in a |
| 4132 | // 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] | 4133 | private void updateProxy(LinkProperties newLp, LinkProperties oldLp) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4134 | ProxyInfo newProxyInfo = newLp == null ? null : newLp.getHttpProxy(); |
| 4135 | ProxyInfo oldProxyInfo = oldLp == null ? null : oldLp.getHttpProxy(); |
| 4136 | |
Chalard Jean | 4949dd7 | 2018-06-07 17:41:29 +0900 | [diff] [blame] | 4137 | if (!ProxyTracker.proxyInfoEqual(newProxyInfo, oldProxyInfo)) { |
Chalard Jean | b50a276 | 2018-06-08 19:46:44 +0900 | [diff] [blame] | 4138 | mProxyTracker.sendProxyBroadcast(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4139 | } |
| 4140 | } |
| 4141 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4142 | private static class SettingsObserver extends ContentObserver { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4143 | final private HashMap<Uri, Integer> mUriEventMap; |
| 4144 | final private Context mContext; |
| 4145 | final private Handler mHandler; |
| 4146 | |
| 4147 | SettingsObserver(Context context, Handler handler) { |
| 4148 | super(null); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4149 | mUriEventMap = new HashMap<>(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4150 | mContext = context; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4151 | mHandler = handler; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4152 | } |
| 4153 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4154 | void observe(Uri uri, int what) { |
| 4155 | mUriEventMap.put(uri, what); |
| 4156 | final ContentResolver resolver = mContext.getContentResolver(); |
| 4157 | resolver.registerContentObserver(uri, false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4158 | } |
| 4159 | |
| 4160 | @Override |
| 4161 | public void onChange(boolean selfChange) { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4162 | Slog.wtf(TAG, "Should never be reached."); |
| 4163 | } |
| 4164 | |
| 4165 | @Override |
| 4166 | public void onChange(boolean selfChange, Uri uri) { |
| 4167 | final Integer what = mUriEventMap.get(uri); |
| 4168 | if (what != null) { |
| 4169 | mHandler.obtainMessage(what.intValue()).sendToTarget(); |
| 4170 | } else { |
| 4171 | loge("No matching event to send for URI=" + uri); |
| 4172 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4173 | } |
| 4174 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4175 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 4176 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4177 | Slog.d(TAG, s); |
| 4178 | } |
| 4179 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 4180 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4181 | Slog.e(TAG, s); |
| 4182 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4183 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4184 | private static void loge(String s, Throwable t) { |
| 4185 | Slog.e(TAG, s, t); |
| 4186 | } |
| 4187 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4188 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 4189 | * Prepare for a VPN application. |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4190 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 4191 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 4192 | * |
| 4193 | * @param oldPackage Package name of the application which currently controls VPN, which will |
| 4194 | * be replaced. If there is no such application, this should should either be |
| 4195 | * {@code null} or {@link VpnConfig.LEGACY_VPN}. |
| 4196 | * @param newPackage Package name of the application which should gain control of VPN, or |
| 4197 | * {@code null} to disable. |
| 4198 | * @param userId User for whom to prepare the new VPN. |
| 4199 | * |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4200 | * @hide |
| 4201 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4202 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4203 | public boolean prepareVpn(@Nullable String oldPackage, @Nullable String newPackage, |
| 4204 | int userId) { |
| 4205 | enforceCrossUserPermission(userId); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4206 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4207 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4208 | throwIfLockdownEnabled(); |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4209 | Vpn vpn = mVpns.get(userId); |
| 4210 | if (vpn != null) { |
| 4211 | return vpn.prepare(oldPackage, newPackage); |
| 4212 | } else { |
| 4213 | return false; |
| 4214 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4215 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4216 | } |
| 4217 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4218 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4219 | * Set whether the VPN package has the ability to launch VPNs without user intervention. |
| 4220 | * This method is used by system-privileged apps. |
| 4221 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 4222 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 4223 | * |
| 4224 | * @param packageName The package for which authorization state should change. |
| 4225 | * @param userId User for whom {@code packageName} is installed. |
| 4226 | * @param authorized {@code true} if this app should be able to start a VPN connection without |
| 4227 | * explicit user approval, {@code false} if not. |
| 4228 | * |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4229 | * @hide |
| 4230 | */ |
| 4231 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4232 | public void setVpnPackageAuthorization(String packageName, int userId, boolean authorized) { |
| 4233 | enforceCrossUserPermission(userId); |
| 4234 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4235 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4236 | Vpn vpn = mVpns.get(userId); |
| 4237 | if (vpn != null) { |
| 4238 | vpn.setPackageAuthorization(packageName, authorized); |
| 4239 | } |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4240 | } |
| 4241 | } |
| 4242 | |
| 4243 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4244 | * Configure a TUN interface and return its file descriptor. Parameters |
| 4245 | * 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] | 4246 | * and not available in ConnectivityManager. Permissions are checked in |
| 4247 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4248 | * @hide |
| 4249 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4250 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4251 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4252 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4253 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4254 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4255 | return mVpns.get(user).establish(config); |
| 4256 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4259 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4260 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 4261 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4262 | */ |
| 4263 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4264 | public void startLegacyVpn(VpnProfile profile) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4265 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4266 | final LinkProperties egress = getActiveLinkProperties(); |
| 4267 | if (egress == null) { |
| 4268 | throw new IllegalStateException("Missing active network connection"); |
| 4269 | } |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4270 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4271 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4272 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 4273 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4274 | } |
| 4275 | |
| 4276 | /** |
| 4277 | * Return the information of the ongoing legacy VPN. This method is used |
| 4278 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 4279 | * are checked in Vpn class. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4280 | */ |
| 4281 | @Override |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4282 | public LegacyVpnInfo getLegacyVpnInfo(int userId) { |
| 4283 | enforceCrossUserPermission(userId); |
Hung-ying Tyan | 44c8c5c | 2015-07-29 12:39:21 +0800 | [diff] [blame] | 4284 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4285 | synchronized (mVpns) { |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4286 | return mVpns.get(userId).getLegacyVpnInfo(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4287 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4290 | /** |
Varun Anand | d33cbc6 | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 4291 | * Return the information of all ongoing VPNs. |
| 4292 | * |
| 4293 | * <p>This method is used to update NetworkStatsService. |
| 4294 | * |
| 4295 | * <p>Must be called on the handler thread. |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4296 | */ |
Varun Anand | d33cbc6 | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 4297 | private VpnInfo[] getAllVpnInfo() { |
| 4298 | ensureRunningOnConnectivityServiceThread(); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4299 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4300 | if (mLockdownEnabled) { |
| 4301 | return new VpnInfo[0]; |
| 4302 | } |
| 4303 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4304 | List<VpnInfo> infoList = new ArrayList<>(); |
| 4305 | for (int i = 0; i < mVpns.size(); i++) { |
| 4306 | VpnInfo info = createVpnInfo(mVpns.valueAt(i)); |
| 4307 | if (info != null) { |
| 4308 | infoList.add(info); |
| 4309 | } |
| 4310 | } |
| 4311 | return infoList.toArray(new VpnInfo[infoList.size()]); |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | /** |
| 4316 | * @return VPN information for accounting, or null if we can't retrieve all required |
| 4317 | * information, e.g primary underlying iface. |
| 4318 | */ |
| 4319 | @Nullable |
| 4320 | private VpnInfo createVpnInfo(Vpn vpn) { |
| 4321 | VpnInfo info = vpn.getVpnInfo(); |
| 4322 | if (info == null) { |
| 4323 | return null; |
| 4324 | } |
| 4325 | Network[] underlyingNetworks = vpn.getUnderlyingNetworks(); |
| 4326 | // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret |
| 4327 | // the underlyingNetworks list. |
| 4328 | if (underlyingNetworks == null) { |
| 4329 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 4330 | if (defaultNetwork != null && defaultNetwork.linkProperties != null) { |
| 4331 | info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName(); |
| 4332 | } |
| 4333 | } else if (underlyingNetworks.length > 0) { |
| 4334 | LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]); |
| 4335 | if (linkProperties != null) { |
| 4336 | info.primaryUnderlyingIface = linkProperties.getInterfaceName(); |
| 4337 | } |
| 4338 | } |
| 4339 | return info.primaryUnderlyingIface == null ? null : info; |
| 4340 | } |
| 4341 | |
| 4342 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4343 | * Returns the information of the ongoing VPN for {@code userId}. This method is used by |
| 4344 | * VpnDialogs and not available in ConnectivityManager. |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4345 | * Permissions are checked in Vpn class. |
| 4346 | * @hide |
| 4347 | */ |
| 4348 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4349 | public VpnConfig getVpnConfig(int userId) { |
| 4350 | enforceCrossUserPermission(userId); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4351 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4352 | Vpn vpn = mVpns.get(userId); |
| 4353 | if (vpn != null) { |
| 4354 | return vpn.getVpnConfig(); |
| 4355 | } else { |
| 4356 | return null; |
| 4357 | } |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4358 | } |
| 4359 | } |
| 4360 | |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4361 | /** |
| 4362 | * Ask all VPN objects to recompute and update their capabilities. |
| 4363 | * |
| 4364 | * When underlying networks change, VPNs may have to update capabilities to reflect things |
| 4365 | * like the metered bit, their transports, and so on. This asks the VPN objects to update |
| 4366 | * their capabilities, and as this will cause them to send messages to the ConnectivityService |
| 4367 | * handler thread through their agent, this is asynchronous. When the capabilities objects |
| 4368 | * are computed they will be up-to-date as they are computed synchronously from here and |
| 4369 | * this is running on the ConnectivityService thread. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4370 | */ |
| 4371 | private void updateAllVpnsCapabilities() { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4372 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4373 | synchronized (mVpns) { |
| 4374 | for (int i = 0; i < mVpns.size(); i++) { |
| 4375 | final Vpn vpn = mVpns.valueAt(i); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4376 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4377 | updateVpnCapabilities(vpn, nc); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4378 | } |
| 4379 | } |
| 4380 | } |
| 4381 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4382 | private void updateVpnCapabilities(Vpn vpn, @Nullable NetworkCapabilities nc) { |
| 4383 | ensureRunningOnConnectivityServiceThread(); |
| 4384 | NetworkAgentInfo vpnNai = getNetworkAgentInfoForNetId(vpn.getNetId()); |
| 4385 | if (vpnNai == null || nc == null) { |
| 4386 | return; |
| 4387 | } |
| 4388 | updateCapabilities(vpnNai.getCurrentScore(), vpnNai, nc); |
| 4389 | } |
| 4390 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4391 | @Override |
| 4392 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 4393 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4394 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 4395 | return false; |
| 4396 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4397 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4398 | synchronized (mVpns) { |
| 4399 | // Tear down existing lockdown if profile was removed |
| 4400 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 4401 | if (mLockdownEnabled) { |
| 4402 | byte[] profileTag = mKeyStore.get(Credentials.LOCKDOWN_VPN); |
| 4403 | if (profileTag == null) { |
| 4404 | Slog.e(TAG, "Lockdown VPN configured but cannot be read from keystore"); |
| 4405 | return false; |
| 4406 | } |
| 4407 | String profileName = new String(profileTag); |
| 4408 | final VpnProfile profile = VpnProfile.decode( |
| 4409 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
| 4410 | if (profile == null) { |
| 4411 | Slog.e(TAG, "Lockdown VPN configured invalid profile " + profileName); |
| 4412 | setLockdownTracker(null); |
| 4413 | return true; |
| 4414 | } |
| 4415 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robin Lee | 18566c1 | 2016-03-14 13:08:48 +0000 | [diff] [blame] | 4416 | Vpn vpn = mVpns.get(user); |
| 4417 | if (vpn == null) { |
| 4418 | Slog.w(TAG, "VPN for user " + user + " not ready yet. Skipping lockdown"); |
| 4419 | return false; |
| 4420 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4421 | setLockdownTracker(new LockdownVpnTracker(mContext, mNMS, this, vpn, profile)); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4422 | } else { |
| 4423 | setLockdownTracker(null); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4424 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | return true; |
| 4428 | } |
| 4429 | |
| 4430 | /** |
| 4431 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4432 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4433 | */ |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4434 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4435 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4436 | // Shutdown any existing tracker |
| 4437 | final LockdownVpnTracker existing = mLockdownTracker; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4438 | // TODO: Add a trigger when the always-on VPN enable/disable to reevaluate and send the |
| 4439 | // necessary onBlockedStatusChanged callbacks. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4440 | mLockdownTracker = null; |
| 4441 | if (existing != null) { |
| 4442 | existing.shutdown(); |
| 4443 | } |
| 4444 | |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 4445 | if (tracker != null) { |
| 4446 | mLockdownTracker = tracker; |
| 4447 | mLockdownTracker.init(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4448 | } |
| 4449 | } |
| 4450 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4451 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4452 | private void throwIfLockdownEnabled() { |
| 4453 | if (mLockdownEnabled) { |
| 4454 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4455 | } |
| 4456 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4457 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4458 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4459 | * Starts the always-on VPN {@link VpnService} for user {@param userId}, which should perform |
| 4460 | * some setup and then call {@code establish()} to connect. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4461 | * |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4462 | * @return {@code true} if the service was started, the service was already connected, or there |
| 4463 | * was no always-on VPN to start. {@code false} otherwise. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4464 | */ |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4465 | private boolean startAlwaysOnVpn(int userId) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4466 | synchronized (mVpns) { |
| 4467 | Vpn vpn = mVpns.get(userId); |
| 4468 | if (vpn == null) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4469 | // 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] | 4470 | // exists already. |
| 4471 | Slog.wtf(TAG, "User " + userId + " has no Vpn configuration"); |
| 4472 | return false; |
| 4473 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4474 | |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 4475 | return vpn.startAlwaysOnVpn(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4476 | } |
| 4477 | } |
| 4478 | |
| 4479 | @Override |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 4480 | public boolean isAlwaysOnVpnPackageSupported(int userId, String packageName) { |
| 4481 | enforceSettingsPermission(); |
| 4482 | enforceCrossUserPermission(userId); |
| 4483 | |
| 4484 | synchronized (mVpns) { |
| 4485 | Vpn vpn = mVpns.get(userId); |
| 4486 | if (vpn == null) { |
| 4487 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4488 | return false; |
| 4489 | } |
| 4490 | return vpn.isAlwaysOnPackageSupported(packageName); |
| 4491 | } |
| 4492 | } |
| 4493 | |
| 4494 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4495 | public boolean setAlwaysOnVpnPackage( |
| 4496 | int userId, String packageName, boolean lockdown, List<String> lockdownWhitelist) { |
| 4497 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4498 | enforceCrossUserPermission(userId); |
| 4499 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4500 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4501 | // Can't set always-on VPN if legacy VPN is already in lockdown mode. |
| 4502 | if (LockdownVpnTracker.isEnabled()) { |
| 4503 | return false; |
| 4504 | } |
| 4505 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4506 | Vpn vpn = mVpns.get(userId); |
| 4507 | if (vpn == null) { |
| 4508 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4509 | return false; |
| 4510 | } |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4511 | if (!vpn.setAlwaysOnPackage(packageName, lockdown, lockdownWhitelist)) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4512 | return false; |
| 4513 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4514 | if (!startAlwaysOnVpn(userId)) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4515 | vpn.setAlwaysOnPackage(null, false, null); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4516 | return false; |
| 4517 | } |
| 4518 | } |
| 4519 | return true; |
| 4520 | } |
| 4521 | |
| 4522 | @Override |
| 4523 | public String getAlwaysOnVpnPackage(int userId) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4524 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4525 | enforceCrossUserPermission(userId); |
| 4526 | |
| 4527 | synchronized (mVpns) { |
| 4528 | Vpn vpn = mVpns.get(userId); |
| 4529 | if (vpn == null) { |
| 4530 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4531 | return null; |
| 4532 | } |
| 4533 | return vpn.getAlwaysOnPackage(); |
| 4534 | } |
| 4535 | } |
| 4536 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4537 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4538 | public boolean isVpnLockdownEnabled(int userId) { |
| 4539 | enforceControlAlwaysOnVpnPermission(); |
| 4540 | enforceCrossUserPermission(userId); |
| 4541 | |
| 4542 | synchronized (mVpns) { |
| 4543 | Vpn vpn = mVpns.get(userId); |
| 4544 | if (vpn == null) { |
| 4545 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4546 | return false; |
| 4547 | } |
| 4548 | return vpn.getLockdown(); |
| 4549 | } |
| 4550 | } |
| 4551 | |
| 4552 | @Override |
| 4553 | public List<String> getVpnLockdownWhitelist(int userId) { |
| 4554 | enforceControlAlwaysOnVpnPermission(); |
| 4555 | enforceCrossUserPermission(userId); |
| 4556 | |
| 4557 | synchronized (mVpns) { |
| 4558 | Vpn vpn = mVpns.get(userId); |
| 4559 | if (vpn == null) { |
| 4560 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4561 | return null; |
| 4562 | } |
| 4563 | return vpn.getLockdownWhitelist(); |
| 4564 | } |
| 4565 | } |
| 4566 | |
| 4567 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4568 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4569 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 4570 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4571 | } |
| 4572 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4573 | /** Location to an updatable file listing carrier provisioning urls. |
| 4574 | * An example: |
| 4575 | * |
| 4576 | * <?xml version="1.0" encoding="utf-8"?> |
| 4577 | * <provisioningUrls> |
| 4578 | * <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] | 4579 | * </provisioningUrls> |
| 4580 | */ |
| 4581 | private static final String PROVISIONING_URL_PATH = |
| 4582 | "/data/misc/radio/provisioning_urls.xml"; |
| 4583 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4584 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4585 | /** XML tag for root element. */ |
| 4586 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 4587 | /** XML tag for individual url */ |
| 4588 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4589 | /** XML attribute for mcc */ |
| 4590 | private static final String ATTR_MCC = "mcc"; |
| 4591 | /** XML attribute for mnc */ |
| 4592 | private static final String ATTR_MNC = "mnc"; |
| 4593 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4594 | private String getProvisioningUrlBaseFromFile() { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4595 | FileReader fileReader = null; |
| 4596 | XmlPullParser parser = null; |
| 4597 | Configuration config = mContext.getResources().getConfiguration(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4598 | |
| 4599 | try { |
| 4600 | fileReader = new FileReader(mProvisioningUrlFile); |
| 4601 | parser = Xml.newPullParser(); |
| 4602 | parser.setInput(fileReader); |
| 4603 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 4604 | |
| 4605 | while (true) { |
| 4606 | XmlUtils.nextElement(parser); |
| 4607 | |
| 4608 | String element = parser.getName(); |
| 4609 | if (element == null) break; |
| 4610 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4611 | if (element.equals(TAG_PROVISIONING_URL)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4612 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 4613 | try { |
| 4614 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 4615 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 4616 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 4617 | parser.next(); |
| 4618 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 4619 | return parser.getText(); |
| 4620 | } |
| 4621 | } |
| 4622 | } |
| 4623 | } catch (NumberFormatException e) { |
| 4624 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 4625 | } |
| 4626 | } |
| 4627 | } |
| 4628 | return null; |
| 4629 | } catch (FileNotFoundException e) { |
| 4630 | loge("Carrier Provisioning Urls file not found"); |
| 4631 | } catch (XmlPullParserException e) { |
| 4632 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 4633 | } catch (IOException e) { |
| 4634 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 4635 | } finally { |
| 4636 | if (fileReader != null) { |
| 4637 | try { |
| 4638 | fileReader.close(); |
| 4639 | } catch (IOException e) {} |
| 4640 | } |
| 4641 | } |
| 4642 | return null; |
| 4643 | } |
| 4644 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4645 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4646 | public String getMobileProvisioningUrl() { |
| 4647 | enforceConnectivityInternalPermission(); |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4648 | String url = getProvisioningUrlBaseFromFile(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4649 | if (TextUtils.isEmpty(url)) { |
| 4650 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4651 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4652 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4653 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4654 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4655 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4656 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4657 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 4658 | if (TextUtils.isEmpty(phoneNumber)) { |
| 4659 | phoneNumber = "0000000000"; |
| 4660 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4661 | url = String.format(url, |
| 4662 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 4663 | mTelephonyManager.getDeviceId() /* IMEI */, |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4664 | phoneNumber /* Phone number */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4665 | } |
| 4666 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4667 | return url; |
| 4668 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4669 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4670 | @Override |
| 4671 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4672 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4673 | enforceConnectivityInternalPermission(); |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 4674 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 4675 | return; |
| 4676 | } |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4677 | final long ident = Binder.clearCallingIdentity(); |
| 4678 | try { |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 4679 | // Concatenate the range of types onto the range of NetIDs. |
| 4680 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
| 4681 | mNotifier.setProvNotificationVisible(visible, id, action); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4682 | } finally { |
| 4683 | Binder.restoreCallingIdentity(ident); |
| 4684 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4685 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 4686 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4687 | @Override |
| 4688 | public void setAirplaneMode(boolean enable) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 4689 | enforceNetworkStackSettingsOrSetup(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4690 | final long ident = Binder.clearCallingIdentity(); |
| 4691 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4692 | final ContentResolver cr = mContext.getContentResolver(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 4693 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, encodeBool(enable)); |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4694 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 4695 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 4696 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4697 | } finally { |
| 4698 | Binder.restoreCallingIdentity(ident); |
| 4699 | } |
| 4700 | } |
| 4701 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4702 | private void onUserStart(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4703 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4704 | Vpn userVpn = mVpns.get(userId); |
| 4705 | if (userVpn != null) { |
| 4706 | loge("Starting user already has a VPN"); |
| 4707 | return; |
| 4708 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4709 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNMS, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4710 | mVpns.put(userId, userVpn); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4711 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4712 | updateLockdownVpn(); |
| 4713 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4714 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | private void onUserStop(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4718 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4719 | Vpn userVpn = mVpns.get(userId); |
| 4720 | if (userVpn == null) { |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4721 | loge("Stopped user has no VPN"); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4722 | return; |
| 4723 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4724 | userVpn.onUserStopped(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4725 | mVpns.delete(userId); |
| 4726 | } |
| 4727 | } |
| 4728 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4729 | private void onUserAdded(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4730 | mPermissionMonitor.onUserAdded(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4731 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4732 | synchronized (mVpns) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4733 | final int vpnsSize = mVpns.size(); |
| 4734 | for (int i = 0; i < vpnsSize; i++) { |
| 4735 | Vpn vpn = mVpns.valueAt(i); |
| 4736 | vpn.onUserAdded(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4737 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4738 | updateVpnCapabilities(vpn, nc); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4739 | } |
| 4740 | } |
| 4741 | } |
| 4742 | |
| 4743 | private void onUserRemoved(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4744 | mPermissionMonitor.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4745 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4746 | synchronized (mVpns) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4747 | final int vpnsSize = mVpns.size(); |
| 4748 | for (int i = 0; i < vpnsSize; i++) { |
| 4749 | Vpn vpn = mVpns.valueAt(i); |
| 4750 | vpn.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4751 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4752 | updateVpnCapabilities(vpn, nc); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4753 | } |
| 4754 | } |
| 4755 | } |
| 4756 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4757 | private void onPackageAdded(String packageName, int uid) { |
| 4758 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4759 | Slog.wtf(TAG, "Invalid package in onPackageAdded: " + packageName + " | " + uid); |
| 4760 | return; |
| 4761 | } |
| 4762 | mPermissionMonitor.onPackageAdded(packageName, uid); |
| 4763 | } |
| 4764 | |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4765 | private void onPackageReplaced(String packageName, int uid) { |
| 4766 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4767 | Slog.wtf(TAG, "Invalid package in onPackageReplaced: " + packageName + " | " + uid); |
| 4768 | return; |
| 4769 | } |
| 4770 | final int userId = UserHandle.getUserId(uid); |
| 4771 | synchronized (mVpns) { |
| 4772 | final Vpn vpn = mVpns.get(userId); |
| 4773 | if (vpn == null) { |
| 4774 | return; |
| 4775 | } |
| 4776 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4777 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName)) { |
| 4778 | Slog.d(TAG, "Restarting always-on VPN package " + packageName + " for user " |
| 4779 | + userId); |
| 4780 | vpn.startAlwaysOnVpn(); |
| 4781 | } |
| 4782 | } |
| 4783 | } |
| 4784 | |
| 4785 | private void onPackageRemoved(String packageName, int uid, boolean isReplacing) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4786 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4787 | Slog.wtf(TAG, "Invalid package in onPackageRemoved: " + packageName + " | " + uid); |
| 4788 | return; |
| 4789 | } |
| 4790 | mPermissionMonitor.onPackageRemoved(uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4791 | |
| 4792 | final int userId = UserHandle.getUserId(uid); |
| 4793 | synchronized (mVpns) { |
| 4794 | final Vpn vpn = mVpns.get(userId); |
| 4795 | if (vpn == null) { |
| 4796 | return; |
| 4797 | } |
| 4798 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4799 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName) && !isReplacing) { |
| 4800 | Slog.d(TAG, "Removing always-on VPN package " + packageName + " for user " |
| 4801 | + userId); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4802 | vpn.setAlwaysOnPackage(null, false, null); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4803 | } |
| 4804 | } |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4805 | } |
| 4806 | |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4807 | private void onUserUnlocked(int userId) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4808 | synchronized (mVpns) { |
| 4809 | // User present may be sent because of an unlock, which might mean an unlocked keystore. |
| 4810 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4811 | updateLockdownVpn(); |
| 4812 | } else { |
| 4813 | startAlwaysOnVpn(userId); |
| 4814 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4815 | } |
| 4816 | } |
| 4817 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4818 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4819 | @Override |
| 4820 | public void onReceive(Context context, Intent intent) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4821 | ensureRunningOnConnectivityServiceThread(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4822 | final String action = intent.getAction(); |
| 4823 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4824 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 4825 | final Uri packageData = intent.getData(); |
| 4826 | final String packageName = |
| 4827 | packageData != null ? packageData.getSchemeSpecificPart() : null; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4828 | if (userId == UserHandle.USER_NULL) return; |
| 4829 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 4830 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4831 | onUserStart(userId); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4832 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4833 | onUserStop(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4834 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 4835 | onUserAdded(userId); |
| 4836 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 4837 | onUserRemoved(userId); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4838 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 4839 | onUserUnlocked(userId); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4840 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 4841 | onPackageAdded(packageName, uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4842 | } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 4843 | onPackageReplaced(packageName, uid); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4844 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4845 | final boolean isReplacing = intent.getBooleanExtra( |
| 4846 | Intent.EXTRA_REPLACING, false); |
| 4847 | onPackageRemoved(packageName, uid, isReplacing); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4848 | } |
| 4849 | } |
| 4850 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 4851 | |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 4852 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 4853 | @Override |
| 4854 | public void onReceive(Context context, Intent intent) { |
| 4855 | // Try creating lockdown tracker, since user present usually means |
| 4856 | // unlocked keystore. |
| 4857 | updateLockdownVpn(); |
| 4858 | mContext.unregisterReceiver(this); |
| 4859 | } |
| 4860 | }; |
| 4861 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4862 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = new HashMap<>(); |
| 4863 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = new HashMap<>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4864 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4865 | private static final int MAX_NETWORK_REQUESTS_PER_UID = 100; |
| 4866 | // Map from UID to number of NetworkRequests that UID has filed. |
| 4867 | @GuardedBy("mUidToNetworkRequestCount") |
| 4868 | private final SparseIntArray mUidToNetworkRequestCount = new SparseIntArray(); |
| 4869 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4870 | private static class NetworkFactoryInfo { |
| 4871 | public final String name; |
| 4872 | public final Messenger messenger; |
| 4873 | public final AsyncChannel asyncChannel; |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4874 | public final int factorySerialNumber; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4875 | |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4876 | NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel, |
| 4877 | int factorySerialNumber) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4878 | this.name = name; |
| 4879 | this.messenger = messenger; |
| 4880 | this.asyncChannel = asyncChannel; |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4881 | this.factorySerialNumber = factorySerialNumber; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4882 | } |
| 4883 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4884 | |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4885 | private void ensureNetworkRequestHasType(NetworkRequest request) { |
| 4886 | if (request.type == NetworkRequest.Type.NONE) { |
| 4887 | throw new IllegalArgumentException( |
| 4888 | "All NetworkRequests in ConnectivityService must have a type"); |
| 4889 | } |
| 4890 | } |
| 4891 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4892 | /** |
| 4893 | * Tracks info about the requester. |
| 4894 | * Also used to notice when the calling process dies so we can self-expire |
| 4895 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4896 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4897 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4898 | final PendingIntent mPendingIntent; |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4899 | boolean mPendingIntentSent; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4900 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4901 | final int mPid; |
| 4902 | final int mUid; |
| 4903 | final Messenger messenger; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4904 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4905 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4906 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4907 | ensureNetworkRequestHasType(request); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4908 | mPendingIntent = pi; |
| 4909 | messenger = null; |
| 4910 | mBinder = null; |
| 4911 | mPid = getCallingPid(); |
| 4912 | mUid = getCallingUid(); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4913 | enforceRequestCountLimit(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4914 | } |
| 4915 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4916 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4917 | super(); |
| 4918 | messenger = m; |
| 4919 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4920 | ensureNetworkRequestHasType(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4921 | mBinder = binder; |
| 4922 | mPid = getCallingPid(); |
| 4923 | mUid = getCallingUid(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4924 | mPendingIntent = null; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4925 | enforceRequestCountLimit(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4926 | |
| 4927 | try { |
| 4928 | mBinder.linkToDeath(this, 0); |
| 4929 | } catch (RemoteException e) { |
| 4930 | binderDied(); |
| 4931 | } |
| 4932 | } |
| 4933 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4934 | private void enforceRequestCountLimit() { |
| 4935 | synchronized (mUidToNetworkRequestCount) { |
| 4936 | int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1; |
| 4937 | if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4938 | throw new ServiceSpecificException( |
| 4939 | ConnectivityManager.Errors.TOO_MANY_REQUESTS); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4940 | } |
| 4941 | mUidToNetworkRequestCount.put(mUid, networkRequests); |
| 4942 | } |
| 4943 | } |
| 4944 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4945 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4946 | if (mBinder != null) { |
| 4947 | mBinder.unlinkToDeath(this, 0); |
| 4948 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4949 | } |
| 4950 | |
| 4951 | public void binderDied() { |
| 4952 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 4953 | request + ", " + mBinder + ")"); |
| 4954 | releaseNetworkRequest(request); |
| 4955 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4956 | |
| 4957 | public String toString() { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4958 | return "uid/pid:" + mUid + "/" + mPid + " " + request + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4959 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4960 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4961 | } |
| 4962 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4963 | private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { |
| 4964 | final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); |
| 4965 | if (badCapability != null) { |
| 4966 | throw new IllegalArgumentException("Cannot request network with " + badCapability); |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 4967 | } |
| 4968 | } |
| 4969 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame^] | 4970 | // This checks that the passed capabilities either do not request a specific SSID/SignalStrength |
| 4971 | // , or the calling app has permission to do so. |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4972 | private void ensureSufficientPermissionsForRequest(NetworkCapabilities nc, |
| 4973 | int callerPid, int callerUid) { |
Chalard Jean | b5120ca | 2018-04-16 12:25:22 +0900 | [diff] [blame] | 4974 | if (null != nc.getSSID() && !checkSettingsPermission(callerPid, callerUid)) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4975 | throw new SecurityException("Insufficient permissions to request a specific SSID"); |
| 4976 | } |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame^] | 4977 | |
| 4978 | if (nc.hasSignalStrength() |
| 4979 | && !checkNetworkSignalStrengthWakeupPermission(callerPid, callerUid)) { |
| 4980 | throw new SecurityException( |
| 4981 | "Insufficient permissions to request a specific signal strength"); |
| 4982 | } |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4983 | } |
| 4984 | |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 4985 | private ArrayList<Integer> getSignalStrengthThresholds(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4986 | final SortedSet<Integer> thresholds = new TreeSet<>(); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4987 | synchronized (nai) { |
| 4988 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 4989 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 4990 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 4991 | thresholds.add(nri.request.networkCapabilities.getSignalStrength()); |
| 4992 | } |
| 4993 | } |
| 4994 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4995 | return new ArrayList<>(thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 4996 | } |
| 4997 | |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 4998 | private void updateSignalStrengthThresholds( |
| 4999 | NetworkAgentInfo nai, String reason, NetworkRequest request) { |
| 5000 | ArrayList<Integer> thresholdsArray = getSignalStrengthThresholds(nai); |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 5001 | Bundle thresholds = new Bundle(); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 5002 | thresholds.putIntegerArrayList("thresholds", thresholdsArray); |
| 5003 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5004 | if (VDBG || (DBG && !"CONNECT".equals(reason))) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 5005 | String detail; |
| 5006 | if (request != null && request.networkCapabilities.hasSignalStrength()) { |
| 5007 | detail = reason + " " + request.networkCapabilities.getSignalStrength(); |
| 5008 | } else { |
| 5009 | detail = reason; |
| 5010 | } |
| 5011 | log(String.format("updateSignalStrengthThresholds: %s, sending %s to %s", |
| 5012 | detail, Arrays.toString(thresholdsArray.toArray()), nai.name())); |
| 5013 | } |
| 5014 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5015 | nai.asyncChannel.sendMessage( |
| 5016 | android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 5017 | 0, 0, thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5018 | } |
| 5019 | |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5020 | private void ensureValidNetworkSpecifier(NetworkCapabilities nc) { |
| 5021 | if (nc == null) { |
| 5022 | return; |
| 5023 | } |
| 5024 | NetworkSpecifier ns = nc.getNetworkSpecifier(); |
| 5025 | if (ns == null) { |
| 5026 | return; |
| 5027 | } |
| 5028 | MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(ns); |
| 5029 | ns.assertValidFromUid(Binder.getCallingUid()); |
| 5030 | } |
| 5031 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5032 | @Override |
| 5033 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5034 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5035 | final NetworkRequest.Type type = (networkCapabilities == null) |
| 5036 | ? NetworkRequest.Type.TRACK_DEFAULT |
| 5037 | : NetworkRequest.Type.REQUEST; |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5038 | // If the requested networkCapabilities is null, take them instead from |
| 5039 | // the default network request. This allows callers to keep track of |
| 5040 | // the system default network. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5041 | if (type == NetworkRequest.Type.TRACK_DEFAULT) { |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 5042 | networkCapabilities = createDefaultNetworkCapabilitiesForUid(Binder.getCallingUid()); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5043 | enforceAccessPermission(); |
| 5044 | } else { |
| 5045 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 5046 | enforceNetworkRequestPermissions(networkCapabilities); |
Lorenzo Colitti | b60570c | 2016-07-01 13:20:10 +0900 | [diff] [blame] | 5047 | // TODO: this is incorrect. We mark the request as metered or not depending on the state |
| 5048 | // of the app when the request is filed, but we never change the request if the app |
| 5049 | // changes network state. http://b/29964605 |
| 5050 | enforceMeteredApnPolicy(networkCapabilities); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5051 | } |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 5052 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5053 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5054 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5055 | // Set the UID range for this request to the single UID of the requester, or to an empty |
| 5056 | // 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] | 5057 | // This will overwrite any allowed UIDs in the requested capabilities. Though there |
| 5058 | // are no visible methods to set the UIDs, an app could use reflection to try and get |
| 5059 | // 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] | 5060 | restrictRequestUidsForCaller(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 5061 | |
Etan Cohen | ba07c8c | 2017-02-05 10:42:27 -0800 | [diff] [blame] | 5062 | if (timeoutMs < 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5063 | throw new IllegalArgumentException("Bad timeout specified"); |
| 5064 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5065 | ensureValidNetworkSpecifier(networkCapabilities); |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 5066 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 5067 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5068 | nextNetworkRequestId(), type); |
| 5069 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 5070 | if (DBG) log("requestNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5071 | |
| 5072 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5073 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5074 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5075 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5076 | } |
| 5077 | return networkRequest; |
| 5078 | } |
| 5079 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5080 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 5081 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) { |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 5082 | enforceConnectivityRestrictedNetworksPermission(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5083 | } else { |
| 5084 | enforceChangePermission(); |
| 5085 | } |
| 5086 | } |
| 5087 | |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5088 | @Override |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 5089 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5090 | enforceAccessPermission(); |
| 5091 | NetworkAgentInfo nai = null; |
| 5092 | if (network == null) { |
| 5093 | return false; |
| 5094 | } |
| 5095 | synchronized (mNetworkForNetId) { |
| 5096 | nai = mNetworkForNetId.get(network.netId); |
| 5097 | } |
| 5098 | if (nai != null) { |
| 5099 | nai.asyncChannel.sendMessage(android.net.NetworkAgent.CMD_REQUEST_BANDWIDTH_UPDATE); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 5100 | synchronized (mBandwidthRequests) { |
| 5101 | final int uid = Binder.getCallingUid(); |
| 5102 | Integer uidReqs = mBandwidthRequests.get(uid); |
| 5103 | if (uidReqs == null) { |
| 5104 | uidReqs = new Integer(0); |
| 5105 | } |
| 5106 | mBandwidthRequests.put(uid, ++uidReqs); |
| 5107 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5108 | return true; |
| 5109 | } |
| 5110 | return false; |
| 5111 | } |
| 5112 | |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5113 | private boolean isSystem(int uid) { |
| 5114 | return uid < Process.FIRST_APPLICATION_UID; |
| 5115 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5116 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5117 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5118 | final int uid = Binder.getCallingUid(); |
| 5119 | if (isSystem(uid)) { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5120 | // Exemption for system uid. |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5121 | return; |
| 5122 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5123 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 5124 | // Policy already enforced. |
| 5125 | return; |
| 5126 | } |
| 5127 | if (mPolicyManagerInternal.isUidRestrictedOnMeteredNetworks(uid)) { |
| 5128 | // If UID is restricted, don't allow them to bring up metered APNs. |
| 5129 | networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5130 | } |
| 5131 | } |
| 5132 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5133 | @Override |
| 5134 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 5135 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5136 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 5137 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 5138 | enforceNetworkRequestPermissions(networkCapabilities); |
| 5139 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 5140 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5141 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5142 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5143 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5144 | restrictRequestUidsForCaller(networkCapabilities); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5145 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5146 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5147 | nextNetworkRequestId(), NetworkRequest.Type.REQUEST); |
| 5148 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 5149 | if (DBG) log("pendingRequest for " + nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5150 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 5151 | nri)); |
| 5152 | return networkRequest; |
| 5153 | } |
| 5154 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5155 | private void releasePendingNetworkRequestWithDelay(PendingIntent operation) { |
| 5156 | mHandler.sendMessageDelayed( |
| 5157 | mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 5158 | getCallingUid(), 0, operation), mReleasePendingIntentDelayMs); |
| 5159 | } |
| 5160 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5161 | @Override |
| 5162 | public void releasePendingNetworkRequest(PendingIntent operation) { |
Paul Jensen | 1a81c39 | 2015-05-21 08:15:08 -0400 | [diff] [blame] | 5163 | checkNotNull(operation, "PendingIntent cannot be null."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5164 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 5165 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5166 | } |
| 5167 | |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 5168 | // In order to implement the compatibility measure for pre-M apps that call |
| 5169 | // WifiManager.enableNetwork(..., true) without also binding to that network explicitly, |
| 5170 | // WifiManager registers a network listen for the purpose of calling setProcessDefaultNetwork. |
| 5171 | // This ensures it has permission to do so. |
| 5172 | private boolean hasWifiNetworkListenPermission(NetworkCapabilities nc) { |
| 5173 | if (nc == null) { |
| 5174 | return false; |
| 5175 | } |
| 5176 | int[] transportTypes = nc.getTransportTypes(); |
| 5177 | if (transportTypes.length != 1 || transportTypes[0] != NetworkCapabilities.TRANSPORT_WIFI) { |
| 5178 | return false; |
| 5179 | } |
| 5180 | try { |
| 5181 | mContext.enforceCallingOrSelfPermission( |
| 5182 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 5183 | "ConnectivityService"); |
| 5184 | } catch (SecurityException e) { |
| 5185 | return false; |
| 5186 | } |
| 5187 | return true; |
| 5188 | } |
| 5189 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5190 | @Override |
| 5191 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 5192 | Messenger messenger, IBinder binder) { |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 5193 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 5194 | enforceAccessPermission(); |
| 5195 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5196 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5197 | NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5198 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5199 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5200 | restrictRequestUidsForCaller(nc); |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 5201 | // Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so |
| 5202 | // make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get |
| 5203 | // onLost and onAvailable callbacks when networks move in and out of the background. |
| 5204 | // There is no need to do this for requests because an app without CHANGE_NETWORK_STATE |
| 5205 | // can't request networks. |
| 5206 | restrictBackgroundRequestForCaller(nc); |
| 5207 | ensureValidNetworkSpecifier(nc); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5208 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5209 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5210 | NetworkRequest.Type.LISTEN); |
| 5211 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5212 | if (VDBG) log("listenForNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5213 | |
| 5214 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 5215 | return networkRequest; |
| 5216 | } |
| 5217 | |
| 5218 | @Override |
| 5219 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 5220 | PendingIntent operation) { |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5221 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 5222 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 5223 | enforceAccessPermission(); |
| 5224 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5225 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5226 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5227 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5228 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5229 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5230 | restrictRequestUidsForCaller(nc); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5231 | |
| 5232 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5233 | NetworkRequest.Type.LISTEN); |
| 5234 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5235 | if (VDBG) log("pendingListenForNetwork for " + nri); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5236 | |
| 5237 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5240 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5241 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 5242 | ensureNetworkRequestHasType(networkRequest); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5243 | mHandler.sendMessage(mHandler.obtainMessage( |
| 5244 | EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5245 | } |
| 5246 | |
| 5247 | @Override |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5248 | public int registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5249 | enforceConnectivityInternalPermission(); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5250 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel(), |
| 5251 | NetworkFactory.SerialNumber.nextSerialNumber()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5252 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5253 | return nfi.factorySerialNumber; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5254 | } |
| 5255 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5256 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5257 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5258 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 5259 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 5260 | } |
| 5261 | |
| 5262 | @Override |
| 5263 | public void unregisterNetworkFactory(Messenger messenger) { |
| 5264 | enforceConnectivityInternalPermission(); |
| 5265 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 5266 | } |
| 5267 | |
| 5268 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 5269 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 5270 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5271 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5272 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5273 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5274 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5275 | } |
| 5276 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5277 | /** |
| 5278 | * NetworkAgentInfo supporting a request by requestId. |
| 5279 | * These have already been vetted (their Capabilities satisfy the request) |
| 5280 | * and the are the highest scored network available. |
| 5281 | * the are keyed off the Requests requestId. |
| 5282 | */ |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5283 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5284 | @GuardedBy("mNetworkForRequestId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5285 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = new SparseArray<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5286 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5287 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5288 | @GuardedBy("mNetworkForNetId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5289 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = new SparseArray<>(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5290 | // NOTE: Accessed on multiple threads, synchronized with mNetworkForNetId. |
| 5291 | // An entry is first added to mNetIdInUse, prior to mNetworkForNetId, so |
| 5292 | // there may not be a strict 1:1 correlation between the two. |
| 5293 | @GuardedBy("mNetworkForNetId") |
| 5294 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5295 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5296 | // NetworkAgentInfo keyed off its connecting messenger |
| 5297 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5298 | // NOTE: Only should be accessed on ConnectivityServiceThread, except dump(). |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5299 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = new HashMap<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5300 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5301 | @GuardedBy("mBlockedAppUids") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5302 | private final HashSet<Integer> mBlockedAppUids = new HashSet<>(); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5303 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5304 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5305 | private final NetworkRequest mDefaultRequest; |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5306 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 5307 | // Request used to optionally keep mobile data active even when higher |
| 5308 | // priority networks like Wi-Fi are active. |
| 5309 | private final NetworkRequest mDefaultMobileDataRequest; |
| 5310 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 5311 | // Request used to optionally keep wifi data active even when higher |
| 5312 | // priority networks like ethernet are active. |
| 5313 | private final NetworkRequest mDefaultWifiRequest; |
| 5314 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5315 | private NetworkAgentInfo getNetworkForRequest(int requestId) { |
| 5316 | synchronized (mNetworkForRequestId) { |
| 5317 | return mNetworkForRequestId.get(requestId); |
| 5318 | } |
| 5319 | } |
| 5320 | |
| 5321 | private void clearNetworkForRequest(int requestId) { |
| 5322 | synchronized (mNetworkForRequestId) { |
| 5323 | mNetworkForRequestId.remove(requestId); |
| 5324 | } |
| 5325 | } |
| 5326 | |
| 5327 | private void setNetworkForRequest(int requestId, NetworkAgentInfo nai) { |
| 5328 | synchronized (mNetworkForRequestId) { |
| 5329 | mNetworkForRequestId.put(requestId, nai); |
| 5330 | } |
| 5331 | } |
| 5332 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5333 | private NetworkAgentInfo getDefaultNetwork() { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5334 | return getNetworkForRequest(mDefaultRequest.requestId); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5335 | } |
| 5336 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5337 | @Nullable |
| 5338 | private Network getNetwork(@Nullable NetworkAgentInfo nai) { |
| 5339 | return nai != null ? nai.network : null; |
| 5340 | } |
| 5341 | |
| 5342 | private void ensureRunningOnConnectivityServiceThread() { |
| 5343 | if (mHandler.getLooper().getThread() != Thread.currentThread()) { |
| 5344 | throw new IllegalStateException( |
| 5345 | "Not running on ConnectivityService thread: " |
| 5346 | + Thread.currentThread().getName()); |
| 5347 | } |
| 5348 | } |
| 5349 | |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 5350 | private boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5351 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 5352 | } |
| 5353 | |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 5354 | private boolean isDefaultRequest(NetworkRequestInfo nri) { |
| 5355 | return nri.request.requestId == mDefaultRequest.requestId; |
| 5356 | } |
| 5357 | |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5358 | // TODO : remove this method. It's a stopgap measure to help sheperding a number of dependent |
| 5359 | // changes that would conflict throughout the automerger graph. Having this method temporarily |
| 5360 | // helps with the process of going through with all these dependent changes across the entire |
| 5361 | // tree. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5362 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5363 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 5364 | int currentScore, NetworkMisc networkMisc) { |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5365 | return registerNetworkAgent(messenger, networkInfo, linkProperties, networkCapabilities, |
| 5366 | currentScore, networkMisc, NetworkFactory.SerialNumber.NONE); |
| 5367 | } |
| 5368 | |
| 5369 | /** |
| 5370 | * Register a new agent with ConnectivityService to handle a network. |
| 5371 | * |
| 5372 | * @param messenger a messenger for ConnectivityService to contact the agent asynchronously. |
| 5373 | * @param networkInfo the initial info associated with this network. It can be updated later : |
| 5374 | * see {@link #updateNetworkInfo}. |
| 5375 | * @param linkProperties the initial link properties of this network. They can be updated |
| 5376 | * later : see {@link #updateLinkProperties}. |
| 5377 | * @param networkCapabilities the initial capabilites of this network. They can be updated |
| 5378 | * later : see {@link #updateNetworkCapabilities}. |
| 5379 | * @param currentScore the initial score of the network. See |
| 5380 | * {@link NetworkAgentInfo#getCurrentScore}. |
| 5381 | * @param networkMisc metadata about the network. This is never updated. |
| 5382 | * @param factorySerialNumber the serial number of the factory owning this NetworkAgent. |
| 5383 | */ |
| 5384 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 5385 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
| 5386 | int currentScore, NetworkMisc networkMisc, int factorySerialNumber) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5387 | enforceConnectivityInternalPermission(); |
| 5388 | |
Rubin Xu | 1bb5c08 | 2017-09-05 18:40:49 +0100 | [diff] [blame] | 5389 | LinkProperties lp = new LinkProperties(linkProperties); |
| 5390 | lp.ensureDirectlyConnectedRoutes(); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5391 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 5392 | // satisfies mDefaultRequest. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5393 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5394 | final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5395 | new Network(reserveNetId()), new NetworkInfo(networkInfo), lp, nc, currentScore, |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5396 | mContext, mTrackerHandler, new NetworkMisc(networkMisc), this, mNetd, mNMS, |
| 5397 | factorySerialNumber); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5398 | // Make sure the network capabilities reflect what the agent info says. |
| 5399 | nai.networkCapabilities = mixInCapabilities(nai, nc); |
Chalard Jean | d771aa0 | 2018-04-26 16:16:10 +0900 | [diff] [blame] | 5400 | final String extraInfo = networkInfo.getExtraInfo(); |
| 5401 | final String name = TextUtils.isEmpty(extraInfo) |
| 5402 | ? nai.networkCapabilities.getSSID() : extraInfo; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5403 | if (DBG) log("registerNetworkAgent " + nai); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5404 | final long token = Binder.clearCallingIdentity(); |
| 5405 | try { |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5406 | getNetworkStack().makeNetworkMonitor( |
Remi NGUYEN VAN | 904a38b | 2019-03-15 02:25:09 +0900 | [diff] [blame] | 5407 | nai.network, name, new NetworkMonitorCallbacks(nai)); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5408 | } finally { |
| 5409 | Binder.restoreCallingIdentity(token); |
| 5410 | } |
| 5411 | // NetworkAgentInfo registration will finish when the NetworkMonitor is created. |
| 5412 | // If the network disconnects or sends any other event before that, messages are deferred by |
| 5413 | // NetworkAgent until nai.asyncChannel.connect(), which will be called when finalizing the |
| 5414 | // registration. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5415 | return nai.network.netId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5416 | } |
| 5417 | |
Remi NGUYEN VAN | 5db454c | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5418 | @VisibleForTesting |
| 5419 | protected NetworkStackClient getNetworkStack() { |
| 5420 | return NetworkStackClient.getInstance(); |
| 5421 | } |
| 5422 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5423 | private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) { |
| 5424 | nai.onNetworkMonitorCreated(networkMonitor); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5425 | if (VDBG) log("Got NetworkAgent Messenger"); |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5426 | mNetworkAgentInfos.put(nai.messenger, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5427 | synchronized (mNetworkForNetId) { |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5428 | mNetworkForNetId.put(nai.network.netId, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5429 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5430 | synchronized (this) { |
| 5431 | if (mSystemReady) { |
| 5432 | try { |
| 5433 | networkMonitor.notifySystemReady(); |
| 5434 | } catch (RemoteException e) { |
| 5435 | e.rethrowFromSystemServer(); |
| 5436 | } |
| 5437 | } |
| 5438 | } |
| 5439 | |
| 5440 | try { |
| 5441 | networkMonitor.start(); |
| 5442 | } catch (RemoteException e) { |
| 5443 | e.rethrowFromSystemServer(); |
| 5444 | } |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5445 | nai.asyncChannel.connect(mContext, mTrackerHandler, nai.messenger); |
| 5446 | NetworkInfo networkInfo = nai.networkInfo; |
| 5447 | nai.networkInfo = null; |
| 5448 | updateNetworkInfo(nai, networkInfo); |
| 5449 | updateUids(nai, null, nai.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5450 | } |
| 5451 | |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5452 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties newLp, |
| 5453 | LinkProperties oldLp) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5454 | int netId = networkAgent.network.netId; |
| 5455 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 5456 | // The NetworkAgentInfo does not know whether clatd is running on its network or not, or |
| 5457 | // whether there is a NAT64 prefix. Before we do anything else, make sure its LinkProperties |
| 5458 | // are accurate. |
| 5459 | networkAgent.clatd.fixupLinkProperties(oldLp, newLp); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 5460 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5461 | updateInterfaces(newLp, oldLp, netId, networkAgent.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5462 | updateMtu(newLp, oldLp); |
| 5463 | // TODO - figure out what to do for clat |
| 5464 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 5465 | // updateMtu(lp, null); |
| 5466 | // } |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5467 | if (isDefaultNetwork(networkAgent)) { |
| 5468 | updateTcpBufferSizes(newLp.getTcpBufferSizes()); |
| 5469 | } |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5470 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5471 | updateRoutes(newLp, oldLp, netId); |
| 5472 | updateDnses(newLp, oldLp, netId); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 5473 | // Make sure LinkProperties represents the latest private DNS status. |
| 5474 | // This does not need to be done before updateDnses because the |
| 5475 | // LinkProperties are not the source of the private DNS configuration. |
| 5476 | // updateDnses will fetch the private DNS configuration from DnsManager. |
| 5477 | mDnsManager.updatePrivateDnsStatus(netId, newLp); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5478 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5479 | if (isDefaultNetwork(networkAgent)) { |
| 5480 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
| 5481 | } else { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5482 | updateProxy(newLp, oldLp); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5483 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5484 | // TODO - move this check to cover the whole function |
| 5485 | if (!Objects.equals(newLp, oldLp)) { |
Chalard Jean | c4f53ba | 2018-05-23 09:07:51 +0900 | [diff] [blame] | 5486 | synchronized (networkAgent) { |
| 5487 | networkAgent.linkProperties = newLp; |
| 5488 | } |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 5489 | // Start or stop DNS64 detection and 464xlat according to network state. |
| 5490 | networkAgent.clatd.update(); |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5491 | notifyIfacesChangedForNetworkStats(); |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5492 | if (networkAgent.everConnected) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5493 | try { |
| 5494 | networkAgent.networkMonitor().notifyLinkPropertiesChanged(); |
| 5495 | } catch (RemoteException e) { |
| 5496 | e.rethrowFromSystemServer(); |
| 5497 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5498 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 5499 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5500 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 5501 | |
| 5502 | mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5503 | } |
| 5504 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5505 | private void wakeupModifyInterface(String iface, NetworkCapabilities caps, boolean add) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5506 | // Marks are only available on WiFi interfaces. Checking for |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5507 | // marks on unsupported interfaces is harmless. |
| 5508 | if (!caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 5509 | return; |
| 5510 | } |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5511 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5512 | int mark = mContext.getResources().getInteger( |
| 5513 | com.android.internal.R.integer.config_networkWakeupPacketMark); |
| 5514 | int mask = mContext.getResources().getInteger( |
| 5515 | com.android.internal.R.integer.config_networkWakeupPacketMask); |
| 5516 | |
| 5517 | // Mask/mark of zero will not detect anything interesting. |
| 5518 | // Don't install rules unless both values are nonzero. |
| 5519 | if (mark == 0 || mask == 0) { |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5520 | return; |
| 5521 | } |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5522 | |
| 5523 | final String prefix = "iface:" + iface; |
| 5524 | try { |
| 5525 | if (add) { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5526 | mNetd.wakeupAddInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5527 | } else { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5528 | mNetd.wakeupDelInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5529 | } |
| 5530 | } catch (Exception e) { |
| 5531 | loge("Exception modifying wakeup packet monitoring: " + e); |
| 5532 | } |
| 5533 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId, |
| 5537 | NetworkCapabilities caps) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5538 | CompareResult<String> interfaceDiff = new CompareResult<>( |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5539 | oldLp != null ? oldLp.getAllInterfaceNames() : null, |
| 5540 | newLp != null ? newLp.getAllInterfaceNames() : null); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5541 | for (String iface : interfaceDiff.added) { |
| 5542 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5543 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5544 | mNMS.addInterfaceToNetwork(iface, netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5545 | wakeupModifyInterface(iface, caps, true); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5546 | } catch (Exception e) { |
| 5547 | loge("Exception adding interface: " + e); |
| 5548 | } |
| 5549 | } |
| 5550 | for (String iface : interfaceDiff.removed) { |
| 5551 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5552 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5553 | wakeupModifyInterface(iface, caps, false); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5554 | mNMS.removeInterfaceFromNetwork(iface, netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5555 | } catch (Exception e) { |
| 5556 | loge("Exception removing interface: " + e); |
| 5557 | } |
| 5558 | } |
| 5559 | } |
| 5560 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5561 | /** |
| 5562 | * Have netd update routes from oldLp to newLp. |
| 5563 | * @return true if routes changed between oldLp and newLp |
| 5564 | */ |
| 5565 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5566 | // Compare the route diff to determine which routes should be added and removed. |
| 5567 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>( |
| 5568 | oldLp != null ? oldLp.getAllRoutes() : null, |
| 5569 | newLp != null ? newLp.getAllRoutes() : null); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5570 | |
| 5571 | // add routes before removing old in case it helps with continuous connectivity |
| 5572 | |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5573 | // 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] | 5574 | for (RouteInfo route : routeDiff.added) { |
| 5575 | if (route.hasGateway()) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5576 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5577 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5578 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5579 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5580 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 5581 | loge("Exception in addRoute for non-gateway: " + e); |
| 5582 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5583 | } |
| 5584 | } |
| 5585 | for (RouteInfo route : routeDiff.added) { |
| 5586 | if (route.hasGateway() == false) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5587 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5588 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5589 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5590 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5591 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 5592 | loge("Exception in addRoute for gateway: " + e); |
| 5593 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5594 | } |
| 5595 | } |
| 5596 | |
| 5597 | for (RouteInfo route : routeDiff.removed) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5598 | if (VDBG || DDBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5599 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5600 | mNMS.removeRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5601 | } catch (Exception e) { |
| 5602 | loge("Exception in removeRoute: " + e); |
| 5603 | } |
| 5604 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5605 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5606 | } |
Erik Kline | 4136850 | 2015-06-17 13:19:54 +0900 | [diff] [blame] | 5607 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5608 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5609 | if (oldLp != null && newLp.isIdenticalDnses(oldLp)) { |
| 5610 | return; // no updating necessary |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5611 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5612 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5613 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
| 5614 | final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId); |
| 5615 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5616 | if (DBG) { |
| 5617 | final Collection<InetAddress> dnses = newLp.getDnsServers(); |
| 5618 | log("Setting DNS servers for network " + netId + " to " + dnses); |
| 5619 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5620 | try { |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5621 | mDnsManager.setDnsConfigurationForNetwork(netId, newLp, isDefaultNetwork); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5622 | } catch (Exception e) { |
Pierre Imai | bd8759b | 2016-04-28 17:00:04 +0900 | [diff] [blame] | 5623 | loge("Exception in setDnsConfigurationForNetwork: " + e); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5624 | } |
Erik Kline | 4edba01 | 2017-04-07 15:29:29 +0900 | [diff] [blame] | 5625 | } |
| 5626 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5627 | private int getNetworkPermission(NetworkCapabilities nc) { |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5628 | if (!nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5629 | return INetd.PERMISSION_SYSTEM; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5630 | } |
| 5631 | if (!nc.hasCapability(NET_CAPABILITY_FOREGROUND)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5632 | return INetd.PERMISSION_NETWORK; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5633 | } |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5634 | return INetd.PERMISSION_NONE; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5635 | } |
| 5636 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5637 | /** |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5638 | * Augments the NetworkCapabilities passed in by a NetworkAgent with capabilities that are |
| 5639 | * maintained here that the NetworkAgent is not aware of (e.g., validated, captive portal, |
| 5640 | * and foreground status). |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5641 | */ |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5642 | private NetworkCapabilities mixInCapabilities(NetworkAgentInfo nai, NetworkCapabilities nc) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5643 | // Once a NetworkAgent is connected, complain if some immutable capabilities are removed. |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5644 | // Don't complain for VPNs since they're not driven by requests and there is no risk of |
| 5645 | // causing a connect/teardown loop. |
| 5646 | // TODO: remove this altogether and make it the responsibility of the NetworkFactories to |
| 5647 | // avoid connect/teardown loops. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5648 | if (nai.everConnected && |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5649 | !nai.isVPN() && |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5650 | !nai.networkCapabilities.satisfiedByImmutableNetworkCapabilities(nc)) { |
| 5651 | // 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] | 5652 | // does not cause any request (that is not a listen) currently matching that agent to |
| 5653 | // stop being matched by the updated agent. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5654 | String diff = nai.networkCapabilities.describeImmutableDifferences(nc); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5655 | if (!TextUtils.isEmpty(diff)) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5656 | Slog.wtf(TAG, "BUG: " + nai + " lost immutable capabilities:" + diff); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5657 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5658 | } |
| 5659 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5660 | // Don't modify caller's NetworkCapabilities. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5661 | NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5662 | if (nai.lastValidated) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5663 | newNc.addCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5664 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5665 | newNc.removeCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5666 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5667 | if (nai.lastCaptivePortalDetected) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5668 | newNc.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5669 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5670 | newNc.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5671 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5672 | if (nai.isBackgroundNetwork()) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5673 | newNc.removeCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5674 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5675 | newNc.addCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5676 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5677 | if (nai.isSuspended()) { |
| 5678 | newNc.removeCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5679 | } else { |
| 5680 | newNc.addCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5681 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 5682 | if (nai.partialConnectivity) { |
| 5683 | newNc.addCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); |
| 5684 | } else { |
| 5685 | newNc.removeCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); |
| 5686 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5687 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5688 | return newNc; |
| 5689 | } |
| 5690 | |
| 5691 | /** |
| 5692 | * Update the NetworkCapabilities for {@code nai} to {@code nc}. Specifically: |
| 5693 | * |
| 5694 | * 1. Calls mixInCapabilities to merge the passed-in NetworkCapabilities {@code nc} with the |
| 5695 | * capabilities we manage and store in {@code nai}, such as validated status and captive |
| 5696 | * portal status) |
| 5697 | * 2. Takes action on the result: changes network permissions, sends CAP_CHANGED callbacks, and |
| 5698 | * potentially triggers rematches. |
| 5699 | * 3. Directly informs other network stack components (NetworkStatsService, VPNs, etc. of the |
| 5700 | * change.) |
| 5701 | * |
| 5702 | * @param oldScore score of the network before any of the changes that prompted us |
| 5703 | * to call this function. |
| 5704 | * @param nai the network having its capabilities updated. |
| 5705 | * @param nc the new network capabilities. |
| 5706 | */ |
| 5707 | private void updateCapabilities(int oldScore, NetworkAgentInfo nai, NetworkCapabilities nc) { |
| 5708 | NetworkCapabilities newNc = mixInCapabilities(nai, nc); |
| 5709 | |
| 5710 | if (Objects.equals(nai.networkCapabilities, newNc)) return; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5711 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5712 | final int oldPermission = getNetworkPermission(nai.networkCapabilities); |
| 5713 | final int newPermission = getNetworkPermission(newNc); |
| 5714 | if (oldPermission != newPermission && nai.created && !nai.isVPN()) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5715 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5716 | mNMS.setNetworkPermission(nai.network.netId, newPermission); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5717 | } catch (RemoteException e) { |
| 5718 | loge("Exception in setNetworkPermission: " + e); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 5719 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5720 | } |
| 5721 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5722 | final NetworkCapabilities prevNc; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5723 | synchronized (nai) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5724 | prevNc = nai.networkCapabilities; |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5725 | nai.networkCapabilities = newNc; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5726 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5727 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5728 | updateUids(nai, prevNc, newNc); |
| 5729 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5730 | if (nai.getCurrentScore() == oldScore && newNc.equalRequestableCapabilities(prevNc)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5731 | // If the requestable capabilities haven't changed, and the score hasn't changed, then |
| 5732 | // the change we're processing can't affect any requests, it can only affect the listens |
| 5733 | // on this network. We might have been called by rematchNetworkAndRequests when a |
| 5734 | // network changed foreground state. |
| 5735 | processListenRequests(nai, true); |
| 5736 | } else { |
| 5737 | // If the requestable capabilities have changed or the score changed, we can't have been |
| 5738 | // called by rematchNetworkAndRequests, so it's safe to start a rematch. |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5739 | rematchAllNetworksAndRequests(nai, oldScore); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5740 | try { |
| 5741 | nai.networkMonitor().notifyNetworkCapabilitiesChanged(); |
| 5742 | } catch (RemoteException e) { |
| 5743 | e.rethrowFromSystemServer(); |
| 5744 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5745 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 5746 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5747 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5748 | if (prevNc != null) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5749 | final boolean oldMetered = prevNc.isMetered(); |
| 5750 | final boolean newMetered = newNc.isMetered(); |
| 5751 | final boolean meteredChanged = oldMetered != newMetered; |
| 5752 | |
| 5753 | if (meteredChanged) { |
| 5754 | maybeNotifyNetworkBlocked(nai, oldMetered, newMetered, mRestrictBackground, |
| 5755 | mRestrictBackground); |
| 5756 | } |
| 5757 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5758 | final boolean roamingChanged = prevNc.hasCapability(NET_CAPABILITY_NOT_ROAMING) != |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5759 | newNc.hasCapability(NET_CAPABILITY_NOT_ROAMING); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5760 | |
| 5761 | // Report changes that are interesting for network statistics tracking. |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5762 | if (meteredChanged || roamingChanged) { |
| 5763 | notifyIfacesChangedForNetworkStats(); |
| 5764 | } |
| 5765 | } |
| 5766 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5767 | if (!newNc.hasTransport(TRANSPORT_VPN)) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5768 | // Tell VPNs about updated capabilities, since they may need to |
| 5769 | // bubble those changes through. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5770 | updateAllVpnsCapabilities(); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5771 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5772 | } |
| 5773 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5774 | private void updateUids(NetworkAgentInfo nai, NetworkCapabilities prevNc, |
| 5775 | NetworkCapabilities newNc) { |
| 5776 | Set<UidRange> prevRanges = null == prevNc ? null : prevNc.getUids(); |
| 5777 | Set<UidRange> newRanges = null == newNc ? null : newNc.getUids(); |
| 5778 | if (null == prevRanges) prevRanges = new ArraySet<>(); |
| 5779 | if (null == newRanges) newRanges = new ArraySet<>(); |
| 5780 | final Set<UidRange> prevRangesCopy = new ArraySet<>(prevRanges); |
| 5781 | |
| 5782 | prevRanges.removeAll(newRanges); |
| 5783 | newRanges.removeAll(prevRangesCopy); |
| 5784 | |
| 5785 | try { |
| 5786 | if (!newRanges.isEmpty()) { |
| 5787 | final UidRange[] addedRangesArray = new UidRange[newRanges.size()]; |
| 5788 | newRanges.toArray(addedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5789 | mNMS.addVpnUidRanges(nai.network.netId, addedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5790 | } |
| 5791 | if (!prevRanges.isEmpty()) { |
| 5792 | final UidRange[] removedRangesArray = new UidRange[prevRanges.size()]; |
| 5793 | prevRanges.toArray(removedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5794 | mNMS.removeVpnUidRanges(nai.network.netId, removedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5795 | } |
| 5796 | } catch (Exception e) { |
| 5797 | // Never crash! |
| 5798 | loge("Exception in updateUids: " + e); |
| 5799 | } |
| 5800 | } |
| 5801 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5802 | public void handleUpdateLinkProperties(NetworkAgentInfo nai, LinkProperties newLp) { |
Lorenzo Colitti | 94229b3 | 2019-02-20 21:34:01 +0900 | [diff] [blame] | 5803 | ensureRunningOnConnectivityServiceThread(); |
| 5804 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 5805 | if (getNetworkAgentInfoForNetId(nai.network.netId) != nai) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5806 | // Ignore updates for disconnected networks |
| 5807 | return; |
| 5808 | } |
Rubin Xu | 6c1f6fd | 2017-09-11 15:21:10 +0100 | [diff] [blame] | 5809 | // newLp is already a defensive copy. |
| 5810 | newLp.ensureDirectlyConnectedRoutes(); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5811 | if (VDBG || DDBG) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5812 | log("Update of LinkProperties for " + nai.name() + |
| 5813 | "; created=" + nai.created + |
| 5814 | "; everConnected=" + nai.everConnected); |
| 5815 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5816 | updateLinkProperties(nai, newLp, new LinkProperties(nai.linkProperties)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5817 | } |
| 5818 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5819 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5820 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5821 | NetworkRequest nr = nai.requestAt(i); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5822 | // Don't send listening requests to factories. b/17393458 |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5823 | if (nr.isListen()) continue; |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5824 | sendUpdatedScoreToFactories(nr, nai); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5825 | } |
| 5826 | } |
| 5827 | |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5828 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, NetworkAgentInfo nai) { |
| 5829 | int score = 0; |
| 5830 | int serial = 0; |
| 5831 | if (nai != null) { |
| 5832 | score = nai.getCurrentScore(); |
| 5833 | serial = nai.factorySerialNumber; |
| 5834 | } |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5835 | if (VDBG || DDBG){ |
| 5836 | log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
| 5837 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5838 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5839 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, |
| 5840 | serial, networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5841 | } |
| 5842 | } |
| 5843 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5844 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 5845 | int notificationType) { |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5846 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE && !nri.mPendingIntentSent) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5847 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 5848 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 5849 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5850 | nri.mPendingIntentSent = true; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5851 | sendIntent(nri.mPendingIntent, intent); |
| 5852 | } |
| 5853 | // else not handled |
| 5854 | } |
| 5855 | |
| 5856 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 5857 | mPendingIntentWakeLock.acquire(); |
| 5858 | try { |
| 5859 | if (DBG) log("Sending " + pendingIntent); |
| 5860 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 5861 | } catch (PendingIntent.CanceledException e) { |
| 5862 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 5863 | mPendingIntentWakeLock.release(); |
| 5864 | releasePendingNetworkRequest(pendingIntent); |
| 5865 | } |
| 5866 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 5867 | } |
| 5868 | |
| 5869 | @Override |
| 5870 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 5871 | String resultData, Bundle resultExtras) { |
| 5872 | if (DBG) log("Finished sending " + pendingIntent); |
| 5873 | mPendingIntentWakeLock.release(); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5874 | // Release with a delay so the receiving client has an opportunity to put in its |
| 5875 | // own request. |
| 5876 | releasePendingNetworkRequestWithDelay(pendingIntent); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5877 | } |
| 5878 | |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5879 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5880 | NetworkAgentInfo networkAgent, int notificationType, int arg1) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5881 | if (nri.messenger == null) { |
| 5882 | return; // Default request has no msgr |
| 5883 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5884 | Bundle bundle = new Bundle(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5885 | // TODO: check if defensive copies of data is needed. |
| 5886 | putParcelable(bundle, new NetworkRequest(nri.request)); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5887 | Message msg = Message.obtain(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5888 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL) { |
| 5889 | putParcelable(bundle, networkAgent.network); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5890 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5891 | switch (notificationType) { |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5892 | case ConnectivityManager.CALLBACK_AVAILABLE: { |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 5893 | putParcelable(bundle, networkCapabilitiesRestrictedForCallerPermissions( |
| 5894 | networkAgent.networkCapabilities, nri.mPid, nri.mUid)); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5895 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5896 | // For this notification, arg1 contains the blocked status. |
| 5897 | msg.arg1 = arg1; |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5898 | break; |
| 5899 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5900 | case ConnectivityManager.CALLBACK_LOSING: { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5901 | msg.arg1 = arg1; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5902 | break; |
| 5903 | } |
| 5904 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5905 | // 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] | 5906 | final NetworkCapabilities nc = networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5907 | networkAgent.networkCapabilities, nri.mPid, nri.mUid); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5908 | putParcelable(bundle, nc); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5909 | break; |
| 5910 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5911 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5912 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5913 | break; |
| 5914 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5915 | case ConnectivityManager.CALLBACK_BLK_CHANGED: { |
junyulai | e6b3651 | 2018-10-24 22:38:06 +0800 | [diff] [blame] | 5916 | maybeLogBlockedStatusChanged(nri, networkAgent.network, arg1 != 0); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5917 | msg.arg1 = arg1; |
| 5918 | break; |
| 5919 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5920 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5921 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5922 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5923 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5924 | if (VDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 5925 | String notification = ConnectivityManager.getCallbackName(notificationType); |
| 5926 | log("sending notification " + notification + " for " + nri.request); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5927 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5928 | nri.messenger.send(msg); |
| 5929 | } catch (RemoteException e) { |
| 5930 | // may occur naturally in the race of binder death. |
| 5931 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 5932 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5933 | } |
| 5934 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5935 | private static <T extends Parcelable> void putParcelable(Bundle bundle, T t) { |
| 5936 | bundle.putParcelable(t.getClass().getSimpleName(), t); |
| 5937 | } |
| 5938 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5939 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5940 | if (nai.numRequestNetworkRequests() != 0) { |
| 5941 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5942 | NetworkRequest nr = nai.requestAt(i); |
| 5943 | // Ignore listening requests. |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5944 | if (nr.isListen()) continue; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5945 | loge("Dead network still had at least " + nr); |
| 5946 | break; |
| 5947 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5948 | } |
| 5949 | nai.asyncChannel.disconnect(); |
| 5950 | } |
| 5951 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5952 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 5953 | if (oldNetwork == null) { |
| 5954 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 5955 | return; |
| 5956 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5957 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5958 | |
| 5959 | // If we get here it means that the last linger timeout for this network expired. So there |
| 5960 | // must be no other active linger timers, and we must stop lingering. |
| 5961 | oldNetwork.clearLingerState(); |
| 5962 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 5963 | if (unneeded(oldNetwork, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5964 | // Tear the network down. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5965 | teardownUnneededNetwork(oldNetwork); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5966 | } else { |
| 5967 | // Put the network in the background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 5968 | updateCapabilities(oldNetwork.getCurrentScore(), oldNetwork, |
| 5969 | oldNetwork.networkCapabilities); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5970 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5971 | } |
| 5972 | |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 5973 | private void makeDefault(NetworkAgentInfo newNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5974 | if (DBG) log("Switching to new default network: " + newNetwork); |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 5975 | |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5976 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5977 | mNMS.setDefaultNetId(newNetwork.network.netId); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5978 | } catch (Exception e) { |
| 5979 | loge("Exception setting default network :" + e); |
| 5980 | } |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5981 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 5982 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5983 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5984 | updateTcpBufferSizes(newNetwork.linkProperties.getTcpBufferSizes()); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5985 | mDnsManager.setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5986 | notifyIfacesChangedForNetworkStats(); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5987 | // Fix up the NetworkCapabilities of any VPNs that don't specify underlying networks. |
| 5988 | updateAllVpnsCapabilities(); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5989 | } |
| 5990 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5991 | private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5992 | // For consistency with previous behaviour, send onLost callbacks before onAvailable. |
| 5993 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5994 | NetworkRequest nr = nri.request; |
| 5995 | if (!nr.isListen()) continue; |
| 5996 | if (nai.isSatisfyingRequest(nr.requestId) && !nai.satisfies(nr)) { |
| 5997 | nai.removeRequest(nri.request.requestId); |
| 5998 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_LOST, 0); |
| 5999 | } |
| 6000 | } |
| 6001 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6002 | if (capabilitiesChanged) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6003 | try { |
| 6004 | nai.networkMonitor().notifyNetworkCapabilitiesChanged(); |
| 6005 | } catch (RemoteException e) { |
| 6006 | e.rethrowFromSystemServer(); |
| 6007 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6008 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
| 6009 | } |
| 6010 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6011 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 6012 | NetworkRequest nr = nri.request; |
| 6013 | if (!nr.isListen()) continue; |
| 6014 | if (nai.satisfies(nr) && !nai.isSatisfyingRequest(nr.requestId)) { |
| 6015 | nai.addRequest(nr); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6016 | notifyNetworkAvailable(nai, nri); |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6017 | } |
| 6018 | } |
| 6019 | } |
| 6020 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6021 | // Handles a network appearing or improving its score. |
| 6022 | // |
| 6023 | // - Evaluates all current NetworkRequests that can be |
| 6024 | // satisfied by newNetwork, and reassigns to newNetwork |
| 6025 | // any such requests for which newNetwork is the best. |
| 6026 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6027 | // - Lingers any validated Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6028 | // needed. A network is needed if it is the best network for |
| 6029 | // one or more NetworkRequests, or if it is a VPN. |
| 6030 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 6031 | // - Tears down newNetwork if it just became validated |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6032 | // but turns out to be unneeded. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6033 | // |
| 6034 | // - If reapUnvalidatedNetworks==REAP, tears down unvalidated |
| 6035 | // networks that have no chance (i.e. even if validated) |
| 6036 | // of becoming the highest scoring network. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6037 | // |
| 6038 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 6039 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 6040 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 6041 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 6042 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 6043 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6044 | // @param newNetwork is the network to be matched against NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6045 | // @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be |
| 6046 | // performed to tear down unvalidated networks that have no chance (i.e. even if |
| 6047 | // validated) of becoming the highest scoring network. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6048 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6049 | ReapUnvalidatedNetworks reapUnvalidatedNetworks, long now) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6050 | if (!newNetwork.everConnected) return; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6051 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6052 | boolean isNewDefault = false; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 6053 | NetworkAgentInfo oldDefaultNetwork = null; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6054 | |
| 6055 | final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork(); |
| 6056 | final int score = newNetwork.getCurrentScore(); |
| 6057 | |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6058 | if (VDBG || DDBG) log("rematching " + newNetwork.name()); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6059 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6060 | // Find and migrate to this Network any NetworkRequests for |
| 6061 | // which this network is now the best. |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 6062 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<>(); |
| 6063 | ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<>(); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6064 | NetworkCapabilities nc = newNetwork.networkCapabilities; |
| 6065 | if (VDBG) log(" network has: " + nc); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6066 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6067 | // Process requests in the first pass and listens in the second pass. This allows us to |
| 6068 | // change a network's capabilities depending on which requests it has. This is only |
| 6069 | // correct if the change in capabilities doesn't affect whether the network satisfies |
| 6070 | // requests or not, and doesn't affect the network's score. |
| 6071 | if (nri.request.isListen()) continue; |
| 6072 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6073 | final NetworkAgentInfo currentNetwork = getNetworkForRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6074 | final boolean satisfies = newNetwork.satisfies(nri.request); |
| 6075 | if (newNetwork == currentNetwork && satisfies) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6076 | if (VDBG) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 6077 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 6078 | " request " + nri.request.requestId + ". No change."); |
| 6079 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 6080 | keep = true; |
| 6081 | continue; |
| 6082 | } |
| 6083 | |
| 6084 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6085 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6086 | if (satisfies) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6087 | // next check if it's better than any current network we're using for |
| 6088 | // this request |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6089 | if (VDBG || DDBG) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6090 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6091 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6092 | ", newScore = " + score); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6093 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6094 | if (currentNetwork == null || currentNetwork.getCurrentScore() < score) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 6095 | if (VDBG) log("rematch for " + newNetwork.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6096 | if (currentNetwork != null) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6097 | if (VDBG || DDBG){ |
| 6098 | log(" accepting network in place of " + currentNetwork.name()); |
| 6099 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6100 | currentNetwork.removeRequest(nri.request.requestId); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6101 | currentNetwork.lingerRequest(nri.request, now, mLingerDelayMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6102 | affectedNetworks.add(currentNetwork); |
| 6103 | } else { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6104 | if (VDBG || DDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6105 | } |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6106 | newNetwork.unlingerRequest(nri.request); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6107 | setNetworkForRequest(nri.request.requestId, newNetwork); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 6108 | if (!newNetwork.addRequest(nri.request)) { |
| 6109 | Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request); |
| 6110 | } |
| 6111 | addedRequests.add(nri); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6112 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6113 | // Tell NetworkFactories about the new score, so they can stop |
| 6114 | // trying to connect if they know they cannot match it. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6115 | // 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] | 6116 | // network. Think about if there is a way to reduce this. Push |
| 6117 | // netid->request mapping to each factory? |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 6118 | sendUpdatedScoreToFactories(nri.request, newNetwork); |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 6119 | if (isDefaultRequest(nri)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6120 | isNewDefault = true; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 6121 | oldDefaultNetwork = currentNetwork; |
Lorenzo Colitti | c2e10bb | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 6122 | if (currentNetwork != null) { |
| 6123 | mLingerMonitor.noteLingerDefaultNetwork(currentNetwork, newNetwork); |
| 6124 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6125 | } |
| 6126 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6127 | } else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6128 | // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri", |
| 6129 | // mark it as no longer satisfying "nri". Because networks are processed by |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 6130 | // rematchAllNetworksAndRequests() in descending score order, "currentNetwork" will |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6131 | // match "newNetwork" before this loop will encounter a "currentNetwork" with higher |
| 6132 | // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri". |
| 6133 | // This means this code doesn't have to handle the case where "currentNetwork" no |
| 6134 | // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork". |
| 6135 | if (DBG) { |
| 6136 | log("Network " + newNetwork.name() + " stopped satisfying" + |
| 6137 | " request " + nri.request.requestId); |
| 6138 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6139 | newNetwork.removeRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6140 | if (currentNetwork == newNetwork) { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6141 | clearNetworkForRequest(nri.request.requestId); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 6142 | sendUpdatedScoreToFactories(nri.request, null); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6143 | } else { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6144 | Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + |
| 6145 | newNetwork.name() + |
| 6146 | " without updating mNetworkForRequestId or factories!"); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6147 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6148 | // TODO: Technically, sending CALLBACK_LOST here is |
| 6149 | // incorrect if there is a replacement network currently |
| 6150 | // connected that can satisfy nri, which is a request |
| 6151 | // (not a listen). However, the only capability that can both |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6152 | // a) be requested and b) change is NET_CAPABILITY_TRUSTED, |
| 6153 | // so this code is only incorrect for a network that loses |
| 6154 | // the TRUSTED capability, which is a rare case. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6155 | callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6156 | } |
| 6157 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6158 | if (isNewDefault) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 6159 | updateDataActivityTracking(newNetwork, oldDefaultNetwork); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6160 | // Notify system services that this network is up. |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 6161 | makeDefault(newNetwork); |
| 6162 | // 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] | 6163 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent( |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 6164 | now, newNetwork, oldDefaultNetwork); |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 6165 | // Have a new default network, release the transition wakelock in |
| 6166 | scheduleReleaseNetworkTransitionWakelock(); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6167 | } |
| 6168 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6169 | if (!newNetwork.networkCapabilities.equalRequestableCapabilities(nc)) { |
| 6170 | Slog.wtf(TAG, String.format( |
| 6171 | "BUG: %s changed requestable capabilities during rematch: %s -> %s", |
Andreas Gampe | d4f64c4 | 2017-07-11 15:14:41 -0700 | [diff] [blame] | 6172 | newNetwork.name(), nc, newNetwork.networkCapabilities)); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6173 | } |
| 6174 | if (newNetwork.getCurrentScore() != score) { |
| 6175 | Slog.wtf(TAG, String.format( |
| 6176 | "BUG: %s changed score during rematch: %d -> %d", |
liangweikang@xiaomi.com | e9a7c26 | 2017-06-29 14:36:30 +0800 | [diff] [blame] | 6177 | newNetwork.name(), score, newNetwork.getCurrentScore())); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6178 | } |
| 6179 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6180 | // Second pass: process all listens. |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6181 | if (wasBackgroundNetwork != newNetwork.isBackgroundNetwork()) { |
| 6182 | // If the network went from background to foreground or vice versa, we need to update |
| 6183 | // its foreground state. It is safe to do this after rematching the requests because |
| 6184 | // NET_CAPABILITY_FOREGROUND does not affect requests, as is not a requestable |
| 6185 | // 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] | 6186 | updateCapabilities(score, newNetwork, newNetwork.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6187 | } else { |
| 6188 | processListenRequests(newNetwork, false); |
| 6189 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6190 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6191 | // do this after the default net is switched, but |
| 6192 | // before LegacyTypeTracker sends legacy broadcasts |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6193 | for (NetworkRequestInfo nri : addedRequests) notifyNetworkAvailable(newNetwork, nri); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6194 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6195 | // Linger any networks that are no longer needed. This should be done after sending the |
| 6196 | // available callback for newNetwork. |
| 6197 | for (NetworkAgentInfo nai : affectedNetworks) { |
| 6198 | updateLingerState(nai, now); |
| 6199 | } |
| 6200 | // Possibly unlinger newNetwork. Unlingering a network does not send any callbacks so it |
| 6201 | // does not need to be done in any particular order. |
| 6202 | updateLingerState(newNetwork, now); |
| 6203 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6204 | if (isNewDefault) { |
| 6205 | // Maintain the illusion: since the legacy API only |
| 6206 | // understands one network at a time, we must pretend |
| 6207 | // that the current default network disconnected before |
| 6208 | // the new one connected. |
| 6209 | if (oldDefaultNetwork != null) { |
| 6210 | mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), |
| 6211 | oldDefaultNetwork, true); |
| 6212 | } |
| 6213 | mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0; |
| 6214 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
| 6215 | notifyLockdownVpn(newNetwork); |
| 6216 | } |
| 6217 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6218 | if (keep) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6219 | // Notify battery stats service about this network, both the normal |
| 6220 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6221 | // 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] | 6222 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6223 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 6224 | final int type = newNetwork.networkInfo.getType(); |
| 6225 | |
| 6226 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 6227 | bs.noteNetworkInterfaceType(baseIface, type); |
| 6228 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 6229 | final String stackedIface = stacked.getInterfaceName(); |
| 6230 | bs.noteNetworkInterfaceType(stackedIface, type); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6231 | } |
| 6232 | } catch (RemoteException ignored) { |
| 6233 | } |
| 6234 | |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6235 | // This has to happen after the notifyNetworkCallbacks as that tickles each |
| 6236 | // ConnectivityManager instance so that legacy requests correctly bind dns |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6237 | // requests to this network. The legacy users are listening for this broadcast |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6238 | // and will generally do a dns request so they can ensureRouteToHost and if |
| 6239 | // they do that before the callbacks happen they'll use the default network. |
| 6240 | // |
| 6241 | // TODO: Is there still a race here? We send the broadcast |
| 6242 | // after sending the callback, but if the app can receive the |
| 6243 | // broadcast before the callback, it might still break. |
| 6244 | // |
| 6245 | // This *does* introduce a race where if the user uses the new api |
| 6246 | // (notification callbacks) and then uses the old api (getNetworkInfo(type)) |
| 6247 | // they may get old info. Reverse this after the old startUsing api is removed. |
| 6248 | // 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] | 6249 | for (int i = 0; i < newNetwork.numNetworkRequests(); i++) { |
| 6250 | NetworkRequest nr = newNetwork.requestAt(i); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 6251 | if (nr.legacyType != TYPE_NONE && nr.isRequest()) { |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6252 | // legacy type tracker filters out repeat adds |
| 6253 | mLegacyTypeTracker.add(nr.legacyType, newNetwork); |
| 6254 | } |
| 6255 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 6256 | |
| 6257 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 6258 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 6259 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 6260 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 6261 | if (newNetwork.isVPN()) { |
| 6262 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 6263 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6264 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6265 | if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) { |
| 6266 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 6267 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6268 | if (nai.getLingerExpiry() > 0) { |
| 6269 | // This network has active linger timers and no requests, but is not |
| 6270 | // lingering. Linger it. |
| 6271 | // |
| 6272 | // One way (the only way?) this can happen if this network is unvalidated |
| 6273 | // and became unneeded due to another network improving its score to the |
| 6274 | // point where this network will no longer be able to satisfy any requests |
| 6275 | // even if it validates. |
| 6276 | updateLingerState(nai, now); |
| 6277 | } else { |
| 6278 | if (DBG) log("Reaping " + nai.name()); |
| 6279 | teardownUnneededNetwork(nai); |
| 6280 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6281 | } |
| 6282 | } |
| 6283 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6284 | } |
| 6285 | |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6286 | /** |
| 6287 | * Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 6288 | * being disconnected. |
| 6289 | * @param changed If only one Network's score or capabilities have been modified since the last |
| 6290 | * time this function was called, pass this Network in this argument, otherwise pass |
| 6291 | * null. |
| 6292 | * @param oldScore If only one Network has been changed but its NetworkCapabilities have not |
| 6293 | * changed, pass in the Network's score (from getCurrentScore()) prior to the change via |
| 6294 | * this argument, otherwise pass {@code changed.getCurrentScore()} or 0 if |
| 6295 | * {@code changed} is {@code null}. This is because NetworkCapabilities influence a |
| 6296 | * network's score. |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6297 | */ |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6298 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6299 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 6300 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 6301 | // 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] | 6302 | // satisfying a NetworkRequest that "changed" currently satisfies. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6303 | |
| 6304 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 6305 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 6306 | // rematchNetworkAndRequests() handles. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6307 | final long now = SystemClock.elapsedRealtime(); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6308 | if (changed != null && oldScore < changed.getCurrentScore()) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6309 | rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP, now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6310 | } else { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6311 | final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray( |
| 6312 | new NetworkAgentInfo[mNetworkAgentInfos.size()]); |
| 6313 | // Rematch higher scoring networks first to prevent requests first matching a lower |
| 6314 | // scoring network and then a higher scoring network, which could produce multiple |
| 6315 | // callbacks and inadvertently unlinger networks. |
| 6316 | Arrays.sort(nais); |
| 6317 | for (NetworkAgentInfo nai : nais) { |
| 6318 | rematchNetworkAndRequests(nai, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6319 | // Only reap the last time through the loop. Reaping before all rematching |
| 6320 | // is complete could incorrectly teardown a network that hasn't yet been |
| 6321 | // rematched. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6322 | (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6323 | : ReapUnvalidatedNetworks.REAP, |
| 6324 | now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6325 | } |
| 6326 | } |
| 6327 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6328 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6329 | private void updateInetCondition(NetworkAgentInfo nai) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6330 | // 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] | 6331 | if (!nai.everValidated) return; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6332 | // For now only update icons for default connection. |
| 6333 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 6334 | if (!isDefaultNetwork(nai)) return; |
| 6335 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6336 | int newInetCondition = nai.lastValidated ? 100 : 0; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6337 | // Don't repeat publish. |
| 6338 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 6339 | |
| 6340 | mDefaultInetConditionPublished = newInetCondition; |
| 6341 | sendInetConditionBroadcast(nai.networkInfo); |
| 6342 | } |
| 6343 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6344 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6345 | synchronized (mVpns) { |
| 6346 | if (mLockdownTracker != null) { |
| 6347 | if (nai != null && nai.isVPN()) { |
| 6348 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 6349 | } else { |
| 6350 | mLockdownTracker.onNetworkInfoChanged(); |
| 6351 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6352 | } |
| 6353 | } |
| 6354 | } |
| 6355 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6356 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6357 | final NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6358 | NetworkInfo oldInfo = null; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6359 | final int oldScore = networkAgent.getCurrentScore(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6360 | synchronized (networkAgent) { |
| 6361 | oldInfo = networkAgent.networkInfo; |
| 6362 | networkAgent.networkInfo = newInfo; |
| 6363 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6364 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6365 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6366 | if (DBG) { |
| 6367 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 6368 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 6369 | " to " + state); |
| 6370 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6371 | |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6372 | if (!networkAgent.created |
| 6373 | && (state == NetworkInfo.State.CONNECTED |
| 6374 | || (state == NetworkInfo.State.CONNECTING && networkAgent.isVPN()))) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6375 | |
| 6376 | // A network that has just connected has zero requests and is thus a foreground network. |
| 6377 | networkAgent.networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND); |
| 6378 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6379 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6380 | // 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] | 6381 | if (networkAgent.isVPN()) { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6382 | mNMS.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 6383 | (networkAgent.networkMisc == null || |
| 6384 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6385 | } else { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6386 | mNMS.createPhysicalNetwork(networkAgent.network.netId, |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 6387 | getNetworkPermission(networkAgent.networkCapabilities)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6388 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6389 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 6390 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 6391 | + e.getMessage()); |
| 6392 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6393 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6394 | networkAgent.created = true; |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6395 | } |
| 6396 | |
| 6397 | if (!networkAgent.everConnected && state == NetworkInfo.State.CONNECTED) { |
| 6398 | networkAgent.everConnected = true; |
| 6399 | |
Lorenzo Colitti | 2df4c7d | 2018-02-27 22:47:01 +0900 | [diff] [blame] | 6400 | if (networkAgent.linkProperties == null) { |
| 6401 | Slog.wtf(TAG, networkAgent.name() + " connected with null LinkProperties"); |
| 6402 | } |
| 6403 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 6404 | handlePerNetworkPrivateDnsConfig(networkAgent, mDnsManager.getPrivateDnsConfig()); |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 6405 | updateLinkProperties(networkAgent, new LinkProperties(networkAgent.linkProperties), |
| 6406 | null); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6407 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6408 | // Until parceled LinkProperties are sent directly to NetworkMonitor, the connect |
| 6409 | // command must be sent after updating LinkProperties to maximize chances of |
| 6410 | // NetworkMonitor seeing the correct LinkProperties when starting. |
| 6411 | // TODO: pass LinkProperties to the NetworkMonitor in the notifyNetworkConnected call. |
| 6412 | try { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 6413 | if (networkAgent.networkMisc.acceptPartialConnectivity) { |
| 6414 | networkAgent.networkMonitor().setAcceptPartialConnectivity(); |
| 6415 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6416 | networkAgent.networkMonitor().notifyNetworkConnected(); |
| 6417 | } catch (RemoteException e) { |
| 6418 | e.rethrowFromSystemServer(); |
| 6419 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 6420 | scheduleUnvalidatedPrompt(networkAgent); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6421 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6422 | // Whether a particular NetworkRequest listen should cause signal strength thresholds to |
| 6423 | // be communicated to a particular NetworkAgent depends only on the network's immutable, |
| 6424 | // capabilities, so it only needs to be done once on initial connect, not every time the |
| 6425 | // network's capabilities change. Note that we do this before rematching the network, |
| 6426 | // so we could decide to tear it down immediately afterwards. That's fine though - on |
| 6427 | // disconnection NetworkAgents should stop any signal strength monitoring they have been |
| 6428 | // doing. |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 6429 | updateSignalStrengthThresholds(networkAgent, "CONNECT", null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6430 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6431 | if (networkAgent.isVPN()) { |
| 6432 | updateAllVpnsCapabilities(); |
| 6433 | } |
| 6434 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6435 | // Consider network even though it is not yet validated. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6436 | final long now = SystemClock.elapsedRealtime(); |
| 6437 | rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP, now); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6438 | |
| 6439 | // This has to happen after matching the requests, because callbacks are just requests. |
| 6440 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6441 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6442 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6443 | if (networkAgent.isVPN()) { |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 6444 | updateUids(networkAgent, networkAgent.networkCapabilities, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6445 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 6446 | disconnectAndDestroyNetwork(networkAgent); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 6447 | if (networkAgent.isVPN()) { |
| 6448 | // As the active or bound network changes for apps, broadcast the default proxy, as |
| 6449 | // apps may need to update their proxy data. This is called after disconnecting from |
| 6450 | // VPN to make sure we do not broadcast the old proxy data. |
| 6451 | // TODO(b/122649188): send the broadcast only to VPN users. |
| 6452 | mProxyTracker.sendProxyBroadcast(); |
| 6453 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6454 | } else if ((oldInfo != null && oldInfo.getState() == NetworkInfo.State.SUSPENDED) || |
| 6455 | state == NetworkInfo.State.SUSPENDED) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6456 | // going into or coming out of SUSPEND: re-score and notify |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6457 | if (networkAgent.getCurrentScore() != oldScore) { |
Paul Jensen | 3b9ce37 | 2015-07-10 12:19:38 -0400 | [diff] [blame] | 6458 | rematchAllNetworksAndRequests(networkAgent, oldScore); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6459 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 6460 | updateCapabilities(networkAgent.getCurrentScore(), networkAgent, |
| 6461 | networkAgent.networkCapabilities); |
| 6462 | // TODO (b/73132094) : remove this call once the few users of onSuspended and |
| 6463 | // onResumed have been removed. |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6464 | notifyNetworkCallbacks(networkAgent, (state == NetworkInfo.State.SUSPENDED ? |
| 6465 | ConnectivityManager.CALLBACK_SUSPENDED : |
| 6466 | ConnectivityManager.CALLBACK_RESUMED)); |
| 6467 | mLegacyTypeTracker.update(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6468 | } |
| 6469 | } |
| 6470 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6471 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6472 | if (VDBG || DDBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 6473 | if (score < 0) { |
| 6474 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 6475 | "). Bumping score to min of 0"); |
| 6476 | score = 0; |
| 6477 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6478 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6479 | final int oldScore = nai.getCurrentScore(); |
| 6480 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6481 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6482 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6483 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 6484 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 6485 | } |
| 6486 | |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6487 | // Notify only this one new request of the current state. Transfer all the |
| 6488 | // current state by calling NetworkCapabilities and LinkProperties callbacks |
| 6489 | // so that callers can be guaranteed to have as close to atomicity in state |
| 6490 | // transfer as can be supported by this current API. |
| 6491 | protected void notifyNetworkAvailable(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
Etan Cohen | 681fcda | 2016-10-27 15:05:50 -0700 | [diff] [blame] | 6492 | mHandler.removeMessages(EVENT_TIMEOUT_NETWORK_REQUEST, nri); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6493 | if (nri.mPendingIntent != null) { |
| 6494 | sendPendingIntentForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE); |
| 6495 | // Attempt no subsequent state pushes where intents are involved. |
| 6496 | return; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6497 | } |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6498 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 6499 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6500 | final boolean blocked = isUidNetworkingWithVpnBlocked(nri.mUid, mUidRules.get(nri.mUid), |
| 6501 | metered, mRestrictBackground); |
| 6502 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE, blocked ? 1 : 0); |
| 6503 | } |
| 6504 | |
| 6505 | /** |
| 6506 | * Notify of the blocked state apps with a registered callback matching a given NAI. |
| 6507 | * |
| 6508 | * Unlike other callbacks, blocked status is different between each individual uid. So for |
| 6509 | * any given nai, all requests need to be considered according to the uid who filed it. |
| 6510 | * |
| 6511 | * @param nai The target NetworkAgentInfo. |
| 6512 | * @param oldMetered True if the previous network capabilities is metered. |
| 6513 | * @param newRestrictBackground True if data saver is enabled. |
| 6514 | */ |
| 6515 | private void maybeNotifyNetworkBlocked(NetworkAgentInfo nai, boolean oldMetered, |
| 6516 | boolean newMetered, boolean oldRestrictBackground, boolean newRestrictBackground) { |
| 6517 | |
| 6518 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6519 | NetworkRequest nr = nai.requestAt(i); |
| 6520 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6521 | final int uidRules = mUidRules.get(nri.mUid); |
| 6522 | final boolean oldBlocked, newBlocked; |
| 6523 | // mVpns lock needs to be hold here to ensure that the active VPN cannot be changed |
| 6524 | // between these two calls. |
| 6525 | synchronized (mVpns) { |
| 6526 | oldBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, oldMetered, |
| 6527 | oldRestrictBackground); |
| 6528 | newBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, newMetered, |
| 6529 | newRestrictBackground); |
| 6530 | } |
| 6531 | if (oldBlocked != newBlocked) { |
| 6532 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, |
| 6533 | encodeBool(newBlocked)); |
| 6534 | } |
| 6535 | } |
| 6536 | } |
| 6537 | |
| 6538 | /** |
| 6539 | * Notify apps with a given UID of the new blocked state according to new uid rules. |
| 6540 | * @param uid The uid for which the rules changed. |
| 6541 | * @param newRules The new rules to apply. |
| 6542 | */ |
| 6543 | private void maybeNotifyNetworkBlockedForNewUidRules(int uid, int newRules) { |
| 6544 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6545 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6546 | final boolean oldBlocked, newBlocked; |
| 6547 | // TODO: Consider that doze mode or turn on/off battery saver would deliver lots of uid |
| 6548 | // rules changed event. And this function actually loop through all connected nai and |
| 6549 | // its requests. It seems that mVpns lock will be grabbed frequently in this case. |
| 6550 | // Reduce the number of locking or optimize the use of lock are likely needed in future. |
| 6551 | synchronized (mVpns) { |
| 6552 | oldBlocked = isUidNetworkingWithVpnBlocked( |
| 6553 | uid, mUidRules.get(uid), metered, mRestrictBackground); |
| 6554 | newBlocked = isUidNetworkingWithVpnBlocked( |
| 6555 | uid, newRules, metered, mRestrictBackground); |
| 6556 | } |
| 6557 | if (oldBlocked == newBlocked) { |
| 6558 | return; |
| 6559 | } |
| 6560 | final int arg = encodeBool(newBlocked); |
| 6561 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6562 | NetworkRequest nr = nai.requestAt(i); |
| 6563 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6564 | if (nri != null && nri.mUid == uid) { |
| 6565 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, arg); |
| 6566 | } |
| 6567 | } |
| 6568 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6569 | } |
| 6570 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6571 | private void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, DetailedState state, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 6572 | // The NetworkInfo we actually send out has no bearing on the real |
| 6573 | // state of affairs. For example, if the default connection is mobile, |
| 6574 | // and a request for HIPRI has just gone away, we need to pretend that |
| 6575 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 6576 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 6577 | // and is still connected. |
| 6578 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 6579 | info.setType(type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6580 | if (state != DetailedState.DISCONNECTED) { |
| 6581 | info.setDetailedState(state, null, info.getExtraInfo()); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6582 | sendConnectedBroadcast(info); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6583 | } else { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6584 | info.setDetailedState(state, info.getReason(), info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6585 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 6586 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 6587 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 6588 | if (info.isFailover()) { |
| 6589 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 6590 | nai.networkInfo.setFailover(false); |
| 6591 | } |
| 6592 | if (info.getReason() != null) { |
| 6593 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 6594 | } |
| 6595 | if (info.getExtraInfo() != null) { |
| 6596 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 6597 | } |
| 6598 | NetworkAgentInfo newDefaultAgent = null; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6599 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6600 | newDefaultAgent = getDefaultNetwork(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6601 | if (newDefaultAgent != null) { |
| 6602 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 6603 | newDefaultAgent.networkInfo); |
| 6604 | } else { |
| 6605 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 6606 | } |
| 6607 | } |
| 6608 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 6609 | mDefaultInetConditionPublished); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6610 | sendStickyBroadcast(intent); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6611 | if (newDefaultAgent != null) { |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6612 | sendConnectedBroadcast(newDefaultAgent.networkInfo); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6613 | } |
| 6614 | } |
| 6615 | } |
| 6616 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6617 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType, int arg1) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6618 | if (VDBG || DDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 6619 | String notification = ConnectivityManager.getCallbackName(notifyType); |
| 6620 | log("notifyType " + notification + " for " + networkAgent.name()); |
| 6621 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6622 | for (int i = 0; i < networkAgent.numNetworkRequests(); i++) { |
| 6623 | NetworkRequest nr = networkAgent.requestAt(i); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6624 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6625 | if (VDBG) log(" sending notification for " + nr); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6626 | // TODO: if we're in the middle of a rematch, can we send a CAP_CHANGED callback for |
| 6627 | // a network that no longer satisfies the listen? |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6628 | if (nri.mPendingIntent == null) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6629 | callCallbackForRequest(nri, networkAgent, notifyType, arg1); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6630 | } else { |
| 6631 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 6632 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6633 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6634 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6635 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6636 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
| 6637 | notifyNetworkCallbacks(networkAgent, notifyType, 0); |
| 6638 | } |
| 6639 | |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6640 | /** |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6641 | * Returns the list of all interfaces that could be used by network traffic that does not |
| 6642 | * explicitly specify a network. This includes the default network, but also all VPNs that are |
| 6643 | * currently connected. |
| 6644 | * |
| 6645 | * Must be called on the handler thread. |
| 6646 | */ |
| 6647 | private Network[] getDefaultNetworks() { |
Varun Anand | d33cbc6 | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6648 | ensureRunningOnConnectivityServiceThread(); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6649 | ArrayList<Network> defaultNetworks = new ArrayList<>(); |
| 6650 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 6651 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6652 | if (nai.everConnected && (nai == defaultNetwork || nai.isVPN())) { |
| 6653 | defaultNetworks.add(nai.network); |
| 6654 | } |
| 6655 | } |
| 6656 | return defaultNetworks.toArray(new Network[0]); |
| 6657 | } |
| 6658 | |
| 6659 | /** |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6660 | * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the |
| 6661 | * properties tracked by NetworkStatsService on an active iface has changed. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6662 | */ |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6663 | private void notifyIfacesChangedForNetworkStats() { |
Varun Anand | d33cbc6 | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6664 | ensureRunningOnConnectivityServiceThread(); |
| 6665 | String activeIface = null; |
| 6666 | LinkProperties activeLinkProperties = getActiveLinkProperties(); |
| 6667 | if (activeLinkProperties != null) { |
| 6668 | activeIface = activeLinkProperties.getInterfaceName(); |
| 6669 | } |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6670 | try { |
Varun Anand | d33cbc6 | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6671 | mStatsService.forceUpdateIfaces( |
| 6672 | getDefaultNetworks(), getAllVpnInfo(), getAllNetworkState(), activeIface); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6673 | } catch (Exception ignored) { |
| 6674 | } |
| 6675 | } |
| 6676 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6677 | @Override |
| 6678 | public boolean addVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6679 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6680 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6681 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6682 | return mVpns.get(user).addAddress(address, prefixLength); |
| 6683 | } |
| 6684 | } |
| 6685 | |
| 6686 | @Override |
| 6687 | public boolean removeVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6688 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6689 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6690 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6691 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 6692 | } |
| 6693 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6694 | |
| 6695 | @Override |
| 6696 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6697 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6698 | final boolean success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6699 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6700 | throwIfLockdownEnabled(); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6701 | success = mVpns.get(user).setUnderlyingNetworks(networks); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6702 | } |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6703 | if (success) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6704 | mHandler.post(() -> { |
| 6705 | // Update VPN's capabilities based on updated underlying network set. |
| 6706 | updateAllVpnsCapabilities(); |
| 6707 | notifyIfacesChangedForNetworkStats(); |
| 6708 | }); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6709 | } |
| 6710 | return success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6711 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6712 | |
| 6713 | @Override |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6714 | public String getCaptivePortalServerUrl() { |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 6715 | enforceConnectivityInternalPermission(); |
Remi NGUYEN VAN | 0bd294c | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 6716 | final String defaultUrl = mContext.getResources().getString( |
| 6717 | R.string.config_networkDefaultCaptivePortalServerUrl); |
| 6718 | return NetworkMonitorUtils.getCaptivePortalServerHttpUrl(mContext, defaultUrl); |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6719 | } |
| 6720 | |
| 6721 | @Override |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6722 | public void startNattKeepalive(Network network, int intervalSeconds, |
| 6723 | ISocketKeepaliveCallback cb, String srcAddr, int srcPort, String dstAddr) { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6724 | enforceKeepalivePermission(); |
| 6725 | mKeepaliveTracker.startNattKeepalive( |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 6726 | getNetworkAgentInfoForNetwork(network), null /* fd */, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6727 | intervalSeconds, cb, |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6728 | srcAddr, srcPort, dstAddr, NattSocketKeepalive.NATT_PORT); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6729 | } |
| 6730 | |
| 6731 | @Override |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6732 | public void startNattKeepaliveWithFd(Network network, FileDescriptor fd, int resourceId, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6733 | int intervalSeconds, ISocketKeepaliveCallback cb, String srcAddr, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6734 | String dstAddr) { |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6735 | mKeepaliveTracker.startNattKeepalive( |
| 6736 | getNetworkAgentInfoForNetwork(network), fd, resourceId, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6737 | intervalSeconds, cb, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6738 | srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT); |
| 6739 | } |
| 6740 | |
| 6741 | @Override |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6742 | public void startTcpKeepalive(Network network, FileDescriptor fd, int intervalSeconds, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6743 | ISocketKeepaliveCallback cb) { |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6744 | enforceKeepalivePermission(); |
| 6745 | mKeepaliveTracker.startTcpKeepalive( |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6746 | getNetworkAgentInfoForNetwork(network), fd, intervalSeconds, cb); |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6747 | } |
| 6748 | |
| 6749 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6750 | public void stopKeepalive(Network network, int slot) { |
| 6751 | mHandler.sendMessage(mHandler.obtainMessage( |
junyulai | 0683511 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6752 | NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE, slot, SocketKeepalive.SUCCESS, network)); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6753 | } |
| 6754 | |
| 6755 | @Override |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6756 | public void factoryReset() { |
| 6757 | enforceConnectivityInternalPermission(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6758 | |
| 6759 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 6760 | return; |
| 6761 | } |
| 6762 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 6763 | final int userId = UserHandle.getCallingUserId(); |
| 6764 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6765 | // Turn airplane mode off |
| 6766 | setAirplaneMode(false); |
| 6767 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6768 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 6769 | // Untether |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6770 | String pkgName = mContext.getOpPackageName(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6771 | for (String tether : getTetheredIfaces()) { |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6772 | untether(tether, pkgName); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6773 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6774 | } |
| 6775 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6776 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6777 | // Remove always-on package |
| 6778 | synchronized (mVpns) { |
| 6779 | final String alwaysOnPackage = getAlwaysOnVpnPackage(userId); |
| 6780 | if (alwaysOnPackage != null) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 6781 | setAlwaysOnVpnPackage(userId, null, false, null); |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6782 | setVpnPackageAuthorization(alwaysOnPackage, userId, false); |
| 6783 | } |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6784 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6785 | // Turn Always-on VPN off |
| 6786 | if (mLockdownEnabled && userId == UserHandle.USER_SYSTEM) { |
| 6787 | final long ident = Binder.clearCallingIdentity(); |
| 6788 | try { |
| 6789 | mKeyStore.delete(Credentials.LOCKDOWN_VPN); |
| 6790 | mLockdownEnabled = false; |
| 6791 | setLockdownTracker(null); |
| 6792 | } finally { |
| 6793 | Binder.restoreCallingIdentity(ident); |
| 6794 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6795 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6796 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6797 | // Turn VPN off |
| 6798 | VpnConfig vpnConfig = getVpnConfig(userId); |
| 6799 | if (vpnConfig != null) { |
| 6800 | if (vpnConfig.legacy) { |
| 6801 | prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, userId); |
| 6802 | } else { |
| 6803 | // Prevent this app (packagename = vpnConfig.user) from initiating |
| 6804 | // VPN connections in the future without user intervention. |
| 6805 | setVpnPackageAuthorization(vpnConfig.user, userId, false); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6806 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6807 | prepareVpn(null, VpnConfig.LEGACY_VPN, userId); |
| 6808 | } |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6809 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6810 | } |
| 6811 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 6812 | |
| 6813 | Settings.Global.putString(mContext.getContentResolver(), |
| 6814 | Settings.Global.NETWORK_AVOID_BAD_WIFI, null); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6815 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6816 | |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 6817 | @Override |
| 6818 | public byte[] getNetworkWatchlistConfigHash() { |
| 6819 | NetworkWatchlistManager nwm = mContext.getSystemService(NetworkWatchlistManager.class); |
| 6820 | if (nwm == null) { |
| 6821 | loge("Unable to get NetworkWatchlistManager"); |
| 6822 | return null; |
| 6823 | } |
| 6824 | // Redirect it to network watchlist service to access watchlist file and calculate hash. |
| 6825 | return nwm.getWatchlistConfigHash(); |
| 6826 | } |
| 6827 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6828 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 6829 | MultinetworkPolicyTracker createMultinetworkPolicyTracker(Context c, Handler h, Runnable r) { |
| 6830 | return new MultinetworkPolicyTracker(c, h, r); |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | @VisibleForTesting |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6834 | public WakeupMessage makeWakeupMessage(Context c, Handler h, String s, int cmd, Object obj) { |
| 6835 | return new WakeupMessage(c, h, s, cmd, 0, 0, obj); |
| 6836 | } |
| 6837 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 6838 | @VisibleForTesting |
| 6839 | public boolean hasService(String name) { |
| 6840 | return ServiceManager.checkService(name) != null; |
| 6841 | } |
| 6842 | |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 6843 | @VisibleForTesting |
| 6844 | protected IpConnectivityMetrics.Logger metricsLogger() { |
| 6845 | return checkNotNull(LocalServices.getService(IpConnectivityMetrics.Logger.class), |
| 6846 | "no IpConnectivityMetrics service"); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 6847 | } |
| 6848 | |
| 6849 | private void logNetworkEvent(NetworkAgentInfo nai, int evtype) { |
Hugo Benichi | edf5c24 | 2017-11-11 08:06:43 +0900 | [diff] [blame] | 6850 | int[] transports = nai.networkCapabilities.getTransportTypes(); |
| 6851 | mMetricsLog.log(nai.network.netId, transports, new NetworkEvent(evtype)); |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 6852 | } |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 6853 | |
| 6854 | private static boolean toBool(int encodedBoolean) { |
| 6855 | return encodedBoolean != 0; // Only 0 means false. |
| 6856 | } |
| 6857 | |
| 6858 | private static int encodeBool(boolean b) { |
| 6859 | return b ? 1 : 0; |
| 6860 | } |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 6861 | |
| 6862 | @Override |
| 6863 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 6864 | FileDescriptor err, String[] args, ShellCallback callback, |
| 6865 | ResultReceiver resultReceiver) { |
| 6866 | (new ShellCmd()).exec(this, in, out, err, args, callback, resultReceiver); |
| 6867 | } |
| 6868 | |
| 6869 | private class ShellCmd extends ShellCommand { |
| 6870 | |
| 6871 | @Override |
| 6872 | public int onCommand(String cmd) { |
| 6873 | if (cmd == null) { |
| 6874 | return handleDefaultCommands(cmd); |
| 6875 | } |
| 6876 | final PrintWriter pw = getOutPrintWriter(); |
| 6877 | try { |
| 6878 | switch (cmd) { |
| 6879 | case "airplane-mode": |
| 6880 | final String action = getNextArg(); |
| 6881 | if ("enable".equals(action)) { |
| 6882 | setAirplaneMode(true); |
| 6883 | return 0; |
| 6884 | } else if ("disable".equals(action)) { |
| 6885 | setAirplaneMode(false); |
| 6886 | return 0; |
| 6887 | } else if (action == null) { |
| 6888 | final ContentResolver cr = mContext.getContentResolver(); |
| 6889 | final int enabled = Settings.Global.getInt(cr, |
| 6890 | Settings.Global.AIRPLANE_MODE_ON); |
| 6891 | pw.println(enabled == 0 ? "disabled" : "enabled"); |
| 6892 | return 0; |
| 6893 | } else { |
| 6894 | onHelp(); |
| 6895 | return -1; |
| 6896 | } |
| 6897 | default: |
| 6898 | return handleDefaultCommands(cmd); |
| 6899 | } |
| 6900 | } catch (Exception e) { |
| 6901 | pw.println(e); |
| 6902 | } |
| 6903 | return -1; |
| 6904 | } |
| 6905 | |
| 6906 | @Override |
| 6907 | public void onHelp() { |
| 6908 | PrintWriter pw = getOutPrintWriter(); |
| 6909 | pw.println("Connectivity service commands:"); |
| 6910 | pw.println(" help"); |
| 6911 | pw.println(" Print this help text."); |
| 6912 | pw.println(" airplane-mode [enable|disable]"); |
| 6913 | pw.println(" Turn airplane mode on or off."); |
| 6914 | pw.println(" airplane-mode"); |
| 6915 | pw.println(" Get airplane mode."); |
| 6916 | } |
| 6917 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6918 | |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6919 | @GuardedBy("mVpns") |
| 6920 | private Vpn getVpnIfOwner() { |
| 6921 | final int uid = Binder.getCallingUid(); |
| 6922 | final int user = UserHandle.getUserId(uid); |
| 6923 | |
| 6924 | final Vpn vpn = mVpns.get(user); |
| 6925 | if (vpn == null) { |
| 6926 | return null; |
| 6927 | } else { |
| 6928 | final VpnInfo info = vpn.getVpnInfo(); |
| 6929 | return (info == null || info.ownerUid != uid) ? null : vpn; |
| 6930 | } |
| 6931 | } |
| 6932 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6933 | /** |
| 6934 | * Caller either needs to be an active VPN, or hold the NETWORK_STACK permission |
| 6935 | * for testing. |
| 6936 | */ |
| 6937 | private Vpn enforceActiveVpnOrNetworkStackPermission() { |
| 6938 | if (checkNetworkStackPermission()) { |
| 6939 | return null; |
| 6940 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6941 | synchronized (mVpns) { |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6942 | Vpn vpn = getVpnIfOwner(); |
| 6943 | if (vpn != null) { |
| 6944 | return vpn; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6945 | } |
| 6946 | } |
| 6947 | throw new SecurityException("App must either be an active VPN or have the NETWORK_STACK " |
| 6948 | + "permission"); |
| 6949 | } |
| 6950 | |
| 6951 | /** |
| 6952 | * @param connectionInfo the connection to resolve. |
| 6953 | * @return {@code uid} if the connection is found and the app has permission to observe it |
| 6954 | * (e.g., if it is associated with the calling VPN app's tunnel) or {@code INVALID_UID} if the |
| 6955 | * connection is not found. |
| 6956 | */ |
| 6957 | public int getConnectionOwnerUid(ConnectionInfo connectionInfo) { |
| 6958 | final Vpn vpn = enforceActiveVpnOrNetworkStackPermission(); |
| 6959 | if (connectionInfo.protocol != IPPROTO_TCP && connectionInfo.protocol != IPPROTO_UDP) { |
| 6960 | throw new IllegalArgumentException("Unsupported protocol " + connectionInfo.protocol); |
| 6961 | } |
| 6962 | |
| 6963 | final int uid = InetDiagMessage.getConnectionOwnerUid(connectionInfo.protocol, |
| 6964 | connectionInfo.local, connectionInfo.remote); |
| 6965 | |
| 6966 | /* Filter out Uids not associated with the VPN. */ |
| 6967 | if (vpn != null && !vpn.appliesToUid(uid)) { |
| 6968 | return INVALID_UID; |
| 6969 | } |
| 6970 | |
| 6971 | return uid; |
| 6972 | } |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6973 | |
| 6974 | @Override |
| 6975 | public boolean isCallerCurrentAlwaysOnVpnApp() { |
| 6976 | synchronized (mVpns) { |
| 6977 | Vpn vpn = getVpnIfOwner(); |
| 6978 | return vpn != null && vpn.getAlwaysOn(); |
| 6979 | } |
| 6980 | } |
| 6981 | |
| 6982 | @Override |
| 6983 | public boolean isCallerCurrentAlwaysOnVpnLockdownApp() { |
| 6984 | synchronized (mVpns) { |
| 6985 | Vpn vpn = getVpnIfOwner(); |
| 6986 | return vpn != null && vpn.getLockdown(); |
| 6987 | } |
| 6988 | } |
Benedict Wong | a341fbc | 2018-11-09 14:45:34 -0800 | [diff] [blame] | 6989 | |
| 6990 | /** |
| 6991 | * Returns a IBinder to a TestNetworkService. Will be lazily created as needed. |
| 6992 | * |
| 6993 | * <p>The TestNetworkService must be run in the system server due to TUN creation. |
| 6994 | */ |
| 6995 | @Override |
| 6996 | public IBinder startOrGetTestNetworkService() { |
| 6997 | synchronized (mTNSLock) { |
| 6998 | TestNetworkService.enforceTestNetworkPermissions(mContext); |
| 6999 | |
| 7000 | if (mTNS == null) { |
| 7001 | mTNS = new TestNetworkService(mContext, mNMS); |
| 7002 | } |
| 7003 | |
| 7004 | return mTNS; |
| 7005 | } |
| 7006 | } |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 7007 | } |