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 | |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 50 | import android.annotation.NonNull; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 51 | import android.annotation.Nullable; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 52 | import android.app.BroadcastOptions; |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 53 | import android.app.NotificationManager; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 54 | import android.app.PendingIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 55 | import android.content.BroadcastReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | import android.content.ContentResolver; |
| 57 | import android.content.Context; |
| 58 | import android.content.Intent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 59 | import android.content.IntentFilter; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 60 | import android.content.res.Configuration; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 61 | import android.database.ContentObserver; |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 62 | import android.net.CaptivePortal; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 63 | import android.net.ConnectionInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | import android.net.ConnectivityManager; |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 65 | import android.net.ICaptivePortal; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.net.IConnectivityManager; |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 67 | import android.net.IDnsResolver; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 68 | import android.net.IIpConnectivityMetrics; |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 69 | import android.net.INetd; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 70 | import android.net.INetdEventCallback; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 71 | import android.net.INetworkManagementEventObserver; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 72 | import android.net.INetworkMonitor; |
| 73 | import android.net.INetworkMonitorCallbacks; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 74 | import android.net.INetworkPolicyListener; |
| 75 | import android.net.INetworkPolicyManager; |
Jeff Sharkey | 367d15a | 2011-09-22 14:59:51 -0700 | [diff] [blame] | 76 | import android.net.INetworkStatsService; |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 77 | import android.net.ISocketKeepaliveCallback; |
markchien | 6ea3454 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 78 | import android.net.ITetheringEventCallback; |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 79 | import android.net.InetAddresses; |
| 80 | import android.net.IpPrefix; |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 81 | import android.net.LinkProperties; |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 82 | import android.net.LinkProperties.CompareResult; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 83 | import android.net.MatchAllNetworkSpecifier; |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 84 | import android.net.NattSocketKeepalive; |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 85 | import android.net.Network; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 86 | import android.net.NetworkAgent; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 87 | import android.net.NetworkCapabilities; |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 88 | import android.net.NetworkConfig; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 89 | import android.net.NetworkFactory; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | import android.net.NetworkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 91 | import android.net.NetworkInfo.DetailedState; |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 92 | import android.net.NetworkMisc; |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 93 | import android.net.NetworkPolicyManager; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 94 | import android.net.NetworkQuotaInfo; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 95 | import android.net.NetworkRequest; |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 96 | import android.net.NetworkSpecifier; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 97 | import android.net.NetworkStack; |
Remi NGUYEN VAN | 310da6f | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 98 | import android.net.NetworkStackClient; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 99 | import android.net.NetworkState; |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 100 | import android.net.NetworkUtils; |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 101 | import android.net.NetworkWatchlistManager; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 102 | import android.net.PrivateDnsConfigParcel; |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 103 | import android.net.ProxyInfo; |
Robert Greenwalt | aa70f10 | 2011-04-28 14:28:50 -0700 | [diff] [blame] | 104 | import android.net.RouteInfo; |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 105 | import android.net.SocketKeepalive; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 106 | import android.net.UidRange; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 107 | import android.net.Uri; |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 108 | import android.net.VpnService; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 109 | import android.net.metrics.IpConnectivityLog; |
Hugo Benichi | cc92c6e | 2016-04-21 15:02:38 +0900 | [diff] [blame] | 110 | import android.net.metrics.NetworkEvent; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 111 | import android.net.netlink.InetDiagMessage; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 112 | import android.net.shared.PrivateDnsConfig; |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 113 | import android.net.util.MultinetworkPolicyTracker; |
Remi NGUYEN VAN | 231b52b | 2019-01-29 15:38:52 +0900 | [diff] [blame] | 114 | import android.net.util.NetdService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | import android.os.Binder; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 116 | import android.os.Build; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 117 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | import android.os.Handler; |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 119 | import android.os.HandlerThread; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 120 | import android.os.IBinder; |
Chia-chi Yeh | c933830 | 2011-05-11 16:35:13 -0700 | [diff] [blame] | 121 | import android.os.INetworkManagementService; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | import android.os.Looper; |
| 123 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 124 | import android.os.Messenger; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 125 | import android.os.ParcelFileDescriptor; |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 126 | import android.os.Parcelable; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 127 | import android.os.PowerManager; |
Jeff Sharkey | f56e243 | 2012-09-06 17:54:29 -0700 | [diff] [blame] | 128 | import android.os.Process; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 129 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 130 | import android.os.ResultReceiver; |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 131 | import android.os.ServiceManager; |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 132 | import android.os.ServiceSpecificException; |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 133 | import android.os.ShellCallback; |
| 134 | import android.os.ShellCommand; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 135 | import android.os.SystemClock; |
Anil Admal | e41992c | 2019-04-08 12:36:02 -0700 | [diff] [blame] | 136 | import android.os.SystemProperties; |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 137 | import android.os.UserHandle; |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 138 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | import android.provider.Settings; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 140 | import android.security.Credentials; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 141 | import android.security.KeyStore; |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 142 | import android.telephony.TelephonyManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 143 | import android.text.TextUtils; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 144 | import android.util.ArraySet; |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 145 | import android.util.LocalLog; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 146 | import android.util.Log; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 147 | import android.util.Pair; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 148 | import android.util.Slog; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 149 | import android.util.SparseArray; |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 150 | import android.util.SparseBooleanArray; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 151 | import android.util.SparseIntArray; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 152 | import android.util.Xml; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 154 | import com.android.internal.R; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 155 | import com.android.internal.annotations.GuardedBy; |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 156 | import com.android.internal.annotations.VisibleForTesting; |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 157 | import com.android.internal.app.IBatteryStats; |
Remi NGUYEN VAN | d4a9de20 | 2019-01-30 22:01:20 +0900 | [diff] [blame] | 158 | import com.android.internal.logging.MetricsLogger; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 159 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 160 | import com.android.internal.net.VpnConfig; |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 161 | import com.android.internal.net.VpnInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 162 | import com.android.internal.net.VpnProfile; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 163 | import com.android.internal.util.ArrayUtils; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 164 | import com.android.internal.util.AsyncChannel; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 165 | import com.android.internal.util.DumpUtils; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 166 | import com.android.internal.util.IndentingPrintWriter; |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 167 | import com.android.internal.util.MessageUtils; |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 168 | import com.android.internal.util.WakeupMessage; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 169 | import com.android.internal.util.XmlUtils; |
Jeff Sharkey | 1059c3c | 2011-10-04 16:54:49 -0700 | [diff] [blame] | 170 | import com.android.server.am.BatteryStatsService; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 171 | import com.android.server.connectivity.DataConnectionStats; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 172 | import com.android.server.connectivity.DnsManager; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 173 | import com.android.server.connectivity.DnsManager.PrivateDnsValidationUpdate; |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 174 | import com.android.server.connectivity.IpConnectivityMetrics; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 175 | import com.android.server.connectivity.KeepaliveTracker; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 176 | import com.android.server.connectivity.LingerMonitor; |
Christopher Wiley | 497c147 | 2016-10-11 13:26:03 -0700 | [diff] [blame] | 177 | import com.android.server.connectivity.MockableSystemProperties; |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 178 | import com.android.server.connectivity.MultipathPolicyTracker; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 179 | import com.android.server.connectivity.NetworkAgentInfo; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 180 | import com.android.server.connectivity.NetworkDiagnostics; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 181 | import com.android.server.connectivity.NetworkNotificationManager; |
| 182 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 183 | import com.android.server.connectivity.PermissionMonitor; |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 184 | import com.android.server.connectivity.ProxyTracker; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 185 | import com.android.server.connectivity.Tethering; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 186 | import com.android.server.connectivity.Vpn; |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 187 | import com.android.server.connectivity.tethering.TetheringDependencies; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 188 | import com.android.server.net.BaseNetdEventCallback; |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 189 | import com.android.server.net.BaseNetworkObserver; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 190 | import com.android.server.net.LockdownVpnTracker; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 191 | import com.android.server.net.NetworkPolicyManagerInternal; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 192 | import com.android.server.utils.PriorityDump; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 193 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 194 | import com.google.android.collect.Lists; |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 195 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 196 | import org.xmlpull.v1.XmlPullParser; |
| 197 | import org.xmlpull.v1.XmlPullParserException; |
| 198 | |
| 199 | import java.io.File; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | import java.io.FileDescriptor; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 201 | import java.io.FileNotFoundException; |
| 202 | import java.io.FileReader; |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 203 | import java.io.IOException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | import java.io.PrintWriter; |
Wink Saville | c9822c5 | 2011-07-14 12:23:28 -0700 | [diff] [blame] | 205 | import java.net.Inet4Address; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 206 | import java.net.InetAddress; |
| 207 | import java.net.UnknownHostException; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 208 | import java.util.ArrayList; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 209 | import java.util.Arrays; |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 210 | import java.util.Collection; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 211 | import java.util.Comparator; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 212 | import java.util.ConcurrentModificationException; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 213 | import java.util.HashMap; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 214 | import java.util.HashSet; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 215 | import java.util.List; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 216 | import java.util.Map; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 217 | import java.util.Objects; |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 218 | import java.util.Set; |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 219 | import java.util.SortedSet; |
| 220 | import java.util.TreeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | |
| 222 | /** |
| 223 | * @hide |
| 224 | */ |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 225 | public class ConnectivityService extends IConnectivityManager.Stub |
| 226 | implements PendingIntent.OnFinished { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 227 | private static final String TAG = ConnectivityService.class.getSimpleName(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 229 | private static final String DIAG_ARG = "--diag"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 230 | public static final String SHORT_ARG = "--short"; |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 231 | private static final String TETHERING_ARG = "tethering"; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 232 | private static final String NETWORK_ARG = "networks"; |
| 233 | private static final String REQUEST_ARG = "requests"; |
Erik Kline | 7747fd4 | 2017-05-12 16:52:48 +0900 | [diff] [blame] | 234 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 235 | private static final boolean DBG = true; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 236 | private static final boolean DDBG = Log.isLoggable(TAG, Log.DEBUG); |
| 237 | 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] | 238 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 239 | private static final boolean LOGD_BLOCKED_NETWORKINFO = true; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 240 | |
Remi NGUYEN VAN | 9ca4c62 | 2019-04-09 02:04:54 -0700 | [diff] [blame] | 241 | /** |
| 242 | * Default URL to use for {@link #getCaptivePortalServerUrl()}. This should not be changed |
| 243 | * by OEMs for configuration purposes, as this value is overridden by |
| 244 | * Settings.Global.CAPTIVE_PORTAL_HTTP_URL. |
| 245 | * R.string.config_networkCaptivePortalServerUrl should be overridden instead for this purpose |
| 246 | * (preferably via runtime resource overlays). |
| 247 | */ |
| 248 | private static final String DEFAULT_CAPTIVE_PORTAL_HTTP_URL = |
| 249 | "http://connectivitycheck.gstatic.com/generate_204"; |
| 250 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 251 | // TODO: create better separation between radio types and network types |
| 252 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 253 | // how long to wait before switching back to a radio's default network |
| 254 | private static final int RESTORE_DEFAULT_NETWORK_DELAY = 1 * 60 * 1000; |
| 255 | // system property that can override the above value |
| 256 | private static final String NETWORK_RESTORE_DELAY_PROP_NAME = |
| 257 | "android.telephony.apn-restore"; |
| 258 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 259 | // How long to wait before putting up a "This network doesn't have an Internet connection, |
| 260 | // connect anyway?" dialog after the user selects a network that doesn't validate. |
| 261 | private static final int PROMPT_UNVALIDATED_DELAY_MS = 8 * 1000; |
| 262 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 263 | // How long to dismiss network notification. |
| 264 | private static final int TIMEOUT_NOTIFICATION_DELAY_MS = 20 * 1000; |
| 265 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 266 | // Default to 30s linger time-out. Modifiable only for testing. |
| 267 | private static final String LINGER_DELAY_PROPERTY = "persist.netmon.linger"; |
| 268 | private static final int DEFAULT_LINGER_DELAY_MS = 30_000; |
| 269 | @VisibleForTesting |
| 270 | protected int mLingerDelayMs; // Can't be final, or test subclass constructors can't change it. |
| 271 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 272 | // How long to delay to removal of a pending intent based request. |
| 273 | // See Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS |
| 274 | private final int mReleasePendingIntentDelayMs; |
| 275 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 276 | private MockableSystemProperties mSystemProperties; |
| 277 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 278 | private Tethering mTethering; |
| 279 | |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 280 | private final PermissionMonitor mPermissionMonitor; |
| 281 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 282 | private KeyStore mKeyStore; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 283 | |
Chalard Jean | fb0c87e | 2018-04-18 19:18:58 +0900 | [diff] [blame] | 284 | @VisibleForTesting |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 285 | @GuardedBy("mVpns") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 286 | protected final SparseArray<Vpn> mVpns = new SparseArray<>(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 287 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 288 | // TODO: investigate if mLockdownEnabled can be removed and replaced everywhere by |
| 289 | // a direct call to LockdownVpnTracker.isEnabled(). |
| 290 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 291 | private boolean mLockdownEnabled; |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 292 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 293 | private LockdownVpnTracker mLockdownTracker; |
| 294 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 295 | /** |
| 296 | * Stale copy of uid rules provided by NPMS. As long as they are accessed only in internal |
| 297 | * handler thread, they don't need a lock. |
| 298 | */ |
| 299 | private SparseIntArray mUidRules = new SparseIntArray(); |
| 300 | /** Flag indicating if background data is restricted. */ |
| 301 | private boolean mRestrictBackground; |
| 302 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 303 | final private Context mContext; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 304 | // 0 is full bad, 100 is full good |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 305 | private int mDefaultInetConditionPublished = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 306 | |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 307 | private INetworkManagementService mNMS; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 308 | @VisibleForTesting |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 309 | protected IDnsResolver mDnsResolver; |
| 310 | @VisibleForTesting |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 311 | protected INetd mNetd; |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 312 | private INetworkStatsService mStatsService; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 313 | private INetworkPolicyManager mPolicyManager; |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 314 | private NetworkPolicyManagerInternal mPolicyManagerInternal; |
Robert Greenwalt | 59b1a4e | 2011-05-10 15:05:02 -0700 | [diff] [blame] | 315 | |
Benedict Wong | 99a4841 | 2018-11-09 14:45:34 -0800 | [diff] [blame] | 316 | /** |
| 317 | * TestNetworkService (lazily) created upon first usage. Locked to prevent creation of multiple |
| 318 | * instances. |
| 319 | */ |
| 320 | @GuardedBy("mTNSLock") |
| 321 | private TestNetworkService mTNS; |
| 322 | |
| 323 | private final Object mTNSLock = new Object(); |
| 324 | |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 325 | private String mCurrentTcpBufferSizes; |
| 326 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 327 | private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames( |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 328 | new Class[] { AsyncChannel.class, ConnectivityService.class, NetworkAgent.class, |
| 329 | NetworkAgentInfo.class }); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 330 | |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 331 | private enum ReapUnvalidatedNetworks { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 332 | // Tear down networks that have no chance (e.g. even if validated) of becoming |
| 333 | // the highest scoring network satisfying a NetworkRequest. This should be passed when |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 334 | // all networks have been rematched against all NetworkRequests. |
| 335 | REAP, |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 336 | // Don't reap networks. This should be passed when some networks have not yet been |
| 337 | // rematched against all NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 338 | DONT_REAP |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 339 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 340 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 341 | private enum UnneededFor { |
| 342 | LINGER, // Determine whether this network is unneeded and should be lingered. |
| 343 | TEARDOWN, // Determine whether this network is unneeded and should be torn down. |
| 344 | } |
| 345 | |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 346 | /** |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 347 | * used internally to clear a wakelock when transitioning |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 348 | * from one net to another. Clear happens when we get a new |
| 349 | * network - EVENT_EXPIRE_NET_TRANSITION_WAKELOCK happens |
| 350 | * after a timeout if no network is found (typically 1 min). |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 351 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 352 | private static final int EVENT_CLEAR_NET_TRANSITION_WAKELOCK = 8; |
Robert Greenwalt | f333123 | 2010-09-24 14:32:21 -0700 | [diff] [blame] | 353 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 354 | /** |
| 355 | * used internally to reload global proxy settings |
| 356 | */ |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 357 | private static final int EVENT_APPLY_GLOBAL_HTTP_PROXY = 9; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 358 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 359 | /** |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 360 | * PAC manager has received new port. |
| 361 | */ |
| 362 | private static final int EVENT_PROXY_HAS_CHANGED = 16; |
| 363 | |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 364 | /** |
| 365 | * used internally when registering NetworkFactories |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 366 | * obj = NetworkFactoryInfo |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 367 | */ |
| 368 | private static final int EVENT_REGISTER_NETWORK_FACTORY = 17; |
| 369 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 370 | /** |
| 371 | * used internally when registering NetworkAgents |
| 372 | * obj = Messenger |
| 373 | */ |
| 374 | private static final int EVENT_REGISTER_NETWORK_AGENT = 18; |
| 375 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 376 | /** |
| 377 | * used to add a network request |
| 378 | * includes a NetworkRequestInfo |
| 379 | */ |
| 380 | private static final int EVENT_REGISTER_NETWORK_REQUEST = 19; |
| 381 | |
| 382 | /** |
| 383 | * 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] | 384 | * 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] | 385 | * cancel it. |
| 386 | * includes a NetworkRequestInfo |
| 387 | */ |
| 388 | private static final int EVENT_TIMEOUT_NETWORK_REQUEST = 20; |
| 389 | |
| 390 | /** |
| 391 | * used to add a network listener - no request |
| 392 | * includes a NetworkRequestInfo |
| 393 | */ |
| 394 | private static final int EVENT_REGISTER_NETWORK_LISTENER = 21; |
| 395 | |
| 396 | /** |
| 397 | * 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] | 398 | * arg1 = UID of caller |
| 399 | * obj = NetworkRequest |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 400 | */ |
| 401 | private static final int EVENT_RELEASE_NETWORK_REQUEST = 22; |
| 402 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 403 | /** |
| 404 | * used internally when registering NetworkFactories |
| 405 | * obj = Messenger |
| 406 | */ |
| 407 | private static final int EVENT_UNREGISTER_NETWORK_FACTORY = 23; |
| 408 | |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 409 | /** |
| 410 | * used internally to expire a wakelock when transitioning |
| 411 | * from one net to another. Expire happens when we fail to find |
| 412 | * a new network (typically after 1 minute) - |
| 413 | * EVENT_CLEAR_NET_TRANSITION_WAKELOCK happens if we had found |
| 414 | * a replacement network. |
| 415 | */ |
| 416 | private static final int EVENT_EXPIRE_NET_TRANSITION_WAKELOCK = 24; |
| 417 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 418 | /** |
| 419 | * Used internally to indicate the system is ready. |
| 420 | */ |
| 421 | private static final int EVENT_SYSTEM_READY = 25; |
| 422 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 423 | /** |
| 424 | * used to add a network request with a pending intent |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 425 | * obj = NetworkRequestInfo |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 426 | */ |
| 427 | private static final int EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT = 26; |
| 428 | |
| 429 | /** |
| 430 | * used to remove a pending intent and its associated network request. |
| 431 | * arg1 = UID of caller |
| 432 | * obj = PendingIntent |
| 433 | */ |
| 434 | private static final int EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT = 27; |
| 435 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 436 | /** |
| 437 | * used to specify whether a network should be used even if unvalidated. |
| 438 | * arg1 = whether to accept the network if it's unvalidated (1 or 0) |
| 439 | * arg2 = whether to remember this choice in the future (1 or 0) |
| 440 | * obj = network |
| 441 | */ |
| 442 | private static final int EVENT_SET_ACCEPT_UNVALIDATED = 28; |
| 443 | |
| 444 | /** |
| 445 | * used to ask the user to confirm a connection to an unvalidated network. |
| 446 | * obj = network |
| 447 | */ |
| 448 | private static final int EVENT_PROMPT_UNVALIDATED = 29; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 449 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 450 | /** |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 451 | * used internally to (re)configure always-on networks. |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 452 | */ |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 453 | private static final int EVENT_CONFIGURE_ALWAYS_ON_NETWORKS = 30; |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 454 | |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 455 | /** |
| 456 | * used to add a network listener with a pending intent |
| 457 | * obj = NetworkRequestInfo |
| 458 | */ |
| 459 | private static final int EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT = 31; |
| 460 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 461 | /** |
| 462 | * used to specify whether a network should not be penalized when it becomes unvalidated. |
| 463 | */ |
| 464 | private static final int EVENT_SET_AVOID_UNVALIDATED = 35; |
| 465 | |
| 466 | /** |
| 467 | * used to trigger revalidation of a network. |
| 468 | */ |
| 469 | private static final int EVENT_REVALIDATE_NETWORK = 36; |
| 470 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 471 | // Handle changes in Private DNS settings. |
| 472 | private static final int EVENT_PRIVATE_DNS_SETTINGS_CHANGED = 37; |
| 473 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 474 | // Handle private DNS validation status updates. |
| 475 | private static final int EVENT_PRIVATE_DNS_VALIDATION_UPDATE = 38; |
| 476 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 477 | /** |
| 478 | * Used to handle onUidRulesChanged event from NetworkPolicyManagerService. |
| 479 | */ |
| 480 | private static final int EVENT_UID_RULES_CHANGED = 39; |
| 481 | |
| 482 | /** |
| 483 | * Used to handle onRestrictBackgroundChanged event from NetworkPolicyManagerService. |
| 484 | */ |
| 485 | private static final int EVENT_DATA_SAVER_CHANGED = 40; |
| 486 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 487 | /** |
| 488 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the network has |
| 489 | * been tested. |
| 490 | * obj = String representing URL that Internet probe was redirect to, if it was redirected. |
| 491 | * arg1 = One of the NETWORK_TESTED_RESULT_* constants. |
| 492 | * arg2 = NetID. |
| 493 | */ |
| 494 | public static final int EVENT_NETWORK_TESTED = 41; |
| 495 | |
| 496 | /** |
| 497 | * Event for NetworkMonitor/NetworkAgentInfo to inform ConnectivityService that the private DNS |
| 498 | * config was resolved. |
| 499 | * obj = PrivateDnsConfig |
| 500 | * arg2 = netid |
| 501 | */ |
| 502 | public static final int EVENT_PRIVATE_DNS_CONFIG_RESOLVED = 42; |
| 503 | |
| 504 | /** |
| 505 | * Request ConnectivityService display provisioning notification. |
| 506 | * arg1 = Whether to make the notification visible. |
| 507 | * arg2 = NetID. |
| 508 | * obj = Intent to be launched when notification selected by user, null if !arg1. |
| 509 | */ |
| 510 | public static final int EVENT_PROVISIONING_NOTIFICATION = 43; |
| 511 | |
| 512 | /** |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 513 | * This event can handle dismissing notification by given network id. |
| 514 | */ |
| 515 | public static final int EVENT_TIMEOUT_NOTIFICATION = 44; |
| 516 | |
| 517 | /** |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 518 | * Used to specify whether a network should be used even if connectivity is partial. |
| 519 | * arg1 = whether to accept the network if its connectivity is partial (1 for true or 0 for |
| 520 | * false) |
| 521 | * arg2 = whether to remember this choice in the future (1 for true or 0 for false) |
| 522 | * obj = network |
| 523 | */ |
| 524 | private static final int EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY = 45; |
| 525 | |
| 526 | /** |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 527 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 528 | * should be shown. |
| 529 | */ |
| 530 | public static final int PROVISIONING_NOTIFICATION_SHOW = 1; |
| 531 | |
| 532 | /** |
| 533 | * Argument for {@link #EVENT_PROVISIONING_NOTIFICATION} to indicate that the notification |
| 534 | * should be hidden. |
| 535 | */ |
| 536 | public static final int PROVISIONING_NOTIFICATION_HIDE = 0; |
| 537 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 538 | private static String eventName(int what) { |
| 539 | return sMagicDecoderRing.get(what, Integer.toString(what)); |
| 540 | } |
| 541 | |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 542 | private static IDnsResolver getDnsResolver() { |
| 543 | return IDnsResolver.Stub |
| 544 | .asInterface(ServiceManager.getService("dnsresolver")); |
| 545 | } |
| 546 | |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 547 | /** Handler thread used for both of the handlers below. */ |
| 548 | @VisibleForTesting |
| 549 | protected final HandlerThread mHandlerThread; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 550 | /** Handler used for internal events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 551 | final private InternalHandler mHandler; |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 552 | /** Handler used for incoming {@link NetworkStateTracker} events. */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 553 | final private NetworkStateTrackerHandler mTrackerHandler; |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 554 | private final DnsManager mDnsManager; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 555 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 556 | private boolean mSystemReady; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 557 | private Intent mInitialBroadcast; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 558 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 559 | private PowerManager.WakeLock mNetTransitionWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 560 | private int mNetTransitionWakeLockTimeout; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 561 | private final PowerManager.WakeLock mPendingIntentWakeLock; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 562 | |
Chalard Jean | 52c2aa7 | 2018-06-07 16:44:04 +0900 | [diff] [blame] | 563 | // A helper object to track the current default HTTP proxy. ConnectivityService needs to tell |
| 564 | // the world when it changes. |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 565 | @VisibleForTesting |
| 566 | protected final ProxyTracker mProxyTracker; |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 567 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 568 | final private SettingsObserver mSettingsObserver; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 569 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 570 | private UserManager mUserManager; |
| 571 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 572 | private NetworkConfig[] mNetConfigs; |
| 573 | private int mNetworksDefined; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 574 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 575 | // 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] | 576 | private List mProtectedNetworks; |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 577 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 578 | private TelephonyManager mTelephonyManager; |
John Spurlock | bf991a8 | 2013-06-24 14:20:23 -0400 | [diff] [blame] | 579 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 580 | private KeepaliveTracker mKeepaliveTracker; |
Lorenzo Colitti | f3ae2ee | 2016-08-22 16:30:00 +0900 | [diff] [blame] | 581 | private NetworkNotificationManager mNotifier; |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 582 | private LingerMonitor mLingerMonitor; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 583 | |
Sreeram Ramachandran | 8f4d42c | 2014-09-05 16:06:34 -0700 | [diff] [blame] | 584 | // sequence number for Networks; keep in sync with system/netd/NetworkController.cpp |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 585 | private static final int MIN_NET_ID = 100; // some reserved marks |
| 586 | 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] | 587 | private int mNextNetId = MIN_NET_ID; |
| 588 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 589 | // sequence number of NetworkRequests |
| 590 | private int mNextNetworkRequestId = 1; |
| 591 | |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 592 | // NetworkRequest activity String log entries. |
| 593 | private static final int MAX_NETWORK_REQUEST_LOGS = 20; |
| 594 | private final LocalLog mNetworkRequestInfoLogs = new LocalLog(MAX_NETWORK_REQUEST_LOGS); |
| 595 | |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 596 | // NetworkInfo blocked and unblocked String log entries |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 597 | private static final int MAX_NETWORK_INFO_LOGS = 40; |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 598 | private final LocalLog mNetworkInfoBlockingLogs = new LocalLog(MAX_NETWORK_INFO_LOGS); |
| 599 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 600 | private static final int MAX_WAKELOCK_LOGS = 20; |
| 601 | private final LocalLog mWakelockLogs = new LocalLog(MAX_WAKELOCK_LOGS); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 602 | private int mTotalWakelockAcquisitions = 0; |
| 603 | private int mTotalWakelockReleases = 0; |
| 604 | private long mTotalWakelockDurationMs = 0; |
| 605 | private long mMaxWakelockDurationMs = 0; |
| 606 | private long mLastWakeLockAcquireTimestamp = 0; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 607 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 608 | private final IpConnectivityLog mMetricsLog; |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 609 | |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 610 | @GuardedBy("mBandwidthRequests") |
| 611 | private final SparseArray<Integer> mBandwidthRequests = new SparseArray(10); |
| 612 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 613 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 614 | final MultinetworkPolicyTracker mMultinetworkPolicyTracker; |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 615 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 616 | @VisibleForTesting |
| 617 | final MultipathPolicyTracker mMultipathPolicyTracker; |
| 618 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 619 | /** |
| 620 | * Implements support for the legacy "one network per network type" model. |
| 621 | * |
| 622 | * We used to have a static array of NetworkStateTrackers, one for each |
| 623 | * network type, but that doesn't work any more now that we can have, |
| 624 | * for example, more that one wifi network. This class stores all the |
| 625 | * NetworkAgentInfo objects that support a given type, but the legacy |
| 626 | * API will only see the first one. |
| 627 | * |
| 628 | * It serves two main purposes: |
| 629 | * |
| 630 | * 1. Provide information about "the network for a given type" (since this |
| 631 | * API only supports one). |
| 632 | * 2. Send legacy connectivity change broadcasts. Broadcasts are sent if |
| 633 | * the first network for a given type changes, or if the default network |
| 634 | * changes. |
| 635 | */ |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 636 | @VisibleForTesting |
| 637 | static class LegacyTypeTracker { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 638 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 639 | private static final boolean DBG = true; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 640 | private static final boolean VDBG = false; |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 641 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 642 | /** |
| 643 | * Array of lists, one per legacy network type (e.g., TYPE_MOBILE_MMS). |
| 644 | * Each list holds references to all NetworkAgentInfos that are used to |
| 645 | * satisfy requests for that network type. |
| 646 | * |
| 647 | * This array is built out at startup such that an unsupported network |
| 648 | * doesn't get an ArrayList instance, making this a tristate: |
| 649 | * unsupported, supported but not active and active. |
| 650 | * |
| 651 | * The actual lists are populated when we scan the network types that |
| 652 | * are supported on this device. |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 653 | * |
| 654 | * Threading model: |
| 655 | * - addSupportedType() is only called in the constructor |
| 656 | * - add(), update(), remove() are only called from the ConnectivityService handler thread. |
| 657 | * They are therefore not thread-safe with respect to each other. |
| 658 | * - getNetworkForType() can be called at any time on binder threads. It is synchronized |
| 659 | * on mTypeLists to be thread-safe with respect to a concurrent remove call. |
| 660 | * - dump is thread-safe with respect to concurrent add and remove calls. |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 661 | */ |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 662 | private final ArrayList<NetworkAgentInfo> mTypeLists[]; |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 663 | @NonNull |
| 664 | private final ConnectivityService mService; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 665 | |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 666 | LegacyTypeTracker(@NonNull ConnectivityService service) { |
| 667 | mService = service; |
| 668 | mTypeLists = new ArrayList[ConnectivityManager.MAX_NETWORK_TYPE + 1]; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | public void addSupportedType(int type) { |
| 672 | if (mTypeLists[type] != null) { |
| 673 | throw new IllegalStateException( |
| 674 | "legacy list for type " + type + "already initialized"); |
| 675 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 676 | mTypeLists[type] = new ArrayList<>(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 677 | } |
| 678 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 679 | public boolean isTypeSupported(int type) { |
| 680 | return isNetworkTypeValid(type) && mTypeLists[type] != null; |
| 681 | } |
| 682 | |
| 683 | public NetworkAgentInfo getNetworkForType(int type) { |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 684 | synchronized (mTypeLists) { |
| 685 | if (isTypeSupported(type) && !mTypeLists[type].isEmpty()) { |
| 686 | return mTypeLists[type].get(0); |
| 687 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 688 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 689 | return null; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 692 | private void maybeLogBroadcast(NetworkAgentInfo nai, DetailedState state, int type, |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 693 | boolean isDefaultNetwork) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 694 | if (DBG) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 695 | log("Sending " + state + |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 696 | " broadcast for type " + type + " " + nai.name() + |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 697 | " isDefaultNetwork=" + isDefaultNetwork); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
| 701 | /** Adds the given network to the specified legacy type list. */ |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 702 | public void add(int type, NetworkAgentInfo nai) { |
| 703 | if (!isTypeSupported(type)) { |
| 704 | return; // Invalid network type. |
| 705 | } |
| 706 | if (VDBG) log("Adding agent " + nai + " for legacy network type " + type); |
| 707 | |
| 708 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 709 | if (list.contains(nai)) { |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 710 | return; |
| 711 | } |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 712 | synchronized (mTypeLists) { |
| 713 | list.add(nai); |
| 714 | } |
Lorenzo Colitti | 061f415 | 2014-09-28 16:08:06 +0900 | [diff] [blame] | 715 | |
| 716 | // Send a broadcast if this is the first network of its type or if it's the default. |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 717 | final boolean isDefaultNetwork = mService.isDefaultNetwork(nai); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 718 | if ((list.size() == 1) || isDefaultNetwork) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 719 | maybeLogBroadcast(nai, DetailedState.CONNECTED, type, isDefaultNetwork); |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 720 | mService.sendLegacyNetworkBroadcast(nai, DetailedState.CONNECTED, type); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 721 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 722 | } |
| 723 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 724 | /** Removes the given network from the specified legacy type list. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 725 | public void remove(int type, NetworkAgentInfo nai, boolean wasDefault) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 726 | ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
| 727 | if (list == null || list.isEmpty()) { |
| 728 | return; |
| 729 | } |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 730 | final boolean wasFirstNetwork = list.get(0).equals(nai); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 731 | |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 732 | synchronized (mTypeLists) { |
| 733 | if (!list.remove(nai)) { |
| 734 | return; |
| 735 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 736 | } |
| 737 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 738 | final DetailedState state = DetailedState.DISCONNECTED; |
| 739 | |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 740 | if (wasFirstNetwork || wasDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 741 | maybeLogBroadcast(nai, state, type, wasDefault); |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 742 | mService.sendLegacyNetworkBroadcast(nai, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (!list.isEmpty() && wasFirstNetwork) { |
| 746 | if (DBG) log("Other network available for type " + type + |
| 747 | ", sending connected broadcast"); |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 748 | final NetworkAgentInfo replacement = list.get(0); |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 749 | maybeLogBroadcast(replacement, state, type, mService.isDefaultNetwork(replacement)); |
| 750 | mService.sendLegacyNetworkBroadcast(replacement, state, type); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 751 | } |
| 752 | } |
| 753 | |
| 754 | /** Removes the given network from all legacy type lists. */ |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 755 | public void remove(NetworkAgentInfo nai, boolean wasDefault) { |
| 756 | if (VDBG) log("Removing agent " + nai + " wasDefault=" + wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 757 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 758 | remove(type, nai, wasDefault); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 759 | } |
| 760 | } |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 761 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 762 | // send out another legacy broadcast - currently only used for suspend/unsuspend |
| 763 | // toggle |
| 764 | public void update(NetworkAgentInfo nai) { |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 765 | final boolean isDefault = mService.isDefaultNetwork(nai); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 766 | final DetailedState state = nai.networkInfo.getDetailedState(); |
| 767 | for (int type = 0; type < mTypeLists.length; type++) { |
| 768 | final ArrayList<NetworkAgentInfo> list = mTypeLists[type]; |
Robert Greenwalt | 3ac71b7 | 2015-07-10 16:00:36 -0700 | [diff] [blame] | 769 | final boolean contains = (list != null && list.contains(nai)); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 770 | final boolean isFirst = contains && (nai == list.get(0)); |
| 771 | if (isFirst || contains && isDefault) { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 772 | maybeLogBroadcast(nai, state, type, isDefault); |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 773 | mService.sendLegacyNetworkBroadcast(nai, state, type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | } |
| 777 | |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 778 | private String naiToString(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 779 | String name = nai.name(); |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 780 | String state = (nai.networkInfo != null) ? |
| 781 | nai.networkInfo.getState() + "/" + nai.networkInfo.getDetailedState() : |
| 782 | "???/???"; |
| 783 | return name + " " + state; |
| 784 | } |
| 785 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 786 | public void dump(IndentingPrintWriter pw) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 787 | pw.println("mLegacyTypeTracker:"); |
| 788 | pw.increaseIndent(); |
| 789 | pw.print("Supported types:"); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 790 | for (int type = 0; type < mTypeLists.length; type++) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 791 | if (mTypeLists[type] != null) pw.print(" " + type); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 792 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 793 | pw.println(); |
| 794 | pw.println("Current state:"); |
| 795 | pw.increaseIndent(); |
Hugo Benichi | 78caa258 | 2016-06-21 09:48:07 +0900 | [diff] [blame] | 796 | synchronized (mTypeLists) { |
| 797 | for (int type = 0; type < mTypeLists.length; type++) { |
| 798 | if (mTypeLists[type] == null || mTypeLists[type].isEmpty()) continue; |
| 799 | for (NetworkAgentInfo nai : mTypeLists[type]) { |
| 800 | pw.println(type + " " + naiToString(nai)); |
| 801 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | pw.decreaseIndent(); |
| 805 | pw.decreaseIndent(); |
| 806 | pw.println(); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 807 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 808 | } |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 809 | private final LegacyTypeTracker mLegacyTypeTracker = new LegacyTypeTracker(this); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 810 | |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 811 | /** |
| 812 | * Helper class which parses out priority arguments and dumps sections according to their |
| 813 | * priority. If priority arguments are omitted, function calls the legacy dump command. |
| 814 | */ |
| 815 | private final PriorityDump.PriorityDumper mPriorityDumper = new PriorityDump.PriorityDumper() { |
| 816 | @Override |
| 817 | public void dumpHigh(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 818 | doDump(fd, pw, new String[] {DIAG_ARG}, asProto); |
| 819 | doDump(fd, pw, new String[] {SHORT_ARG}, asProto); |
| 820 | } |
| 821 | |
| 822 | @Override |
| 823 | public void dumpNormal(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 824 | doDump(fd, pw, args, asProto); |
| 825 | } |
| 826 | |
| 827 | @Override |
| 828 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args, boolean asProto) { |
| 829 | doDump(fd, pw, args, asProto); |
| 830 | } |
| 831 | }; |
| 832 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 833 | public ConnectivityService(Context context, INetworkManagementService netManager, |
Robert Greenwalt | 6831f1d | 2014-07-27 12:06:40 -0700 | [diff] [blame] | 834 | INetworkStatsService statsService, INetworkPolicyManager policyManager) { |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 835 | this(context, netManager, statsService, policyManager, |
| 836 | getDnsResolver(), new IpConnectivityLog()); |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | @VisibleForTesting |
| 840 | protected ConnectivityService(Context context, INetworkManagementService netManager, |
| 841 | INetworkStatsService statsService, INetworkPolicyManager policyManager, |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 842 | IDnsResolver dnsresolver, IpConnectivityLog logger) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 843 | if (DBG) log("ConnectivityService starting up"); |
Robert Greenwalt | de8383c | 2010-01-14 17:47:58 -0800 | [diff] [blame] | 844 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 845 | mSystemProperties = getSystemProperties(); |
| 846 | |
Hugo Benichi | f9fdf87 | 2016-07-28 17:53:06 +0900 | [diff] [blame] | 847 | mMetricsLog = logger; |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 848 | mDefaultRequest = createDefaultInternetRequestForTransport(-1, NetworkRequest.Type.REQUEST); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 849 | NetworkRequestInfo defaultNRI = new NetworkRequestInfo(null, mDefaultRequest, new Binder()); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 850 | mNetworkRequests.put(mDefaultRequest, defaultNRI); |
| 851 | mNetworkRequestInfoLogs.log("REGISTER " + defaultNRI); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 852 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 853 | mDefaultMobileDataRequest = createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 854 | NetworkCapabilities.TRANSPORT_CELLULAR, NetworkRequest.Type.BACKGROUND_REQUEST); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 855 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 856 | // The default WiFi request is a background request so that apps using WiFi are |
| 857 | // migrated to a better network (typically ethernet) when one comes up, instead |
| 858 | // of staying on WiFi forever. |
| 859 | mDefaultWifiRequest = createDefaultInternetRequestForTransport( |
| 860 | NetworkCapabilities.TRANSPORT_WIFI, NetworkRequest.Type.BACKGROUND_REQUEST); |
| 861 | |
Hugo Benichi | ad4db4e | 2016-10-17 15:54:51 +0900 | [diff] [blame] | 862 | mHandlerThread = new HandlerThread("ConnectivityServiceThread"); |
Lorenzo Colitti | e58961a | 2015-08-07 20:17:27 +0900 | [diff] [blame] | 863 | mHandlerThread.start(); |
| 864 | mHandler = new InternalHandler(mHandlerThread.getLooper()); |
| 865 | mTrackerHandler = new NetworkStateTrackerHandler(mHandlerThread.getLooper()); |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 866 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 867 | mReleasePendingIntentDelayMs = Settings.Secure.getInt(context.getContentResolver(), |
| 868 | Settings.Secure.CONNECTIVITY_RELEASE_PENDING_INTENT_DELAY_MS, 5_000); |
| 869 | |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 870 | mLingerDelayMs = mSystemProperties.getInt(LINGER_DELAY_PROPERTY, DEFAULT_LINGER_DELAY_MS); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 871 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 872 | mContext = checkNotNull(context, "missing Context"); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 873 | mNMS = checkNotNull(netManager, "missing INetworkManagementService"); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 874 | mStatsService = checkNotNull(statsService, "missing INetworkStatsService"); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 875 | mPolicyManager = checkNotNull(policyManager, "missing INetworkPolicyManager"); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 876 | mPolicyManagerInternal = checkNotNull( |
| 877 | LocalServices.getService(NetworkPolicyManagerInternal.class), |
| 878 | "missing NetworkPolicyManagerInternal"); |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 879 | mDnsResolver = checkNotNull(dnsresolver, "missing IDnsResolver"); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 880 | mProxyTracker = makeProxyTracker(); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 881 | |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 882 | mNetd = NetdService.getInstance(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 883 | mKeyStore = KeyStore.getInstance(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 884 | mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 885 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 886 | // To ensure uid rules are synchronized with Network Policy, register for |
| 887 | // NetworkPolicyManagerService events must happen prior to NetworkPolicyManagerService |
| 888 | // reading existing policy from disk. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 889 | try { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 890 | mPolicyManager.registerListener(mPolicyListener); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 891 | } catch (RemoteException e) { |
| 892 | // ouch, no rules updates means some processes may never get network |
Felipe Leme | d31a97f | 2016-05-06 14:53:50 -0700 | [diff] [blame] | 893 | loge("unable to register INetworkPolicyListener" + e); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | final PowerManager powerManager = (PowerManager) context.getSystemService( |
| 897 | Context.POWER_SERVICE); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 898 | mNetTransitionWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
| 899 | mNetTransitionWakeLockTimeout = mContext.getResources().getInteger( |
| 900 | com.android.internal.R.integer.config_networkTransitionTimeout); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 901 | mPendingIntentWakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 902 | |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 903 | mNetConfigs = new NetworkConfig[ConnectivityManager.MAX_NETWORK_TYPE+1]; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 904 | |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 905 | // 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] | 906 | boolean wifiOnly = mSystemProperties.getBoolean("ro.radio.noril", false); |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 907 | log("wifiOnly=" + wifiOnly); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 908 | String[] naStrings = context.getResources().getStringArray( |
| 909 | com.android.internal.R.array.networkAttributes); |
| 910 | for (String naString : naStrings) { |
| 911 | try { |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 912 | NetworkConfig n = new NetworkConfig(naString); |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 913 | if (VDBG) log("naString=" + naString + " config=" + n); |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 914 | if (n.type > ConnectivityManager.MAX_NETWORK_TYPE) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 915 | loge("Error in networkAttributes - ignoring attempt to define type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 916 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 917 | continue; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 918 | } |
Wink Saville | 51f456f | 2013-04-23 14:26:51 -0700 | [diff] [blame] | 919 | if (wifiOnly && ConnectivityManager.isNetworkTypeMobile(n.type)) { |
| 920 | log("networkAttributes - ignoring mobile as this dev is wifiOnly " + |
| 921 | n.type); |
| 922 | continue; |
| 923 | } |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 924 | if (mNetConfigs[n.type] != null) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 925 | loge("Error in networkAttributes - ignoring attempt to redefine type " + |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 926 | n.type); |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 927 | continue; |
| 928 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 929 | mLegacyTypeTracker.addSupportedType(n.type); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 930 | |
Wink Saville | 975c848 | 2011-04-07 14:23:45 -0700 | [diff] [blame] | 931 | mNetConfigs[n.type] = n; |
Robert Greenwalt | 5154ae76 | 2009-10-30 14:17:42 -0700 | [diff] [blame] | 932 | mNetworksDefined++; |
| 933 | } catch(Exception e) { |
| 934 | // ignore it - leave the entry null |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 935 | } |
| 936 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 937 | |
| 938 | // Forcibly add TYPE_VPN as a supported type, if it has not already been added via config. |
| 939 | if (mNetConfigs[TYPE_VPN] == null) { |
| 940 | // mNetConfigs is used only for "restore time", which isn't applicable to VPNs, so we |
| 941 | // don't need to add TYPE_VPN to mNetConfigs. |
| 942 | mLegacyTypeTracker.addSupportedType(TYPE_VPN); |
| 943 | mNetworksDefined++; // used only in the log() statement below. |
| 944 | } |
| 945 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 946 | // Do the same for Ethernet, since it's often not specified in the configs, although many |
| 947 | // devices can use it via USB host adapters. |
| 948 | if (mNetConfigs[TYPE_ETHERNET] == null && hasService(Context.ETHERNET_SERVICE)) { |
| 949 | mLegacyTypeTracker.addSupportedType(TYPE_ETHERNET); |
| 950 | mNetworksDefined++; |
| 951 | } |
| 952 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 953 | if (VDBG) log("mNetworksDefined=" + mNetworksDefined); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 954 | |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 955 | mProtectedNetworks = new ArrayList<Integer>(); |
| 956 | int[] protectedNetworks = context.getResources().getIntArray( |
| 957 | com.android.internal.R.array.config_protectedNetworks); |
| 958 | for (int p : protectedNetworks) { |
| 959 | if ((mNetConfigs[p] != null) && (mProtectedNetworks.contains(p) == false)) { |
| 960 | mProtectedNetworks.add(p); |
| 961 | } else { |
| 962 | if (DBG) loge("Ignoring protectedNetwork " + p); |
| 963 | } |
| 964 | } |
| 965 | |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 966 | mTethering = makeTethering(); |
Robert Greenwalt | c9d5fb7 | 2010-02-25 12:29:30 -0800 | [diff] [blame] | 967 | |
Chenbo Feng | 436fe58 | 2019-02-25 22:55:58 -0800 | [diff] [blame] | 968 | mPermissionMonitor = new PermissionMonitor(mContext, mNMS, mNetd); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 969 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 970 | // Set up the listener for user state for creating user VPNs. |
| 971 | // Should run on mHandler to avoid any races. |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 972 | IntentFilter intentFilter = new IntentFilter(); |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 973 | intentFilter.addAction(Intent.ACTION_USER_STARTED); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 974 | intentFilter.addAction(Intent.ACTION_USER_STOPPED); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 975 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 976 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 977 | intentFilter.addAction(Intent.ACTION_USER_UNLOCKED); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 978 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 979 | mIntentReceiver, |
| 980 | UserHandle.ALL, |
| 981 | intentFilter, |
| 982 | null /* broadcastPermission */, |
| 983 | mHandler); |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 984 | mContext.registerReceiverAsUser(mUserPresentReceiver, UserHandle.SYSTEM, |
| 985 | new IntentFilter(Intent.ACTION_USER_PRESENT), null, null); |
Lorenzo Colitti | 13c9fde | 2013-03-15 04:22:37 +0900 | [diff] [blame] | 986 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 987 | // Listen to package add and removal events for all users. |
| 988 | intentFilter = new IntentFilter(); |
| 989 | intentFilter.addAction(Intent.ACTION_PACKAGE_ADDED); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 990 | intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 991 | intentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 992 | intentFilter.addDataScheme("package"); |
| 993 | mContext.registerReceiverAsUser( |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 994 | mIntentReceiver, |
| 995 | UserHandle.ALL, |
| 996 | intentFilter, |
| 997 | null /* broadcastPermission */, |
| 998 | mHandler); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 999 | |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 1000 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1001 | mNMS.registerObserver(mTethering); |
| 1002 | mNMS.registerObserver(mDataActivityObserver); |
Chia-chi Yeh | 008ff39 | 2011-05-23 15:08:29 -0700 | [diff] [blame] | 1003 | } catch (RemoteException e) { |
| 1004 | loge("Error registering observer :" + e); |
| 1005 | } |
| 1006 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1007 | mSettingsObserver = new SettingsObserver(mContext, mHandler); |
| 1008 | registerSettingsCallbacks(); |
Robert Greenwalt | b7090d6 | 2010-12-02 11:31:00 -0800 | [diff] [blame] | 1009 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1010 | final DataConnectionStats dataConnectionStats = new DataConnectionStats(mContext); |
| 1011 | dataConnectionStats.startMonitoring(); |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 1012 | |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 1013 | mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1014 | |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1015 | mKeepaliveTracker = new KeepaliveTracker(mContext, mHandler); |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 1016 | mNotifier = new NetworkNotificationManager(mContext, mTelephonyManager, |
| 1017 | mContext.getSystemService(NotificationManager.class)); |
Lorenzo Colitti | 84e6f123 | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 1018 | |
| 1019 | final int dailyLimit = Settings.Global.getInt(mContext.getContentResolver(), |
| 1020 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_DAILY_LIMIT, |
| 1021 | LingerMonitor.DEFAULT_NOTIFICATION_DAILY_LIMIT); |
| 1022 | final long rateLimit = Settings.Global.getLong(mContext.getContentResolver(), |
| 1023 | Settings.Global.NETWORK_SWITCH_NOTIFICATION_RATE_LIMIT_MILLIS, |
| 1024 | LingerMonitor.DEFAULT_NOTIFICATION_RATE_LIMIT_MILLIS); |
| 1025 | mLingerMonitor = new LingerMonitor(mContext, mNotifier, dailyLimit, rateLimit); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 1026 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 1027 | mMultinetworkPolicyTracker = createMultinetworkPolicyTracker( |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 1028 | mContext, mHandler, () -> rematchForAvoidBadWifiUpdate()); |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 1029 | mMultinetworkPolicyTracker.start(); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 1030 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 1031 | mMultipathPolicyTracker = new MultipathPolicyTracker(mContext, mHandler); |
| 1032 | |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 1033 | mDnsManager = new DnsManager(mContext, mDnsResolver, mSystemProperties); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1034 | registerPrivateDnsSettingsCallbacks(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 1036 | |
Mike Yu | f972975 | 2018-08-17 15:22:05 +0800 | [diff] [blame] | 1037 | @VisibleForTesting |
| 1038 | protected Tethering makeTethering() { |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1039 | // TODO: Move other elements into @Overridden getters. |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1040 | final TetheringDependencies deps = new TetheringDependencies() { |
| 1041 | @Override |
| 1042 | public boolean isTetheringSupported() { |
| 1043 | return ConnectivityService.this.isTetheringSupported(); |
| 1044 | } |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1045 | @Override |
| 1046 | public NetworkRequest getDefaultNetworkRequest() { |
| 1047 | return mDefaultRequest; |
| 1048 | } |
Erik Kline | 465ff3a | 2018-03-09 14:18:02 +0900 | [diff] [blame] | 1049 | }; |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1050 | return new Tethering(mContext, mNMS, mStatsService, mPolicyManager, |
Erik Kline | 47222fc | 2017-04-30 19:36:15 +0900 | [diff] [blame] | 1051 | IoThread.get().getLooper(), new MockableSystemProperties(), |
| 1052 | deps); |
| 1053 | } |
| 1054 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 1055 | @VisibleForTesting |
| 1056 | protected ProxyTracker makeProxyTracker() { |
| 1057 | return new ProxyTracker(mContext, mHandler, EVENT_PROXY_HAS_CHANGED); |
| 1058 | } |
| 1059 | |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1060 | private static NetworkCapabilities createDefaultNetworkCapabilitiesForUid(int uid) { |
| 1061 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1062 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1063 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
| 1064 | netCap.removeCapability(NET_CAPABILITY_NOT_VPN); |
| 1065 | netCap.setSingleUid(uid); |
| 1066 | return netCap; |
| 1067 | } |
| 1068 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 1069 | private NetworkRequest createDefaultInternetRequestForTransport( |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1070 | int transportType, NetworkRequest.Type type) { |
Erik Kline | 7230290 | 2018-06-14 17:36:40 +0900 | [diff] [blame] | 1071 | final NetworkCapabilities netCap = new NetworkCapabilities(); |
Lorenzo Colitti | 8deb341 | 2015-05-14 17:07:20 +0900 | [diff] [blame] | 1072 | netCap.addCapability(NET_CAPABILITY_INTERNET); |
| 1073 | netCap.addCapability(NET_CAPABILITY_NOT_RESTRICTED); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1074 | if (transportType > -1) { |
| 1075 | netCap.addTransportType(transportType); |
| 1076 | } |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 1077 | return new NetworkRequest(netCap, TYPE_NONE, nextNetworkRequestId(), type); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1078 | } |
| 1079 | |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1080 | // Used only for testing. |
| 1081 | // TODO: Delete this and either: |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1082 | // 1. Give FakeSettingsProvider the ability to send settings change notifications (requires |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1083 | // changing ContentResolver to make registerContentObserver non-final). |
| 1084 | // 2. Give FakeSettingsProvider an alternative notification mechanism and have the test use it |
| 1085 | // by subclassing SettingsObserver. |
| 1086 | @VisibleForTesting |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1087 | void updateAlwaysOnNetworks() { |
| 1088 | mHandler.sendEmptyMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Lorenzo Colitti | 762ea7a | 2016-06-05 21:00:23 +0900 | [diff] [blame] | 1089 | } |
| 1090 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1091 | // See FakeSettingsProvider comment above. |
| 1092 | @VisibleForTesting |
| 1093 | void updatePrivateDnsSettings() { |
| 1094 | mHandler.sendEmptyMessage(EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
| 1095 | } |
| 1096 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1097 | private void handleAlwaysOnNetworkRequest( |
| 1098 | NetworkRequest networkRequest, String settingName, boolean defaultValue) { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 1099 | final boolean enable = toBool(Settings.Global.getInt( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1100 | mContext.getContentResolver(), settingName, encodeBool(defaultValue))); |
| 1101 | final boolean isEnabled = (mNetworkRequests.get(networkRequest) != null); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1102 | if (enable == isEnabled) { |
| 1103 | return; // Nothing to do. |
| 1104 | } |
| 1105 | |
| 1106 | if (enable) { |
| 1107 | handleRegisterNetworkRequest(new NetworkRequestInfo( |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1108 | null, networkRequest, new Binder())); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1109 | } else { |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 1110 | handleReleaseNetworkRequest(networkRequest, Process.SYSTEM_UID, |
| 1111 | /* callOnUnavailable */ false); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1115 | private void handleConfigureAlwaysOnNetworks() { |
| 1116 | handleAlwaysOnNetworkRequest( |
| 1117 | mDefaultMobileDataRequest,Settings.Global.MOBILE_DATA_ALWAYS_ON, true); |
| 1118 | handleAlwaysOnNetworkRequest(mDefaultWifiRequest, Settings.Global.WIFI_ALWAYS_REQUESTED, |
| 1119 | false); |
| 1120 | } |
| 1121 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1122 | private void registerSettingsCallbacks() { |
| 1123 | // Watch for global HTTP proxy changes. |
| 1124 | mSettingsObserver.observe( |
| 1125 | Settings.Global.getUriFor(Settings.Global.HTTP_PROXY), |
| 1126 | EVENT_APPLY_GLOBAL_HTTP_PROXY); |
| 1127 | |
| 1128 | // Watch for whether or not to keep mobile data always on. |
| 1129 | mSettingsObserver.observe( |
| 1130 | Settings.Global.getUriFor(Settings.Global.MOBILE_DATA_ALWAYS_ON), |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 1131 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
| 1132 | |
| 1133 | // Watch for whether or not to keep wifi always on. |
| 1134 | mSettingsObserver.observe( |
| 1135 | Settings.Global.getUriFor(Settings.Global.WIFI_ALWAYS_REQUESTED), |
| 1136 | EVENT_CONFIGURE_ALWAYS_ON_NETWORKS); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 1137 | } |
| 1138 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1139 | private void registerPrivateDnsSettingsCallbacks() { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1140 | for (Uri uri : DnsManager.getPrivateDnsSettingsUris()) { |
| 1141 | mSettingsObserver.observe(uri, EVENT_PRIVATE_DNS_SETTINGS_CHANGED); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 1142 | } |
| 1143 | } |
| 1144 | |
Robert Greenwalt | 34524f0 | 2014-05-18 16:22:10 -0700 | [diff] [blame] | 1145 | private synchronized int nextNetworkRequestId() { |
| 1146 | return mNextNetworkRequestId++; |
| 1147 | } |
| 1148 | |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 1149 | @VisibleForTesting |
| 1150 | protected int reserveNetId() { |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1151 | synchronized (mNetworkForNetId) { |
| 1152 | for (int i = MIN_NET_ID; i <= MAX_NET_ID; i++) { |
| 1153 | int netId = mNextNetId; |
| 1154 | if (++mNextNetId > MAX_NET_ID) mNextNetId = MIN_NET_ID; |
| 1155 | // Make sure NetID unused. http://b/16815182 |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1156 | if (!mNetIdInUse.get(netId)) { |
| 1157 | mNetIdInUse.put(netId, true); |
| 1158 | return netId; |
Paul Jensen | 60061a6 | 2014-08-05 14:13:48 -0400 | [diff] [blame] | 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | throw new IllegalStateException("No free netIds"); |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 1163 | } |
| 1164 | |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1165 | private NetworkState getFilteredNetworkState(int networkType, int uid) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1166 | if (mLegacyTypeTracker.isTypeSupported(networkType)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1167 | final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1168 | final NetworkState state; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1169 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1170 | state = nai.getNetworkState(); |
| 1171 | state.networkInfo.setType(networkType); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1172 | } else { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1173 | final NetworkInfo info = new NetworkInfo(networkType, 0, |
| 1174 | getNetworkTypeName(networkType), ""); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1175 | info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null); |
| 1176 | info.setIsAvailable(true); |
Jeff Sharkey | 6226216 | 2017-12-04 15:52:01 -0700 | [diff] [blame] | 1177 | final NetworkCapabilities capabilities = new NetworkCapabilities(); |
| 1178 | capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, |
| 1179 | !info.isRoaming()); |
| 1180 | state = new NetworkState(info, new LinkProperties(), capabilities, |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1181 | null, null, null); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1182 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1183 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1184 | return state; |
| 1185 | } else { |
| 1186 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1187 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1188 | } |
| 1189 | |
junyulai | 4a192e2 | 2018-06-13 15:00:37 +0800 | [diff] [blame] | 1190 | @VisibleForTesting |
| 1191 | protected NetworkAgentInfo getNetworkAgentInfoForNetwork(Network network) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1192 | if (network == null) { |
| 1193 | return null; |
| 1194 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1195 | return getNetworkAgentInfoForNetId(network.netId); |
| 1196 | } |
| 1197 | |
| 1198 | private NetworkAgentInfo getNetworkAgentInfoForNetId(int netId) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1199 | synchronized (mNetworkForNetId) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1200 | return mNetworkForNetId.get(netId); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1201 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1202 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1203 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1204 | private Network[] getVpnUnderlyingNetworks(int uid) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1205 | synchronized (mVpns) { |
| 1206 | if (!mLockdownEnabled) { |
| 1207 | int user = UserHandle.getUserId(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1208 | Vpn vpn = mVpns.get(user); |
| 1209 | if (vpn != null && vpn.appliesToUid(uid)) { |
| 1210 | return vpn.getUnderlyingNetworks(); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
| 1214 | return null; |
| 1215 | } |
| 1216 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1217 | private NetworkState getUnfilteredActiveNetworkState(int uid) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 1218 | NetworkAgentInfo nai = getDefaultNetwork(); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1219 | |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1220 | final Network[] networks = getVpnUnderlyingNetworks(uid); |
| 1221 | if (networks != null) { |
| 1222 | // getUnderlyingNetworks() returns: |
| 1223 | // null => there was no VPN, or the VPN didn't specify anything, so we use the default. |
| 1224 | // empty array => the VPN explicitly said "no default network". |
| 1225 | // non-empty array => the VPN specified one or more default networks; we use the |
| 1226 | // first one. |
| 1227 | if (networks.length > 0) { |
| 1228 | nai = getNetworkAgentInfoForNetwork(networks[0]); |
| 1229 | } else { |
| 1230 | nai = null; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 1231 | } |
| 1232 | } |
| 1233 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1234 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1235 | return nai.getNetworkState(); |
| 1236 | } else { |
| 1237 | return NetworkState.EMPTY; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1238 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | /** |
| 1242 | * Check if UID should be blocked from using the network with the given LinkProperties. |
| 1243 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1244 | private boolean isNetworkWithLinkPropertiesBlocked(LinkProperties lp, int uid, |
| 1245 | boolean ignoreBlocked) { |
| 1246 | // Networks aren't blocked when ignoring blocked status |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1247 | if (ignoreBlocked) { |
| 1248 | return false; |
| 1249 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1250 | synchronized (mVpns) { |
| 1251 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1252 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 1253 | return true; |
| 1254 | } |
| 1255 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1256 | final String iface = (lp == null ? "" : lp.getInterfaceName()); |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1257 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, iface); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1260 | private void maybeLogBlockedNetworkInfo(NetworkInfo ni, int uid) { |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 1261 | if (ni == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1262 | return; |
| 1263 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1264 | final boolean blocked; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1265 | synchronized (mBlockedAppUids) { |
| 1266 | if (ni.getDetailedState() == DetailedState.BLOCKED && mBlockedAppUids.add(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1267 | blocked = true; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1268 | } else if (ni.isConnected() && mBlockedAppUids.remove(uid)) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1269 | blocked = false; |
| 1270 | } else { |
| 1271 | return; |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1272 | } |
| 1273 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 1274 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1275 | log(String.format("Returning %s NetworkInfo to uid=%d", action, uid)); |
| 1276 | mNetworkInfoBlockingLogs.log(action + " " + uid); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 1277 | } |
| 1278 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1279 | private void maybeLogBlockedStatusChanged(NetworkRequestInfo nri, Network net, |
| 1280 | boolean blocked) { |
| 1281 | if (nri == null || net == null || !LOGD_BLOCKED_NETWORKINFO) { |
| 1282 | return; |
| 1283 | } |
| 1284 | String action = blocked ? "BLOCKED" : "UNBLOCKED"; |
| 1285 | log(String.format("Blocked status changed to %s for %d(%d) on netId %d", blocked, |
| 1286 | nri.mUid, nri.request.requestId, net.netId)); |
| 1287 | mNetworkInfoBlockingLogs.log(action + " " + nri.mUid); |
| 1288 | } |
| 1289 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1290 | /** |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1291 | * Apply any relevant filters to {@link NetworkState} for the given UID. For |
| 1292 | * example, this may mark the network as {@link DetailedState#BLOCKED} based |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1293 | * on {@link #isNetworkWithLinkPropertiesBlocked}. |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1294 | */ |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1295 | private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1296 | if (state == null || state.networkInfo == null || state.linkProperties == null) return; |
| 1297 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1298 | if (isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, ignoreBlocked)) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1299 | state.networkInfo.setDetailedState(DetailedState.BLOCKED, null, null); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1300 | } |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1301 | synchronized (mVpns) { |
| 1302 | if (mLockdownTracker != null) { |
| 1303 | mLockdownTracker.augmentNetworkInfo(state.networkInfo); |
| 1304 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1305 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
| 1308 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | * Return NetworkInfo for the active (i.e., connected) network interface. |
| 1310 | * It is assumed that at most one network is active at a time. If more |
| 1311 | * than one is active, it is indeterminate which will be returned. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1312 | * @return the info for the active network, or {@code null} if none is |
| 1313 | * active |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1314 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1315 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1316 | public NetworkInfo getActiveNetworkInfo() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1317 | enforceAccessPermission(); |
| 1318 | final int uid = Binder.getCallingUid(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1319 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1320 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1321 | maybeLogBlockedNetworkInfo(state.networkInfo, uid); |
| 1322 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1323 | } |
| 1324 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1325 | @Override |
| 1326 | public Network getActiveNetwork() { |
| 1327 | enforceAccessPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1328 | return getActiveNetworkForUidInternal(Binder.getCallingUid(), false); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1332 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1333 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1334 | return getActiveNetworkForUidInternal(uid, ignoreBlocked); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1335 | } |
| 1336 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1337 | private Network getActiveNetworkForUidInternal(final int uid, boolean ignoreBlocked) { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1338 | final int user = UserHandle.getUserId(uid); |
| 1339 | int vpnNetId = NETID_UNSET; |
| 1340 | synchronized (mVpns) { |
| 1341 | final Vpn vpn = mVpns.get(user); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1342 | // TODO : now that capabilities contain the UID, the appliesToUid test should |
| 1343 | // be removed as the satisfying test below should be enough. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1344 | if (vpn != null && vpn.appliesToUid(uid)) vpnNetId = vpn.getNetId(); |
| 1345 | } |
| 1346 | NetworkAgentInfo nai; |
| 1347 | if (vpnNetId != NETID_UNSET) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 1348 | nai = getNetworkAgentInfoForNetId(vpnNetId); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1349 | if (nai != null) { |
| 1350 | final NetworkCapabilities requiredCaps = |
| 1351 | createDefaultNetworkCapabilitiesForUid(uid); |
| 1352 | if (requiredCaps.satisfiedByNetworkCapabilities(nai.networkCapabilities)) { |
| 1353 | return nai.network; |
| 1354 | } |
| 1355 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1356 | } |
| 1357 | nai = getDefaultNetwork(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1358 | if (nai != null |
| 1359 | && isNetworkWithLinkPropertiesBlocked(nai.linkProperties, uid, ignoreBlocked)) { |
| 1360 | nai = null; |
| 1361 | } |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1362 | return nai != null ? nai.network : null; |
| 1363 | } |
| 1364 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1365 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1366 | public NetworkInfo getActiveNetworkInfoUnfiltered() { |
| 1367 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1368 | final int uid = Binder.getCallingUid(); |
| 1369 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1370 | return state.networkInfo; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1371 | } |
| 1372 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1373 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1374 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1375 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1376 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1377 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1378 | return state.networkInfo; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1379 | } |
| 1380 | |
| 1381 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1382 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1383 | enforceAccessPermission(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1384 | final int uid = Binder.getCallingUid(); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1385 | if (getVpnUnderlyingNetworks(uid) != null) { |
| 1386 | // A VPN is active, so we may need to return one of its underlying networks. This |
| 1387 | // information is not available in LegacyTypeTracker, so we have to get it from |
| 1388 | // getUnfilteredActiveNetworkState. |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1389 | final NetworkState state = getUnfilteredActiveNetworkState(uid); |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1390 | if (state.networkInfo != null && state.networkInfo.getType() == networkType) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1391 | filterNetworkStateForUid(state, uid, false); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1392 | return state.networkInfo; |
Lorenzo Colitti | d6a7980 | 2015-02-05 13:57:17 +0900 | [diff] [blame] | 1393 | } |
| 1394 | } |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1395 | final NetworkState state = getFilteredNetworkState(networkType, uid); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1396 | return state.networkInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1399 | @Override |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1400 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1401 | enforceAccessPermission(); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1402 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1403 | if (nai != null) { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1404 | final NetworkState state = nai.getNetworkState(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1405 | filterNetworkStateForUid(state, uid, ignoreBlocked); |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1406 | return state.networkInfo; |
| 1407 | } else { |
| 1408 | return null; |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1409 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1413 | public NetworkInfo[] getAllNetworkInfo() { |
| 1414 | enforceAccessPermission(); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1415 | final ArrayList<NetworkInfo> result = Lists.newArrayList(); |
Paul Jensen | f9ee0e5 | 2014-09-19 11:14:12 -0400 | [diff] [blame] | 1416 | for (int networkType = 0; networkType <= ConnectivityManager.MAX_NETWORK_TYPE; |
| 1417 | networkType++) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1418 | NetworkInfo info = getNetworkInfo(networkType); |
| 1419 | if (info != null) { |
| 1420 | result.add(info); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1422 | } |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1423 | return result.toArray(new NetworkInfo[result.size()]); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1424 | } |
| 1425 | |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1426 | @Override |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1427 | public Network getNetworkForType(int networkType) { |
| 1428 | enforceAccessPermission(); |
| 1429 | final int uid = Binder.getCallingUid(); |
Chalard Jean | c098291 | 2018-06-07 16:11:34 +0900 | [diff] [blame] | 1430 | NetworkState state = getFilteredNetworkState(networkType, uid); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1431 | if (!isNetworkWithLinkPropertiesBlocked(state.linkProperties, uid, false)) { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1432 | return state.network; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1433 | } |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1434 | return null; |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | @Override |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1438 | public Network[] getAllNetworks() { |
| 1439 | enforceAccessPermission(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1440 | synchronized (mNetworkForNetId) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1441 | final Network[] result = new Network[mNetworkForNetId.size()]; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1442 | for (int i = 0; i < mNetworkForNetId.size(); i++) { |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1443 | result[i] = mNetworkForNetId.valueAt(i).network; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1444 | } |
Paul Jensen | e75b9e3 | 2015-04-06 11:54:53 -0400 | [diff] [blame] | 1445 | return result; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1446 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1447 | } |
| 1448 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1449 | @Override |
| 1450 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1451 | // The basic principle is: if an app's traffic could possibly go over a |
| 1452 | // network, without the app doing anything multinetwork-specific, |
| 1453 | // (hence, by "default"), then include that network's capabilities in |
| 1454 | // the array. |
| 1455 | // |
| 1456 | // In the normal case, app traffic only goes over the system's default |
| 1457 | // network connection, so that's the only network returned. |
| 1458 | // |
| 1459 | // With a VPN in force, some app traffic may go into the VPN, and thus |
| 1460 | // over whatever underlying networks the VPN specifies, while other app |
| 1461 | // traffic may go over the system default network (e.g.: a split-tunnel |
| 1462 | // VPN, or an app disallowed by the VPN), so the set of networks |
| 1463 | // returned includes the VPN's underlying networks and the system |
| 1464 | // default. |
| 1465 | enforceAccessPermission(); |
| 1466 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1467 | HashMap<Network, NetworkCapabilities> result = new HashMap<>(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1468 | |
| 1469 | NetworkAgentInfo nai = getDefaultNetwork(); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1470 | NetworkCapabilities nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1471 | if (nc != null) { |
| 1472 | result.put(nai.network, nc); |
| 1473 | } |
| 1474 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 1475 | synchronized (mVpns) { |
| 1476 | if (!mLockdownEnabled) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1477 | Vpn vpn = mVpns.get(userId); |
| 1478 | if (vpn != null) { |
| 1479 | Network[] networks = vpn.getUnderlyingNetworks(); |
| 1480 | if (networks != null) { |
| 1481 | for (Network network : networks) { |
| 1482 | nai = getNetworkAgentInfoForNetwork(network); |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1483 | nc = getNetworkCapabilitiesInternal(nai); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1484 | if (nc != null) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1485 | result.put(network, nc); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1486 | } |
| 1487 | } |
| 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | |
| 1493 | NetworkCapabilities[] out = new NetworkCapabilities[result.size()]; |
| 1494 | out = result.values().toArray(out); |
| 1495 | return out; |
| 1496 | } |
| 1497 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1498 | @Override |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1499 | public boolean isNetworkSupported(int networkType) { |
| 1500 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1501 | return mLegacyTypeTracker.isTypeSupported(networkType); |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1504 | /** |
| 1505 | * Return LinkProperties for the active (i.e., connected) default |
| 1506 | * network interface. It is assumed that at most one default network |
| 1507 | * is active at a time. If more than one is active, it is indeterminate |
| 1508 | * which will be returned. |
| 1509 | * @return the ip properties for the active network, or {@code null} if |
| 1510 | * none is active |
| 1511 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1512 | @Override |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1513 | public LinkProperties getActiveLinkProperties() { |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1514 | enforceAccessPermission(); |
| 1515 | final int uid = Binder.getCallingUid(); |
| 1516 | NetworkState state = getUnfilteredActiveNetworkState(uid); |
| 1517 | return state.linkProperties; |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1520 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1521 | public LinkProperties getLinkPropertiesForType(int networkType) { |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1522 | enforceAccessPermission(); |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 1523 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1524 | if (nai != null) { |
| 1525 | synchronized (nai) { |
| 1526 | return new LinkProperties(nai.linkProperties); |
| 1527 | } |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1528 | } |
| 1529 | return null; |
| 1530 | } |
| 1531 | |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 1532 | // TODO - this should be ALL networks |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1533 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1534 | public LinkProperties getLinkProperties(Network network) { |
| 1535 | enforceAccessPermission(); |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1536 | return getLinkProperties(getNetworkAgentInfoForNetwork(network)); |
| 1537 | } |
| 1538 | |
| 1539 | private LinkProperties getLinkProperties(NetworkAgentInfo nai) { |
| 1540 | if (nai == null) { |
| 1541 | return null; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1542 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 1543 | synchronized (nai) { |
| 1544 | return new LinkProperties(nai.linkProperties); |
| 1545 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1548 | private NetworkCapabilities getNetworkCapabilitiesInternal(NetworkAgentInfo nai) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1549 | if (nai != null) { |
| 1550 | synchronized (nai) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1551 | if (nai.networkCapabilities != null) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1552 | return networkCapabilitiesRestrictedForCallerPermissions( |
| 1553 | nai.networkCapabilities, |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1554 | Binder.getCallingPid(), Binder.getCallingUid()); |
Sanket Padawe | 7094d22 | 2015-05-01 16:55:00 -0700 | [diff] [blame] | 1555 | } |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1556 | } |
| 1557 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1558 | return null; |
| 1559 | } |
| 1560 | |
| 1561 | @Override |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1562 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 1563 | enforceAccessPermission(); |
| 1564 | return getNetworkCapabilitiesInternal(getNetworkAgentInfoForNetwork(network)); |
| 1565 | } |
| 1566 | |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1567 | private NetworkCapabilities networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1568 | NetworkCapabilities nc, int callerPid, int callerUid) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1569 | final NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 1570 | if (!checkSettingsPermission(callerPid, callerUid)) { |
| 1571 | newNc.setUids(null); |
| 1572 | newNc.setSSID(null); |
| 1573 | } |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 1574 | if (newNc.getNetworkSpecifier() != null) { |
| 1575 | newNc.setNetworkSpecifier(newNc.getNetworkSpecifier().redact()); |
| 1576 | } |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 1577 | return newNc; |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1578 | } |
| 1579 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1580 | private void restrictRequestUidsForCaller(NetworkCapabilities nc) { |
| 1581 | if (!checkSettingsPermission()) { |
| 1582 | nc.setSingleUid(Binder.getCallingUid()); |
| 1583 | } |
| 1584 | } |
| 1585 | |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 1586 | private void restrictBackgroundRequestForCaller(NetworkCapabilities nc) { |
| 1587 | if (!mPermissionMonitor.hasUseBackgroundNetworksPermission(Binder.getCallingUid())) { |
| 1588 | nc.addCapability(NET_CAPABILITY_FOREGROUND); |
| 1589 | } |
| 1590 | } |
| 1591 | |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 1592 | @Override |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1593 | public NetworkState[] getAllNetworkState() { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1594 | // Require internal since we're handing out IMSI details |
| 1595 | enforceConnectivityInternalPermission(); |
| 1596 | |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1597 | final ArrayList<NetworkState> result = Lists.newArrayList(); |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 1598 | for (Network network : getAllNetworks()) { |
| 1599 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 1600 | if (nai != null) { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 1601 | // TODO (b/73321673) : NetworkState contains a copy of the |
| 1602 | // NetworkCapabilities, which may contain UIDs of apps to which the |
| 1603 | // network applies. Should the UIDs be cleared so as not to leak or |
| 1604 | // interfere ? |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1605 | result.add(nai.getNetworkState()); |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1606 | } |
| 1607 | } |
| 1608 | return result.toArray(new NetworkState[result.size()]); |
| 1609 | } |
| 1610 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1611 | @Override |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1612 | @Deprecated |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1613 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1614 | Log.w(TAG, "Shame on UID " + Binder.getCallingUid() |
| 1615 | + " for calling the hidden API getNetworkQuotaInfo(). Shame!"); |
| 1616 | return new NetworkQuotaInfo(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1617 | } |
| 1618 | |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1619 | @Override |
| 1620 | public boolean isActiveNetworkMetered() { |
| 1621 | enforceAccessPermission(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1622 | |
Varun Anand | c51b06d | 2019-02-25 17:22:02 -0800 | [diff] [blame] | 1623 | final NetworkCapabilities caps = getNetworkCapabilities(getActiveNetwork()); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 1624 | if (caps != null) { |
| 1625 | return !caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED); |
| 1626 | } else { |
| 1627 | // Always return the most conservative value |
| 1628 | return true; |
| 1629 | } |
Jeff Sharkey | 5f4dafb | 2012-04-30 15:47:05 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1632 | private INetworkManagementEventObserver mDataActivityObserver = new BaseNetworkObserver() { |
| 1633 | @Override |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1634 | public void interfaceClassDataActivityChanged(String label, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1635 | int deviceType = Integer.parseInt(label); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 1636 | sendDataActivityBroadcast(deviceType, active, tsNanos); |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1637 | } |
Jeff Sharkey | 216c181 | 2012-08-05 14:29:23 -0700 | [diff] [blame] | 1638 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 1639 | |
Robert Greenwalt | 9c75d4a | 2009-09-27 17:27:04 -0700 | [diff] [blame] | 1640 | /** |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1641 | * Ensures that the system cannot call a particular method. |
| 1642 | */ |
| 1643 | private boolean disallowedBecauseSystemCaller() { |
| 1644 | // TODO: start throwing a SecurityException when GnssLocationProvider stops calling |
Anil Admal | e41992c | 2019-04-08 12:36:02 -0700 | [diff] [blame] | 1645 | // requestRouteToHost. In Q, GnssLocationProvider is changed to not call requestRouteToHost |
| 1646 | // for devices launched with Q and above. However, existing devices upgrading to Q and |
| 1647 | // above must continued to be supported for few more releases. |
| 1648 | if (isSystem(Binder.getCallingUid()) && SystemProperties.getInt( |
| 1649 | "ro.product.first_api_level", 0) > Build.VERSION_CODES.P) { |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1650 | log("This method exists only for app backwards compatibility" |
| 1651 | + " and must not be called by system services."); |
| 1652 | return true; |
| 1653 | } |
| 1654 | return false; |
| 1655 | } |
| 1656 | |
| 1657 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1658 | * Ensure that a network route exists to deliver traffic to the specified |
| 1659 | * host via the specified network interface. |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1660 | * @param networkType the type of the network over which traffic to the |
| 1661 | * specified host is to be routed |
| 1662 | * @param hostAddress the IP address of the host to which the route is |
| 1663 | * desired |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1664 | * @return {@code true} on success, {@code false} on failure |
| 1665 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 1666 | @Override |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1667 | public boolean requestRouteToHostAddress(int networkType, byte[] hostAddress) { |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 1668 | if (disallowedBecauseSystemCaller()) { |
| 1669 | return false; |
| 1670 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | enforceChangePermission(); |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1672 | if (mProtectedNetworks.contains(networkType)) { |
| 1673 | enforceConnectivityInternalPermission(); |
| 1674 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1675 | |
Chad Brubaker | c023453 | 2014-02-14 13:24:29 -0800 | [diff] [blame] | 1676 | InetAddress addr; |
| 1677 | try { |
| 1678 | addr = InetAddress.getByAddress(hostAddress); |
| 1679 | } catch (UnknownHostException e) { |
| 1680 | if (DBG) log("requestRouteToHostAddress got " + e.toString()); |
| 1681 | return false; |
| 1682 | } |
Robert Greenwalt | 5039320 | 2011-06-21 17:26:14 -0700 | [diff] [blame] | 1683 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1684 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1685 | if (DBG) log("requestRouteToHostAddress on invalid network: " + networkType); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1686 | return false; |
| 1687 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1688 | |
| 1689 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
| 1690 | if (nai == null) { |
| 1691 | if (mLegacyTypeTracker.isTypeSupported(networkType) == false) { |
| 1692 | if (DBG) log("requestRouteToHostAddress on unsupported network: " + networkType); |
| 1693 | } else { |
| 1694 | if (DBG) log("requestRouteToHostAddress on down network: " + networkType); |
| 1695 | } |
| 1696 | return false; |
Ken Mixter | 151d303 | 2013-11-07 22:08:24 -0800 | [diff] [blame] | 1697 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1698 | |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1699 | DetailedState netState; |
| 1700 | synchronized (nai) { |
| 1701 | netState = nai.networkInfo.getDetailedState(); |
| 1702 | } |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1703 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1704 | if (netState != DetailedState.CONNECTED && netState != DetailedState.CAPTIVE_PORTAL_CHECK) { |
Robert Greenwalt | 58d4c59 | 2011-08-02 17:18:41 -0700 | [diff] [blame] | 1705 | if (VDBG) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1706 | log("requestRouteToHostAddress on down network " |
| 1707 | + "(" + networkType + ") - dropped" |
Robert Greenwalt | 2d37070 | 2014-06-03 17:22:11 -0700 | [diff] [blame] | 1708 | + " netState=" + netState); |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 1709 | } |
| 1710 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1711 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1712 | |
Sreeram Ramachandran | 515350a | 2014-05-22 16:30:48 -0700 | [diff] [blame] | 1713 | final int uid = Binder.getCallingUid(); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1714 | final long token = Binder.clearCallingIdentity(); |
Robert Greenwalt | 47f69fe | 2010-06-15 15:43:39 -0700 | [diff] [blame] | 1715 | try { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 1716 | LinkProperties lp; |
| 1717 | int netId; |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1718 | synchronized (nai) { |
| 1719 | lp = nai.linkProperties; |
| 1720 | netId = nai.network.netId; |
| 1721 | } |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1722 | boolean ok = addLegacyRouteToHost(lp, addr, netId, uid); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 1723 | if (DBG) log("requestRouteToHostAddress ok=" + ok); |
| 1724 | return ok; |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1725 | } finally { |
| 1726 | Binder.restoreCallingIdentity(token); |
| 1727 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 1728 | } |
| 1729 | |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1730 | private boolean addLegacyRouteToHost(LinkProperties lp, InetAddress addr, int netId, int uid) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1731 | RouteInfo bestRoute = RouteInfo.selectBestRoute(lp.getAllRoutes(), addr); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1732 | if (bestRoute == null) { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1733 | bestRoute = RouteInfo.makeHostRoute(addr, lp.getInterfaceName()); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1734 | } else { |
Lorenzo Colitti | f83d90c | 2013-03-15 13:58:38 +0900 | [diff] [blame] | 1735 | String iface = bestRoute.getInterface(); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1736 | if (bestRoute.getGateway().equals(addr)) { |
| 1737 | // 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] | 1738 | bestRoute = RouteInfo.makeHostRoute(addr, iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1739 | } else { |
| 1740 | // if we will connect to this through another route, add a direct route |
| 1741 | // to it's gateway |
Lorenzo Colitti | e167135 | 2013-03-08 12:30:44 -0800 | [diff] [blame] | 1742 | bestRoute = RouteInfo.makeHostRoute(addr, bestRoute.getGateway(), iface); |
Robert Greenwalt | ad55d35 | 2011-07-22 11:55:33 -0700 | [diff] [blame] | 1743 | } |
| 1744 | } |
Lorenzo Colitti | aa281e2 | 2015-09-04 13:12:42 +0900 | [diff] [blame] | 1745 | if (DBG) log("Adding legacy route " + bestRoute + |
| 1746 | " for UID/PID " + uid + "/" + Binder.getCallingPid()); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1747 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 1748 | mNMS.addLegacyRouteForNetId(netId, bestRoute, uid); |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1749 | } catch (Exception e) { |
| 1750 | // never crash - catch them all |
| 1751 | if (DBG) loge("Exception trying to add a route: " + e); |
Robert Greenwalt | 8beff95 | 2011-12-13 15:26:02 -0800 | [diff] [blame] | 1752 | return false; |
| 1753 | } |
Robert Greenwalt | 0a46db5 | 2011-07-14 14:28:05 -0700 | [diff] [blame] | 1754 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1755 | } |
| 1756 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1757 | @VisibleForTesting |
| 1758 | protected final INetdEventCallback mNetdEventCallback = new BaseNetdEventCallback() { |
| 1759 | @Override |
| 1760 | public void onPrivateDnsValidationEvent(int netId, String ipAddress, |
| 1761 | String hostname, boolean validated) { |
| 1762 | try { |
| 1763 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1764 | EVENT_PRIVATE_DNS_VALIDATION_UPDATE, |
| 1765 | new PrivateDnsValidationUpdate(netId, |
| 1766 | InetAddress.parseNumericAddress(ipAddress), |
| 1767 | hostname, validated))); |
| 1768 | } catch (IllegalArgumentException e) { |
| 1769 | loge("Error parsing ip address in validation event"); |
| 1770 | } |
| 1771 | } |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1772 | |
| 1773 | @Override |
| 1774 | public void onDnsEvent(int netId, int eventType, int returnCode, String hostname, |
| 1775 | String[] ipAddresses, int ipAddressesCount, long timestamp, int uid) { |
| 1776 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
| 1777 | // Netd event only allow registrants from system. Each NetworkMonitor thread is under |
| 1778 | // the caller thread of registerNetworkAgent. Thus, it's not allowed to register netd |
| 1779 | // event callback for certain nai. e.g. cellular. Register here to pass to |
| 1780 | // NetworkMonitor instead. |
| 1781 | // TODO: Move the Dns Event to NetworkMonitor. Use Binder.clearCallingIdentity() in |
| 1782 | // registerNetworkAgent to have NetworkMonitor created with system process as design |
| 1783 | // expectation. Also, NetdEventListenerService only allow one callback from each |
| 1784 | // caller type. Need to re-factor NetdEventListenerService to allow multiple |
| 1785 | // NetworkMonitor registrants. |
| 1786 | if (nai != null && nai.satisfies(mDefaultRequest)) { |
Chalard Jean | 971d9c9 | 2019-04-08 20:28:44 +0900 | [diff] [blame] | 1787 | Binder.withCleanCallingIdentity(() -> |
| 1788 | nai.networkMonitor().notifyDnsResponse(returnCode)); |
Chiachang Wang | 7a70a7e | 2018-11-27 18:00:05 +0800 | [diff] [blame] | 1789 | } |
| 1790 | } |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 1791 | |
| 1792 | @Override |
| 1793 | public void onNat64PrefixEvent(int netId, boolean added, |
| 1794 | String prefixString, int prefixLength) { |
| 1795 | mHandler.post(() -> handleNat64PrefixEvent(netId, added, prefixString, prefixLength)); |
| 1796 | } |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1797 | }; |
| 1798 | |
| 1799 | @VisibleForTesting |
| 1800 | protected void registerNetdEventCallback() { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1801 | final IIpConnectivityMetrics ipConnectivityMetrics = |
| 1802 | IIpConnectivityMetrics.Stub.asInterface( |
| 1803 | ServiceManager.getService(IpConnectivityLog.SERVICE_NAME)); |
| 1804 | if (ipConnectivityMetrics == null) { |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1805 | Slog.wtf(TAG, "Missing IIpConnectivityMetrics"); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1806 | return; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1807 | } |
| 1808 | |
| 1809 | try { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 1810 | ipConnectivityMetrics.addNetdEventCallback( |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 1811 | INetdEventCallback.CALLBACK_CALLER_CONNECTIVITY_SERVICE, |
| 1812 | mNetdEventCallback); |
| 1813 | } catch (Exception e) { |
| 1814 | loge("Error registering netd callback: " + e); |
| 1815 | } |
| 1816 | } |
| 1817 | |
Jeff Sharkey | 75d3189 | 2018-01-18 22:01:59 +0900 | [diff] [blame] | 1818 | private final INetworkPolicyListener mPolicyListener = new NetworkPolicyManager.Listener() { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1819 | @Override |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1820 | public void onUidRulesChanged(int uid, int uidRules) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1821 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UID_RULES_CHANGED, uid, uidRules)); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1822 | } |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 1823 | @Override |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1824 | public void onRestrictBackgroundChanged(boolean restrictBackground) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1825 | // caller is NPMS, since we only register with them |
| 1826 | if (LOGD_BLOCKED_NETWORKINFO) { |
| 1827 | log("onRestrictBackgroundChanged(restrictBackground=" + restrictBackground + ")"); |
| 1828 | } |
| 1829 | mHandler.sendMessage(mHandler.obtainMessage( |
| 1830 | EVENT_DATA_SAVER_CHANGED, restrictBackground ? 1 : 0, 0)); |
| 1831 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 1832 | // TODO: relocate this specific callback in Tethering. |
Felipe Leme | 70c8b9b | 2016-04-25 14:41:31 -0700 | [diff] [blame] | 1833 | if (restrictBackground) { |
| 1834 | log("onRestrictBackgroundChanged(true): disabling tethering"); |
| 1835 | mTethering.untetherAll(); |
| 1836 | } |
Jeff Sharkey | 1f8ea2d | 2012-02-07 12:05:43 -0800 | [diff] [blame] | 1837 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1838 | }; |
| 1839 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1840 | void handleUidRulesChanged(int uid, int newRules) { |
| 1841 | // skip update when we've already applied rules |
| 1842 | final int oldRules = mUidRules.get(uid, RULE_NONE); |
| 1843 | if (oldRules == newRules) return; |
| 1844 | |
| 1845 | maybeNotifyNetworkBlockedForNewUidRules(uid, newRules); |
| 1846 | |
| 1847 | if (newRules == RULE_NONE) { |
| 1848 | mUidRules.delete(uid); |
| 1849 | } else { |
| 1850 | mUidRules.put(uid, newRules); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | void handleRestrictBackgroundChanged(boolean restrictBackground) { |
| 1855 | if (mRestrictBackground == restrictBackground) return; |
| 1856 | |
| 1857 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 1858 | final boolean curMetered = nai.networkCapabilities.isMetered(); |
| 1859 | maybeNotifyNetworkBlocked(nai, curMetered, curMetered, mRestrictBackground, |
| 1860 | restrictBackground); |
| 1861 | } |
| 1862 | |
| 1863 | mRestrictBackground = restrictBackground; |
| 1864 | } |
| 1865 | |
| 1866 | private boolean isUidNetworkingWithVpnBlocked(int uid, int uidRules, boolean isNetworkMetered, |
| 1867 | boolean isBackgroundRestricted) { |
| 1868 | synchronized (mVpns) { |
| 1869 | final Vpn vpn = mVpns.get(UserHandle.getUserId(uid)); |
| 1870 | // Because the return value of this function depends on the list of UIDs the |
| 1871 | // always-on VPN blocks when in lockdown mode, when the always-on VPN changes that |
| 1872 | // list all state depending on the return value of this function has to be recomputed. |
| 1873 | // TODO: add a trigger when the always-on VPN sets its blocked UIDs to reevaluate and |
| 1874 | // send the necessary onBlockedStatusChanged callbacks. |
junyulai | 8ed8915 | 2018-10-25 10:56:17 +0800 | [diff] [blame] | 1875 | if (vpn != null && vpn.getLockdown() && vpn.isBlockingUid(uid)) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 1876 | return true; |
| 1877 | } |
| 1878 | } |
| 1879 | |
| 1880 | return mPolicyManagerInternal.isUidNetworkingBlocked(uid, uidRules, |
| 1881 | isNetworkMetered, isBackgroundRestricted); |
| 1882 | } |
| 1883 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 1884 | /** |
| 1885 | * Require that the caller is either in the same user or has appropriate permission to interact |
| 1886 | * across users. |
| 1887 | * |
| 1888 | * @param userId Target user for whatever operation the current IPC is supposed to perform. |
| 1889 | */ |
| 1890 | private void enforceCrossUserPermission(int userId) { |
| 1891 | if (userId == UserHandle.getCallingUserId()) { |
| 1892 | // Not a cross-user call. |
| 1893 | return; |
| 1894 | } |
| 1895 | mContext.enforceCallingOrSelfPermission( |
| 1896 | android.Manifest.permission.INTERACT_ACROSS_USERS_FULL, |
| 1897 | "ConnectivityService"); |
| 1898 | } |
| 1899 | |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1900 | private boolean checkAnyPermissionOf(String... permissions) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1901 | for (String permission : permissions) { |
| 1902 | if (mContext.checkCallingOrSelfPermission(permission) == PERMISSION_GRANTED) { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1903 | return true; |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1904 | } |
| 1905 | } |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1906 | return false; |
| 1907 | } |
| 1908 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 1909 | private boolean checkAnyPermissionOf(int pid, int uid, String... permissions) { |
| 1910 | for (String permission : permissions) { |
| 1911 | if (mContext.checkPermission(permission, pid, uid) == PERMISSION_GRANTED) { |
| 1912 | return true; |
| 1913 | } |
| 1914 | } |
| 1915 | return false; |
| 1916 | } |
| 1917 | |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1918 | private void enforceAnyPermissionOf(String... permissions) { |
| 1919 | if (!checkAnyPermissionOf(permissions)) { |
| 1920 | throw new SecurityException("Requires one of the following permissions: " |
| 1921 | + String.join(", ", permissions) + "."); |
| 1922 | } |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1923 | } |
| 1924 | |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 1925 | private void enforceInternetPermission() { |
| 1926 | mContext.enforceCallingOrSelfPermission( |
| 1927 | android.Manifest.permission.INTERNET, |
| 1928 | "ConnectivityService"); |
| 1929 | } |
| 1930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1931 | private void enforceAccessPermission() { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 1932 | mContext.enforceCallingOrSelfPermission( |
| 1933 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1934 | "ConnectivityService"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1935 | } |
| 1936 | |
| 1937 | private void enforceChangePermission() { |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1938 | ConnectivityManager.enforceChangePermission(mContext); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1939 | } |
| 1940 | |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1941 | private void enforceSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1942 | enforceAnyPermissionOf( |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1943 | android.Manifest.permission.NETWORK_SETTINGS, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1944 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1945 | } |
| 1946 | |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1947 | private boolean checkSettingsPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1948 | return checkAnyPermissionOf( |
| 1949 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1950 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1951 | } |
| 1952 | |
| 1953 | private boolean checkSettingsPermission(int pid, int uid) { |
| 1954 | return PERMISSION_GRANTED == mContext.checkPermission( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1955 | android.Manifest.permission.NETWORK_SETTINGS, pid, uid) |
| 1956 | || PERMISSION_GRANTED == mContext.checkPermission( |
| 1957 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, pid, uid); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 1958 | } |
| 1959 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1960 | private void enforceTetherAccessPermission() { |
| 1961 | mContext.enforceCallingOrSelfPermission( |
| 1962 | android.Manifest.permission.ACCESS_NETWORK_STATE, |
| 1963 | "ConnectivityService"); |
| 1964 | } |
| 1965 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1966 | private void enforceConnectivityInternalPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1967 | enforceAnyPermissionOf( |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1968 | android.Manifest.permission.CONNECTIVITY_INTERNAL, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1969 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1970 | } |
| 1971 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1972 | private void enforceControlAlwaysOnVpnPermission() { |
| 1973 | mContext.enforceCallingOrSelfPermission( |
| 1974 | android.Manifest.permission.CONTROL_ALWAYS_ON_VPN, |
| 1975 | "ConnectivityService"); |
| 1976 | } |
| 1977 | |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 1978 | private void enforceNetworkStackSettingsOrSetup() { |
| 1979 | enforceAnyPermissionOf( |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1980 | android.Manifest.permission.NETWORK_SETTINGS, |
| 1981 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1982 | android.Manifest.permission.NETWORK_STACK, |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1983 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1984 | } |
| 1985 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1986 | private boolean checkNetworkStackPermission() { |
Remi NGUYEN VAN | dd29f20 | 2019-01-31 16:42:12 +0900 | [diff] [blame] | 1987 | return checkAnyPermissionOf( |
| 1988 | android.Manifest.permission.NETWORK_STACK, |
| 1989 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 1990 | } |
| 1991 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 1992 | private boolean checkNetworkSignalStrengthWakeupPermission(int pid, int uid) { |
| 1993 | return checkAnyPermissionOf(pid, uid, |
| 1994 | android.Manifest.permission.NETWORK_SIGNAL_STRENGTH_WAKEUP, |
| 1995 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
| 1996 | } |
| 1997 | |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 1998 | private void enforceConnectivityRestrictedNetworksPermission() { |
| 1999 | try { |
| 2000 | mContext.enforceCallingOrSelfPermission( |
| 2001 | android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS, |
| 2002 | "ConnectivityService"); |
| 2003 | return; |
| 2004 | } catch (SecurityException e) { /* fallback to ConnectivityInternalPermission */ } |
| 2005 | enforceConnectivityInternalPermission(); |
| 2006 | } |
| 2007 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2008 | private void enforceKeepalivePermission() { |
Lorenzo Colitti | 7914ce5 | 2015-09-08 13:21:48 +0900 | [diff] [blame] | 2009 | mContext.enforceCallingOrSelfPermission(KeepaliveTracker.PERMISSION, "ConnectivityService"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2010 | } |
| 2011 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 2012 | // Public because it's used by mLockdownTracker. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2013 | public void sendConnectedBroadcast(NetworkInfo info) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2014 | enforceConnectivityInternalPermission(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2015 | sendGeneralBroadcast(info, CONNECTIVITY_ACTION); |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2016 | } |
| 2017 | |
| 2018 | private void sendInetConditionBroadcast(NetworkInfo info) { |
| 2019 | sendGeneralBroadcast(info, ConnectivityManager.INET_CONDITION_ACTION); |
| 2020 | } |
| 2021 | |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2022 | private Intent makeGeneralIntent(NetworkInfo info, String bcastType) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 2023 | synchronized (mVpns) { |
| 2024 | if (mLockdownTracker != null) { |
| 2025 | info = new NetworkInfo(info); |
| 2026 | mLockdownTracker.augmentNetworkInfo(info); |
| 2027 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2028 | } |
| 2029 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 2030 | Intent intent = new Intent(bcastType); |
Irfan Sheriff | 9538bdd | 2012-09-20 09:32:41 -0700 | [diff] [blame] | 2031 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, new NetworkInfo(info)); |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 2032 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2033 | if (info.isFailover()) { |
| 2034 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 2035 | info.setFailover(false); |
| 2036 | } |
| 2037 | if (info.getReason() != null) { |
| 2038 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 2039 | } |
| 2040 | if (info.getExtraInfo() != null) { |
Robert Greenwalt | 86e9e55 | 2009-07-16 17:21:39 -0700 | [diff] [blame] | 2041 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, |
| 2042 | info.getExtraInfo()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2043 | } |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2044 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, mDefaultInetConditionPublished); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2045 | return intent; |
| 2046 | } |
| 2047 | |
| 2048 | private void sendGeneralBroadcast(NetworkInfo info, String bcastType) { |
| 2049 | sendStickyBroadcast(makeGeneralIntent(info, bcastType)); |
| 2050 | } |
| 2051 | |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2052 | private void sendDataActivityBroadcast(int deviceType, boolean active, long tsNanos) { |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2053 | Intent intent = new Intent(ConnectivityManager.ACTION_DATA_ACTIVITY_CHANGE); |
| 2054 | intent.putExtra(ConnectivityManager.EXTRA_DEVICE_TYPE, deviceType); |
| 2055 | intent.putExtra(ConnectivityManager.EXTRA_IS_ACTIVE, active); |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 2056 | intent.putExtra(ConnectivityManager.EXTRA_REALTIME_NS, tsNanos); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2057 | final long ident = Binder.clearCallingIdentity(); |
| 2058 | try { |
| 2059 | mContext.sendOrderedBroadcastAsUser(intent, UserHandle.ALL, |
| 2060 | RECEIVE_DATA_ACTIVITY_CHANGE, null, null, 0, null, null); |
| 2061 | } finally { |
| 2062 | Binder.restoreCallingIdentity(ident); |
| 2063 | } |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2066 | private void sendStickyBroadcast(Intent intent) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2067 | synchronized (this) { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 2068 | if (!mSystemReady |
| 2069 | && intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2070 | mInitialBroadcast = new Intent(intent); |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2071 | } |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2072 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2073 | if (VDBG) { |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 2074 | log("sendStickyBroadcast: action=" + intent.getAction()); |
Wink Saville | 628b085 | 2011-08-04 15:01:58 -0700 | [diff] [blame] | 2075 | } |
| 2076 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2077 | Bundle options = null; |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2078 | final long ident = Binder.clearCallingIdentity(); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2079 | if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2080 | final NetworkInfo ni = intent.getParcelableExtra( |
| 2081 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 2082 | if (ni.getType() == ConnectivityManager.TYPE_MOBILE_SUPL) { |
| 2083 | intent.setAction(ConnectivityManager.CONNECTIVITY_ACTION_SUPL); |
| 2084 | intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2085 | } else { |
| 2086 | BroadcastOptions opts = BroadcastOptions.makeBasic(); |
| 2087 | opts.setMaxManifestReceiverApiLevel(Build.VERSION_CODES.M); |
| 2088 | options = opts.toBundle(); |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 2089 | } |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2090 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 2091 | try { |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2092 | bs.noteConnectivityChanged(intent.getIntExtra( |
| 2093 | ConnectivityManager.EXTRA_NETWORK_TYPE, ConnectivityManager.TYPE_NONE), |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 2094 | ni.getState().toString()); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2095 | } catch (RemoteException e) { |
| 2096 | } |
Chad Brubaker | ac92501 | 2018-03-08 10:37:09 -0800 | [diff] [blame] | 2097 | intent.addFlags(Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS); |
Dianne Hackborn | 1e01d16 | 2014-12-04 17:46:42 -0800 | [diff] [blame] | 2098 | } |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2099 | try { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 2100 | mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL, options); |
Dianne Hackborn | fd8bf5c | 2012-09-04 18:48:37 -0700 | [diff] [blame] | 2101 | } finally { |
| 2102 | Binder.restoreCallingIdentity(ident); |
| 2103 | } |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | void systemReady() { |
Chalard Jean | 655ad15 | 2018-06-08 12:47:42 +0900 | [diff] [blame] | 2108 | mProxyTracker.loadGlobalProxy(); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2109 | registerNetdEventCallback(); |
markchien | a6c7287 | 2018-11-13 18:34:56 +0900 | [diff] [blame] | 2110 | mTethering.systemReady(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2111 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 2112 | synchronized (this) { |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2113 | mSystemReady = true; |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2114 | if (mInitialBroadcast != null) { |
Dianne Hackborn | 5ac72a2 | 2012-08-29 18:32:08 -0700 | [diff] [blame] | 2115 | mContext.sendStickyBroadcastAsUser(mInitialBroadcast, UserHandle.ALL); |
Dianne Hackborn | 1c633fc | 2009-12-08 19:45:14 -0800 | [diff] [blame] | 2116 | mInitialBroadcast = null; |
Mike Lockwood | 0f79b54 | 2009-08-14 14:18:49 -0400 | [diff] [blame] | 2117 | } |
| 2118 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2119 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 2120 | // Try bringing up tracker, but KeyStore won't be ready yet for secondary users so wait |
| 2121 | // for user to unlock device too. |
| 2122 | updateLockdownVpn(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2123 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 2124 | // Create network requests for always-on networks. |
| 2125 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_CONFIGURE_ALWAYS_ON_NETWORKS)); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 2126 | |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 2127 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SYSTEM_READY)); |
Sreeram Ramachandran | e4a05af | 2014-09-24 09:16:19 -0700 | [diff] [blame] | 2128 | |
| 2129 | mPermissionMonitor.startMonitoring(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | } |
| 2131 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2132 | /** |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2133 | * Setup data activity tracking for the given network. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2134 | * |
| 2135 | * Every {@code setupDataActivityTracking} should be paired with a |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2136 | * {@link #removeDataActivityTracking} for cleanup. |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2137 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2138 | private void setupDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2139 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2140 | |
| 2141 | final int timeout; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2142 | int type = ConnectivityManager.TYPE_NONE; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2143 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2144 | if (networkAgent.networkCapabilities.hasTransport( |
| 2145 | NetworkCapabilities.TRANSPORT_CELLULAR)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2146 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2147 | Settings.Global.DATA_ACTIVITY_TIMEOUT_MOBILE, |
Adam Lesinski | ed6160d | 2015-08-18 11:47:07 -0700 | [diff] [blame] | 2148 | 10); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2149 | type = ConnectivityManager.TYPE_MOBILE; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2150 | } else if (networkAgent.networkCapabilities.hasTransport( |
| 2151 | NetworkCapabilities.TRANSPORT_WIFI)) { |
Jeff Brown | bf6f6f9 | 2012-09-25 15:03:20 -0700 | [diff] [blame] | 2152 | timeout = Settings.Global.getInt(mContext.getContentResolver(), |
| 2153 | Settings.Global.DATA_ACTIVITY_TIMEOUT_WIFI, |
Adam Lesinski | 06f46cb | 2015-06-23 13:42:53 -0700 | [diff] [blame] | 2154 | 15); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2155 | type = ConnectivityManager.TYPE_WIFI; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2156 | } else { |
| 2157 | // do not track any other networks |
| 2158 | timeout = 0; |
| 2159 | } |
| 2160 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2161 | if (timeout > 0 && iface != null && type != ConnectivityManager.TYPE_NONE) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2162 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2163 | mNMS.addIdleTimer(iface, timeout, type); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2164 | } catch (Exception e) { |
| 2165 | // You shall not crash! |
| 2166 | loge("Exception in setupDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2167 | } |
| 2168 | } |
| 2169 | } |
| 2170 | |
| 2171 | /** |
| 2172 | * Remove data activity tracking when network disconnects. |
| 2173 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2174 | private void removeDataActivityTracking(NetworkAgentInfo networkAgent) { |
| 2175 | final String iface = networkAgent.linkProperties.getInterfaceName(); |
| 2176 | final NetworkCapabilities caps = networkAgent.networkCapabilities; |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2177 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2178 | if (iface != null && (caps.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) || |
| 2179 | caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI))) { |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2180 | try { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2181 | // the call fails silently if no idle timer setup for this interface |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2182 | mNMS.removeIdleTimer(iface); |
Robert Greenwalt | d9cb2f3 | 2014-03-19 14:26:28 -0700 | [diff] [blame] | 2183 | } catch (Exception e) { |
| 2184 | loge("Exception in removeDataActivityTracking " + e); |
Haoyu Bai | 0412423 | 2012-06-28 15:26:19 -0700 | [diff] [blame] | 2185 | } |
| 2186 | } |
| 2187 | } |
| 2188 | |
| 2189 | /** |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 2190 | * Update data activity tracking when network state is updated. |
| 2191 | */ |
| 2192 | private void updateDataActivityTracking(NetworkAgentInfo newNetwork, |
| 2193 | NetworkAgentInfo oldNetwork) { |
| 2194 | if (newNetwork != null) { |
| 2195 | setupDataActivityTracking(newNetwork); |
| 2196 | } |
| 2197 | if (oldNetwork != null) { |
| 2198 | removeDataActivityTracking(oldNetwork); |
| 2199 | } |
| 2200 | } |
| 2201 | /** |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2202 | * Reads the network specific MTU size from resources. |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2203 | * and set it on it's iface. |
| 2204 | */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2205 | private void updateMtu(LinkProperties newLp, LinkProperties oldLp) { |
| 2206 | final String iface = newLp.getInterfaceName(); |
| 2207 | final int mtu = newLp.getMtu(); |
Pierre Imai | 54f0d9e | 2016-02-08 16:01:40 +0900 | [diff] [blame] | 2208 | if (oldLp == null && mtu == 0) { |
| 2209 | // Silently ignore unset MTU value. |
| 2210 | return; |
| 2211 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2212 | if (oldLp != null && newLp.isIdenticalMtu(oldLp)) { |
| 2213 | if (VDBG) log("identical MTU - not setting"); |
| 2214 | return; |
| 2215 | } |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 2216 | if (!LinkProperties.isValidMtu(mtu, newLp.hasGlobalIpv6Address())) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 2217 | if (mtu != 0) loge("Unexpected mtu value: " + mtu + ", " + iface); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2218 | return; |
| 2219 | } |
sy.yun | 9d9b74a | 2013-09-02 05:24:09 +0900 | [diff] [blame] | 2220 | |
w19976 | 15afd81 | 2014-08-05 15:18:11 -0700 | [diff] [blame] | 2221 | // Cannot set MTU without interface name |
| 2222 | if (TextUtils.isEmpty(iface)) { |
| 2223 | loge("Setting MTU size with null iface."); |
| 2224 | return; |
| 2225 | } |
| 2226 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2227 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2228 | if (VDBG || DDBG) log("Setting MTU size: " + iface + ", " + mtu); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 2229 | mNMS.setMtu(iface, mtu); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2230 | } catch (Exception e) { |
| 2231 | Slog.e(TAG, "exception in setMtu()" + e); |
| 2232 | } |
| 2233 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2234 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2235 | @VisibleForTesting |
| 2236 | 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] | 2237 | private static final String DEFAULT_TCP_RWND_KEY = "net.tcp.default_init_rwnd"; |
| 2238 | |
| 2239 | // Overridden for testing purposes to avoid writing to SystemProperties. |
Paul Jensen | 67b0b07 | 2015-06-10 11:22:17 -0400 | [diff] [blame] | 2240 | @VisibleForTesting |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2241 | protected MockableSystemProperties getSystemProperties() { |
| 2242 | return new MockableSystemProperties(); |
Paul Jensen | d7b6ca9 | 2015-05-13 14:05:12 -0400 | [diff] [blame] | 2243 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2244 | |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 2245 | private void updateTcpBufferSizes(String tcpBufferSizes) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2246 | String[] values = null; |
| 2247 | if (tcpBufferSizes != null) { |
| 2248 | values = tcpBufferSizes.split(","); |
| 2249 | } |
| 2250 | |
| 2251 | if (values == null || values.length != 6) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 2252 | if (DBG) log("Invalid tcpBufferSizes string: " + tcpBufferSizes +", using defaults"); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2253 | tcpBufferSizes = DEFAULT_TCP_BUFFER_SIZES; |
| 2254 | values = tcpBufferSizes.split(","); |
| 2255 | } |
| 2256 | |
| 2257 | if (tcpBufferSizes.equals(mCurrentTcpBufferSizes)) return; |
| 2258 | |
| 2259 | try { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 2260 | 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] | 2261 | |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2262 | String rmemValues = String.join(" ", values[0], values[1], values[2]); |
| 2263 | String wmemValues = String.join(" ", values[3], values[4], values[5]); |
| 2264 | mNetd.setTcpRWmemorySize(rmemValues, wmemValues); |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2265 | mCurrentTcpBufferSizes = tcpBufferSizes; |
Chenbo Feng | 7f14dbc | 2018-11-08 17:36:21 -0800 | [diff] [blame] | 2266 | } catch (RemoteException | ServiceSpecificException e) { |
Robert Greenwalt | 3f05bf4 | 2014-08-06 12:00:25 -0700 | [diff] [blame] | 2267 | loge("Can't set TCP buffer sizes:" + e); |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2268 | } |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2269 | |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2270 | Integer rwndValue = Settings.Global.getInt(mContext.getContentResolver(), |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2271 | Settings.Global.TCP_DEFAULT_INIT_RWND, |
| 2272 | mSystemProperties.getInt("net.tcp.default_init_rwnd", 0)); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2273 | final String sysctlKey = "sys.sysctl.tcp_def_init_rwnd"; |
| 2274 | if (rwndValue != 0) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2275 | mSystemProperties.set(sysctlKey, rwndValue.toString()); |
JP Abgrall | 32d1ac4d | 2014-02-21 12:05:20 -0800 | [diff] [blame] | 2276 | } |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2279 | @Override |
| 2280 | public int getRestoreDefaultNetworkDelay(int networkType) { |
Lorenzo Colitti | 42cdf57 | 2017-03-21 18:54:11 +0900 | [diff] [blame] | 2281 | String restoreDefaultNetworkDelayStr = mSystemProperties.get( |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2282 | NETWORK_RESTORE_DELAY_PROP_NAME); |
| 2283 | if(restoreDefaultNetworkDelayStr != null && |
| 2284 | restoreDefaultNetworkDelayStr.length() != 0) { |
| 2285 | try { |
Narayan Kamath | a09b4d2 | 2016-04-15 18:32:45 +0100 | [diff] [blame] | 2286 | return Integer.parseInt(restoreDefaultNetworkDelayStr); |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2287 | } catch (NumberFormatException e) { |
| 2288 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2289 | } |
Robert Greenwalt | f2102f7 | 2011-05-03 19:02:44 -0700 | [diff] [blame] | 2290 | // if the system property isn't set, use the value for the apn type |
| 2291 | int ret = RESTORE_DEFAULT_NETWORK_DELAY; |
| 2292 | |
| 2293 | if ((networkType <= ConnectivityManager.MAX_NETWORK_TYPE) && |
| 2294 | (mNetConfigs[networkType] != null)) { |
| 2295 | ret = mNetConfigs[networkType].restoreTime; |
| 2296 | } |
| 2297 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2298 | } |
| 2299 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2300 | private void dumpNetworkDiagnostics(IndentingPrintWriter pw) { |
| 2301 | final List<NetworkDiagnostics> netDiags = new ArrayList<NetworkDiagnostics>(); |
| 2302 | final long DIAG_TIME_MS = 5000; |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2303 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2304 | // Start gathering diagnostic information. |
| 2305 | netDiags.add(new NetworkDiagnostics( |
| 2306 | nai.network, |
| 2307 | new LinkProperties(nai.linkProperties), // Must be a copy. |
| 2308 | DIAG_TIME_MS)); |
| 2309 | } |
| 2310 | |
| 2311 | for (NetworkDiagnostics netDiag : netDiags) { |
| 2312 | pw.println(); |
| 2313 | netDiag.waitForMeasurements(); |
| 2314 | netDiag.dump(pw); |
| 2315 | } |
| 2316 | } |
| 2317 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | @Override |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2319 | protected void dump(FileDescriptor fd, PrintWriter writer, String[] args) { |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2320 | PriorityDump.dump(mPriorityDumper, fd, writer, args); |
| 2321 | } |
| 2322 | |
| 2323 | private void doDump(FileDescriptor fd, PrintWriter writer, String[] args, boolean asProto) { |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2324 | final IndentingPrintWriter pw = new IndentingPrintWriter(writer, " "); |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 2325 | if (!DumpUtils.checkDumpPermission(mContext, TAG, pw)) return; |
Vishnu Nair | 5c01090 | 2017-10-26 10:08:50 -0700 | [diff] [blame] | 2326 | if (asProto) return; |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2327 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2328 | if (ArrayUtils.contains(args, DIAG_ARG)) { |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2329 | dumpNetworkDiagnostics(pw); |
| 2330 | return; |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2331 | } else if (ArrayUtils.contains(args, TETHERING_ARG)) { |
Erik Kline | ee363c4 | 2017-05-29 09:11:03 +0900 | [diff] [blame] | 2332 | mTethering.dump(fd, pw, args); |
| 2333 | return; |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2334 | } else if (ArrayUtils.contains(args, NETWORK_ARG)) { |
| 2335 | dumpNetworks(pw); |
| 2336 | return; |
| 2337 | } else if (ArrayUtils.contains(args, REQUEST_ARG)) { |
| 2338 | dumpNetworkRequests(pw); |
| 2339 | return; |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2340 | } |
Erik Kline | 379747a | 2015-06-05 17:47:34 +0900 | [diff] [blame] | 2341 | |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2342 | pw.print("NetworkFactories for:"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2343 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2344 | pw.print(" " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2345 | } |
Lorenzo Colitti | e380546 | 2015-06-03 11:18:24 +0900 | [diff] [blame] | 2346 | pw.println(); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2347 | pw.println(); |
| 2348 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 2349 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2350 | pw.print("Active default network: "); |
| 2351 | if (defaultNai == null) { |
| 2352 | pw.println("none"); |
| 2353 | } else { |
| 2354 | pw.println(defaultNai.network.netId); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2355 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2356 | pw.println(); |
| 2357 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2358 | pw.println("Current Networks:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2359 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2360 | dumpNetworks(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2361 | pw.decreaseIndent(); |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2362 | pw.println(); |
Robert Greenwalt | b928535 | 2009-12-21 18:24:07 -0800 | [diff] [blame] | 2363 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 2364 | pw.print("Restrict background: "); |
| 2365 | pw.println(mRestrictBackground); |
| 2366 | pw.println(); |
| 2367 | |
| 2368 | pw.println("Status for known UIDs:"); |
| 2369 | pw.increaseIndent(); |
| 2370 | final int size = mUidRules.size(); |
| 2371 | for (int i = 0; i < size; i++) { |
| 2372 | // Don't crash if the array is modified while dumping in bugreports. |
| 2373 | try { |
| 2374 | final int uid = mUidRules.keyAt(i); |
| 2375 | final int uidRules = mUidRules.get(uid, RULE_NONE); |
| 2376 | pw.println("UID=" + uid + " rules=" + uidRulesToString(uidRules)); |
| 2377 | } catch (ArrayIndexOutOfBoundsException e) { |
| 2378 | pw.println(" ArrayIndexOutOfBoundsException"); |
| 2379 | } catch (ConcurrentModificationException e) { |
| 2380 | pw.println(" ConcurrentModificationException"); |
| 2381 | } |
| 2382 | } |
| 2383 | pw.println(); |
| 2384 | pw.decreaseIndent(); |
| 2385 | |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 2386 | pw.println("Network Requests:"); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2387 | pw.increaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2388 | dumpNetworkRequests(pw); |
Jeff Sharkey | e6e6197 | 2012-09-14 13:47:51 -0700 | [diff] [blame] | 2389 | pw.decreaseIndent(); |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2390 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2391 | |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2392 | mLegacyTypeTracker.dump(pw); |
Robert Greenwalt | d49ac33 | 2014-07-30 16:31:24 -0700 | [diff] [blame] | 2393 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2394 | pw.println(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2395 | mTethering.dump(fd, pw, args); |
Robert Greenwalt | 4e8dfef | 2010-09-20 14:35:25 -0700 | [diff] [blame] | 2396 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2397 | pw.println(); |
| 2398 | mKeepaliveTracker.dump(pw); |
| 2399 | |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2400 | pw.println(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 2401 | dumpAvoidBadWifiSettings(pw); |
Lorenzo Colitti | 5ff640d | 2016-03-03 17:53:46 +0900 | [diff] [blame] | 2402 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 2403 | pw.println(); |
| 2404 | mMultipathPolicyTracker.dump(pw); |
| 2405 | |
Erik Kline | 3f8306b | 2018-05-01 16:51:44 +0900 | [diff] [blame] | 2406 | if (ArrayUtils.contains(args, SHORT_ARG) == false) { |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2407 | pw.println(); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 2408 | pw.println("mNetworkRequestInfoLogs (most recent first):"); |
| 2409 | pw.increaseIndent(); |
| 2410 | mNetworkRequestInfoLogs.reverseDump(fd, pw, args); |
| 2411 | pw.decreaseIndent(); |
Hugo Benichi | c2ae287 | 2016-07-11 11:05:12 +0900 | [diff] [blame] | 2412 | |
| 2413 | pw.println(); |
| 2414 | pw.println("mNetworkInfoBlockingLogs (most recent first):"); |
| 2415 | pw.increaseIndent(); |
| 2416 | mNetworkInfoBlockingLogs.reverseDump(fd, pw, args); |
| 2417 | pw.decreaseIndent(); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2418 | |
| 2419 | pw.println(); |
| 2420 | pw.println("NetTransition WakeLock activity (most recent first):"); |
| 2421 | pw.increaseIndent(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 2422 | pw.println("total acquisitions: " + mTotalWakelockAcquisitions); |
| 2423 | pw.println("total releases: " + mTotalWakelockReleases); |
| 2424 | pw.println("cumulative duration: " + (mTotalWakelockDurationMs / 1000) + "s"); |
| 2425 | pw.println("longest duration: " + (mMaxWakelockDurationMs / 1000) + "s"); |
| 2426 | if (mTotalWakelockAcquisitions > mTotalWakelockReleases) { |
| 2427 | long duration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 2428 | pw.println("currently holding WakeLock for: " + (duration / 1000) + "s"); |
| 2429 | } |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2430 | mWakelockLogs.reverseDump(fd, pw, args); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 2431 | |
| 2432 | pw.println(); |
| 2433 | pw.println("bandwidth update requests (by uid):"); |
| 2434 | pw.increaseIndent(); |
| 2435 | synchronized (mBandwidthRequests) { |
| 2436 | for (int i = 0; i < mBandwidthRequests.size(); i++) { |
| 2437 | pw.println("[" + mBandwidthRequests.keyAt(i) |
| 2438 | + "]: " + mBandwidthRequests.valueAt(i)); |
| 2439 | } |
| 2440 | } |
| 2441 | pw.decreaseIndent(); |
| 2442 | |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2443 | pw.decreaseIndent(); |
Robert Greenwalt | 22b4c6a | 2015-06-23 15:03:33 -0700 | [diff] [blame] | 2444 | } |
Remi NGUYEN VAN | a25379f | 2019-02-18 11:20:28 +0900 | [diff] [blame] | 2445 | |
| 2446 | pw.println(); |
| 2447 | pw.println("NetworkStackClient logs:"); |
| 2448 | pw.increaseIndent(); |
| 2449 | NetworkStackClient.getInstance().dump(pw); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
Hugo Benichi | 1468381 | 2018-09-03 08:32:56 +0900 | [diff] [blame] | 2452 | private void dumpNetworks(IndentingPrintWriter pw) { |
| 2453 | for (NetworkAgentInfo nai : networksSortedById()) { |
| 2454 | pw.println(nai.toString()); |
| 2455 | pw.increaseIndent(); |
| 2456 | pw.println(String.format( |
| 2457 | "Requests: REQUEST:%d LISTEN:%d BACKGROUND_REQUEST:%d total:%d", |
| 2458 | nai.numForegroundNetworkRequests(), |
| 2459 | nai.numNetworkRequests() - nai.numRequestNetworkRequests(), |
| 2460 | nai.numBackgroundNetworkRequests(), |
| 2461 | nai.numNetworkRequests())); |
| 2462 | pw.increaseIndent(); |
| 2463 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 2464 | pw.println(nai.requestAt(i).toString()); |
| 2465 | } |
| 2466 | pw.decreaseIndent(); |
| 2467 | pw.println("Lingered:"); |
| 2468 | pw.increaseIndent(); |
| 2469 | nai.dumpLingerTimers(pw); |
| 2470 | pw.decreaseIndent(); |
| 2471 | pw.decreaseIndent(); |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | private void dumpNetworkRequests(IndentingPrintWriter pw) { |
| 2476 | for (NetworkRequestInfo nri : requestsSortedById()) { |
| 2477 | pw.println(nri.toString()); |
| 2478 | } |
| 2479 | } |
| 2480 | |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 2481 | /** |
| 2482 | * Return an array of all current NetworkAgentInfos sorted by network id. |
| 2483 | */ |
| 2484 | private NetworkAgentInfo[] networksSortedById() { |
| 2485 | NetworkAgentInfo[] networks = new NetworkAgentInfo[0]; |
| 2486 | networks = mNetworkAgentInfos.values().toArray(networks); |
| 2487 | Arrays.sort(networks, Comparator.comparingInt(nai -> nai.network.netId)); |
| 2488 | return networks; |
| 2489 | } |
| 2490 | |
| 2491 | /** |
| 2492 | * Return an array of all current NetworkRequest sorted by request id. |
| 2493 | */ |
| 2494 | private NetworkRequestInfo[] requestsSortedById() { |
| 2495 | NetworkRequestInfo[] requests = new NetworkRequestInfo[0]; |
| 2496 | requests = mNetworkRequests.values().toArray(requests); |
| 2497 | Arrays.sort(requests, Comparator.comparingInt(nri -> nri.request.requestId)); |
| 2498 | return requests; |
| 2499 | } |
| 2500 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2501 | private boolean isLiveNetworkAgent(NetworkAgentInfo nai, int what) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2502 | if (nai.network == null) return false; |
Sreeram Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 2503 | final NetworkAgentInfo officialNai = getNetworkAgentInfoForNetwork(nai.network); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2504 | if (officialNai != null && officialNai.equals(nai)) return true; |
| 2505 | if (officialNai != null || VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2506 | loge(eventName(what) + " - isLiveNetworkAgent found mismatched netId: " + officialNai + |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2507 | " - " + nai); |
| 2508 | } |
| 2509 | return false; |
| 2510 | } |
| 2511 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 2512 | // must be stateless - things change under us. |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2513 | private class NetworkStateTrackerHandler extends Handler { |
| 2514 | public NetworkStateTrackerHandler(Looper looper) { |
Wink Saville | bb08caf | 2010-09-02 19:23:52 -0700 | [diff] [blame] | 2515 | super(looper); |
| 2516 | } |
| 2517 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2518 | private boolean maybeHandleAsyncChannelMessage(Message msg) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2519 | switch (msg.what) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2520 | default: |
| 2521 | return false; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2522 | case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2523 | handleAsyncChannelHalfConnect(msg); |
| 2524 | break; |
| 2525 | } |
| 2526 | case AsyncChannel.CMD_CHANNEL_DISCONNECT: { |
| 2527 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2528 | if (nai != null) nai.asyncChannel.disconnect(); |
| 2529 | break; |
| 2530 | } |
| 2531 | case AsyncChannel.CMD_CHANNEL_DISCONNECTED: { |
| 2532 | handleAsyncChannelDisconnected(msg); |
| 2533 | break; |
| 2534 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2535 | } |
| 2536 | return true; |
| 2537 | } |
| 2538 | |
| 2539 | private void maybeHandleNetworkAgentMessage(Message msg) { |
| 2540 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2541 | if (nai == null) { |
| 2542 | if (VDBG) { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 2543 | log(String.format("%s from unknown NetworkAgent", eventName(msg.what))); |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2544 | } |
| 2545 | return; |
| 2546 | } |
| 2547 | |
| 2548 | switch (msg.what) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2549 | case NetworkAgent.EVENT_NETWORK_CAPABILITIES_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2550 | final NetworkCapabilities networkCapabilities = (NetworkCapabilities) msg.obj; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2551 | if (networkCapabilities.hasConnectivityManagedCapability()) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2552 | Slog.wtf(TAG, "BUG: " + nai + " has CS-managed capability."); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2553 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2554 | updateCapabilities(nai.getCurrentScore(), nai, networkCapabilities); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2555 | break; |
| 2556 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2557 | case NetworkAgent.EVENT_NETWORK_PROPERTIES_CHANGED: { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 2558 | handleUpdateLinkProperties(nai, (LinkProperties) msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2559 | break; |
| 2560 | } |
| 2561 | case NetworkAgent.EVENT_NETWORK_INFO_CHANGED: { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2562 | NetworkInfo info = (NetworkInfo) msg.obj; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2563 | updateNetworkInfo(nai, info); |
| 2564 | break; |
| 2565 | } |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2566 | case NetworkAgent.EVENT_NETWORK_SCORE_CHANGED: { |
Roshan Pius | 64e99ef | 2018-08-02 10:25:02 -0700 | [diff] [blame] | 2567 | updateNetworkScore(nai, msg.arg1); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 2568 | break; |
| 2569 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2570 | case NetworkAgent.EVENT_SET_EXPLICITLY_SELECTED: { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 2571 | if (nai.everConnected && !nai.networkMisc.explicitlySelected) { |
| 2572 | loge("ERROR: already-connected network explicitly selected."); |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 2573 | } |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2574 | nai.networkMisc.explicitlySelected = true; |
Roshan Pius | eaf8dee | 2018-08-10 07:36:39 -0700 | [diff] [blame] | 2575 | nai.networkMisc.acceptUnvalidated = msg.arg1 == 1; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2576 | // Mark the network as temporarily accepting partial connectivity so that it |
| 2577 | // will be validated (and possibly become default) even if it only provides |
| 2578 | // partial internet access. Note that if user connects to partial connectivity |
| 2579 | // and choose "don't ask again", then wifi disconnected by some reasons(maybe |
| 2580 | // out of wifi coverage) and if the same wifi is available again, the device |
| 2581 | // will auto connect to this wifi even though the wifi has "no internet". |
| 2582 | // TODO: Evaluate using a separate setting in IpMemoryStore. |
| 2583 | nai.networkMisc.acceptPartialConnectivity = msg.arg1 == 1; |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 2584 | break; |
| 2585 | } |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 2586 | case NetworkAgent.EVENT_SOCKET_KEEPALIVE: { |
| 2587 | mKeepaliveTracker.handleEventSocketKeepalive(nai, msg); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2588 | break; |
| 2589 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2590 | } |
| 2591 | } |
| 2592 | |
| 2593 | private boolean maybeHandleNetworkMonitorMessage(Message msg) { |
| 2594 | switch (msg.what) { |
| 2595 | default: |
| 2596 | return false; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2597 | case EVENT_NETWORK_TESTED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2598 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2599 | if (nai == null) break; |
| 2600 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2601 | final boolean partialConnectivity = |
| 2602 | (msg.arg1 == NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY) |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2603 | || (nai.networkMisc.acceptPartialConnectivity |
| 2604 | && nai.partialConnectivity); |
| 2605 | // Once a network is determined to have partial connectivity, it cannot |
lucaslin | 975b7d3 | 2019-03-21 11:59:22 +0800 | [diff] [blame] | 2606 | // go back to full connectivity without a disconnect. This is because |
| 2607 | // NetworkMonitor can only communicate either PARTIAL_CONNECTIVITY or VALID, |
| 2608 | // but not both. |
| 2609 | // TODO: Provide multi-testResult to improve the communication between |
| 2610 | // ConnectivityService and NetworkMonitor, so that ConnectivityService could |
| 2611 | // know the real status of network. |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 2612 | final boolean partialConnectivityChanged = |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2613 | (partialConnectivity && !nai.partialConnectivity); |
| 2614 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2615 | final boolean valid = (msg.arg1 == NETWORK_TEST_RESULT_VALID); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2616 | final boolean wasValidated = nai.lastValidated; |
| 2617 | final boolean wasDefault = isDefaultNetwork(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2618 | if (nai.everCaptivePortalDetected && !nai.captivePortalLoginNotified |
| 2619 | && valid) { |
| 2620 | nai.captivePortalLoginNotified = true; |
| 2621 | showNetworkNotification(nai, NotificationType.LOGGED_IN); |
| 2622 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2623 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2624 | final String redirectUrl = (msg.obj instanceof String) ? (String) msg.obj : ""; |
| 2625 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2626 | if (DBG) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2627 | final String logMsg = !TextUtils.isEmpty(redirectUrl) |
| 2628 | ? " with redirect to " + redirectUrl |
| 2629 | : ""; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2630 | log(nai.name() + " validation " + (valid ? "passed" : "failed") + logMsg); |
| 2631 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2632 | if (valid != nai.lastValidated) { |
| 2633 | if (wasDefault) { |
| 2634 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkValidity( |
| 2635 | SystemClock.elapsedRealtime(), valid); |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 2636 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2637 | final int oldScore = nai.getCurrentScore(); |
| 2638 | nai.lastValidated = valid; |
| 2639 | nai.everValidated |= valid; |
| 2640 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
| 2641 | // If score has changed, rebroadcast to NetworkFactories. b/17726566 |
| 2642 | if (oldScore != nai.getCurrentScore()) sendUpdatedScoreToFactories(nai); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2643 | if (valid) { |
| 2644 | handleFreshlyValidatedNetwork(nai); |
| 2645 | // Clear NO_INTERNET and LOST_INTERNET notifications if network becomes |
| 2646 | // valid. |
| 2647 | mNotifier.clearNotification(nai.network.netId, |
| 2648 | NotificationType.NO_INTERNET); |
| 2649 | mNotifier.clearNotification(nai.network.netId, |
| 2650 | NotificationType.LOST_INTERNET); |
| 2651 | } |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 2652 | } else if (partialConnectivityChanged) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2653 | nai.partialConnectivity = partialConnectivity; |
| 2654 | updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2655 | } |
| 2656 | updateInetCondition(nai); |
| 2657 | // Let the NetworkAgent know the state of its network |
| 2658 | Bundle redirectUrlBundle = new Bundle(); |
| 2659 | redirectUrlBundle.putString(NetworkAgent.REDIRECT_URL_KEY, redirectUrl); |
| 2660 | nai.asyncChannel.sendMessage( |
| 2661 | NetworkAgent.CMD_REPORT_NETWORK_STATUS, |
| 2662 | (valid ? NetworkAgent.VALID_NETWORK : NetworkAgent.INVALID_NETWORK), |
| 2663 | 0, redirectUrlBundle); |
| 2664 | if (wasValidated && !nai.lastValidated) { |
| 2665 | handleNetworkUnvalidated(nai); |
Robert Greenwalt | 73ed9d8 | 2014-08-19 18:58:04 -0700 | [diff] [blame] | 2666 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2667 | break; |
| 2668 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2669 | case EVENT_PROVISIONING_NOTIFICATION: { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 2670 | final int netId = msg.arg2; |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 2671 | final boolean visible = toBool(msg.arg1); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2672 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(netId); |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2673 | // If captive portal status has changed, update capabilities or disconnect. |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2674 | if (nai != null && (visible != nai.lastCaptivePortalDetected)) { |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2675 | final int oldScore = nai.getCurrentScore(); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2676 | nai.lastCaptivePortalDetected = visible; |
| 2677 | nai.everCaptivePortalDetected |= visible; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2678 | if (visible) { |
| 2679 | nai.captivePortalLoginNotified = false; |
| 2680 | } |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2681 | if (nai.lastCaptivePortalDetected && |
| 2682 | Settings.Global.CAPTIVE_PORTAL_MODE_AVOID == getCaptivePortalMode()) { |
| 2683 | if (DBG) log("Avoiding captive portal network: " + nai.name()); |
| 2684 | nai.asyncChannel.sendMessage( |
| 2685 | NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 2686 | teardownUnneededNetwork(nai); |
| 2687 | break; |
| 2688 | } |
Hugo Benichi | f15b282 | 2016-09-15 18:18:48 +0900 | [diff] [blame] | 2689 | updateCapabilities(oldScore, nai, nai.networkCapabilities); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 2690 | } |
| 2691 | if (!visible) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2692 | // Only clear SIGN_IN and NETWORK_SWITCH notifications here, or else other |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 2693 | // notifications belong to the same network may be cleared unexpectedly. |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 2694 | mNotifier.clearNotification(netId, NotificationType.SIGN_IN); |
| 2695 | mNotifier.clearNotification(netId, NotificationType.NETWORK_SWITCH); |
Paul Jensen | fdc4e4a | 2014-07-15 12:07:36 -0400 | [diff] [blame] | 2696 | } else { |
Paul Jensen | 5df4bec | 2014-08-25 22:45:39 -0400 | [diff] [blame] | 2697 | if (nai == null) { |
| 2698 | loge("EVENT_PROVISIONING_NOTIFICATION from unknown NetworkMonitor"); |
| 2699 | break; |
| 2700 | } |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2701 | if (!nai.networkMisc.provisioningNotificationDisabled) { |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 2702 | mNotifier.showNotification(netId, NotificationType.SIGN_IN, nai, null, |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 2703 | (PendingIntent) msg.obj, nai.networkMisc.explicitlySelected); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 2704 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2705 | } |
Paul Jensen | 869868be | 2014-05-15 10:33:05 -0400 | [diff] [blame] | 2706 | break; |
| 2707 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2708 | case EVENT_PRIVATE_DNS_CONFIG_RESOLVED: { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2709 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetId(msg.arg2); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2710 | if (nai == null) break; |
| 2711 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2712 | updatePrivateDns(nai, (PrivateDnsConfig) msg.obj); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2713 | break; |
| 2714 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2715 | } |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2716 | return true; |
| 2717 | } |
| 2718 | |
Calvin On | be96da1 | 2016-10-11 15:10:46 -0700 | [diff] [blame] | 2719 | private int getCaptivePortalMode() { |
| 2720 | return Settings.Global.getInt(mContext.getContentResolver(), |
| 2721 | Settings.Global.CAPTIVE_PORTAL_MODE, |
| 2722 | Settings.Global.CAPTIVE_PORTAL_MODE_PROMPT); |
| 2723 | } |
| 2724 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2725 | private boolean maybeHandleNetworkAgentInfoMessage(Message msg) { |
| 2726 | switch (msg.what) { |
| 2727 | default: |
| 2728 | return false; |
| 2729 | case NetworkAgentInfo.EVENT_NETWORK_LINGER_COMPLETE: { |
| 2730 | NetworkAgentInfo nai = (NetworkAgentInfo) msg.obj; |
| 2731 | if (nai != null && isLiveNetworkAgent(nai, msg.what)) { |
| 2732 | handleLingerComplete(nai); |
| 2733 | } |
| 2734 | break; |
| 2735 | } |
| 2736 | } |
| 2737 | return true; |
| 2738 | } |
| 2739 | |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2740 | private boolean maybeHandleNetworkFactoryMessage(Message msg) { |
| 2741 | switch (msg.what) { |
| 2742 | default: |
| 2743 | return false; |
| 2744 | case NetworkFactory.EVENT_UNFULFILLABLE_REQUEST: { |
| 2745 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.sendingUid, |
| 2746 | /* callOnUnavailable */ true); |
| 2747 | break; |
| 2748 | } |
| 2749 | } |
| 2750 | return true; |
| 2751 | } |
| 2752 | |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2753 | @Override |
| 2754 | public void handleMessage(Message msg) { |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2755 | if (!maybeHandleAsyncChannelMessage(msg) |
| 2756 | && !maybeHandleNetworkMonitorMessage(msg) |
| 2757 | && !maybeHandleNetworkAgentInfoMessage(msg) |
| 2758 | && !maybeHandleNetworkFactoryMessage(msg)) { |
Lorenzo Colitti | 9b0c8fd | 2016-04-05 17:52:16 +0900 | [diff] [blame] | 2759 | maybeHandleNetworkAgentMessage(msg); |
| 2760 | } |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 2761 | } |
| 2762 | } |
| 2763 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2764 | private class NetworkMonitorCallbacks extends INetworkMonitorCallbacks.Stub { |
| 2765 | private final NetworkAgentInfo mNai; |
| 2766 | |
| 2767 | private NetworkMonitorCallbacks(NetworkAgentInfo nai) { |
| 2768 | mNai = nai; |
| 2769 | } |
| 2770 | |
| 2771 | @Override |
| 2772 | public void onNetworkMonitorCreated(INetworkMonitor networkMonitor) { |
| 2773 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_AGENT, |
| 2774 | new Pair<>(mNai, networkMonitor))); |
| 2775 | } |
| 2776 | |
| 2777 | @Override |
| 2778 | public void notifyNetworkTested(int testResult, @Nullable String redirectUrl) { |
| 2779 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage(EVENT_NETWORK_TESTED, |
| 2780 | testResult, mNai.network.netId, redirectUrl)); |
| 2781 | } |
| 2782 | |
| 2783 | @Override |
| 2784 | public void notifyPrivateDnsConfigResolved(PrivateDnsConfigParcel config) { |
| 2785 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2786 | EVENT_PRIVATE_DNS_CONFIG_RESOLVED, |
| 2787 | 0, mNai.network.netId, PrivateDnsConfig.fromParcel(config))); |
| 2788 | } |
| 2789 | |
| 2790 | @Override |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2791 | public void showProvisioningNotification(String action, String packageName) { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2792 | final Intent intent = new Intent(action); |
Remi NGUYEN VAN | 9c5d964 | 2019-02-07 21:29:57 +0900 | [diff] [blame] | 2793 | intent.setPackage(packageName); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2794 | |
| 2795 | final PendingIntent pendingIntent; |
| 2796 | // Only the system server can register notifications with package "android" |
| 2797 | final long token = Binder.clearCallingIdentity(); |
| 2798 | try { |
| 2799 | pendingIntent = PendingIntent.getBroadcast(mContext, 0, intent, 0); |
| 2800 | } finally { |
| 2801 | Binder.restoreCallingIdentity(token); |
| 2802 | } |
| 2803 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2804 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_SHOW, |
| 2805 | mNai.network.netId, |
| 2806 | pendingIntent)); |
| 2807 | } |
| 2808 | |
| 2809 | @Override |
| 2810 | public void hideProvisioningNotification() { |
| 2811 | mTrackerHandler.sendMessage(mTrackerHandler.obtainMessage( |
| 2812 | EVENT_PROVISIONING_NOTIFICATION, PROVISIONING_NOTIFICATION_HIDE, |
| 2813 | mNai.network.netId)); |
| 2814 | } |
| 2815 | } |
| 2816 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2817 | private boolean networkRequiresValidation(NetworkAgentInfo nai) { |
Lorenzo Colitti | ed3168e | 2019-01-23 17:54:08 +0900 | [diff] [blame] | 2818 | return isValidationRequired(nai.networkCapabilities); |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2819 | } |
| 2820 | |
Erik Kline | c6d0022 | 2018-06-26 18:53:43 +0900 | [diff] [blame] | 2821 | private void handleFreshlyValidatedNetwork(NetworkAgentInfo nai) { |
| 2822 | if (nai == null) return; |
| 2823 | // If the Private DNS mode is opportunistic, reprogram the DNS servers |
| 2824 | // in order to restart a validation pass from within netd. |
| 2825 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2826 | if (cfg.useTls && TextUtils.isEmpty(cfg.hostname)) { |
| 2827 | updateDnses(nai.linkProperties, null, nai.network.netId); |
| 2828 | } |
| 2829 | } |
| 2830 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2831 | private void handlePrivateDnsSettingsChanged() { |
| 2832 | final PrivateDnsConfig cfg = mDnsManager.getPrivateDnsConfig(); |
| 2833 | |
| 2834 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2835 | handlePerNetworkPrivateDnsConfig(nai, cfg); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2836 | if (networkRequiresValidation(nai)) { |
| 2837 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2838 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2839 | } |
| 2840 | } |
| 2841 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2842 | private void handlePerNetworkPrivateDnsConfig(NetworkAgentInfo nai, PrivateDnsConfig cfg) { |
| 2843 | // Private DNS only ever applies to networks that might provide |
| 2844 | // Internet access and therefore also require validation. |
| 2845 | if (!networkRequiresValidation(nai)) return; |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2846 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2847 | // Notify the NetworkAgentInfo/NetworkMonitor in case NetworkMonitor needs to cancel or |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2848 | // schedule DNS resolutions. If a DNS resolution is required the |
| 2849 | // result will be sent back to us. |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2850 | try { |
| 2851 | nai.networkMonitor().notifyPrivateDnsChanged(cfg.toParcel()); |
| 2852 | } catch (RemoteException e) { |
| 2853 | e.rethrowFromSystemServer(); |
| 2854 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 2855 | |
| 2856 | // With Private DNS bypass support, we can proceed to update the |
| 2857 | // Private DNS config immediately, even if we're in strict mode |
| 2858 | // and have not yet resolved the provider name into a set of IPs. |
| 2859 | updatePrivateDns(nai, cfg); |
| 2860 | } |
| 2861 | |
| 2862 | private void updatePrivateDns(NetworkAgentInfo nai, PrivateDnsConfig newCfg) { |
| 2863 | mDnsManager.updatePrivateDns(nai.network, newCfg); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2864 | updateDnses(nai.linkProperties, null, nai.network.netId); |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 2865 | } |
| 2866 | |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 2867 | private void handlePrivateDnsValidationUpdate(PrivateDnsValidationUpdate update) { |
| 2868 | NetworkAgentInfo nai = getNetworkAgentInfoForNetId(update.netId); |
| 2869 | if (nai == null) { |
| 2870 | return; |
| 2871 | } |
| 2872 | mDnsManager.updatePrivateDnsValidation(update); |
| 2873 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2874 | } |
| 2875 | |
Lorenzo Colitti | 6998fa8 | 2019-01-08 10:04:25 +0900 | [diff] [blame] | 2876 | private void handleNat64PrefixEvent(int netId, boolean added, String prefixString, |
| 2877 | int prefixLength) { |
| 2878 | NetworkAgentInfo nai = mNetworkForNetId.get(netId); |
| 2879 | if (nai == null) return; |
| 2880 | |
| 2881 | log(String.format("NAT64 prefix %s on netId %d: %s/%d", |
| 2882 | (added ? "added" : "removed"), netId, prefixString, prefixLength)); |
| 2883 | |
| 2884 | IpPrefix prefix = null; |
| 2885 | if (added) { |
| 2886 | try { |
| 2887 | prefix = new IpPrefix(InetAddresses.parseNumericAddress(prefixString), |
| 2888 | prefixLength); |
| 2889 | } catch (IllegalArgumentException e) { |
| 2890 | loge("Invalid NAT64 prefix " + prefixString + "/" + prefixLength); |
| 2891 | return; |
| 2892 | } |
| 2893 | } |
| 2894 | |
| 2895 | nai.clatd.setNat64Prefix(prefix); |
| 2896 | handleUpdateLinkProperties(nai, new LinkProperties(nai.linkProperties)); |
| 2897 | } |
| 2898 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2899 | private void updateLingerState(NetworkAgentInfo nai, long now) { |
| 2900 | // 1. Update the linger timer. If it's changed, reschedule or cancel the alarm. |
| 2901 | // 2. If the network was lingering and there are now requests, unlinger it. |
| 2902 | // 3. If this network is unneeded (which implies it is not lingering), and there is at least |
| 2903 | // one lingered request, start lingering. |
| 2904 | nai.updateLingerTimer(); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2905 | if (nai.isLingering() && nai.numForegroundNetworkRequests() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2906 | if (DBG) log("Unlingering " + nai.name()); |
| 2907 | nai.unlinger(); |
| 2908 | logNetworkEvent(nai, NetworkEvent.NETWORK_UNLINGER); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2909 | } else if (unneeded(nai, UnneededFor.LINGER) && nai.getLingerExpiry() > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2910 | int lingerTime = (int) (nai.getLingerExpiry() - now); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 2911 | if (DBG) log("Lingering " + nai.name() + " for " + lingerTime + "ms"); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 2912 | nai.linger(); |
| 2913 | logNetworkEvent(nai, NetworkEvent.NETWORK_LINGER); |
| 2914 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOSING, lingerTime); |
| 2915 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2916 | } |
| 2917 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2918 | private void handleAsyncChannelHalfConnect(Message msg) { |
| 2919 | AsyncChannel ac = (AsyncChannel) msg.obj; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2920 | if (mNetworkFactoryInfos.containsKey(msg.replyTo)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2921 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2922 | if (VDBG) log("NetworkFactory connected"); |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 2923 | // Finish setting up the full connection |
| 2924 | mNetworkFactoryInfos.get(msg.replyTo).asyncChannel.sendMessage( |
| 2925 | AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2926 | // A network factory has connected. Send it all current NetworkRequests. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2927 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 2928 | if (nri.request.isListen()) continue; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 2929 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 2930 | final int score; |
| 2931 | final int serial; |
| 2932 | if (nai != null) { |
| 2933 | score = nai.getCurrentScore(); |
| 2934 | serial = nai.factorySerialNumber; |
| 2935 | } else { |
| 2936 | score = 0; |
| 2937 | serial = NetworkFactory.SerialNumber.NONE; |
| 2938 | } |
| 2939 | ac.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, serial, |
| 2940 | nri.request); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2941 | } |
| 2942 | } else { |
| 2943 | loge("Error connecting NetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2944 | mNetworkFactoryInfos.remove(msg.obj); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2945 | } |
| 2946 | } else if (mNetworkAgentInfos.containsKey(msg.replyTo)) { |
| 2947 | if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { |
| 2948 | if (VDBG) log("NetworkAgent connected"); |
| 2949 | // A network agent has requested a connection. Establish the connection. |
| 2950 | mNetworkAgentInfos.get(msg.replyTo).asyncChannel. |
| 2951 | sendMessage(AsyncChannel.CMD_CHANNEL_FULL_CONNECTION); |
| 2952 | } else { |
| 2953 | loge("Error connecting NetworkAgent"); |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 2954 | NetworkAgentInfo nai = mNetworkAgentInfos.remove(msg.replyTo); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2955 | if (nai != null) { |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2956 | final boolean wasDefault = isDefaultNetwork(nai); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2957 | synchronized (mNetworkForNetId) { |
| 2958 | mNetworkForNetId.remove(nai.network.netId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2959 | mNetIdInUse.delete(nai.network.netId); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 2960 | } |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 2961 | // Just in case. |
Lorenzo Colitti | cc917ce | 2015-05-01 00:30:10 +0900 | [diff] [blame] | 2962 | mLegacyTypeTracker.remove(nai, wasDefault); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2963 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2964 | } |
| 2965 | } |
| 2966 | } |
Paul Jensen | 0cc1732 | 2014-11-25 15:26:53 -0500 | [diff] [blame] | 2967 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2968 | // This is a no-op if it's called with a message designating a network that has |
| 2969 | // already been destroyed, because its reference will not be found in the relevant |
| 2970 | // maps. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2971 | private void handleAsyncChannelDisconnected(Message msg) { |
| 2972 | NetworkAgentInfo nai = mNetworkAgentInfos.get(msg.replyTo); |
| 2973 | if (nai != null) { |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2974 | disconnectAndDestroyNetwork(nai); |
Paul Jensen | 62d3080 | 2015-06-17 14:42:30 -0400 | [diff] [blame] | 2975 | } else { |
| 2976 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(msg.replyTo); |
| 2977 | if (DBG && nfi != null) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2981 | // Destroys a network, remove references to it from the internal state managed by |
| 2982 | // ConnectivityService, free its interfaces and clean up. |
| 2983 | // Must be called on the Handler thread. |
| 2984 | private void disconnectAndDestroyNetwork(NetworkAgentInfo nai) { |
| 2985 | if (DBG) { |
| 2986 | log(nai.name() + " got DISCONNECTED, was satisfying " + nai.numNetworkRequests()); |
| 2987 | } |
lucaslin | f80b83b | 2019-02-12 15:30:13 +0800 | [diff] [blame] | 2988 | // Clear all notifications of this network. |
| 2989 | mNotifier.clearNotification(nai.network.netId); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 2990 | // A network agent has disconnected. |
| 2991 | // TODO - if we move the logic to the network agent (have them disconnect |
| 2992 | // because they lost all their requests or because their score isn't good) |
| 2993 | // then they would disconnect organically, report their new state and then |
| 2994 | // disconnect the channel. |
| 2995 | if (nai.networkInfo.isConnected()) { |
| 2996 | nai.networkInfo.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, |
| 2997 | null, null); |
| 2998 | } |
| 2999 | final boolean wasDefault = isDefaultNetwork(nai); |
| 3000 | if (wasDefault) { |
| 3001 | mDefaultInetConditionPublished = 0; |
| 3002 | // Log default network disconnection before required book-keeping. |
| 3003 | // Let rematchAllNetworksAndRequests() below record a new default network event |
| 3004 | // if there is a fallback. Taken together, the two form a X -> 0, 0 -> Y sequence |
| 3005 | // whose timestamps tell how long it takes to recover a default network. |
| 3006 | long now = SystemClock.elapsedRealtime(); |
| 3007 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent(now, null, nai); |
| 3008 | } |
| 3009 | notifyIfacesChangedForNetworkStats(); |
| 3010 | // TODO - we shouldn't send CALLBACK_LOST to requests that can be satisfied |
| 3011 | // by other networks that are already connected. Perhaps that can be done by |
| 3012 | // sending all CALLBACK_LOST messages (for requests, not listens) at the end |
| 3013 | // of rematchAllNetworksAndRequests |
| 3014 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_LOST); |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3015 | mKeepaliveTracker.handleStopAllKeepalives(nai, SocketKeepalive.ERROR_INVALID_NETWORK); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3016 | for (String iface : nai.linkProperties.getAllInterfaceNames()) { |
| 3017 | // Disable wakeup packet monitoring for each interface. |
| 3018 | wakeupModifyInterface(iface, nai.networkCapabilities, false); |
| 3019 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3020 | try { |
| 3021 | nai.networkMonitor().notifyNetworkDisconnected(); |
| 3022 | } catch (RemoteException e) { |
| 3023 | e.rethrowFromSystemServer(); |
| 3024 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3025 | mNetworkAgentInfos.remove(nai.messenger); |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 3026 | nai.clatd.update(); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3027 | synchronized (mNetworkForNetId) { |
| 3028 | // Remove the NetworkAgent, but don't mark the netId as |
| 3029 | // available until we've told netd to delete it below. |
| 3030 | mNetworkForNetId.remove(nai.network.netId); |
| 3031 | } |
| 3032 | // Remove all previously satisfied requests. |
| 3033 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 3034 | NetworkRequest request = nai.requestAt(i); |
| 3035 | NetworkAgentInfo currentNetwork = getNetworkForRequest(request.requestId); |
| 3036 | if (currentNetwork != null && currentNetwork.network.netId == nai.network.netId) { |
| 3037 | clearNetworkForRequest(request.requestId); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3038 | sendUpdatedScoreToFactories(request, null); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3039 | } |
| 3040 | } |
| 3041 | nai.clearLingerState(); |
| 3042 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 3043 | updateDataActivityTracking(null /* newNetwork */, nai); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3044 | notifyLockdownVpn(nai); |
| 3045 | ensureNetworkTransitionWakelock(nai.name()); |
| 3046 | } |
| 3047 | mLegacyTypeTracker.remove(nai, wasDefault); |
| 3048 | if (!nai.networkCapabilities.hasTransport(TRANSPORT_VPN)) { |
| 3049 | updateAllVpnsCapabilities(); |
| 3050 | } |
| 3051 | rematchAllNetworksAndRequests(null, 0); |
| 3052 | mLingerMonitor.noteDisconnect(nai); |
| 3053 | if (nai.created) { |
| 3054 | // Tell netd to clean up the configuration for this network |
| 3055 | // (routing rules, DNS, etc). |
| 3056 | // This may be slow as it requires a lot of netd shelling out to ip and |
| 3057 | // ip[6]tables to flush routes and remove the incoming packet mark rule, so do it |
| 3058 | // after we've rematched networks with requests which should make a potential |
| 3059 | // fallback network the default or requested a new network from the |
| 3060 | // NetworkFactories, so network traffic isn't interrupted for an unnecessarily |
| 3061 | // long time. |
| 3062 | try { |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 3063 | mNetd.networkDestroy(nai.network.netId); |
| 3064 | } catch (RemoteException | ServiceSpecificException e) { |
| 3065 | loge("Exception destroying network: " + e); |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 3066 | } |
| 3067 | mDnsManager.removeNetwork(nai.network); |
| 3068 | } |
| 3069 | synchronized (mNetworkForNetId) { |
| 3070 | mNetIdInUse.delete(nai.network.netId); |
| 3071 | } |
| 3072 | } |
| 3073 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3074 | // If this method proves to be too slow then we can maintain a separate |
| 3075 | // pendingIntent => NetworkRequestInfo map. |
| 3076 | // This method assumes that every non-null PendingIntent maps to exactly 1 NetworkRequestInfo. |
| 3077 | private NetworkRequestInfo findExistingNetworkRequestInfo(PendingIntent pendingIntent) { |
| 3078 | Intent intent = pendingIntent.getIntent(); |
| 3079 | for (Map.Entry<NetworkRequest, NetworkRequestInfo> entry : mNetworkRequests.entrySet()) { |
| 3080 | PendingIntent existingPendingIntent = entry.getValue().mPendingIntent; |
| 3081 | if (existingPendingIntent != null && |
| 3082 | existingPendingIntent.getIntent().filterEquals(intent)) { |
| 3083 | return entry.getValue(); |
| 3084 | } |
| 3085 | } |
| 3086 | return null; |
| 3087 | } |
| 3088 | |
| 3089 | private void handleRegisterNetworkRequestWithIntent(Message msg) { |
| 3090 | final NetworkRequestInfo nri = (NetworkRequestInfo) (msg.obj); |
| 3091 | |
| 3092 | NetworkRequestInfo existingRequest = findExistingNetworkRequestInfo(nri.mPendingIntent); |
| 3093 | if (existingRequest != null) { // remove the existing request. |
| 3094 | if (DBG) log("Replacing " + existingRequest.request + " with " |
| 3095 | + nri.request + " because their intents matched."); |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3096 | handleReleaseNetworkRequest(existingRequest.request, getCallingUid(), |
| 3097 | /* callOnUnavailable */ false); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3098 | } |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3099 | handleRegisterNetworkRequest(nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3100 | } |
| 3101 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3102 | private void handleRegisterNetworkRequest(NetworkRequestInfo nri) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3103 | mNetworkRequests.put(nri.request, nri); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 3104 | mNetworkRequestInfoLogs.log("REGISTER " + nri); |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 3105 | if (nri.request.isListen()) { |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3106 | for (NetworkAgentInfo network : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 3107 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3108 | network.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3109 | updateSignalStrengthThresholds(network, "REGISTER", nri.request); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 3110 | } |
| 3111 | } |
| 3112 | } |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 3113 | rematchAllNetworksAndRequests(null, 0); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3114 | if (nri.request.isRequest() && getNetworkForRequest(nri.request.requestId) == null) { |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3115 | sendUpdatedScoreToFactories(nri.request, null); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3116 | } |
| 3117 | } |
| 3118 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3119 | private void handleReleaseNetworkRequestWithIntent(PendingIntent pendingIntent, |
| 3120 | int callingUid) { |
| 3121 | NetworkRequestInfo nri = findExistingNetworkRequestInfo(pendingIntent); |
| 3122 | if (nri != null) { |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3123 | handleReleaseNetworkRequest(nri.request, callingUid, /* callOnUnavailable */ false); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3124 | } |
| 3125 | } |
| 3126 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3127 | // Determines whether the network is the best (or could become the best, if it validated), for |
| 3128 | // none of a particular type of NetworkRequests. The type of NetworkRequests considered depends |
| 3129 | // on the value of reason: |
| 3130 | // |
| 3131 | // - UnneededFor.TEARDOWN: non-listen NetworkRequests. If a network is unneeded for this reason, |
| 3132 | // then it should be torn down. |
| 3133 | // - UnneededFor.LINGER: foreground NetworkRequests. If a network is unneeded for this reason, |
| 3134 | // then it should be lingered. |
| 3135 | private boolean unneeded(NetworkAgentInfo nai, UnneededFor reason) { |
| 3136 | final int numRequests; |
| 3137 | switch (reason) { |
| 3138 | case TEARDOWN: |
| 3139 | numRequests = nai.numRequestNetworkRequests(); |
| 3140 | break; |
| 3141 | case LINGER: |
| 3142 | numRequests = nai.numForegroundNetworkRequests(); |
| 3143 | break; |
| 3144 | default: |
| 3145 | Slog.wtf(TAG, "Invalid reason. Cannot happen."); |
| 3146 | return true; |
| 3147 | } |
| 3148 | |
| 3149 | if (!nai.everConnected || nai.isVPN() || nai.isLingering() || numRequests > 0) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 3150 | return false; |
| 3151 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3152 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3153 | if (reason == UnneededFor.LINGER && nri.request.isBackgroundRequest()) { |
| 3154 | // Background requests don't affect lingering. |
| 3155 | continue; |
| 3156 | } |
| 3157 | |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3158 | // If this Network is already the highest scoring Network for a request, or if |
| 3159 | // 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] | 3160 | if (nri.request.isRequest() && nai.satisfies(nri.request) && |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 3161 | (nai.isSatisfyingRequest(nri.request.requestId) || |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3162 | // Note that this catches two important cases: |
| 3163 | // 1. Unvalidated cellular will not be reaped when unvalidated WiFi |
| 3164 | // is currently satisfying the request. This is desirable when |
| 3165 | // cellular ends up validating but WiFi does not. |
| 3166 | // 2. Unvalidated WiFi will not be reaped when validated cellular |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 3167 | // is currently satisfying the request. This is desirable when |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3168 | // WiFi ends up validating and out scoring cellular. |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3169 | getNetworkForRequest(nri.request.requestId).getCurrentScore() < |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3170 | nai.getCurrentScoreAsValidated())) { |
| 3171 | return false; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3172 | } |
| 3173 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 3174 | return true; |
Paul Jensen | 9936484 | 2014-12-09 11:43:45 -0500 | [diff] [blame] | 3175 | } |
| 3176 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3177 | private NetworkRequestInfo getNriForAppRequest( |
| 3178 | NetworkRequest request, int callingUid, String requestedOperation) { |
| 3179 | final NetworkRequestInfo nri = mNetworkRequests.get(request); |
| 3180 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3181 | if (nri != null) { |
Jeff Davidson | 6f91390 | 2014-08-21 15:54:15 -0700 | [diff] [blame] | 3182 | if (Process.SYSTEM_UID != callingUid && nri.mUid != callingUid) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3183 | log(String.format("UID %d attempted to %s for unowned request %s", |
| 3184 | callingUid, requestedOperation, nri)); |
| 3185 | return null; |
Paul Jensen | 7ecb42f | 2014-05-16 14:31:12 -0400 | [diff] [blame] | 3186 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | return nri; |
| 3190 | } |
| 3191 | |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3192 | private void handleTimedOutNetworkRequest(final NetworkRequestInfo nri) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3193 | if (mNetworkRequests.get(nri.request) == null) { |
| 3194 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3195 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3196 | if (getNetworkForRequest(nri.request.requestId) != null) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3197 | return; |
| 3198 | } |
| 3199 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3200 | log("releasing " + nri.request + " (timeout)"); |
| 3201 | } |
| 3202 | handleRemoveNetworkRequest(nri); |
| 3203 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_UNAVAIL, 0); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3204 | } |
| 3205 | |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3206 | private void handleReleaseNetworkRequest(NetworkRequest request, int callingUid, |
| 3207 | boolean callOnUnavailable) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3208 | final NetworkRequestInfo nri = |
| 3209 | getNriForAppRequest(request, callingUid, "release NetworkRequest"); |
| 3210 | if (nri == null) { |
| 3211 | return; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3212 | } |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3213 | if (VDBG || (DBG && nri.request.isRequest())) { |
| 3214 | log("releasing " + nri.request + " (release request)"); |
| 3215 | } |
| 3216 | handleRemoveNetworkRequest(nri); |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3217 | if (callOnUnavailable) { |
| 3218 | callCallbackForRequest(nri, null, ConnectivityManager.CALLBACK_UNAVAIL, 0); |
| 3219 | } |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3220 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3221 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3222 | private void handleRemoveNetworkRequest(final NetworkRequestInfo nri) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3223 | nri.unlinkDeathRecipient(); |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3224 | mNetworkRequests.remove(nri.request); |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3225 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3226 | synchronized (mUidToNetworkRequestCount) { |
| 3227 | int requests = mUidToNetworkRequestCount.get(nri.mUid, 0); |
| 3228 | if (requests < 1) { |
| 3229 | Slog.wtf(TAG, "BUG: too small request count " + requests + " for UID " + |
| 3230 | nri.mUid); |
| 3231 | } else if (requests == 1) { |
| 3232 | mUidToNetworkRequestCount.removeAt( |
| 3233 | mUidToNetworkRequestCount.indexOfKey(nri.mUid)); |
| 3234 | } else { |
| 3235 | mUidToNetworkRequestCount.put(nri.mUid, requests - 1); |
| 3236 | } |
| 3237 | } |
Erik Kline | 33d8e5c | 2018-01-15 17:05:07 +0900 | [diff] [blame] | 3238 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3239 | mNetworkRequestInfoLogs.log("RELEASE " + nri); |
| 3240 | if (nri.request.isRequest()) { |
| 3241 | boolean wasKept = false; |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3242 | NetworkAgentInfo nai = getNetworkForRequest(nri.request.requestId); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3243 | if (nai != null) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3244 | boolean wasBackgroundNetwork = nai.isBackgroundNetwork(); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3245 | nai.removeRequest(nri.request.requestId); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3246 | if (VDBG || DDBG) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3247 | log(" Removing from current network " + nai.name() + |
| 3248 | ", leaving " + nai.numNetworkRequests() + " requests."); |
| 3249 | } |
| 3250 | // If there are still lingered requests on this network, don't tear it down, |
| 3251 | // but resume lingering instead. |
| 3252 | updateLingerState(nai, SystemClock.elapsedRealtime()); |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 3253 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3254 | if (DBG) log("no live requests for " + nai.name() + "; disconnecting"); |
| 3255 | teardownUnneededNetwork(nai); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 3256 | } else { |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3257 | wasKept = true; |
| 3258 | } |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 3259 | clearNetworkForRequest(nri.request.requestId); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3260 | if (!wasBackgroundNetwork && nai.isBackgroundNetwork()) { |
| 3261 | // Went from foreground to background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 3262 | updateCapabilities(nai.getCurrentScore(), nai, nai.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 3263 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3264 | } |
| 3265 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3266 | // Maintain the illusion. When this request arrived, we might have pretended |
| 3267 | // that a network connected to serve it, even though the network was already |
| 3268 | // connected. Now that this request has gone away, we might have to pretend |
| 3269 | // that the network disconnected. LegacyTypeTracker will generate that |
| 3270 | // phantom disconnect for this type. |
| 3271 | if (nri.request.legacyType != TYPE_NONE && nai != null) { |
| 3272 | boolean doRemove = true; |
| 3273 | if (wasKept) { |
| 3274 | // check if any of the remaining requests for this network are for the |
| 3275 | // same legacy type - if so, don't remove the nai |
| 3276 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 3277 | NetworkRequest otherRequest = nai.requestAt(i); |
| 3278 | if (otherRequest.legacyType == nri.request.legacyType && |
| 3279 | otherRequest.isRequest()) { |
| 3280 | if (DBG) log(" still have other legacy request - leaving"); |
| 3281 | doRemove = false; |
Robert Greenwalt | 5148185 | 2015-01-08 14:43:31 -0800 | [diff] [blame] | 3282 | } |
| 3283 | } |
Robert Greenwalt | 4456cf3 | 2014-08-24 22:52:10 -0700 | [diff] [blame] | 3284 | } |
| 3285 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3286 | if (doRemove) { |
| 3287 | mLegacyTypeTracker.remove(nri.request.legacyType, nai, false); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3288 | } |
| 3289 | } |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 3290 | |
| 3291 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
| 3292 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_CANCEL_REQUEST, |
| 3293 | nri.request); |
| 3294 | } |
| 3295 | } else { |
| 3296 | // listens don't have a singular affectedNetwork. Check all networks to see |
| 3297 | // if this listen request applies and remove it. |
| 3298 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 3299 | nai.removeRequest(nri.request.requestId); |
| 3300 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 3301 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 3302 | updateSignalStrengthThresholds(nai, "RELEASE", nri.request); |
| 3303 | } |
| 3304 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3305 | } |
| 3306 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3307 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3308 | @Override |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3309 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3310 | enforceNetworkStackSettingsOrSetup(); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3311 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_UNVALIDATED, |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3312 | encodeBool(accept), encodeBool(always), network)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3313 | } |
| 3314 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3315 | @Override |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3316 | public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) { |
| 3317 | enforceNetworkStackSettingsOrSetup(); |
| 3318 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY, |
| 3319 | encodeBool(accept), encodeBool(always), network)); |
| 3320 | } |
| 3321 | |
| 3322 | @Override |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3323 | public void setAvoidUnvalidated(Network network) { |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3324 | enforceNetworkStackSettingsOrSetup(); |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3325 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_SET_AVOID_UNVALIDATED, network)); |
| 3326 | } |
| 3327 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3328 | private void handleSetAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 3329 | if (DBG) log("handleSetAcceptUnvalidated network=" + network + |
| 3330 | " accept=" + accept + " always=" + always); |
| 3331 | |
| 3332 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3333 | if (nai == null) { |
| 3334 | // Nothing to do. |
| 3335 | return; |
| 3336 | } |
| 3337 | |
| 3338 | if (nai.everValidated) { |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3339 | // The network validated while the dialog box was up. Take no action. |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3340 | return; |
| 3341 | } |
| 3342 | |
| 3343 | if (!nai.networkMisc.explicitlySelected) { |
| 3344 | Slog.wtf(TAG, "BUG: setAcceptUnvalidated non non-explicitly selected network"); |
| 3345 | } |
| 3346 | |
| 3347 | if (accept != nai.networkMisc.acceptUnvalidated) { |
| 3348 | int oldScore = nai.getCurrentScore(); |
| 3349 | nai.networkMisc.acceptUnvalidated = accept; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3350 | // If network becomes partial connectivity and user already accepted to use this |
| 3351 | // network, we should respect the user's option and don't need to popup the |
| 3352 | // PARTIAL_CONNECTIVITY notification to user again. |
| 3353 | nai.networkMisc.acceptPartialConnectivity = accept; |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 3354 | rematchAllNetworksAndRequests(nai, oldScore); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3355 | sendUpdatedScoreToFactories(nai); |
| 3356 | } |
| 3357 | |
| 3358 | if (always) { |
| 3359 | nai.asyncChannel.sendMessage( |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3360 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, encodeBool(accept)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3361 | } |
| 3362 | |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3363 | if (!accept) { |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3364 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 3365 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3366 | // Teardown the network. |
Paul Jensen | f95d220 | 2015-07-13 15:19:51 -0400 | [diff] [blame] | 3367 | teardownUnneededNetwork(nai); |
Lorenzo Colitti | f6673d0 | 2015-05-21 16:17:05 +0900 | [diff] [blame] | 3368 | } |
| 3369 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3370 | } |
| 3371 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3372 | private void handleSetAcceptPartialConnectivity(Network network, boolean accept, |
| 3373 | boolean always) { |
| 3374 | if (DBG) { |
| 3375 | log("handleSetAcceptPartialConnectivity network=" + network + " accept=" + accept |
| 3376 | + " always=" + always); |
| 3377 | } |
| 3378 | |
| 3379 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3380 | if (nai == null) { |
| 3381 | // Nothing to do. |
| 3382 | return; |
| 3383 | } |
| 3384 | |
| 3385 | if (nai.lastValidated) { |
| 3386 | // The network validated while the dialog box was up. Take no action. |
| 3387 | return; |
| 3388 | } |
| 3389 | |
| 3390 | if (accept != nai.networkMisc.acceptPartialConnectivity) { |
| 3391 | nai.networkMisc.acceptPartialConnectivity = accept; |
| 3392 | } |
| 3393 | |
| 3394 | // TODO: Use the current design or save the user choice into IpMemoryStore. |
| 3395 | if (always) { |
| 3396 | nai.asyncChannel.sendMessage( |
| 3397 | NetworkAgent.CMD_SAVE_ACCEPT_UNVALIDATED, encodeBool(accept)); |
| 3398 | } |
| 3399 | |
| 3400 | if (!accept) { |
| 3401 | // Tell the NetworkAgent to not automatically reconnect to the network. |
| 3402 | nai.asyncChannel.sendMessage(NetworkAgent.CMD_PREVENT_AUTOMATIC_RECONNECT); |
| 3403 | // Tear down the network. |
| 3404 | teardownUnneededNetwork(nai); |
| 3405 | } else { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3406 | // Inform NetworkMonitor that partial connectivity is acceptable. This will likely |
| 3407 | // result in a partial connectivity result which will be processed by |
| 3408 | // maybeHandleNetworkMonitorMessage. |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3409 | try { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3410 | nai.networkMonitor().setAcceptPartialConnectivity(); |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3411 | } catch (RemoteException e) { |
| 3412 | e.rethrowFromSystemServer(); |
| 3413 | } |
| 3414 | } |
| 3415 | } |
| 3416 | |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3417 | private void handleSetAvoidUnvalidated(Network network) { |
| 3418 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3419 | if (nai == null || nai.lastValidated) { |
| 3420 | // Nothing to do. The network either disconnected or revalidated. |
| 3421 | return; |
| 3422 | } |
| 3423 | if (!nai.avoidUnvalidated) { |
| 3424 | int oldScore = nai.getCurrentScore(); |
| 3425 | nai.avoidUnvalidated = true; |
| 3426 | rematchAllNetworksAndRequests(nai, oldScore); |
| 3427 | sendUpdatedScoreToFactories(nai); |
| 3428 | } |
| 3429 | } |
| 3430 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3431 | private void scheduleUnvalidatedPrompt(NetworkAgentInfo nai) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 3432 | if (VDBG) log("scheduleUnvalidatedPrompt " + nai.network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3433 | mHandler.sendMessageDelayed( |
| 3434 | mHandler.obtainMessage(EVENT_PROMPT_UNVALIDATED, nai.network), |
| 3435 | PROMPT_UNVALIDATED_DELAY_MS); |
| 3436 | } |
| 3437 | |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3438 | @Override |
| 3439 | public void startCaptivePortalApp(Network network) { |
| 3440 | enforceConnectivityInternalPermission(); |
| 3441 | mHandler.post(() -> { |
| 3442 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3443 | if (nai == null) return; |
| 3444 | if (!nai.networkCapabilities.hasCapability(NET_CAPABILITY_CAPTIVE_PORTAL)) return; |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3445 | try { |
| 3446 | nai.networkMonitor().launchCaptivePortalApp(); |
| 3447 | } catch (RemoteException e) { |
| 3448 | e.rethrowFromSystemServer(); |
| 3449 | } |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 3450 | }); |
| 3451 | } |
| 3452 | |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3453 | /** |
| 3454 | * NetworkStack endpoint to start the captive portal app. The NetworkStack needs to use this |
| 3455 | * endpoint as it does not have INTERACT_ACROSS_USERS_FULL itself. |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3456 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3457 | * @param appExtras Bundle to use as intent extras for the captive portal application. |
| 3458 | * Must be treated as opaque to avoid preventing the captive portal app to |
| 3459 | * update its arguments. |
| 3460 | */ |
| 3461 | @Override |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3462 | public void startCaptivePortalAppInternal(Network network, Bundle appExtras) { |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3463 | mContext.checkCallingOrSelfPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK); |
| 3464 | |
| 3465 | final Intent appIntent = new Intent(ConnectivityManager.ACTION_CAPTIVE_PORTAL_SIGN_IN); |
| 3466 | appIntent.putExtras(appExtras); |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3467 | appIntent.putExtra(ConnectivityManager.EXTRA_CAPTIVE_PORTAL, |
| 3468 | new CaptivePortal(new CaptivePortalImpl(network).asBinder())); |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 3469 | appIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3470 | |
| 3471 | Binder.withCleanCallingIdentity(() -> |
| 3472 | mContext.startActivityAsUser(appIntent, UserHandle.CURRENT)); |
| 3473 | } |
| 3474 | |
Remi NGUYEN VAN | 5a1b74b | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 3475 | private class CaptivePortalImpl extends ICaptivePortal.Stub { |
| 3476 | private final Network mNetwork; |
| 3477 | |
| 3478 | private CaptivePortalImpl(Network network) { |
| 3479 | mNetwork = network; |
| 3480 | } |
| 3481 | |
| 3482 | @Override |
| 3483 | public void appResponse(final int response) throws RemoteException { |
| 3484 | if (response == CaptivePortal.APP_RETURN_WANTED_AS_IS) { |
| 3485 | enforceSettingsPermission(); |
| 3486 | } |
| 3487 | |
| 3488 | // getNetworkAgentInfoForNetwork is thread-safe |
| 3489 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(mNetwork); |
| 3490 | if (nai == null) return; |
| 3491 | |
| 3492 | // nai.networkMonitor() is thread-safe |
| 3493 | final INetworkMonitor nm = nai.networkMonitor(); |
| 3494 | if (nm == null) return; |
| 3495 | |
| 3496 | final long token = Binder.clearCallingIdentity(); |
| 3497 | try { |
| 3498 | nm.notifyCaptivePortalAppFinished(response); |
| 3499 | } finally { |
| 3500 | // Not using Binder.withCleanCallingIdentity() to keep the checked RemoteException |
| 3501 | Binder.restoreCallingIdentity(token); |
| 3502 | } |
| 3503 | } |
| 3504 | |
| 3505 | @Override |
| 3506 | public void logEvent(int eventId, String packageName) { |
| 3507 | enforceSettingsPermission(); |
| 3508 | |
| 3509 | new MetricsLogger().action(eventId, packageName); |
| 3510 | } |
| 3511 | } |
| 3512 | |
Hugo Benichi | c8e9e12 | 2016-09-14 23:23:08 +0000 | [diff] [blame] | 3513 | public boolean avoidBadWifi() { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3514 | return mMultinetworkPolicyTracker.getAvoidBadWifi(); |
Lorenzo Colitti | 2618c1b | 2016-09-16 23:43:38 +0900 | [diff] [blame] | 3515 | } |
| 3516 | |
Remi NGUYEN VAN | 24ec3ff | 2019-03-22 17:16:52 +0900 | [diff] [blame] | 3517 | /** |
| 3518 | * Return whether the device should maintain continuous, working connectivity by switching away |
| 3519 | * from WiFi networks having no connectivity. |
| 3520 | * @see MultinetworkPolicyTracker#getAvoidBadWifi() |
| 3521 | */ |
| 3522 | public boolean shouldAvoidBadWifi() { |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 3523 | if (!checkNetworkStackPermission()) { |
| 3524 | throw new SecurityException("avoidBadWifi requires NETWORK_STACK permission"); |
| 3525 | } |
| 3526 | return avoidBadWifi(); |
| 3527 | } |
| 3528 | |
| 3529 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3530 | private void rematchForAvoidBadWifiUpdate() { |
| 3531 | rematchAllNetworksAndRequests(null, 0); |
| 3532 | for (NetworkAgentInfo nai: mNetworkAgentInfos.values()) { |
| 3533 | if (nai.networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 3534 | sendUpdatedScoreToFactories(nai); |
| 3535 | } |
| 3536 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3537 | } |
| 3538 | |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 3539 | // TODO: Evaluate whether this is of interest to other consumers of |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3540 | // MultinetworkPolicyTracker and worth moving out of here. |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3541 | private void dumpAvoidBadWifiSettings(IndentingPrintWriter pw) { |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3542 | final boolean configRestrict = mMultinetworkPolicyTracker.configRestrictsAvoidBadWifi(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3543 | if (!configRestrict) { |
| 3544 | pw.println("Bad Wi-Fi avoidance: unrestricted"); |
| 3545 | return; |
| 3546 | } |
| 3547 | |
| 3548 | pw.println("Bad Wi-Fi avoidance: " + avoidBadWifi()); |
| 3549 | pw.increaseIndent(); |
| 3550 | pw.println("Config restrict: " + configRestrict); |
| 3551 | |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 3552 | final String value = mMultinetworkPolicyTracker.getAvoidBadWifiSetting(); |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3553 | String description; |
| 3554 | // Can't use a switch statement because strings are legal case labels, but null is not. |
| 3555 | if ("0".equals(value)) { |
| 3556 | description = "get stuck"; |
| 3557 | } else if (value == null) { |
| 3558 | description = "prompt"; |
| 3559 | } else if ("1".equals(value)) { |
| 3560 | description = "avoid"; |
| 3561 | } else { |
| 3562 | description = value + " (?)"; |
| 3563 | } |
| 3564 | pw.println("User setting: " + description); |
| 3565 | pw.println("Network overrides:"); |
| 3566 | pw.increaseIndent(); |
Hugo Benichi | a2a917c | 2018-09-03 08:19:02 +0900 | [diff] [blame] | 3567 | for (NetworkAgentInfo nai : networksSortedById()) { |
Lorenzo Colitti | 29bd384 | 2016-09-20 16:03:27 +0900 | [diff] [blame] | 3568 | if (nai.avoidUnvalidated) { |
| 3569 | pw.println(nai.name()); |
| 3570 | } |
| 3571 | } |
| 3572 | pw.decreaseIndent(); |
| 3573 | pw.decreaseIndent(); |
| 3574 | } |
| 3575 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3576 | private void showNetworkNotification(NetworkAgentInfo nai, NotificationType type) { |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3577 | final String action; |
| 3578 | switch (type) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3579 | case LOGGED_IN: |
| 3580 | action = Settings.ACTION_WIFI_SETTINGS; |
| 3581 | mHandler.removeMessages(EVENT_TIMEOUT_NOTIFICATION); |
| 3582 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NOTIFICATION, |
| 3583 | nai.network.netId, 0), TIMEOUT_NOTIFICATION_DELAY_MS); |
| 3584 | break; |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3585 | case NO_INTERNET: |
| 3586 | action = ConnectivityManager.ACTION_PROMPT_UNVALIDATED; |
| 3587 | break; |
| 3588 | case LOST_INTERNET: |
| 3589 | action = ConnectivityManager.ACTION_PROMPT_LOST_VALIDATION; |
| 3590 | break; |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3591 | case PARTIAL_CONNECTIVITY: |
| 3592 | action = ConnectivityManager.ACTION_PROMPT_PARTIAL_CONNECTIVITY; |
| 3593 | break; |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3594 | default: |
| 3595 | Slog.wtf(TAG, "Unknown notification type " + type); |
| 3596 | return; |
| 3597 | } |
| 3598 | |
| 3599 | Intent intent = new Intent(action); |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3600 | if (type != NotificationType.LOGGED_IN) { |
| 3601 | intent.setData(Uri.fromParts("netId", Integer.toString(nai.network.netId), null)); |
| 3602 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 3603 | intent.setClassName("com.android.settings", |
| 3604 | "com.android.settings.wifi.WifiNoInternetDialog"); |
| 3605 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3606 | |
| 3607 | PendingIntent pendingIntent = PendingIntent.getActivityAsUser( |
| 3608 | mContext, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT); |
| 3609 | mNotifier.showNotification(nai.network.netId, type, nai, null, pendingIntent, true); |
| 3610 | } |
| 3611 | |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3612 | private void handlePromptUnvalidated(Network network) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 3613 | if (VDBG || DDBG) log("handlePromptUnvalidated " + network); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3614 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 3615 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3616 | // Only prompt if the network is unvalidated or network has partial internet connectivity |
| 3617 | // and was explicitly selected by the user, and if we haven't already been told to switch |
| 3618 | // to it regardless of whether it validated or not. Also don't prompt on captive portals |
| 3619 | // because we're already prompting the user to sign in. |
| 3620 | if (nai == null || nai.everValidated || nai.everCaptivePortalDetected |
| 3621 | || !nai.networkMisc.explicitlySelected || nai.networkMisc.acceptUnvalidated |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 3622 | // TODO: Once the value of acceptPartialConnectivity is moved to IpMemoryStore, |
| 3623 | // we should reevaluate how to handle acceptPartialConnectivity when network just |
| 3624 | // connected. |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3625 | || nai.networkMisc.acceptPartialConnectivity) { |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3626 | return; |
| 3627 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3628 | // TODO: Evaluate if it's needed to wait 8 seconds for triggering notification when |
| 3629 | // NetworkMonitor detects the network is partial connectivity. Need to change the design to |
| 3630 | // popup the notification immediately when the network is partial connectivity. |
| 3631 | if (nai.partialConnectivity) { |
lucaslin | 975b7d3 | 2019-03-21 11:59:22 +0800 | [diff] [blame] | 3632 | showNetworkNotification(nai, NotificationType.PARTIAL_CONNECTIVITY); |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3633 | } else { |
| 3634 | showNetworkNotification(nai, NotificationType.NO_INTERNET); |
| 3635 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3636 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3637 | |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3638 | private void handleNetworkUnvalidated(NetworkAgentInfo nai) { |
| 3639 | NetworkCapabilities nc = nai.networkCapabilities; |
| 3640 | if (DBG) log("handleNetworkUnvalidated " + nai.name() + " cap=" + nc); |
fionaxu | 1bf6ec2 | 2016-05-23 16:33:16 -0700 | [diff] [blame] | 3641 | |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3642 | if (!nc.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 3643 | return; |
| 3644 | } |
| 3645 | |
| 3646 | if (mMultinetworkPolicyTracker.shouldNotifyWifiUnvalidated()) { |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3647 | showNetworkNotification(nai, NotificationType.LOST_INTERNET); |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 3648 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3649 | } |
| 3650 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3651 | @Override |
| 3652 | public int getMultipathPreference(Network network) { |
| 3653 | enforceAccessPermission(); |
| 3654 | |
| 3655 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 3656 | if (nai != null && nai.networkCapabilities |
| 3657 | .hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED)) { |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3658 | return ConnectivityManager.MULTIPATH_PREFERENCE_UNMETERED; |
| 3659 | } |
| 3660 | |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3661 | Integer networkPreference = mMultipathPolicyTracker.getMultipathPreference(network); |
| 3662 | if (networkPreference != null) { |
| 3663 | return networkPreference; |
| 3664 | } |
| 3665 | |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 3666 | return mMultinetworkPolicyTracker.getMeteredMultipathPreference(); |
| 3667 | } |
| 3668 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3669 | @Override |
| 3670 | public NetworkRequest getDefaultRequest() { |
| 3671 | return mDefaultRequest; |
| 3672 | } |
| 3673 | |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3674 | private class InternalHandler extends Handler { |
| 3675 | public InternalHandler(Looper looper) { |
| 3676 | super(looper); |
| 3677 | } |
| 3678 | |
| 3679 | @Override |
| 3680 | public void handleMessage(Message msg) { |
Jeff Sharkey | 4c628eb | 2012-07-23 13:19:46 -0700 | [diff] [blame] | 3681 | switch (msg.what) { |
Robert Greenwalt | 2771181 | 2014-06-25 16:45:57 -0700 | [diff] [blame] | 3682 | case EVENT_EXPIRE_NET_TRANSITION_WAKELOCK: |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3683 | case EVENT_CLEAR_NET_TRANSITION_WAKELOCK: { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3684 | handleReleaseNetworkTransitionWakelock(msg.what); |
Robert Greenwalt | 057d5e9 | 2010-09-09 14:05:10 -0700 | [diff] [blame] | 3685 | break; |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3686 | } |
Sreeram Ramachandran | b1486ae | 2013-08-27 11:41:19 -0700 | [diff] [blame] | 3687 | case EVENT_APPLY_GLOBAL_HTTP_PROXY: { |
Chalard Jean | b9d45fd | 2018-06-08 14:24:49 +0900 | [diff] [blame] | 3688 | mProxyTracker.loadDeprecatedGlobalHttpProxy(); |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3689 | break; |
| 3690 | } |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3691 | case EVENT_PROXY_HAS_CHANGED: { |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3692 | handleApplyDefaultProxy((ProxyInfo)msg.obj); |
Jason Monk | decd295 | 2013-10-10 14:02:51 -0400 | [diff] [blame] | 3693 | break; |
| 3694 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3695 | case EVENT_REGISTER_NETWORK_FACTORY: { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3696 | handleRegisterNetworkFactory((NetworkFactoryInfo)msg.obj); |
| 3697 | break; |
| 3698 | } |
| 3699 | case EVENT_UNREGISTER_NETWORK_FACTORY: { |
| 3700 | handleUnregisterNetworkFactory((Messenger)msg.obj); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3701 | break; |
| 3702 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3703 | case EVENT_REGISTER_NETWORK_AGENT: { |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 3704 | final Pair<NetworkAgentInfo, INetworkMonitor> arg = |
| 3705 | (Pair<NetworkAgentInfo, INetworkMonitor>) msg.obj; |
| 3706 | handleRegisterNetworkAgent(arg.first, arg.second); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3707 | break; |
| 3708 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3709 | case EVENT_REGISTER_NETWORK_REQUEST: |
| 3710 | case EVENT_REGISTER_NETWORK_LISTENER: { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3711 | handleRegisterNetworkRequest((NetworkRequestInfo) msg.obj); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3712 | break; |
| 3713 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3714 | case EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT: |
| 3715 | case EVENT_REGISTER_NETWORK_LISTENER_WITH_INTENT: { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3716 | handleRegisterNetworkRequestWithIntent(msg); |
| 3717 | break; |
| 3718 | } |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3719 | case EVENT_TIMEOUT_NETWORK_REQUEST: { |
| 3720 | NetworkRequestInfo nri = (NetworkRequestInfo) msg.obj; |
| 3721 | handleTimedOutNetworkRequest(nri); |
| 3722 | break; |
| 3723 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3724 | case EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT: { |
| 3725 | handleReleaseNetworkRequestWithIntent((PendingIntent) msg.obj, msg.arg1); |
| 3726 | break; |
| 3727 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3728 | case EVENT_RELEASE_NETWORK_REQUEST: { |
Etan Cohen | ae574a8 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3729 | handleReleaseNetworkRequest((NetworkRequest) msg.obj, msg.arg1, |
| 3730 | /* callOnUnavailable */ false); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3731 | break; |
| 3732 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3733 | case EVENT_SET_ACCEPT_UNVALIDATED: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3734 | Network network = (Network) msg.obj; |
| 3735 | handleSetAcceptUnvalidated(network, toBool(msg.arg1), toBool(msg.arg2)); |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3736 | break; |
| 3737 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 3738 | case EVENT_SET_ACCEPT_PARTIAL_CONNECTIVITY: { |
| 3739 | Network network = (Network) msg.obj; |
| 3740 | handleSetAcceptPartialConnectivity(network, toBool(msg.arg1), |
| 3741 | toBool(msg.arg2)); |
| 3742 | break; |
| 3743 | } |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 3744 | case EVENT_SET_AVOID_UNVALIDATED: { |
| 3745 | handleSetAvoidUnvalidated((Network) msg.obj); |
| 3746 | break; |
| 3747 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 3748 | case EVENT_PROMPT_UNVALIDATED: { |
| 3749 | handlePromptUnvalidated((Network) msg.obj); |
| 3750 | break; |
| 3751 | } |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 3752 | case EVENT_CONFIGURE_ALWAYS_ON_NETWORKS: { |
| 3753 | handleConfigureAlwaysOnNetworks(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 3754 | break; |
| 3755 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3756 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3757 | case NetworkAgent.CMD_START_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3758 | mKeepaliveTracker.handleStartKeepalive(msg); |
| 3759 | break; |
| 3760 | } |
| 3761 | // Sent by KeepaliveTracker to process an app request on the state machine thread. |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 3762 | case NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE: { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 3763 | NetworkAgentInfo nai = getNetworkAgentInfoForNetwork((Network) msg.obj); |
| 3764 | int slot = msg.arg1; |
| 3765 | int reason = msg.arg2; |
| 3766 | mKeepaliveTracker.handleStopKeepalive(nai, slot, reason); |
| 3767 | break; |
| 3768 | } |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3769 | case EVENT_SYSTEM_READY: { |
Lorenzo Colitti | d260ef2 | 2018-01-24 17:35:07 +0900 | [diff] [blame] | 3770 | mMultipathPolicyTracker.start(); |
Robert Greenwalt | fb68f8f | 2014-08-13 13:43:32 -0700 | [diff] [blame] | 3771 | break; |
| 3772 | } |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3773 | case EVENT_REVALIDATE_NETWORK: { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3774 | handleReportNetworkConnectivity((Network) msg.obj, msg.arg1, toBool(msg.arg2)); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 3775 | break; |
| 3776 | } |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 3777 | case EVENT_PRIVATE_DNS_SETTINGS_CHANGED: |
| 3778 | handlePrivateDnsSettingsChanged(); |
| 3779 | break; |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 3780 | case EVENT_PRIVATE_DNS_VALIDATION_UPDATE: |
| 3781 | handlePrivateDnsValidationUpdate( |
| 3782 | (PrivateDnsValidationUpdate) msg.obj); |
| 3783 | break; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3784 | case EVENT_UID_RULES_CHANGED: |
| 3785 | handleUidRulesChanged(msg.arg1, msg.arg2); |
| 3786 | break; |
| 3787 | case EVENT_DATA_SAVER_CHANGED: |
| 3788 | handleRestrictBackgroundChanged(toBool(msg.arg1)); |
| 3789 | break; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 3790 | case EVENT_TIMEOUT_NOTIFICATION: |
| 3791 | mNotifier.clearNotification(msg.arg1, NotificationType.LOGGED_IN); |
| 3792 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3793 | } |
| 3794 | } |
| 3795 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3796 | |
| 3797 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3798 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3799 | public int tether(String iface, String callerPkg) { |
| 3800 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3801 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3802 | return mTethering.tether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3803 | } else { |
| 3804 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3805 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3806 | } |
| 3807 | |
| 3808 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3809 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3810 | public int untether(String iface, String callerPkg) { |
| 3811 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3812 | |
| 3813 | if (isTetheringSupported()) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 3814 | return mTethering.untether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3815 | } else { |
| 3816 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3817 | } |
| 3818 | } |
| 3819 | |
| 3820 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3821 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3822 | public int getLastTetherError(String iface) { |
| 3823 | enforceTetherAccessPermission(); |
| 3824 | |
| 3825 | if (isTetheringSupported()) { |
| 3826 | return mTethering.getLastTetherError(iface); |
| 3827 | } else { |
| 3828 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3829 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | // TODO - proper iface API for selection by property, inspection, etc |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3833 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3834 | public String[] getTetherableUsbRegexs() { |
| 3835 | enforceTetherAccessPermission(); |
| 3836 | if (isTetheringSupported()) { |
| 3837 | return mTethering.getTetherableUsbRegexs(); |
| 3838 | } else { |
| 3839 | return new String[0]; |
| 3840 | } |
| 3841 | } |
| 3842 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3843 | @Override |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3844 | public String[] getTetherableWifiRegexs() { |
| 3845 | enforceTetherAccessPermission(); |
| 3846 | if (isTetheringSupported()) { |
| 3847 | return mTethering.getTetherableWifiRegexs(); |
| 3848 | } else { |
| 3849 | return new String[0]; |
| 3850 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3851 | } |
| 3852 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3853 | @Override |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 3854 | public String[] getTetherableBluetoothRegexs() { |
| 3855 | enforceTetherAccessPermission(); |
| 3856 | if (isTetheringSupported()) { |
| 3857 | return mTethering.getTetherableBluetoothRegexs(); |
| 3858 | } else { |
| 3859 | return new String[0]; |
| 3860 | } |
| 3861 | } |
| 3862 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3863 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3864 | public int setUsbTethering(boolean enable, String callerPkg) { |
| 3865 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 3866 | if (isTetheringSupported()) { |
| 3867 | return mTethering.setUsbTethering(enable); |
| 3868 | } else { |
| 3869 | return ConnectivityManager.TETHER_ERROR_UNSUPPORTED; |
| 3870 | } |
| 3871 | } |
| 3872 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3873 | // TODO - move iface listing, queries, etc to new module |
| 3874 | // javadoc from interface |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3875 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3876 | public String[] getTetherableIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3877 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3878 | return mTethering.getTetherableIfaces(); |
| 3879 | } |
| 3880 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3881 | @Override |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3882 | public String[] getTetheredIfaces() { |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3883 | enforceTetherAccessPermission(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 3884 | return mTethering.getTetheredIfaces(); |
| 3885 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3886 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3887 | @Override |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 3888 | public String[] getTetheringErroredIfaces() { |
| 3889 | enforceTetherAccessPermission(); |
| 3890 | return mTethering.getErroredIfaces(); |
| 3891 | } |
| 3892 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 3893 | @Override |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 3894 | public String[] getTetheredDhcpRanges() { |
| 3895 | enforceConnectivityInternalPermission(); |
| 3896 | return mTethering.getTetheredDhcpRanges(); |
| 3897 | } |
| 3898 | |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3899 | @Override |
| 3900 | public boolean isTetheringSupported(String callerPkg) { |
| 3901 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3902 | return isTetheringSupported(); |
| 3903 | } |
| 3904 | |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3905 | // if ro.tether.denied = true we default to no tethering |
| 3906 | // gservices could set the secure setting to 1 though to enable it on a build where it |
| 3907 | // had previously been turned off. |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 3908 | private boolean isTetheringSupported() { |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3909 | int defaultVal = encodeBool(!mSystemProperties.get("ro.tether.denied").equals("true")); |
| 3910 | boolean tetherSupported = toBool(Settings.Global.getInt(mContext.getContentResolver(), |
| 3911 | Settings.Global.TETHER_SUPPORTED, defaultVal)); |
| 3912 | boolean tetherEnabledInSettings = tetherSupported |
Julia Reynolds | fc6b2a0 | 2014-06-24 10:56:55 -0400 | [diff] [blame] | 3913 | && !mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING); |
Jeremy Klein | 246a1fe | 2017-03-17 14:30:02 -0700 | [diff] [blame] | 3914 | |
| 3915 | // Elevate to system UID to avoid caller requiring MANAGE_USERS permission. |
| 3916 | boolean adminUser = false; |
| 3917 | final long token = Binder.clearCallingIdentity(); |
| 3918 | try { |
| 3919 | adminUser = mUserManager.isAdminUser(); |
| 3920 | } finally { |
| 3921 | Binder.restoreCallingIdentity(token); |
| 3922 | } |
| 3923 | |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 3924 | return tetherEnabledInSettings && adminUser && mTethering.hasTetherableConfiguration(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 3925 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3926 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3927 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3928 | public void startTethering(int type, ResultReceiver receiver, boolean showProvisioningUi, |
| 3929 | String callerPkg) { |
| 3930 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3931 | if (!isTetheringSupported()) { |
| 3932 | receiver.send(ConnectivityManager.TETHER_ERROR_UNSUPPORTED, null); |
| 3933 | return; |
| 3934 | } |
| 3935 | mTethering.startTethering(type, receiver, showProvisioningUi); |
| 3936 | } |
| 3937 | |
Jeremy Klein | aaa20ad | 2016-01-29 12:06:33 -0800 | [diff] [blame] | 3938 | @Override |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 3939 | public void stopTethering(int type, String callerPkg) { |
| 3940 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 3941 | mTethering.stopTethering(type); |
| 3942 | } |
| 3943 | |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3944 | /** |
| 3945 | * Get the latest value of the tethering entitlement check. |
| 3946 | * |
| 3947 | * Note: Allow privileged apps who have TETHER_PRIVILEGED permission to access. If it turns |
| 3948 | * out some such apps are observed to abuse this API, change to per-UID limits on this API |
| 3949 | * if it's really needed. |
| 3950 | */ |
| 3951 | @Override |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3952 | public void getLatestTetheringEntitlementResult(int type, ResultReceiver receiver, |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3953 | boolean showEntitlementUi, String callerPkg) { |
| 3954 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3955 | mTethering.getLatestTetheringEntitlementResult(type, receiver, showEntitlementUi); |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3956 | } |
| 3957 | |
markchien | 6ea3454 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 3958 | /** Register tethering event callback. */ |
| 3959 | @Override |
| 3960 | public void registerTetheringEventCallback(ITetheringEventCallback callback, |
| 3961 | String callerPkg) { |
| 3962 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3963 | mTethering.registerTetheringEventCallback(callback); |
| 3964 | } |
| 3965 | |
| 3966 | /** Unregister tethering event callback. */ |
| 3967 | @Override |
| 3968 | public void unregisterTetheringEventCallback(ITetheringEventCallback callback, |
| 3969 | String callerPkg) { |
| 3970 | ConnectivityManager.enforceTetherChangePermission(mContext, callerPkg); |
| 3971 | mTethering.unregisterTetheringEventCallback(callback); |
| 3972 | } |
| 3973 | |
Robert Greenwalt | 17c3e0f | 2014-07-02 09:59:16 -0700 | [diff] [blame] | 3974 | // Called when we lose the default network and have no replacement yet. |
| 3975 | // This will automatically be cleared after X seconds or a new default network |
| 3976 | // becomes CONNECTED, whichever happens first. The timer is started by the |
| 3977 | // first caller and not restarted by subsequent callers. |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3978 | private void ensureNetworkTransitionWakelock(String forWhom) { |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3979 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3980 | if (mNetTransitionWakeLock.isHeld()) { |
| 3981 | return; |
| 3982 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3983 | mNetTransitionWakeLock.acquire(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 3984 | mLastWakeLockAcquireTimestamp = SystemClock.elapsedRealtime(); |
| 3985 | mTotalWakelockAcquisitions++; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3986 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3987 | mWakelockLogs.log("ACQUIRE for " + forWhom); |
| 3988 | Message msg = mHandler.obtainMessage(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 3989 | mHandler.sendMessageDelayed(msg, mNetTransitionWakeLockTimeout); |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3990 | } |
Robert Greenwalt | ca4306c | 2010-09-09 13:15:32 -0700 | [diff] [blame] | 3991 | |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3992 | // Called when we gain a new default network to release the network transition wakelock in a |
| 3993 | // second, to allow a grace period for apps to reconnect over the new network. Pending expiry |
| 3994 | // message is cancelled. |
| 3995 | private void scheduleReleaseNetworkTransitionWakelock() { |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3996 | synchronized (this) { |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 3997 | if (!mNetTransitionWakeLock.isHeld()) { |
| 3998 | return; // expiry message released the lock first. |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 3999 | } |
| 4000 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 4001 | // Cancel self timeout on wakelock hold. |
| 4002 | mHandler.removeMessages(EVENT_EXPIRE_NET_TRANSITION_WAKELOCK); |
| 4003 | Message msg = mHandler.obtainMessage(EVENT_CLEAR_NET_TRANSITION_WAKELOCK); |
| 4004 | mHandler.sendMessageDelayed(msg, 1000); |
| 4005 | } |
| 4006 | |
| 4007 | // Called when either message of ensureNetworkTransitionWakelock or |
| 4008 | // scheduleReleaseNetworkTransitionWakelock is processed. |
| 4009 | private void handleReleaseNetworkTransitionWakelock(int eventId) { |
| 4010 | String event = eventName(eventId); |
| 4011 | synchronized (this) { |
| 4012 | if (!mNetTransitionWakeLock.isHeld()) { |
| 4013 | mWakelockLogs.log(String.format("RELEASE: already released (%s)", event)); |
| 4014 | Slog.w(TAG, "expected Net Transition WakeLock to be held"); |
| 4015 | return; |
| 4016 | } |
| 4017 | mNetTransitionWakeLock.release(); |
Hugo Benichi | 26bcfa1 | 2017-09-05 13:25:07 +0900 | [diff] [blame] | 4018 | long lockDuration = SystemClock.elapsedRealtime() - mLastWakeLockAcquireTimestamp; |
| 4019 | mTotalWakelockDurationMs += lockDuration; |
| 4020 | mMaxWakelockDurationMs = Math.max(mMaxWakelockDurationMs, lockDuration); |
| 4021 | mTotalWakelockReleases++; |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 4022 | } |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 4023 | mWakelockLogs.log(String.format("RELEASE (%s)", event)); |
Hugo Benichi | af52d7a | 2017-03-30 10:46:05 +0900 | [diff] [blame] | 4024 | } |
| 4025 | |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 4026 | // 100 percent is full good, 0 is full bad. |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4027 | @Override |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 4028 | public void reportInetCondition(int networkType, int percentage) { |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 4029 | NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType); |
Lorenzo Colitti | 0831f66 | 2015-02-11 07:39:20 +0900 | [diff] [blame] | 4030 | if (nai == null) return; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4031 | reportNetworkConnectivity(nai.network, percentage > 50); |
Robert Greenwalt | 8dcc28b | 2010-09-23 10:05:56 -0700 | [diff] [blame] | 4032 | } |
| 4033 | |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4034 | @Override |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4035 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4036 | enforceAccessPermission(); |
| 4037 | enforceInternetPermission(); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4038 | final int uid = Binder.getCallingUid(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 4039 | final int connectivityInfo = encodeBool(hasConnectivity); |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4040 | mHandler.sendMessage( |
| 4041 | mHandler.obtainMessage(EVENT_REVALIDATE_NETWORK, uid, connectivityInfo, network)); |
| 4042 | } |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4043 | |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4044 | private void handleReportNetworkConnectivity( |
| 4045 | Network network, int uid, boolean hasConnectivity) { |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4046 | final NetworkAgentInfo nai; |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4047 | if (network == null) { |
| 4048 | nai = getDefaultNetwork(); |
| 4049 | } else { |
| 4050 | nai = getNetworkAgentInfoForNetwork(network); |
| 4051 | } |
Paul Jensen | 4e8050e | 2015-06-25 10:28:34 -0400 | [diff] [blame] | 4052 | if (nai == null || nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTING || |
| 4053 | nai.networkInfo.getState() == NetworkInfo.State.DISCONNECTED) { |
| 4054 | return; |
| 4055 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4056 | // Revalidate if the app report does not match our current validated state. |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4057 | if (hasConnectivity == nai.lastValidated) { |
| 4058 | return; |
| 4059 | } |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4060 | if (DBG) { |
Hugo Benichi | 1c51d7a | 2017-04-06 17:22:18 +0900 | [diff] [blame] | 4061 | int netid = nai.network.netId; |
| 4062 | log("reportNetworkConnectivity(" + netid + ", " + hasConnectivity + ") by " + uid); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 4063 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4064 | // Validating a network that has not yet connected could result in a call to |
| 4065 | // rematchNetworkAndRequests() which is not meant to work on such networks. |
| 4066 | if (!nai.everConnected) { |
| 4067 | return; |
Paul Jensen | 7ccd3df | 2014-08-29 09:54:01 -0400 | [diff] [blame] | 4068 | } |
Hugo Benichi | 0fd4af9 | 2017-04-06 16:01:44 +0900 | [diff] [blame] | 4069 | LinkProperties lp = getLinkProperties(nai); |
| 4070 | if (isNetworkWithLinkPropertiesBlocked(lp, uid, false)) { |
| 4071 | return; |
| 4072 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 4073 | try { |
| 4074 | nai.networkMonitor().forceReevaluation(uid); |
| 4075 | } catch (RemoteException e) { |
| 4076 | e.rethrowFromSystemServer(); |
| 4077 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4078 | } |
| 4079 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4080 | /** |
| 4081 | * Returns information about the proxy a certain network is using. If given a null network, it |
| 4082 | * it will return the proxy for the bound network for the caller app or the default proxy if |
| 4083 | * none. |
| 4084 | * |
| 4085 | * @param network the network we want to get the proxy information for. |
| 4086 | * @return Proxy information if a network has a proxy configured, or otherwise null. |
| 4087 | */ |
Lorenzo Colitti | 1866028 | 2016-07-04 12:55:44 +0900 | [diff] [blame] | 4088 | @Override |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4089 | public ProxyInfo getProxyForNetwork(Network network) { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4090 | final ProxyInfo globalProxy = mProxyTracker.getGlobalProxy(); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4091 | if (globalProxy != null) return globalProxy; |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4092 | if (network == null) { |
| 4093 | // Get the network associated with the calling UID. |
| 4094 | final Network activeNetwork = getActiveNetworkForUidInternal(Binder.getCallingUid(), |
| 4095 | true); |
| 4096 | if (activeNetwork == null) { |
| 4097 | return null; |
| 4098 | } |
| 4099 | return getLinkPropertiesProxyInfo(activeNetwork); |
| 4100 | } else if (queryUserAccess(Binder.getCallingUid(), network.netId)) { |
| 4101 | // Don't call getLinkProperties() as it requires ACCESS_NETWORK_STATE permission, which |
| 4102 | // caller may not have. |
| 4103 | return getLinkPropertiesProxyInfo(network); |
| 4104 | } |
| 4105 | // No proxy info available if the calling UID does not have network access. |
| 4106 | return null; |
| 4107 | } |
| 4108 | |
| 4109 | @VisibleForTesting |
| 4110 | protected boolean queryUserAccess(int uid, int netId) { |
| 4111 | return NetworkUtils.queryUserAccess(uid, netId); |
| 4112 | } |
| 4113 | |
| 4114 | private ProxyInfo getLinkPropertiesProxyInfo(Network network) { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4115 | final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); |
| 4116 | if (nai == null) return null; |
| 4117 | synchronized (nai) { |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4118 | final ProxyInfo linkHttpProxy = nai.linkProperties.getHttpProxy(); |
| 4119 | return linkHttpProxy == null ? null : new ProxyInfo(linkHttpProxy); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 4120 | } |
| 4121 | } |
| 4122 | |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 4123 | @Override |
| 4124 | public void setGlobalProxy(final ProxyInfo proxyProperties) { |
Robert Greenwalt | a9bebc2 | 2013-04-10 15:32:18 -0700 | [diff] [blame] | 4125 | enforceConnectivityInternalPermission(); |
Chalard Jean | 3c443fc | 2018-06-07 18:37:59 +0900 | [diff] [blame] | 4126 | mProxyTracker.setGlobalProxy(proxyProperties); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4129 | @Override |
| 4130 | @Nullable |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 4131 | public ProxyInfo getGlobalProxy() { |
Chalard Jean | 5afbc83 | 2018-06-07 18:02:37 +0900 | [diff] [blame] | 4132 | return mProxyTracker.getGlobalProxy(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4133 | } |
| 4134 | |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 4135 | private void handleApplyDefaultProxy(ProxyInfo proxy) { |
Jason Monk | 602b232 | 2013-07-03 17:04:33 -0400 | [diff] [blame] | 4136 | if (proxy != null && TextUtils.isEmpty(proxy.getHost()) |
Geoffrey Borggaard | 79adc95 | 2014-11-20 14:35:32 -0500 | [diff] [blame] | 4137 | && Uri.EMPTY.equals(proxy.getPacFileUrl())) { |
Chia-chi Yeh | 4c12a47 | 2011-10-03 15:34:04 -0700 | [diff] [blame] | 4138 | proxy = null; |
| 4139 | } |
Chalard Jean | daab8f9 | 2018-06-08 12:20:15 +0900 | [diff] [blame] | 4140 | mProxyTracker.setDefaultProxy(proxy); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4141 | } |
| 4142 | |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 4143 | // If the proxy has changed from oldLp to newLp, resend proxy broadcast. This method gets called |
| 4144 | // when any network changes proxy. |
| 4145 | // TODO: Remove usage of broadcast extras as they are deprecated and not applicable in a |
| 4146 | // 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] | 4147 | private void updateProxy(LinkProperties newLp, LinkProperties oldLp) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4148 | ProxyInfo newProxyInfo = newLp == null ? null : newLp.getHttpProxy(); |
| 4149 | ProxyInfo oldProxyInfo = oldLp == null ? null : oldLp.getHttpProxy(); |
| 4150 | |
Chalard Jean | 4949dd7 | 2018-06-07 17:41:29 +0900 | [diff] [blame] | 4151 | if (!ProxyTracker.proxyInfoEqual(newProxyInfo, oldProxyInfo)) { |
Chalard Jean | b50a276 | 2018-06-08 19:46:44 +0900 | [diff] [blame] | 4152 | mProxyTracker.sendProxyBroadcast(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4153 | } |
| 4154 | } |
| 4155 | |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4156 | private static class SettingsObserver extends ContentObserver { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4157 | final private HashMap<Uri, Integer> mUriEventMap; |
| 4158 | final private Context mContext; |
| 4159 | final private Handler mHandler; |
| 4160 | |
| 4161 | SettingsObserver(Context context, Handler handler) { |
| 4162 | super(null); |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4163 | mUriEventMap = new HashMap<>(); |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4164 | mContext = context; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4165 | mHandler = handler; |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4166 | } |
| 4167 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4168 | void observe(Uri uri, int what) { |
| 4169 | mUriEventMap.put(uri, what); |
| 4170 | final ContentResolver resolver = mContext.getContentResolver(); |
| 4171 | resolver.registerContentObserver(uri, false, this); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4172 | } |
| 4173 | |
| 4174 | @Override |
| 4175 | public void onChange(boolean selfChange) { |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 4176 | Slog.wtf(TAG, "Should never be reached."); |
| 4177 | } |
| 4178 | |
| 4179 | @Override |
| 4180 | public void onChange(boolean selfChange, Uri uri) { |
| 4181 | final Integer what = mUriEventMap.get(uri); |
| 4182 | if (what != null) { |
| 4183 | mHandler.obtainMessage(what.intValue()).sendToTarget(); |
| 4184 | } else { |
| 4185 | loge("No matching event to send for URI=" + uri); |
| 4186 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4189 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 4190 | private static void log(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4191 | Slog.d(TAG, s); |
| 4192 | } |
| 4193 | |
Jeff Sharkey | fb878b6 | 2012-07-26 18:32:30 -0700 | [diff] [blame] | 4194 | private static void loge(String s) { |
Wink Saville | ed9c02b | 2010-12-03 12:01:38 -0800 | [diff] [blame] | 4195 | Slog.e(TAG, s); |
| 4196 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4197 | |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 4198 | private static void loge(String s, Throwable t) { |
| 4199 | Slog.e(TAG, s, t); |
| 4200 | } |
| 4201 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4202 | /** |
Jeff Davidson | 11008a7 | 2014-11-20 13:12:46 -0800 | [diff] [blame] | 4203 | * Prepare for a VPN application. |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4204 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 4205 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 4206 | * |
| 4207 | * @param oldPackage Package name of the application which currently controls VPN, which will |
| 4208 | * be replaced. If there is no such application, this should should either be |
| 4209 | * {@code null} or {@link VpnConfig.LEGACY_VPN}. |
| 4210 | * @param newPackage Package name of the application which should gain control of VPN, or |
| 4211 | * {@code null} to disable. |
| 4212 | * @param userId User for whom to prepare the new VPN. |
| 4213 | * |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4214 | * @hide |
| 4215 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4216 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4217 | public boolean prepareVpn(@Nullable String oldPackage, @Nullable String newPackage, |
| 4218 | int userId) { |
| 4219 | enforceCrossUserPermission(userId); |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4220 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4221 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4222 | throwIfLockdownEnabled(); |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4223 | Vpn vpn = mVpns.get(userId); |
| 4224 | if (vpn != null) { |
| 4225 | return vpn.prepare(oldPackage, newPackage); |
| 4226 | } else { |
| 4227 | return false; |
| 4228 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4229 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4230 | } |
| 4231 | |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4232 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4233 | * Set whether the VPN package has the ability to launch VPNs without user intervention. |
| 4234 | * This method is used by system-privileged apps. |
| 4235 | * VPN permissions are checked in the {@link Vpn} class. If the caller is not {@code userId}, |
| 4236 | * {@link android.Manifest.permission.INTERACT_ACROSS_USERS_FULL} permission is required. |
| 4237 | * |
| 4238 | * @param packageName The package for which authorization state should change. |
| 4239 | * @param userId User for whom {@code packageName} is installed. |
| 4240 | * @param authorized {@code true} if this app should be able to start a VPN connection without |
| 4241 | * explicit user approval, {@code false} if not. |
| 4242 | * |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4243 | * @hide |
| 4244 | */ |
| 4245 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4246 | public void setVpnPackageAuthorization(String packageName, int userId, boolean authorized) { |
| 4247 | enforceCrossUserPermission(userId); |
| 4248 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4249 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4250 | Vpn vpn = mVpns.get(userId); |
| 4251 | if (vpn != null) { |
| 4252 | vpn.setPackageAuthorization(packageName, authorized); |
| 4253 | } |
Jeff Davidson | 0554260 | 2014-08-11 14:07:27 -0700 | [diff] [blame] | 4254 | } |
| 4255 | } |
| 4256 | |
| 4257 | /** |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4258 | * Configure a TUN interface and return its file descriptor. Parameters |
| 4259 | * 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] | 4260 | * and not available in ConnectivityManager. Permissions are checked in |
| 4261 | * Vpn class. |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4262 | * @hide |
| 4263 | */ |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4264 | @Override |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 4265 | public ParcelFileDescriptor establishVpn(VpnConfig config) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4266 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4267 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4268 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4269 | return mVpns.get(user).establish(config); |
| 4270 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4271 | } |
| 4272 | |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4273 | /** |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4274 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 4275 | * secondary thread to perform connection work, returning quickly. |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4276 | */ |
| 4277 | @Override |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4278 | public void startLegacyVpn(VpnProfile profile) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4279 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 4280 | final LinkProperties egress = getActiveLinkProperties(); |
| 4281 | if (egress == null) { |
| 4282 | throw new IllegalStateException("Missing active network connection"); |
| 4283 | } |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4284 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4285 | throwIfLockdownEnabled(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4286 | mVpns.get(user).startLegacyVpn(profile, mKeyStore, egress); |
| 4287 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /** |
| 4291 | * Return the information of the ongoing legacy VPN. This method is used |
| 4292 | * by VpnSettings and not available in ConnectivityManager. Permissions |
| 4293 | * are checked in Vpn class. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 4294 | */ |
| 4295 | @Override |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4296 | public LegacyVpnInfo getLegacyVpnInfo(int userId) { |
| 4297 | enforceCrossUserPermission(userId); |
Hung-ying Tyan | 44c8c5c | 2015-07-29 12:39:21 +0800 | [diff] [blame] | 4298 | |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4299 | synchronized (mVpns) { |
Robin Lee | 3eed5ec | 2015-07-07 12:28:13 -0700 | [diff] [blame] | 4300 | return mVpns.get(userId).getLegacyVpnInfo(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4301 | } |
Chia-chi Yeh | 77fd485 | 2011-07-02 17:15:00 -0700 | [diff] [blame] | 4302 | } |
| 4303 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 4304 | /** |
Varun Anand | 02c50ef | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 4305 | * Return the information of all ongoing VPNs. |
| 4306 | * |
| 4307 | * <p>This method is used to update NetworkStatsService. |
| 4308 | * |
| 4309 | * <p>Must be called on the handler thread. |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4310 | */ |
Varun Anand | 02c50ef | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 4311 | private VpnInfo[] getAllVpnInfo() { |
| 4312 | ensureRunningOnConnectivityServiceThread(); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4313 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4314 | if (mLockdownEnabled) { |
| 4315 | return new VpnInfo[0]; |
| 4316 | } |
| 4317 | |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 4318 | List<VpnInfo> infoList = new ArrayList<>(); |
| 4319 | for (int i = 0; i < mVpns.size(); i++) { |
| 4320 | VpnInfo info = createVpnInfo(mVpns.valueAt(i)); |
| 4321 | if (info != null) { |
| 4322 | infoList.add(info); |
| 4323 | } |
| 4324 | } |
| 4325 | return infoList.toArray(new VpnInfo[infoList.size()]); |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | /** |
| 4330 | * @return VPN information for accounting, or null if we can't retrieve all required |
| 4331 | * information, e.g primary underlying iface. |
| 4332 | */ |
| 4333 | @Nullable |
| 4334 | private VpnInfo createVpnInfo(Vpn vpn) { |
| 4335 | VpnInfo info = vpn.getVpnInfo(); |
| 4336 | if (info == null) { |
| 4337 | return null; |
| 4338 | } |
| 4339 | Network[] underlyingNetworks = vpn.getUnderlyingNetworks(); |
| 4340 | // see VpnService.setUnderlyingNetworks()'s javadoc about how to interpret |
| 4341 | // the underlyingNetworks list. |
| 4342 | if (underlyingNetworks == null) { |
| 4343 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 4344 | if (defaultNetwork != null && defaultNetwork.linkProperties != null) { |
| 4345 | info.primaryUnderlyingIface = getDefaultNetwork().linkProperties.getInterfaceName(); |
| 4346 | } |
| 4347 | } else if (underlyingNetworks.length > 0) { |
| 4348 | LinkProperties linkProperties = getLinkProperties(underlyingNetworks[0]); |
| 4349 | if (linkProperties != null) { |
| 4350 | info.primaryUnderlyingIface = linkProperties.getInterfaceName(); |
| 4351 | } |
| 4352 | } |
| 4353 | return info.primaryUnderlyingIface == null ? null : info; |
| 4354 | } |
| 4355 | |
| 4356 | /** |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4357 | * Returns the information of the ongoing VPN for {@code userId}. This method is used by |
| 4358 | * VpnDialogs and not available in ConnectivityManager. |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4359 | * Permissions are checked in Vpn class. |
| 4360 | * @hide |
| 4361 | */ |
| 4362 | @Override |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 4363 | public VpnConfig getVpnConfig(int userId) { |
| 4364 | enforceCrossUserPermission(userId); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4365 | synchronized (mVpns) { |
Robin Lee | 4728345 | 2015-06-01 10:57:03 -0700 | [diff] [blame] | 4366 | Vpn vpn = mVpns.get(userId); |
| 4367 | if (vpn != null) { |
| 4368 | return vpn.getVpnConfig(); |
| 4369 | } else { |
| 4370 | return null; |
| 4371 | } |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 4372 | } |
| 4373 | } |
| 4374 | |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4375 | /** |
| 4376 | * Ask all VPN objects to recompute and update their capabilities. |
| 4377 | * |
| 4378 | * When underlying networks change, VPNs may have to update capabilities to reflect things |
| 4379 | * like the metered bit, their transports, and so on. This asks the VPN objects to update |
| 4380 | * their capabilities, and as this will cause them to send messages to the ConnectivityService |
| 4381 | * handler thread through their agent, this is asynchronous. When the capabilities objects |
| 4382 | * are computed they will be up-to-date as they are computed synchronously from here and |
| 4383 | * this is running on the ConnectivityService thread. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4384 | */ |
| 4385 | private void updateAllVpnsCapabilities() { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4386 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4387 | synchronized (mVpns) { |
| 4388 | for (int i = 0; i < mVpns.size(); i++) { |
| 4389 | final Vpn vpn = mVpns.valueAt(i); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4390 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4391 | updateVpnCapabilities(vpn, nc); |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 4392 | } |
| 4393 | } |
| 4394 | } |
| 4395 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4396 | private void updateVpnCapabilities(Vpn vpn, @Nullable NetworkCapabilities nc) { |
| 4397 | ensureRunningOnConnectivityServiceThread(); |
| 4398 | NetworkAgentInfo vpnNai = getNetworkAgentInfoForNetId(vpn.getNetId()); |
| 4399 | if (vpnNai == null || nc == null) { |
| 4400 | return; |
| 4401 | } |
| 4402 | updateCapabilities(vpnNai.getCurrentScore(), vpnNai, nc); |
| 4403 | } |
| 4404 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4405 | @Override |
| 4406 | public boolean updateLockdownVpn() { |
Jeff Sharkey | 3671b1e | 2013-01-31 17:22:26 -0800 | [diff] [blame] | 4407 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 4408 | Slog.w(TAG, "Lockdown VPN only available to AID_SYSTEM"); |
| 4409 | return false; |
| 4410 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4411 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4412 | synchronized (mVpns) { |
| 4413 | // Tear down existing lockdown if profile was removed |
| 4414 | mLockdownEnabled = LockdownVpnTracker.isEnabled(); |
| 4415 | if (mLockdownEnabled) { |
| 4416 | byte[] profileTag = mKeyStore.get(Credentials.LOCKDOWN_VPN); |
| 4417 | if (profileTag == null) { |
| 4418 | Slog.e(TAG, "Lockdown VPN configured but cannot be read from keystore"); |
| 4419 | return false; |
| 4420 | } |
| 4421 | String profileName = new String(profileTag); |
| 4422 | final VpnProfile profile = VpnProfile.decode( |
| 4423 | profileName, mKeyStore.get(Credentials.VPN + profileName)); |
| 4424 | if (profile == null) { |
| 4425 | Slog.e(TAG, "Lockdown VPN configured invalid profile " + profileName); |
| 4426 | setLockdownTracker(null); |
| 4427 | return true; |
| 4428 | } |
| 4429 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Robin Lee | 18566c1 | 2016-03-14 13:08:48 +0000 | [diff] [blame] | 4430 | Vpn vpn = mVpns.get(user); |
| 4431 | if (vpn == null) { |
| 4432 | Slog.w(TAG, "VPN for user " + user + " not ready yet. Skipping lockdown"); |
| 4433 | return false; |
| 4434 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4435 | setLockdownTracker(new LockdownVpnTracker(mContext, mNMS, this, vpn, profile)); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4436 | } else { |
| 4437 | setLockdownTracker(null); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4438 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4439 | } |
| 4440 | |
| 4441 | return true; |
| 4442 | } |
| 4443 | |
| 4444 | /** |
| 4445 | * Internally set new {@link LockdownVpnTracker}, shutting down any existing |
| 4446 | * {@link LockdownVpnTracker}. Can be {@code null} to disable lockdown. |
| 4447 | */ |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4448 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4449 | private void setLockdownTracker(LockdownVpnTracker tracker) { |
| 4450 | // Shutdown any existing tracker |
| 4451 | final LockdownVpnTracker existing = mLockdownTracker; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 4452 | // TODO: Add a trigger when the always-on VPN enable/disable to reevaluate and send the |
| 4453 | // necessary onBlockedStatusChanged callbacks. |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4454 | mLockdownTracker = null; |
| 4455 | if (existing != null) { |
| 4456 | existing.shutdown(); |
| 4457 | } |
| 4458 | |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 4459 | if (tracker != null) { |
| 4460 | mLockdownTracker = tracker; |
| 4461 | mLockdownTracker.init(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4462 | } |
| 4463 | } |
| 4464 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4465 | @GuardedBy("mVpns") |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 4466 | private void throwIfLockdownEnabled() { |
| 4467 | if (mLockdownEnabled) { |
| 4468 | throw new IllegalStateException("Unavailable in lockdown mode"); |
| 4469 | } |
| 4470 | } |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 4471 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4472 | /** |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4473 | * Starts the always-on VPN {@link VpnService} for user {@param userId}, which should perform |
| 4474 | * some setup and then call {@code establish()} to connect. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4475 | * |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4476 | * @return {@code true} if the service was started, the service was already connected, or there |
| 4477 | * was no always-on VPN to start. {@code false} otherwise. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4478 | */ |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4479 | private boolean startAlwaysOnVpn(int userId) { |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4480 | synchronized (mVpns) { |
| 4481 | Vpn vpn = mVpns.get(userId); |
| 4482 | if (vpn == null) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4483 | // 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] | 4484 | // exists already. |
| 4485 | Slog.wtf(TAG, "User " + userId + " has no Vpn configuration"); |
| 4486 | return false; |
| 4487 | } |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4488 | |
Robin Lee | 812800c | 2016-05-13 15:38:08 +0100 | [diff] [blame] | 4489 | return vpn.startAlwaysOnVpn(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4490 | } |
| 4491 | } |
| 4492 | |
| 4493 | @Override |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 4494 | public boolean isAlwaysOnVpnPackageSupported(int userId, String packageName) { |
| 4495 | enforceSettingsPermission(); |
| 4496 | enforceCrossUserPermission(userId); |
| 4497 | |
| 4498 | synchronized (mVpns) { |
| 4499 | Vpn vpn = mVpns.get(userId); |
| 4500 | if (vpn == null) { |
| 4501 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4502 | return false; |
| 4503 | } |
| 4504 | return vpn.isAlwaysOnPackageSupported(packageName); |
| 4505 | } |
| 4506 | } |
| 4507 | |
| 4508 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4509 | public boolean setAlwaysOnVpnPackage( |
| 4510 | int userId, String packageName, boolean lockdown, List<String> lockdownWhitelist) { |
| 4511 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4512 | enforceCrossUserPermission(userId); |
| 4513 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4514 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4515 | // Can't set always-on VPN if legacy VPN is already in lockdown mode. |
| 4516 | if (LockdownVpnTracker.isEnabled()) { |
| 4517 | return false; |
| 4518 | } |
| 4519 | |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4520 | Vpn vpn = mVpns.get(userId); |
| 4521 | if (vpn == null) { |
| 4522 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4523 | return false; |
| 4524 | } |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4525 | if (!vpn.setAlwaysOnPackage(packageName, lockdown, lockdownWhitelist)) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4526 | return false; |
| 4527 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4528 | if (!startAlwaysOnVpn(userId)) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4529 | vpn.setAlwaysOnPackage(null, false, null); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4530 | return false; |
| 4531 | } |
| 4532 | } |
| 4533 | return true; |
| 4534 | } |
| 4535 | |
| 4536 | @Override |
| 4537 | public String getAlwaysOnVpnPackage(int userId) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4538 | enforceControlAlwaysOnVpnPermission(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 4539 | enforceCrossUserPermission(userId); |
| 4540 | |
| 4541 | synchronized (mVpns) { |
| 4542 | Vpn vpn = mVpns.get(userId); |
| 4543 | if (vpn == null) { |
| 4544 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4545 | return null; |
| 4546 | } |
| 4547 | return vpn.getAlwaysOnPackage(); |
| 4548 | } |
| 4549 | } |
| 4550 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4551 | @Override |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4552 | public boolean isVpnLockdownEnabled(int userId) { |
| 4553 | enforceControlAlwaysOnVpnPermission(); |
| 4554 | enforceCrossUserPermission(userId); |
| 4555 | |
| 4556 | synchronized (mVpns) { |
| 4557 | Vpn vpn = mVpns.get(userId); |
| 4558 | if (vpn == null) { |
| 4559 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4560 | return false; |
| 4561 | } |
| 4562 | return vpn.getLockdown(); |
| 4563 | } |
| 4564 | } |
| 4565 | |
| 4566 | @Override |
| 4567 | public List<String> getVpnLockdownWhitelist(int userId) { |
| 4568 | enforceControlAlwaysOnVpnPermission(); |
| 4569 | enforceCrossUserPermission(userId); |
| 4570 | |
| 4571 | synchronized (mVpns) { |
| 4572 | Vpn vpn = mVpns.get(userId); |
| 4573 | if (vpn == null) { |
| 4574 | Slog.w(TAG, "User " + userId + " has no Vpn configuration"); |
| 4575 | return null; |
| 4576 | } |
| 4577 | return vpn.getLockdownWhitelist(); |
| 4578 | } |
| 4579 | } |
| 4580 | |
| 4581 | @Override |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4582 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4583 | // TODO: Remove? Any reason to trigger a provisioning check? |
| 4584 | return -1; |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4585 | } |
| 4586 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4587 | /** Location to an updatable file listing carrier provisioning urls. |
| 4588 | * An example: |
| 4589 | * |
| 4590 | * <?xml version="1.0" encoding="utf-8"?> |
| 4591 | * <provisioningUrls> |
| 4592 | * <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] | 4593 | * </provisioningUrls> |
| 4594 | */ |
| 4595 | private static final String PROVISIONING_URL_PATH = |
| 4596 | "/data/misc/radio/provisioning_urls.xml"; |
| 4597 | private final File mProvisioningUrlFile = new File(PROVISIONING_URL_PATH); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4598 | |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4599 | /** XML tag for root element. */ |
| 4600 | private static final String TAG_PROVISIONING_URLS = "provisioningUrls"; |
| 4601 | /** XML tag for individual url */ |
| 4602 | private static final String TAG_PROVISIONING_URL = "provisioningUrl"; |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4603 | /** XML attribute for mcc */ |
| 4604 | private static final String ATTR_MCC = "mcc"; |
| 4605 | /** XML attribute for mnc */ |
| 4606 | private static final String ATTR_MNC = "mnc"; |
| 4607 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4608 | private String getProvisioningUrlBaseFromFile() { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4609 | FileReader fileReader = null; |
| 4610 | XmlPullParser parser = null; |
| 4611 | Configuration config = mContext.getResources().getConfiguration(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4612 | |
| 4613 | try { |
| 4614 | fileReader = new FileReader(mProvisioningUrlFile); |
| 4615 | parser = Xml.newPullParser(); |
| 4616 | parser.setInput(fileReader); |
| 4617 | XmlUtils.beginDocument(parser, TAG_PROVISIONING_URLS); |
| 4618 | |
| 4619 | while (true) { |
| 4620 | XmlUtils.nextElement(parser); |
| 4621 | |
| 4622 | String element = parser.getName(); |
| 4623 | if (element == null) break; |
| 4624 | |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4625 | if (element.equals(TAG_PROVISIONING_URL)) { |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4626 | String mcc = parser.getAttributeValue(null, ATTR_MCC); |
| 4627 | try { |
| 4628 | if (mcc != null && Integer.parseInt(mcc) == config.mcc) { |
| 4629 | String mnc = parser.getAttributeValue(null, ATTR_MNC); |
| 4630 | if (mnc != null && Integer.parseInt(mnc) == config.mnc) { |
| 4631 | parser.next(); |
| 4632 | if (parser.getEventType() == XmlPullParser.TEXT) { |
| 4633 | return parser.getText(); |
| 4634 | } |
| 4635 | } |
| 4636 | } |
| 4637 | } catch (NumberFormatException e) { |
| 4638 | loge("NumberFormatException in getProvisioningUrlBaseFromFile: " + e); |
| 4639 | } |
| 4640 | } |
| 4641 | } |
| 4642 | return null; |
| 4643 | } catch (FileNotFoundException e) { |
| 4644 | loge("Carrier Provisioning Urls file not found"); |
| 4645 | } catch (XmlPullParserException e) { |
| 4646 | loge("Xml parser exception reading Carrier Provisioning Urls file: " + e); |
| 4647 | } catch (IOException e) { |
| 4648 | loge("I/O exception reading Carrier Provisioning Urls file: " + e); |
| 4649 | } finally { |
| 4650 | if (fileReader != null) { |
| 4651 | try { |
| 4652 | fileReader.close(); |
| 4653 | } catch (IOException e) {} |
| 4654 | } |
| 4655 | } |
| 4656 | return null; |
| 4657 | } |
| 4658 | |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4659 | @Override |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4660 | public String getMobileProvisioningUrl() { |
| 4661 | enforceConnectivityInternalPermission(); |
Paul Jensen | 434dde8 | 2015-06-11 09:43:30 -0400 | [diff] [blame] | 4662 | String url = getProvisioningUrlBaseFromFile(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4663 | if (TextUtils.isEmpty(url)) { |
| 4664 | url = mContext.getResources().getString(R.string.mobile_provisioning_url); |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4665 | log("getMobileProvisioningUrl: mobile_provisioining_url from resource =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4666 | } else { |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 4667 | log("getMobileProvisioningUrl: mobile_provisioning_url from File =" + url); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 4668 | } |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4669 | // populate the iccid, imei and phone number in the provisioning url. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4670 | if (!TextUtils.isEmpty(url)) { |
Wink Saville | 8cf3560 | 2013-07-10 23:00:07 -0700 | [diff] [blame] | 4671 | String phoneNumber = mTelephonyManager.getLine1Number(); |
| 4672 | if (TextUtils.isEmpty(phoneNumber)) { |
| 4673 | phoneNumber = "0000000000"; |
| 4674 | } |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4675 | url = String.format(url, |
| 4676 | mTelephonyManager.getSimSerialNumber() /* ICCID */, |
| 4677 | mTelephonyManager.getDeviceId() /* IMEI */, |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4678 | phoneNumber /* Phone number */); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4679 | } |
| 4680 | |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 4681 | return url; |
| 4682 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4683 | |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4684 | @Override |
| 4685 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4686 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4687 | enforceConnectivityInternalPermission(); |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 4688 | if (!ConnectivityManager.isNetworkTypeValid(networkType)) { |
| 4689 | return; |
| 4690 | } |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4691 | final long ident = Binder.clearCallingIdentity(); |
| 4692 | try { |
Lorenzo Colitti | 0b59906 | 2016-08-22 22:36:19 +0900 | [diff] [blame] | 4693 | // Concatenate the range of types onto the range of NetIDs. |
| 4694 | int id = MAX_NET_ID + 1 + (networkType - ConnectivityManager.TYPE_NONE); |
| 4695 | mNotifier.setProvNotificationVisible(visible, id, action); |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 4696 | } finally { |
| 4697 | Binder.restoreCallingIdentity(ident); |
| 4698 | } |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 4699 | } |
Wink Saville | 7788c61 | 2013-08-29 14:57:08 -0700 | [diff] [blame] | 4700 | |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4701 | @Override |
| 4702 | public void setAirplaneMode(boolean enable) { |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 4703 | enforceNetworkStackSettingsOrSetup(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4704 | final long ident = Binder.clearCallingIdentity(); |
| 4705 | try { |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4706 | final ContentResolver cr = mContext.getContentResolver(); |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 4707 | Settings.Global.putInt(cr, Settings.Global.AIRPLANE_MODE_ON, encodeBool(enable)); |
Yuhao Zheng | 5530e4b | 2013-09-11 09:36:41 -0700 | [diff] [blame] | 4708 | Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); |
| 4709 | intent.putExtra("state", enable); |
xinhe | 98e25fc | 2014-11-17 11:35:01 -0800 | [diff] [blame] | 4710 | mContext.sendBroadcastAsUser(intent, UserHandle.ALL); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 4711 | } finally { |
| 4712 | Binder.restoreCallingIdentity(ident); |
| 4713 | } |
| 4714 | } |
| 4715 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4716 | private void onUserStart(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4717 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4718 | Vpn userVpn = mVpns.get(userId); |
| 4719 | if (userVpn != null) { |
| 4720 | loge("Starting user already has a VPN"); |
| 4721 | return; |
| 4722 | } |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 4723 | userVpn = new Vpn(mHandler.getLooper(), mContext, mNMS, userId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4724 | mVpns.put(userId, userVpn); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4725 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4726 | updateLockdownVpn(); |
| 4727 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4728 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | private void onUserStop(int userId) { |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4732 | synchronized (mVpns) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4733 | Vpn userVpn = mVpns.get(userId); |
| 4734 | if (userVpn == null) { |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4735 | loge("Stopped user has no VPN"); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4736 | return; |
| 4737 | } |
Robin Lee | 17e6183 | 2016-05-09 13:46:28 +0100 | [diff] [blame] | 4738 | userVpn.onUserStopped(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4739 | mVpns.delete(userId); |
| 4740 | } |
| 4741 | } |
| 4742 | |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4743 | private void onUserAdded(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4744 | mPermissionMonitor.onUserAdded(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.onUserAdded(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 | |
| 4757 | private void onUserRemoved(int userId) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4758 | mPermissionMonitor.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4759 | Network defaultNetwork = getNetwork(getDefaultNetwork()); |
Hugo Benichi | 20035e0 | 2017-04-26 14:53:28 +0900 | [diff] [blame] | 4760 | synchronized (mVpns) { |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4761 | final int vpnsSize = mVpns.size(); |
| 4762 | for (int i = 0; i < vpnsSize; i++) { |
| 4763 | Vpn vpn = mVpns.valueAt(i); |
| 4764 | vpn.onUserRemoved(userId); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4765 | NetworkCapabilities nc = vpn.updateCapabilities(defaultNetwork); |
| 4766 | updateVpnCapabilities(vpn, nc); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4767 | } |
| 4768 | } |
| 4769 | } |
| 4770 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4771 | private void onPackageAdded(String packageName, int uid) { |
| 4772 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4773 | Slog.wtf(TAG, "Invalid package in onPackageAdded: " + packageName + " | " + uid); |
| 4774 | return; |
| 4775 | } |
| 4776 | mPermissionMonitor.onPackageAdded(packageName, uid); |
| 4777 | } |
| 4778 | |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4779 | private void onPackageReplaced(String packageName, int uid) { |
| 4780 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4781 | Slog.wtf(TAG, "Invalid package in onPackageReplaced: " + packageName + " | " + uid); |
| 4782 | return; |
| 4783 | } |
| 4784 | final int userId = UserHandle.getUserId(uid); |
| 4785 | synchronized (mVpns) { |
| 4786 | final Vpn vpn = mVpns.get(userId); |
| 4787 | if (vpn == null) { |
| 4788 | return; |
| 4789 | } |
| 4790 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4791 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName)) { |
| 4792 | Slog.d(TAG, "Restarting always-on VPN package " + packageName + " for user " |
| 4793 | + userId); |
| 4794 | vpn.startAlwaysOnVpn(); |
| 4795 | } |
| 4796 | } |
| 4797 | } |
| 4798 | |
| 4799 | private void onPackageRemoved(String packageName, int uid, boolean isReplacing) { |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4800 | if (TextUtils.isEmpty(packageName) || uid < 0) { |
| 4801 | Slog.wtf(TAG, "Invalid package in onPackageRemoved: " + packageName + " | " + uid); |
| 4802 | return; |
| 4803 | } |
| 4804 | mPermissionMonitor.onPackageRemoved(uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4805 | |
| 4806 | final int userId = UserHandle.getUserId(uid); |
| 4807 | synchronized (mVpns) { |
| 4808 | final Vpn vpn = mVpns.get(userId); |
| 4809 | if (vpn == null) { |
| 4810 | return; |
| 4811 | } |
| 4812 | // Legacy always-on VPN won't be affected since the package name is not set. |
| 4813 | if (TextUtils.equals(vpn.getAlwaysOnPackage(), packageName) && !isReplacing) { |
| 4814 | Slog.d(TAG, "Removing always-on VPN package " + packageName + " for user " |
| 4815 | + userId); |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 4816 | vpn.setAlwaysOnPackage(null, false, null); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4817 | } |
| 4818 | } |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4819 | } |
| 4820 | |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4821 | private void onUserUnlocked(int userId) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 4822 | synchronized (mVpns) { |
| 4823 | // User present may be sent because of an unlock, which might mean an unlocked keystore. |
| 4824 | if (mUserManager.getUserInfo(userId).isPrimary() && LockdownVpnTracker.isEnabled()) { |
| 4825 | updateLockdownVpn(); |
| 4826 | } else { |
| 4827 | startAlwaysOnVpn(userId); |
| 4828 | } |
Robin Lee | 9a5f485 | 2015-12-17 11:42:22 +0000 | [diff] [blame] | 4829 | } |
| 4830 | } |
| 4831 | |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4832 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4833 | @Override |
| 4834 | public void onReceive(Context context, Intent intent) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 4835 | ensureRunningOnConnectivityServiceThread(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4836 | final String action = intent.getAction(); |
| 4837 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, UserHandle.USER_NULL); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4838 | final int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 4839 | final Uri packageData = intent.getData(); |
| 4840 | final String packageName = |
| 4841 | packageData != null ? packageData.getSchemeSpecificPart() : null; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4842 | if (userId == UserHandle.USER_NULL) return; |
| 4843 | |
Robin Lee | 323f29d | 2016-05-04 16:38:06 +0100 | [diff] [blame] | 4844 | if (Intent.ACTION_USER_STARTED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4845 | onUserStart(userId); |
Amith Yamasani | ad2e4bf | 2016-04-26 14:35:54 -0700 | [diff] [blame] | 4846 | } else if (Intent.ACTION_USER_STOPPED.equals(action)) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4847 | onUserStop(userId); |
Fyodor Kupolov | 1c36315 | 2015-09-02 13:27:21 -0700 | [diff] [blame] | 4848 | } else if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 4849 | onUserAdded(userId); |
| 4850 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 4851 | onUserRemoved(userId); |
Robin Lee | 89e7a69 | 2016-02-29 14:38:17 +0000 | [diff] [blame] | 4852 | } else if (Intent.ACTION_USER_UNLOCKED.equals(action)) { |
| 4853 | onUserUnlocked(userId); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4854 | } else if (Intent.ACTION_PACKAGE_ADDED.equals(action)) { |
| 4855 | onPackageAdded(packageName, uid); |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4856 | } else if (Intent.ACTION_PACKAGE_REPLACED.equals(action)) { |
| 4857 | onPackageReplaced(packageName, uid); |
junyulai | 2454b69 | 2018-11-01 17:16:31 +0800 | [diff] [blame] | 4858 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
junyulai | efb04d3 | 2018-11-12 22:39:30 +0800 | [diff] [blame] | 4859 | final boolean isReplacing = intent.getBooleanExtra( |
| 4860 | Intent.EXTRA_REPLACING, false); |
| 4861 | onPackageRemoved(packageName, uid, isReplacing); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 4862 | } |
| 4863 | } |
| 4864 | }; |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 4865 | |
Robin Lee | 95204e0 | 2017-01-27 11:59:22 +0000 | [diff] [blame] | 4866 | private BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() { |
| 4867 | @Override |
| 4868 | public void onReceive(Context context, Intent intent) { |
| 4869 | // Try creating lockdown tracker, since user present usually means |
| 4870 | // unlocked keystore. |
| 4871 | updateLockdownVpn(); |
| 4872 | mContext.unregisterReceiver(this); |
| 4873 | } |
| 4874 | }; |
| 4875 | |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 4876 | private final HashMap<Messenger, NetworkFactoryInfo> mNetworkFactoryInfos = new HashMap<>(); |
| 4877 | private final HashMap<NetworkRequest, NetworkRequestInfo> mNetworkRequests = new HashMap<>(); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 4878 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4879 | private static final int MAX_NETWORK_REQUESTS_PER_UID = 100; |
| 4880 | // Map from UID to number of NetworkRequests that UID has filed. |
| 4881 | @GuardedBy("mUidToNetworkRequestCount") |
| 4882 | private final SparseIntArray mUidToNetworkRequestCount = new SparseIntArray(); |
| 4883 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4884 | private static class NetworkFactoryInfo { |
| 4885 | public final String name; |
| 4886 | public final Messenger messenger; |
| 4887 | public final AsyncChannel asyncChannel; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4888 | public final int factorySerialNumber; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4889 | |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4890 | NetworkFactoryInfo(String name, Messenger messenger, AsyncChannel asyncChannel, |
| 4891 | int factorySerialNumber) { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4892 | this.name = name; |
| 4893 | this.messenger = messenger; |
| 4894 | this.asyncChannel = asyncChannel; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 4895 | this.factorySerialNumber = factorySerialNumber; |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4896 | } |
| 4897 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4898 | |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4899 | private void ensureNetworkRequestHasType(NetworkRequest request) { |
| 4900 | if (request.type == NetworkRequest.Type.NONE) { |
| 4901 | throw new IllegalArgumentException( |
| 4902 | "All NetworkRequests in ConnectivityService must have a type"); |
| 4903 | } |
| 4904 | } |
| 4905 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 4906 | /** |
| 4907 | * Tracks info about the requester. |
| 4908 | * Also used to notice when the calling process dies so we can self-expire |
| 4909 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4910 | private class NetworkRequestInfo implements IBinder.DeathRecipient { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4911 | final NetworkRequest request; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4912 | final PendingIntent mPendingIntent; |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 4913 | boolean mPendingIntentSent; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4914 | private final IBinder mBinder; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4915 | final int mPid; |
| 4916 | final int mUid; |
| 4917 | final Messenger messenger; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4918 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4919 | NetworkRequestInfo(NetworkRequest r, PendingIntent pi) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4920 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4921 | ensureNetworkRequestHasType(request); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4922 | mPendingIntent = pi; |
| 4923 | messenger = null; |
| 4924 | mBinder = null; |
| 4925 | mPid = getCallingPid(); |
| 4926 | mUid = getCallingUid(); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4927 | enforceRequestCountLimit(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4928 | } |
| 4929 | |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4930 | NetworkRequestInfo(Messenger m, NetworkRequest r, IBinder binder) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4931 | super(); |
| 4932 | messenger = m; |
| 4933 | request = r; |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 4934 | ensureNetworkRequestHasType(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4935 | mBinder = binder; |
| 4936 | mPid = getCallingPid(); |
| 4937 | mUid = getCallingUid(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4938 | mPendingIntent = null; |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4939 | enforceRequestCountLimit(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4940 | |
| 4941 | try { |
| 4942 | mBinder.linkToDeath(this, 0); |
| 4943 | } catch (RemoteException e) { |
| 4944 | binderDied(); |
| 4945 | } |
| 4946 | } |
| 4947 | |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4948 | private void enforceRequestCountLimit() { |
| 4949 | synchronized (mUidToNetworkRequestCount) { |
| 4950 | int networkRequests = mUidToNetworkRequestCount.get(mUid, 0) + 1; |
| 4951 | if (networkRequests >= MAX_NETWORK_REQUESTS_PER_UID) { |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4952 | throw new ServiceSpecificException( |
| 4953 | ConnectivityManager.Errors.TOO_MANY_REQUESTS); |
Paul Jensen | 4e1d3fd | 2016-04-08 13:56:52 -0400 | [diff] [blame] | 4954 | } |
| 4955 | mUidToNetworkRequestCount.put(mUid, networkRequests); |
| 4956 | } |
| 4957 | } |
| 4958 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4959 | void unlinkDeathRecipient() { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4960 | if (mBinder != null) { |
| 4961 | mBinder.unlinkToDeath(this, 0); |
| 4962 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | public void binderDied() { |
| 4966 | log("ConnectivityService NetworkRequestInfo binderDied(" + |
| 4967 | request + ", " + mBinder + ")"); |
| 4968 | releaseNetworkRequest(request); |
| 4969 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4970 | |
| 4971 | public String toString() { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 4972 | return "uid/pid:" + mUid + "/" + mPid + " " + request + |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4973 | (mPendingIntent == null ? "" : " to trigger " + mPendingIntent); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 4974 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4975 | } |
| 4976 | |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 4977 | private void ensureRequestableCapabilities(NetworkCapabilities networkCapabilities) { |
| 4978 | final String badCapability = networkCapabilities.describeFirstNonRequestableCapability(); |
| 4979 | if (badCapability != null) { |
| 4980 | throw new IllegalArgumentException("Cannot request network with " + badCapability); |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 4981 | } |
| 4982 | } |
| 4983 | |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 4984 | // This checks that the passed capabilities either do not request a specific SSID/SignalStrength |
| 4985 | // , or the calling app has permission to do so. |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4986 | private void ensureSufficientPermissionsForRequest(NetworkCapabilities nc, |
| 4987 | int callerPid, int callerUid) { |
Chalard Jean | b5120ca | 2018-04-16 12:25:22 +0900 | [diff] [blame] | 4988 | if (null != nc.getSSID() && !checkSettingsPermission(callerPid, callerUid)) { |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4989 | throw new SecurityException("Insufficient permissions to request a specific SSID"); |
| 4990 | } |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 4991 | |
| 4992 | if (nc.hasSignalStrength() |
| 4993 | && !checkNetworkSignalStrengthWakeupPermission(callerPid, callerUid)) { |
| 4994 | throw new SecurityException( |
| 4995 | "Insufficient permissions to request a specific signal strength"); |
| 4996 | } |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 4997 | } |
| 4998 | |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 4999 | private ArrayList<Integer> getSignalStrengthThresholds(NetworkAgentInfo nai) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5000 | final SortedSet<Integer> thresholds = new TreeSet<>(); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5001 | synchronized (nai) { |
| 5002 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5003 | if (nri.request.networkCapabilities.hasSignalStrength() && |
| 5004 | nai.satisfiesImmutableCapabilitiesOf(nri.request)) { |
| 5005 | thresholds.add(nri.request.networkCapabilities.getSignalStrength()); |
| 5006 | } |
| 5007 | } |
| 5008 | } |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5009 | return new ArrayList<>(thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5010 | } |
| 5011 | |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 5012 | private void updateSignalStrengthThresholds( |
| 5013 | NetworkAgentInfo nai, String reason, NetworkRequest request) { |
| 5014 | ArrayList<Integer> thresholdsArray = getSignalStrengthThresholds(nai); |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 5015 | Bundle thresholds = new Bundle(); |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 5016 | thresholds.putIntegerArrayList("thresholds", thresholdsArray); |
| 5017 | |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5018 | if (VDBG || (DBG && !"CONNECT".equals(reason))) { |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 5019 | String detail; |
| 5020 | if (request != null && request.networkCapabilities.hasSignalStrength()) { |
| 5021 | detail = reason + " " + request.networkCapabilities.getSignalStrength(); |
| 5022 | } else { |
| 5023 | detail = reason; |
| 5024 | } |
| 5025 | log(String.format("updateSignalStrengthThresholds: %s, sending %s to %s", |
| 5026 | detail, Arrays.toString(thresholdsArray.toArray()), nai.name())); |
| 5027 | } |
| 5028 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5029 | nai.asyncChannel.sendMessage( |
| 5030 | android.net.NetworkAgent.CMD_SET_SIGNAL_STRENGTH_THRESHOLDS, |
Erik Kline | 9d598e1 | 2015-07-13 16:37:51 +0900 | [diff] [blame] | 5031 | 0, 0, thresholds); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 5032 | } |
| 5033 | |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5034 | private void ensureValidNetworkSpecifier(NetworkCapabilities nc) { |
| 5035 | if (nc == null) { |
| 5036 | return; |
| 5037 | } |
| 5038 | NetworkSpecifier ns = nc.getNetworkSpecifier(); |
| 5039 | if (ns == null) { |
| 5040 | return; |
| 5041 | } |
| 5042 | MatchAllNetworkSpecifier.checkNotMatchAllNetworkSpecifier(ns); |
| 5043 | ns.assertValidFromUid(Binder.getCallingUid()); |
| 5044 | } |
| 5045 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5046 | @Override |
| 5047 | public NetworkRequest requestNetwork(NetworkCapabilities networkCapabilities, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5048 | Messenger messenger, int timeoutMs, IBinder binder, int legacyType) { |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5049 | final NetworkRequest.Type type = (networkCapabilities == null) |
| 5050 | ? NetworkRequest.Type.TRACK_DEFAULT |
| 5051 | : NetworkRequest.Type.REQUEST; |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5052 | // If the requested networkCapabilities is null, take them instead from |
| 5053 | // the default network request. This allows callers to keep track of |
| 5054 | // the system default network. |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5055 | if (type == NetworkRequest.Type.TRACK_DEFAULT) { |
Chalard Jean | 2640049 | 2018-04-18 20:18:38 +0900 | [diff] [blame] | 5056 | networkCapabilities = createDefaultNetworkCapabilitiesForUid(Binder.getCallingUid()); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5057 | enforceAccessPermission(); |
| 5058 | } else { |
| 5059 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 5060 | enforceNetworkRequestPermissions(networkCapabilities); |
Lorenzo Colitti | b60570c | 2016-07-01 13:20:10 +0900 | [diff] [blame] | 5061 | // TODO: this is incorrect. We mark the request as metered or not depending on the state |
| 5062 | // of the app when the request is filed, but we never change the request if the app |
| 5063 | // changes network state. http://b/29964605 |
| 5064 | enforceMeteredApnPolicy(networkCapabilities); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 5065 | } |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 5066 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5067 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5068 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5069 | // Set the UID range for this request to the single UID of the requester, or to an empty |
| 5070 | // 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] | 5071 | // This will overwrite any allowed UIDs in the requested capabilities. Though there |
| 5072 | // are no visible methods to set the UIDs, an app could use reflection to try and get |
| 5073 | // 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] | 5074 | restrictRequestUidsForCaller(networkCapabilities); |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 5075 | |
Etan Cohen | ba07c8c | 2017-02-05 10:42:27 -0800 | [diff] [blame] | 5076 | if (timeoutMs < 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5077 | throw new IllegalArgumentException("Bad timeout specified"); |
| 5078 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5079 | ensureValidNetworkSpecifier(networkCapabilities); |
Etan Cohen | ddb9ef0 | 2015-11-18 10:56:15 -0800 | [diff] [blame] | 5080 | |
Robert Greenwalt | 39fa65a | 2014-07-27 10:56:49 -0700 | [diff] [blame] | 5081 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, legacyType, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5082 | nextNetworkRequestId(), type); |
| 5083 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 5084 | if (DBG) log("requestNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5085 | |
| 5086 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST, nri)); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5087 | if (timeoutMs > 0) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5088 | mHandler.sendMessageDelayed(mHandler.obtainMessage(EVENT_TIMEOUT_NETWORK_REQUEST, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 5089 | nri), timeoutMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5090 | } |
| 5091 | return networkRequest; |
| 5092 | } |
| 5093 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5094 | private void enforceNetworkRequestPermissions(NetworkCapabilities networkCapabilities) { |
Lorenzo Colitti | 76f6779 | 2015-05-14 17:28:27 +0900 | [diff] [blame] | 5095 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_RESTRICTED) == false) { |
Hugo Benichi | 514da60 | 2016-07-19 15:59:27 +0900 | [diff] [blame] | 5096 | enforceConnectivityRestrictedNetworksPermission(); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5097 | } else { |
| 5098 | enforceChangePermission(); |
| 5099 | } |
| 5100 | } |
| 5101 | |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5102 | @Override |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 5103 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5104 | enforceAccessPermission(); |
| 5105 | NetworkAgentInfo nai = null; |
| 5106 | if (network == null) { |
| 5107 | return false; |
| 5108 | } |
| 5109 | synchronized (mNetworkForNetId) { |
| 5110 | nai = mNetworkForNetId.get(network.netId); |
| 5111 | } |
| 5112 | if (nai != null) { |
| 5113 | nai.asyncChannel.sendMessage(android.net.NetworkAgent.CMD_REQUEST_BANDWIDTH_UPDATE); |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 5114 | synchronized (mBandwidthRequests) { |
| 5115 | final int uid = Binder.getCallingUid(); |
| 5116 | Integer uidReqs = mBandwidthRequests.get(uid); |
| 5117 | if (uidReqs == null) { |
| 5118 | uidReqs = new Integer(0); |
| 5119 | } |
| 5120 | mBandwidthRequests.put(uid, ++uidReqs); |
| 5121 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5122 | return true; |
| 5123 | } |
| 5124 | return false; |
| 5125 | } |
| 5126 | |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5127 | private boolean isSystem(int uid) { |
| 5128 | return uid < Process.FIRST_APPLICATION_UID; |
| 5129 | } |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 5130 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5131 | private void enforceMeteredApnPolicy(NetworkCapabilities networkCapabilities) { |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5132 | final int uid = Binder.getCallingUid(); |
| 5133 | if (isSystem(uid)) { |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5134 | // Exemption for system uid. |
Felipe Leme | ee27cab | 2016-06-20 16:36:29 -0700 | [diff] [blame] | 5135 | return; |
| 5136 | } |
Hugo Benichi | 938ab4f | 2017-02-11 17:04:43 +0900 | [diff] [blame] | 5137 | if (networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { |
| 5138 | // Policy already enforced. |
| 5139 | return; |
| 5140 | } |
| 5141 | if (mPolicyManagerInternal.isUidRestrictedOnMeteredNetworks(uid)) { |
| 5142 | // If UID is restricted, don't allow them to bring up metered APNs. |
| 5143 | networkCapabilities.addCapability(NET_CAPABILITY_NOT_METERED); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5144 | } |
| 5145 | } |
| 5146 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5147 | @Override |
| 5148 | public NetworkRequest pendingRequestForNetwork(NetworkCapabilities networkCapabilities, |
| 5149 | PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5150 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 5151 | networkCapabilities = new NetworkCapabilities(networkCapabilities); |
| 5152 | enforceNetworkRequestPermissions(networkCapabilities); |
| 5153 | enforceMeteredApnPolicy(networkCapabilities); |
Lorenzo Colitti | 260a36d | 2015-07-08 12:49:04 +0900 | [diff] [blame] | 5154 | ensureRequestableCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5155 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5156 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5157 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5158 | restrictRequestUidsForCaller(networkCapabilities); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5159 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5160 | NetworkRequest networkRequest = new NetworkRequest(networkCapabilities, TYPE_NONE, |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5161 | nextNetworkRequestId(), NetworkRequest.Type.REQUEST); |
| 5162 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Erik Kline | 7523eb3 | 2015-07-09 18:24:03 +0900 | [diff] [blame] | 5163 | if (DBG) log("pendingRequest for " + nri); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5164 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_REQUEST_WITH_INTENT, |
| 5165 | nri)); |
| 5166 | return networkRequest; |
| 5167 | } |
| 5168 | |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5169 | private void releasePendingNetworkRequestWithDelay(PendingIntent operation) { |
| 5170 | mHandler.sendMessageDelayed( |
| 5171 | mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 5172 | getCallingUid(), 0, operation), mReleasePendingIntentDelayMs); |
| 5173 | } |
| 5174 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5175 | @Override |
| 5176 | public void releasePendingNetworkRequest(PendingIntent operation) { |
Paul Jensen | 1a81c39 | 2015-05-21 08:15:08 -0400 | [diff] [blame] | 5177 | checkNotNull(operation, "PendingIntent cannot be null."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5178 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_RELEASE_NETWORK_REQUEST_WITH_INTENT, |
| 5179 | getCallingUid(), 0, operation)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5180 | } |
| 5181 | |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 5182 | // In order to implement the compatibility measure for pre-M apps that call |
| 5183 | // WifiManager.enableNetwork(..., true) without also binding to that network explicitly, |
| 5184 | // WifiManager registers a network listen for the purpose of calling setProcessDefaultNetwork. |
| 5185 | // This ensures it has permission to do so. |
| 5186 | private boolean hasWifiNetworkListenPermission(NetworkCapabilities nc) { |
| 5187 | if (nc == null) { |
| 5188 | return false; |
| 5189 | } |
| 5190 | int[] transportTypes = nc.getTransportTypes(); |
| 5191 | if (transportTypes.length != 1 || transportTypes[0] != NetworkCapabilities.TRANSPORT_WIFI) { |
| 5192 | return false; |
| 5193 | } |
| 5194 | try { |
| 5195 | mContext.enforceCallingOrSelfPermission( |
| 5196 | android.Manifest.permission.ACCESS_WIFI_STATE, |
| 5197 | "ConnectivityService"); |
| 5198 | } catch (SecurityException e) { |
| 5199 | return false; |
| 5200 | } |
| 5201 | return true; |
| 5202 | } |
| 5203 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5204 | @Override |
| 5205 | public NetworkRequest listenForNetwork(NetworkCapabilities networkCapabilities, |
| 5206 | Messenger messenger, IBinder binder) { |
Lorenzo Colitti | fa57c48 | 2015-04-22 10:44:49 +0900 | [diff] [blame] | 5207 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 5208 | enforceAccessPermission(); |
| 5209 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5210 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5211 | NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5212 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5213 | Binder.getCallingPid(), Binder.getCallingUid()); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5214 | restrictRequestUidsForCaller(nc); |
Chalard Jean | 26aa91a | 2018-03-20 19:13:57 +0900 | [diff] [blame] | 5215 | // Apps without the CHANGE_NETWORK_STATE permission can't use background networks, so |
| 5216 | // make all their listens include NET_CAPABILITY_FOREGROUND. That way, they will get |
| 5217 | // onLost and onAvailable callbacks when networks move in and out of the background. |
| 5218 | // There is no need to do this for requests because an app without CHANGE_NETWORK_STATE |
| 5219 | // can't request networks. |
| 5220 | restrictBackgroundRequestForCaller(nc); |
| 5221 | ensureValidNetworkSpecifier(nc); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5222 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5223 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5224 | NetworkRequest.Type.LISTEN); |
| 5225 | NetworkRequestInfo nri = new NetworkRequestInfo(messenger, networkRequest, binder); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5226 | if (VDBG) log("listenForNetwork for " + nri); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5227 | |
| 5228 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
| 5229 | return networkRequest; |
| 5230 | } |
| 5231 | |
| 5232 | @Override |
| 5233 | public void pendingListenForNetwork(NetworkCapabilities networkCapabilities, |
| 5234 | PendingIntent operation) { |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5235 | checkNotNull(operation, "PendingIntent cannot be null."); |
| 5236 | if (!hasWifiNetworkListenPermission(networkCapabilities)) { |
| 5237 | enforceAccessPermission(); |
| 5238 | } |
Etan Cohen | 859748f | 2017-04-03 17:42:34 -0700 | [diff] [blame] | 5239 | ensureValidNetworkSpecifier(networkCapabilities); |
Chalard Jean | b03a622 | 2018-04-11 21:09:10 +0900 | [diff] [blame] | 5240 | ensureSufficientPermissionsForRequest(networkCapabilities, |
| 5241 | Binder.getCallingPid(), Binder.getCallingUid()); |
Etan Cohen | a743427 | 2017-04-03 12:17:51 -0700 | [diff] [blame] | 5242 | |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5243 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5244 | restrictRequestUidsForCaller(nc); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5245 | |
| 5246 | NetworkRequest networkRequest = new NetworkRequest(nc, TYPE_NONE, nextNetworkRequestId(), |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 5247 | NetworkRequest.Type.LISTEN); |
| 5248 | NetworkRequestInfo nri = new NetworkRequestInfo(networkRequest, operation); |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 5249 | if (VDBG) log("pendingListenForNetwork for " + nri); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 5250 | |
| 5251 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_LISTENER, nri)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5252 | } |
| 5253 | |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5254 | @Override |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5255 | public void releaseNetworkRequest(NetworkRequest networkRequest) { |
Lorenzo Colitti | 1ec11eb | 2016-07-05 01:22:13 +0900 | [diff] [blame] | 5256 | ensureNetworkRequestHasType(networkRequest); |
Erik Kline | acdd639 | 2016-07-07 16:50:58 +0900 | [diff] [blame] | 5257 | mHandler.sendMessage(mHandler.obtainMessage( |
| 5258 | EVENT_RELEASE_NETWORK_REQUEST, getCallingUid(), 0, networkRequest)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5259 | } |
| 5260 | |
| 5261 | @Override |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5262 | public int registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5263 | enforceConnectivityInternalPermission(); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5264 | NetworkFactoryInfo nfi = new NetworkFactoryInfo(name, messenger, new AsyncChannel(), |
| 5265 | NetworkFactory.SerialNumber.nextSerialNumber()); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5266 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_REGISTER_NETWORK_FACTORY, nfi)); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5267 | return nfi.factorySerialNumber; |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5268 | } |
| 5269 | |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5270 | private void handleRegisterNetworkFactory(NetworkFactoryInfo nfi) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5271 | if (DBG) log("Got NetworkFactory Messenger for " + nfi.name); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5272 | mNetworkFactoryInfos.put(nfi.messenger, nfi); |
| 5273 | nfi.asyncChannel.connect(mContext, mTrackerHandler, nfi.messenger); |
| 5274 | } |
| 5275 | |
| 5276 | @Override |
| 5277 | public void unregisterNetworkFactory(Messenger messenger) { |
| 5278 | enforceConnectivityInternalPermission(); |
| 5279 | mHandler.sendMessage(mHandler.obtainMessage(EVENT_UNREGISTER_NETWORK_FACTORY, messenger)); |
| 5280 | } |
| 5281 | |
| 5282 | private void handleUnregisterNetworkFactory(Messenger messenger) { |
| 5283 | NetworkFactoryInfo nfi = mNetworkFactoryInfos.remove(messenger); |
| 5284 | if (nfi == null) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5285 | loge("Failed to find Messenger in unregisterNetworkFactory"); |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5286 | return; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5287 | } |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5288 | if (DBG) log("unregisterNetworkFactory for " + nfi.name); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 5289 | } |
| 5290 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5291 | /** |
| 5292 | * NetworkAgentInfo supporting a request by requestId. |
| 5293 | * These have already been vetted (their Capabilities satisfy the request) |
| 5294 | * and the are the highest scored network available. |
| 5295 | * the are keyed off the Requests requestId. |
| 5296 | */ |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5297 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5298 | @GuardedBy("mNetworkForRequestId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5299 | private final SparseArray<NetworkAgentInfo> mNetworkForRequestId = new SparseArray<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5300 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5301 | // NOTE: Accessed on multiple threads, must be synchronized on itself. |
| 5302 | @GuardedBy("mNetworkForNetId") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5303 | private final SparseArray<NetworkAgentInfo> mNetworkForNetId = new SparseArray<>(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5304 | // NOTE: Accessed on multiple threads, synchronized with mNetworkForNetId. |
| 5305 | // An entry is first added to mNetIdInUse, prior to mNetworkForNetId, so |
| 5306 | // there may not be a strict 1:1 correlation between the two. |
| 5307 | @GuardedBy("mNetworkForNetId") |
| 5308 | private final SparseBooleanArray mNetIdInUse = new SparseBooleanArray(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5309 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5310 | // NetworkAgentInfo keyed off its connecting messenger |
| 5311 | // TODO - eval if we can reduce the number of lists/hashmaps/sparsearrays |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5312 | // NOTE: Only should be accessed on ConnectivityServiceThread, except dump(). |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5313 | private final HashMap<Messenger, NetworkAgentInfo> mNetworkAgentInfos = new HashMap<>(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5314 | |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5315 | @GuardedBy("mBlockedAppUids") |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5316 | private final HashSet<Integer> mBlockedAppUids = new HashSet<>(); |
Lorenzo Colitti | c1a6ce7 | 2016-01-22 04:04:57 +0900 | [diff] [blame] | 5317 | |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5318 | // Note: if mDefaultRequest is changed, NetworkMonitor needs to be updated. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5319 | private final NetworkRequest mDefaultRequest; |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5320 | |
Erik Kline | da4bfa8 | 2015-04-30 12:58:40 +0900 | [diff] [blame] | 5321 | // Request used to optionally keep mobile data active even when higher |
| 5322 | // priority networks like Wi-Fi are active. |
| 5323 | private final NetworkRequest mDefaultMobileDataRequest; |
| 5324 | |
Leif Hendrik Wilden | fd30663 | 2018-05-02 12:05:24 -0700 | [diff] [blame] | 5325 | // Request used to optionally keep wifi data active even when higher |
| 5326 | // priority networks like ethernet are active. |
| 5327 | private final NetworkRequest mDefaultWifiRequest; |
| 5328 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5329 | private NetworkAgentInfo getNetworkForRequest(int requestId) { |
| 5330 | synchronized (mNetworkForRequestId) { |
| 5331 | return mNetworkForRequestId.get(requestId); |
| 5332 | } |
| 5333 | } |
| 5334 | |
| 5335 | private void clearNetworkForRequest(int requestId) { |
| 5336 | synchronized (mNetworkForRequestId) { |
| 5337 | mNetworkForRequestId.remove(requestId); |
| 5338 | } |
| 5339 | } |
| 5340 | |
| 5341 | private void setNetworkForRequest(int requestId, NetworkAgentInfo nai) { |
| 5342 | synchronized (mNetworkForRequestId) { |
| 5343 | mNetworkForRequestId.put(requestId, nai); |
| 5344 | } |
| 5345 | } |
| 5346 | |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5347 | private NetworkAgentInfo getDefaultNetwork() { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 5348 | return getNetworkForRequest(mDefaultRequest.requestId); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5349 | } |
| 5350 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5351 | @Nullable |
| 5352 | private Network getNetwork(@Nullable NetworkAgentInfo nai) { |
| 5353 | return nai != null ? nai.network : null; |
| 5354 | } |
| 5355 | |
| 5356 | private void ensureRunningOnConnectivityServiceThread() { |
| 5357 | if (mHandler.getLooper().getThread() != Thread.currentThread()) { |
| 5358 | throw new IllegalStateException( |
| 5359 | "Not running on ConnectivityService thread: " |
| 5360 | + Thread.currentThread().getName()); |
| 5361 | } |
| 5362 | } |
| 5363 | |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 5364 | @VisibleForTesting |
| 5365 | protected boolean isDefaultNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 5366 | return nai == getDefaultNetwork(); |
Robert Greenwalt | bf4eed7 | 2014-08-06 21:32:18 -0700 | [diff] [blame] | 5367 | } |
| 5368 | |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 5369 | private boolean isDefaultRequest(NetworkRequestInfo nri) { |
| 5370 | return nri.request.requestId == mDefaultRequest.requestId; |
| 5371 | } |
| 5372 | |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5373 | // TODO : remove this method. It's a stopgap measure to help sheperding a number of dependent |
| 5374 | // changes that would conflict throughout the automerger graph. Having this method temporarily |
| 5375 | // helps with the process of going through with all these dependent changes across the entire |
| 5376 | // tree. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5377 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5378 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 5379 | int currentScore, NetworkMisc networkMisc) { |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5380 | return registerNetworkAgent(messenger, networkInfo, linkProperties, networkCapabilities, |
| 5381 | currentScore, networkMisc, NetworkFactory.SerialNumber.NONE); |
| 5382 | } |
| 5383 | |
| 5384 | /** |
| 5385 | * Register a new agent with ConnectivityService to handle a network. |
| 5386 | * |
| 5387 | * @param messenger a messenger for ConnectivityService to contact the agent asynchronously. |
| 5388 | * @param networkInfo the initial info associated with this network. It can be updated later : |
| 5389 | * see {@link #updateNetworkInfo}. |
| 5390 | * @param linkProperties the initial link properties of this network. They can be updated |
| 5391 | * later : see {@link #updateLinkProperties}. |
| 5392 | * @param networkCapabilities the initial capabilites of this network. They can be updated |
| 5393 | * later : see {@link #updateNetworkCapabilities}. |
| 5394 | * @param currentScore the initial score of the network. See |
| 5395 | * {@link NetworkAgentInfo#getCurrentScore}. |
| 5396 | * @param networkMisc metadata about the network. This is never updated. |
| 5397 | * @param factorySerialNumber the serial number of the factory owning this NetworkAgent. |
| 5398 | */ |
| 5399 | public int registerNetworkAgent(Messenger messenger, NetworkInfo networkInfo, |
| 5400 | LinkProperties linkProperties, NetworkCapabilities networkCapabilities, |
| 5401 | int currentScore, NetworkMisc networkMisc, int factorySerialNumber) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5402 | enforceConnectivityInternalPermission(); |
| 5403 | |
Rubin Xu | 1bb5c08 | 2017-09-05 18:40:49 +0100 | [diff] [blame] | 5404 | LinkProperties lp = new LinkProperties(linkProperties); |
| 5405 | lp.ensureDirectlyConnectedRoutes(); |
Paul Jensen | 2c311d6 | 2014-11-17 12:34:51 -0500 | [diff] [blame] | 5406 | // TODO: Instead of passing mDefaultRequest, provide an API to determine whether a Network |
| 5407 | // satisfies mDefaultRequest. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5408 | final NetworkCapabilities nc = new NetworkCapabilities(networkCapabilities); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 5409 | final NetworkAgentInfo nai = new NetworkAgentInfo(messenger, new AsyncChannel(), |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5410 | new Network(reserveNetId()), new NetworkInfo(networkInfo), lp, nc, currentScore, |
Luke Huang | 6591477 | 2019-03-16 00:31:46 +0800 | [diff] [blame] | 5411 | mContext, mTrackerHandler, new NetworkMisc(networkMisc), this, mNetd, mDnsResolver, |
| 5412 | mNMS, factorySerialNumber); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5413 | // Make sure the network capabilities reflect what the agent info says. |
Remi NGUYEN VAN | 3962f67 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 5414 | nai.setNetworkCapabilities(mixInCapabilities(nai, nc)); |
Chalard Jean | d771aa0 | 2018-04-26 16:16:10 +0900 | [diff] [blame] | 5415 | final String extraInfo = networkInfo.getExtraInfo(); |
| 5416 | final String name = TextUtils.isEmpty(extraInfo) |
| 5417 | ? nai.networkCapabilities.getSSID() : extraInfo; |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5418 | if (DBG) log("registerNetworkAgent " + nai); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5419 | final long token = Binder.clearCallingIdentity(); |
| 5420 | try { |
Remi NGUYEN VAN | 310da6f | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5421 | getNetworkStack().makeNetworkMonitor( |
Remi NGUYEN VAN | 904a38b | 2019-03-15 02:25:09 +0900 | [diff] [blame] | 5422 | nai.network, name, new NetworkMonitorCallbacks(nai)); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5423 | } finally { |
| 5424 | Binder.restoreCallingIdentity(token); |
| 5425 | } |
| 5426 | // NetworkAgentInfo registration will finish when the NetworkMonitor is created. |
| 5427 | // If the network disconnects or sends any other event before that, messages are deferred by |
| 5428 | // NetworkAgent until nai.asyncChannel.connect(), which will be called when finalizing the |
| 5429 | // registration. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5430 | return nai.network.netId; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
Remi NGUYEN VAN | 310da6f | 2019-02-14 18:04:20 +0900 | [diff] [blame] | 5433 | @VisibleForTesting |
| 5434 | protected NetworkStackClient getNetworkStack() { |
| 5435 | return NetworkStackClient.getInstance(); |
| 5436 | } |
| 5437 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5438 | private void handleRegisterNetworkAgent(NetworkAgentInfo nai, INetworkMonitor networkMonitor) { |
| 5439 | nai.onNetworkMonitorCreated(networkMonitor); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5440 | if (VDBG) log("Got NetworkAgent Messenger"); |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5441 | mNetworkAgentInfos.put(nai.messenger, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5442 | synchronized (mNetworkForNetId) { |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5443 | mNetworkForNetId.put(nai.network.netId, nai); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 5444 | } |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 5445 | |
| 5446 | try { |
| 5447 | networkMonitor.start(); |
| 5448 | } catch (RemoteException e) { |
| 5449 | e.rethrowFromSystemServer(); |
| 5450 | } |
Erik Kline | e5dac90 | 2018-03-04 21:01:01 +0900 | [diff] [blame] | 5451 | nai.asyncChannel.connect(mContext, mTrackerHandler, nai.messenger); |
| 5452 | NetworkInfo networkInfo = nai.networkInfo; |
| 5453 | nai.networkInfo = null; |
| 5454 | updateNetworkInfo(nai, networkInfo); |
| 5455 | updateUids(nai, null, nai.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5456 | } |
| 5457 | |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5458 | private void updateLinkProperties(NetworkAgentInfo networkAgent, LinkProperties newLp, |
| 5459 | LinkProperties oldLp) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5460 | int netId = networkAgent.network.netId; |
| 5461 | |
Lorenzo Colitti | df59563 | 2019-01-08 14:43:37 +0900 | [diff] [blame] | 5462 | // The NetworkAgentInfo does not know whether clatd is running on its network or not, or |
| 5463 | // whether there is a NAT64 prefix. Before we do anything else, make sure its LinkProperties |
| 5464 | // are accurate. |
| 5465 | networkAgent.clatd.fixupLinkProperties(oldLp, newLp); |
Lorenzo Colitti | 1df5fa5 | 2014-09-20 13:47:47 +0900 | [diff] [blame] | 5466 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5467 | updateInterfaces(newLp, oldLp, netId, networkAgent.networkCapabilities); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5468 | updateMtu(newLp, oldLp); |
| 5469 | // TODO - figure out what to do for clat |
| 5470 | // for (LinkProperties lp : newLp.getStackedLinks()) { |
| 5471 | // updateMtu(lp, null); |
| 5472 | // } |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5473 | if (isDefaultNetwork(networkAgent)) { |
| 5474 | updateTcpBufferSizes(newLp.getTcpBufferSizes()); |
| 5475 | } |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5476 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5477 | updateRoutes(newLp, oldLp, netId); |
| 5478 | updateDnses(newLp, oldLp, netId); |
dalyk | 7301aa4 | 2018-03-05 12:42:22 -0500 | [diff] [blame] | 5479 | // Make sure LinkProperties represents the latest private DNS status. |
| 5480 | // This does not need to be done before updateDnses because the |
| 5481 | // LinkProperties are not the source of the private DNS configuration. |
| 5482 | // updateDnses will fetch the private DNS configuration from DnsManager. |
| 5483 | mDnsManager.updatePrivateDnsStatus(netId, newLp); |
Lorenzo Colitti | 829dfa7 | 2014-11-28 20:07:46 +0900 | [diff] [blame] | 5484 | |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5485 | if (isDefaultNetwork(networkAgent)) { |
| 5486 | handleApplyDefaultProxy(newLp.getHttpProxy()); |
| 5487 | } else { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5488 | updateProxy(newLp, oldLp); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 5489 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5490 | // TODO - move this check to cover the whole function |
| 5491 | if (!Objects.equals(newLp, oldLp)) { |
Chalard Jean | c4f53ba | 2018-05-23 09:07:51 +0900 | [diff] [blame] | 5492 | synchronized (networkAgent) { |
| 5493 | networkAgent.linkProperties = newLp; |
| 5494 | } |
Lorenzo Colitti | d593e29 | 2019-02-19 13:21:56 +0900 | [diff] [blame] | 5495 | // Start or stop DNS64 detection and 464xlat according to network state. |
| 5496 | networkAgent.clatd.update(); |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 5497 | notifyIfacesChangedForNetworkStats(); |
Remi NGUYEN VAN | 3962f67 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 5498 | try { |
| 5499 | networkAgent.networkMonitor().notifyLinkPropertiesChanged(newLp); |
| 5500 | } catch (RemoteException e) { |
| 5501 | e.rethrowFromSystemServer(); |
| 5502 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5503 | if (networkAgent.everConnected) { |
| 5504 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_IP_CHANGED); |
| 5505 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5506 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 5507 | |
| 5508 | mKeepaliveTracker.handleCheckKeepalivesStillValid(networkAgent); |
Paul Jensen | 3b75982 | 2014-05-13 11:44:01 -0400 | [diff] [blame] | 5509 | } |
| 5510 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5511 | private void wakeupModifyInterface(String iface, NetworkCapabilities caps, boolean add) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5512 | // Marks are only available on WiFi interfaces. Checking for |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5513 | // marks on unsupported interfaces is harmless. |
| 5514 | if (!caps.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) { |
| 5515 | return; |
| 5516 | } |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5517 | |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5518 | int mark = mContext.getResources().getInteger( |
| 5519 | com.android.internal.R.integer.config_networkWakeupPacketMark); |
| 5520 | int mask = mContext.getResources().getInteger( |
| 5521 | com.android.internal.R.integer.config_networkWakeupPacketMask); |
| 5522 | |
| 5523 | // Mask/mark of zero will not detect anything interesting. |
| 5524 | // Don't install rules unless both values are nonzero. |
| 5525 | if (mark == 0 || mask == 0) { |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5526 | return; |
| 5527 | } |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5528 | |
| 5529 | final String prefix = "iface:" + iface; |
| 5530 | try { |
| 5531 | if (add) { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5532 | mNetd.wakeupAddInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5533 | } else { |
Luke Huang | 674660f | 2018-09-27 19:33:11 +0800 | [diff] [blame] | 5534 | mNetd.wakeupDelInterface(iface, prefix, mark, mask); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5535 | } |
| 5536 | } catch (Exception e) { |
| 5537 | loge("Exception modifying wakeup packet monitoring: " + e); |
| 5538 | } |
| 5539 | |
Joel Scherpelz | b369bf5 | 2017-05-22 13:47:41 +0900 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | private void updateInterfaces(LinkProperties newLp, LinkProperties oldLp, int netId, |
| 5543 | NetworkCapabilities caps) { |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 5544 | CompareResult<String> interfaceDiff = new CompareResult<>( |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5545 | oldLp != null ? oldLp.getAllInterfaceNames() : null, |
| 5546 | newLp != null ? newLp.getAllInterfaceNames() : null); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5547 | for (String iface : interfaceDiff.added) { |
| 5548 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5549 | if (DBG) log("Adding iface " + iface + " to network " + netId); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5550 | mNMS.addInterfaceToNetwork(iface, netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5551 | wakeupModifyInterface(iface, caps, true); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5552 | } catch (Exception e) { |
| 5553 | loge("Exception adding interface: " + e); |
| 5554 | } |
| 5555 | } |
| 5556 | for (String iface : interfaceDiff.removed) { |
| 5557 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5558 | if (DBG) log("Removing iface " + iface + " from network " + netId); |
Joel Scherpelz | 668370b | 2017-06-08 15:35:21 +0900 | [diff] [blame] | 5559 | wakeupModifyInterface(iface, caps, false); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5560 | mNMS.removeInterfaceFromNetwork(iface, netId); |
Paul Jensen | 992f252 | 2014-04-28 10:33:11 -0400 | [diff] [blame] | 5561 | } catch (Exception e) { |
| 5562 | loge("Exception removing interface: " + e); |
| 5563 | } |
| 5564 | } |
| 5565 | } |
| 5566 | |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5567 | /** |
| 5568 | * Have netd update routes from oldLp to newLp. |
| 5569 | * @return true if routes changed between oldLp and newLp |
| 5570 | */ |
| 5571 | private boolean updateRoutes(LinkProperties newLp, LinkProperties oldLp, int netId) { |
Rubin Xu | 2fc72f7 | 2017-08-22 16:35:52 +0100 | [diff] [blame] | 5572 | // Compare the route diff to determine which routes should be added and removed. |
| 5573 | CompareResult<RouteInfo> routeDiff = new CompareResult<RouteInfo>( |
| 5574 | oldLp != null ? oldLp.getAllRoutes() : null, |
| 5575 | newLp != null ? newLp.getAllRoutes() : null); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5576 | |
| 5577 | // add routes before removing old in case it helps with continuous connectivity |
| 5578 | |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 5579 | // 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] | 5580 | for (RouteInfo route : routeDiff.added) { |
| 5581 | if (route.hasGateway()) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5582 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5583 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5584 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5585 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5586 | if ((route.getDestination().getAddress() instanceof Inet4Address) || VDBG) { |
| 5587 | loge("Exception in addRoute for non-gateway: " + e); |
| 5588 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5589 | } |
| 5590 | } |
| 5591 | for (RouteInfo route : routeDiff.added) { |
| 5592 | if (route.hasGateway() == false) continue; |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5593 | if (VDBG || DDBG) log("Adding Route [" + route + "] to network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5594 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5595 | mNMS.addRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5596 | } catch (Exception e) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5597 | if ((route.getGateway() instanceof Inet4Address) || VDBG) { |
| 5598 | loge("Exception in addRoute for gateway: " + e); |
| 5599 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5600 | } |
| 5601 | } |
| 5602 | |
| 5603 | for (RouteInfo route : routeDiff.removed) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5604 | if (VDBG || DDBG) log("Removing Route [" + route + "] from network " + netId); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5605 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5606 | mNMS.removeRoute(netId, route); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5607 | } catch (Exception e) { |
| 5608 | loge("Exception in removeRoute: " + e); |
| 5609 | } |
| 5610 | } |
Paul Jensen | 5fb2c6ff | 2014-08-06 15:51:33 -0400 | [diff] [blame] | 5611 | return !routeDiff.added.isEmpty() || !routeDiff.removed.isEmpty(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5612 | } |
Erik Kline | 4136850 | 2015-06-17 13:19:54 +0900 | [diff] [blame] | 5613 | |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5614 | private void updateDnses(LinkProperties newLp, LinkProperties oldLp, int netId) { |
| 5615 | if (oldLp != null && newLp.isIdenticalDnses(oldLp)) { |
| 5616 | return; // no updating necessary |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5617 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5618 | |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5619 | final NetworkAgentInfo defaultNai = getDefaultNetwork(); |
| 5620 | final boolean isDefaultNetwork = (defaultNai != null && defaultNai.network.netId == netId); |
| 5621 | |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5622 | if (DBG) { |
| 5623 | final Collection<InetAddress> dnses = newLp.getDnsServers(); |
| 5624 | log("Setting DNS servers for network " + netId + " to " + dnses); |
| 5625 | } |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5626 | try { |
Erik Kline | a24d459 | 2018-01-11 21:07:29 +0900 | [diff] [blame] | 5627 | mDnsManager.setDnsConfigurationForNetwork(netId, newLp, isDefaultNetwork); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5628 | } catch (Exception e) { |
Pierre Imai | bd8759b | 2016-04-28 17:00:04 +0900 | [diff] [blame] | 5629 | loge("Exception in setDnsConfigurationForNetwork: " + e); |
Erik Kline | 9488787 | 2016-04-05 13:30:49 +0900 | [diff] [blame] | 5630 | } |
Erik Kline | 4edba01 | 2017-04-07 15:29:29 +0900 | [diff] [blame] | 5631 | } |
| 5632 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5633 | private int getNetworkPermission(NetworkCapabilities nc) { |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5634 | if (!nc.hasCapability(NET_CAPABILITY_NOT_RESTRICTED)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5635 | return INetd.PERMISSION_SYSTEM; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5636 | } |
| 5637 | if (!nc.hasCapability(NET_CAPABILITY_FOREGROUND)) { |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5638 | return INetd.PERMISSION_NETWORK; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5639 | } |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5640 | return INetd.PERMISSION_NONE; |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 5641 | } |
| 5642 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5643 | /** |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5644 | * Augments the NetworkCapabilities passed in by a NetworkAgent with capabilities that are |
| 5645 | * maintained here that the NetworkAgent is not aware of (e.g., validated, captive portal, |
| 5646 | * and foreground status). |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5647 | */ |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5648 | private NetworkCapabilities mixInCapabilities(NetworkAgentInfo nai, NetworkCapabilities nc) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5649 | // Once a NetworkAgent is connected, complain if some immutable capabilities are removed. |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5650 | // Don't complain for VPNs since they're not driven by requests and there is no risk of |
| 5651 | // causing a connect/teardown loop. |
| 5652 | // TODO: remove this altogether and make it the responsibility of the NetworkFactories to |
| 5653 | // avoid connect/teardown loops. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5654 | if (nai.everConnected && |
Lorenzo Colitti | 6f192a5 | 2018-05-30 16:44:47 +0900 | [diff] [blame] | 5655 | !nai.isVPN() && |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5656 | !nai.networkCapabilities.satisfiedByImmutableNetworkCapabilities(nc)) { |
| 5657 | // 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] | 5658 | // does not cause any request (that is not a listen) currently matching that agent to |
| 5659 | // stop being matched by the updated agent. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5660 | String diff = nai.networkCapabilities.describeImmutableDifferences(nc); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5661 | if (!TextUtils.isEmpty(diff)) { |
Hugo Benichi | 86fc53a | 2017-08-16 13:19:04 +0900 | [diff] [blame] | 5662 | Slog.wtf(TAG, "BUG: " + nai + " lost immutable capabilities:" + diff); |
Hugo Benichi | 683ea48 | 2017-07-25 11:40:56 +0900 | [diff] [blame] | 5663 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5664 | } |
| 5665 | |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5666 | // Don't modify caller's NetworkCapabilities. |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5667 | NetworkCapabilities newNc = new NetworkCapabilities(nc); |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5668 | if (nai.lastValidated) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5669 | newNc.addCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5670 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5671 | newNc.removeCapability(NET_CAPABILITY_VALIDATED); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5672 | } |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5673 | if (nai.lastCaptivePortalDetected) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5674 | newNc.addCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5675 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5676 | newNc.removeCapability(NET_CAPABILITY_CAPTIVE_PORTAL); |
Paul Jensen | 3d194ea | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 5677 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5678 | if (nai.isBackgroundNetwork()) { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5679 | newNc.removeCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5680 | } else { |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5681 | newNc.addCapability(NET_CAPABILITY_FOREGROUND); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5682 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5683 | if (nai.isSuspended()) { |
| 5684 | newNc.removeCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5685 | } else { |
| 5686 | newNc.addCapability(NET_CAPABILITY_NOT_SUSPENDED); |
| 5687 | } |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 5688 | if (nai.partialConnectivity) { |
| 5689 | newNc.addCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); |
| 5690 | } else { |
| 5691 | newNc.removeCapability(NET_CAPABILITY_PARTIAL_CONNECTIVITY); |
| 5692 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5693 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5694 | return newNc; |
| 5695 | } |
| 5696 | |
| 5697 | /** |
| 5698 | * Update the NetworkCapabilities for {@code nai} to {@code nc}. Specifically: |
| 5699 | * |
| 5700 | * 1. Calls mixInCapabilities to merge the passed-in NetworkCapabilities {@code nc} with the |
| 5701 | * capabilities we manage and store in {@code nai}, such as validated status and captive |
| 5702 | * portal status) |
| 5703 | * 2. Takes action on the result: changes network permissions, sends CAP_CHANGED callbacks, and |
| 5704 | * potentially triggers rematches. |
| 5705 | * 3. Directly informs other network stack components (NetworkStatsService, VPNs, etc. of the |
| 5706 | * change.) |
| 5707 | * |
| 5708 | * @param oldScore score of the network before any of the changes that prompted us |
| 5709 | * to call this function. |
| 5710 | * @param nai the network having its capabilities updated. |
| 5711 | * @param nc the new network capabilities. |
| 5712 | */ |
| 5713 | private void updateCapabilities(int oldScore, NetworkAgentInfo nai, NetworkCapabilities nc) { |
| 5714 | NetworkCapabilities newNc = mixInCapabilities(nai, nc); |
| 5715 | |
| 5716 | if (Objects.equals(nai.networkCapabilities, newNc)) return; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5717 | |
Luke Huang | 8a462ec | 2018-08-24 20:33:16 +0800 | [diff] [blame] | 5718 | final int oldPermission = getNetworkPermission(nai.networkCapabilities); |
| 5719 | final int newPermission = getNetworkPermission(newNc); |
| 5720 | if (oldPermission != newPermission && nai.created && !nai.isVPN()) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5721 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5722 | mNMS.setNetworkPermission(nai.network.netId, newPermission); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5723 | } catch (RemoteException e) { |
| 5724 | loge("Exception in setNetworkPermission: " + e); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 5725 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5726 | } |
| 5727 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5728 | final NetworkCapabilities prevNc; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5729 | synchronized (nai) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5730 | prevNc = nai.networkCapabilities; |
Remi NGUYEN VAN | 3962f67 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 5731 | nai.setNetworkCapabilities(newNc); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5732 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5733 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5734 | updateUids(nai, prevNc, newNc); |
| 5735 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5736 | if (nai.getCurrentScore() == oldScore && newNc.equalRequestableCapabilities(prevNc)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5737 | // If the requestable capabilities haven't changed, and the score hasn't changed, then |
| 5738 | // the change we're processing can't affect any requests, it can only affect the listens |
| 5739 | // on this network. We might have been called by rematchNetworkAndRequests when a |
| 5740 | // network changed foreground state. |
| 5741 | processListenRequests(nai, true); |
| 5742 | } else { |
| 5743 | // If the requestable capabilities have changed or the score changed, we can't have been |
| 5744 | // called by rematchNetworkAndRequests, so it's safe to start a rematch. |
Paul Jensen | e098854 | 2015-06-25 15:30:08 -0400 | [diff] [blame] | 5745 | rematchAllNetworksAndRequests(nai, oldScore); |
| 5746 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 5747 | } |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5748 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5749 | if (prevNc != null) { |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5750 | final boolean oldMetered = prevNc.isMetered(); |
| 5751 | final boolean newMetered = newNc.isMetered(); |
| 5752 | final boolean meteredChanged = oldMetered != newMetered; |
| 5753 | |
| 5754 | if (meteredChanged) { |
| 5755 | maybeNotifyNetworkBlocked(nai, oldMetered, newMetered, mRestrictBackground, |
| 5756 | mRestrictBackground); |
| 5757 | } |
| 5758 | |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5759 | final boolean roamingChanged = prevNc.hasCapability(NET_CAPABILITY_NOT_ROAMING) != |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5760 | newNc.hasCapability(NET_CAPABILITY_NOT_ROAMING); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5761 | |
| 5762 | // Report changes that are interesting for network statistics tracking. |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5763 | if (meteredChanged || roamingChanged) { |
| 5764 | notifyIfacesChangedForNetworkStats(); |
| 5765 | } |
| 5766 | } |
| 5767 | |
Lorenzo Colitti | aa7f7e4 | 2018-01-16 00:52:07 +0900 | [diff] [blame] | 5768 | if (!newNc.hasTransport(TRANSPORT_VPN)) { |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5769 | // Tell VPNs about updated capabilities, since they may need to |
| 5770 | // bubble those changes through. |
Chalard Jean | 6b65ec7 | 2018-05-18 22:02:56 +0900 | [diff] [blame] | 5771 | updateAllVpnsCapabilities(); |
Jeff Sharkey | 72f9c42 | 2017-10-27 17:22:59 -0600 | [diff] [blame] | 5772 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5773 | } |
| 5774 | |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5775 | private void updateUids(NetworkAgentInfo nai, NetworkCapabilities prevNc, |
| 5776 | NetworkCapabilities newNc) { |
| 5777 | Set<UidRange> prevRanges = null == prevNc ? null : prevNc.getUids(); |
| 5778 | Set<UidRange> newRanges = null == newNc ? null : newNc.getUids(); |
| 5779 | if (null == prevRanges) prevRanges = new ArraySet<>(); |
| 5780 | if (null == newRanges) newRanges = new ArraySet<>(); |
| 5781 | final Set<UidRange> prevRangesCopy = new ArraySet<>(prevRanges); |
| 5782 | |
| 5783 | prevRanges.removeAll(newRanges); |
| 5784 | newRanges.removeAll(prevRangesCopy); |
| 5785 | |
| 5786 | try { |
| 5787 | if (!newRanges.isEmpty()) { |
| 5788 | final UidRange[] addedRangesArray = new UidRange[newRanges.size()]; |
| 5789 | newRanges.toArray(addedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5790 | mNMS.addVpnUidRanges(nai.network.netId, addedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5791 | } |
| 5792 | if (!prevRanges.isEmpty()) { |
| 5793 | final UidRange[] removedRangesArray = new UidRange[prevRanges.size()]; |
| 5794 | prevRanges.toArray(removedRangesArray); |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5795 | mNMS.removeVpnUidRanges(nai.network.netId, removedRangesArray); |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 5796 | } |
| 5797 | } catch (Exception e) { |
| 5798 | // Never crash! |
| 5799 | loge("Exception in updateUids: " + e); |
| 5800 | } |
| 5801 | } |
| 5802 | |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5803 | public void handleUpdateLinkProperties(NetworkAgentInfo nai, LinkProperties newLp) { |
Lorenzo Colitti | 94229b3 | 2019-02-20 21:34:01 +0900 | [diff] [blame] | 5804 | ensureRunningOnConnectivityServiceThread(); |
| 5805 | |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 5806 | if (getNetworkAgentInfoForNetId(nai.network.netId) != nai) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5807 | // Ignore updates for disconnected networks |
| 5808 | return; |
| 5809 | } |
Rubin Xu | 6c1f6fd | 2017-09-11 15:21:10 +0100 | [diff] [blame] | 5810 | // newLp is already a defensive copy. |
| 5811 | newLp.ensureDirectlyConnectedRoutes(); |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5812 | if (VDBG || DDBG) { |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5813 | log("Update of LinkProperties for " + nai.name() + |
| 5814 | "; created=" + nai.created + |
| 5815 | "; everConnected=" + nai.everConnected); |
| 5816 | } |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 5817 | updateLinkProperties(nai, newLp, new LinkProperties(nai.linkProperties)); |
Hugo Benichi | ef50288 | 2017-09-01 01:23:32 +0000 | [diff] [blame] | 5818 | } |
| 5819 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5820 | private void sendUpdatedScoreToFactories(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5821 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5822 | NetworkRequest nr = nai.requestAt(i); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5823 | // Don't send listening requests to factories. b/17393458 |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5824 | if (nr.isListen()) continue; |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5825 | sendUpdatedScoreToFactories(nr, nai); |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5826 | } |
| 5827 | } |
| 5828 | |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5829 | private void sendUpdatedScoreToFactories(NetworkRequest networkRequest, NetworkAgentInfo nai) { |
| 5830 | int score = 0; |
| 5831 | int serial = 0; |
| 5832 | if (nai != null) { |
| 5833 | score = nai.getCurrentScore(); |
| 5834 | serial = nai.factorySerialNumber; |
| 5835 | } |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 5836 | if (VDBG || DDBG){ |
| 5837 | log("sending new Min Network Score(" + score + "): " + networkRequest.toString()); |
| 5838 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 5839 | for (NetworkFactoryInfo nfi : mNetworkFactoryInfos.values()) { |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 5840 | nfi.asyncChannel.sendMessage(android.net.NetworkFactory.CMD_REQUEST_NETWORK, score, |
| 5841 | serial, networkRequest); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5842 | } |
| 5843 | } |
| 5844 | |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5845 | private void sendPendingIntentForRequest(NetworkRequestInfo nri, NetworkAgentInfo networkAgent, |
| 5846 | int notificationType) { |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5847 | if (notificationType == ConnectivityManager.CALLBACK_AVAILABLE && !nri.mPendingIntentSent) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5848 | Intent intent = new Intent(); |
Jeremy Joslin | a68e7d7 | 2014-11-26 14:24:15 -0800 | [diff] [blame] | 5849 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK, networkAgent.network); |
| 5850 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_REQUEST, nri.request); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5851 | nri.mPendingIntentSent = true; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5852 | sendIntent(nri.mPendingIntent, intent); |
| 5853 | } |
| 5854 | // else not handled |
| 5855 | } |
| 5856 | |
| 5857 | private void sendIntent(PendingIntent pendingIntent, Intent intent) { |
| 5858 | mPendingIntentWakeLock.acquire(); |
| 5859 | try { |
| 5860 | if (DBG) log("Sending " + pendingIntent); |
| 5861 | pendingIntent.send(mContext, 0, intent, this /* onFinished */, null /* Handler */); |
| 5862 | } catch (PendingIntent.CanceledException e) { |
| 5863 | if (DBG) log(pendingIntent + " was not sent, it had been canceled."); |
| 5864 | mPendingIntentWakeLock.release(); |
| 5865 | releasePendingNetworkRequest(pendingIntent); |
| 5866 | } |
| 5867 | // ...otherwise, mPendingIntentWakeLock.release() gets called by onSendFinished() |
| 5868 | } |
| 5869 | |
| 5870 | @Override |
| 5871 | public void onSendFinished(PendingIntent pendingIntent, Intent intent, int resultCode, |
| 5872 | String resultData, Bundle resultExtras) { |
| 5873 | if (DBG) log("Finished sending " + pendingIntent); |
| 5874 | mPendingIntentWakeLock.release(); |
Jeremy Joslin | 7929484 | 2014-12-03 17:15:28 -0800 | [diff] [blame] | 5875 | // Release with a delay so the receiving client has an opportunity to put in its |
| 5876 | // own request. |
| 5877 | releasePendingNetworkRequestWithDelay(pendingIntent); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 5878 | } |
| 5879 | |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5880 | private void callCallbackForRequest(NetworkRequestInfo nri, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5881 | NetworkAgentInfo networkAgent, int notificationType, int arg1) { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5882 | if (nri.messenger == null) { |
| 5883 | return; // Default request has no msgr |
| 5884 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5885 | Bundle bundle = new Bundle(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5886 | // TODO: check if defensive copies of data is needed. |
| 5887 | putParcelable(bundle, new NetworkRequest(nri.request)); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5888 | Message msg = Message.obtain(); |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5889 | if (notificationType != ConnectivityManager.CALLBACK_UNAVAIL) { |
| 5890 | putParcelable(bundle, networkAgent.network); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5891 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5892 | switch (notificationType) { |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5893 | case ConnectivityManager.CALLBACK_AVAILABLE: { |
Etan Cohen | 836ad57 | 2018-12-30 17:59:59 -0800 | [diff] [blame] | 5894 | putParcelable(bundle, networkCapabilitiesRestrictedForCallerPermissions( |
| 5895 | networkAgent.networkCapabilities, nri.mPid, nri.mUid)); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5896 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5897 | // For this notification, arg1 contains the blocked status. |
| 5898 | msg.arg1 = arg1; |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 5899 | break; |
| 5900 | } |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5901 | case ConnectivityManager.CALLBACK_LOSING: { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5902 | msg.arg1 = arg1; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5903 | break; |
| 5904 | } |
| 5905 | case ConnectivityManager.CALLBACK_CAP_CHANGED: { |
Chalard Jean | f19db37 | 2018-01-26 19:24:40 +0900 | [diff] [blame] | 5906 | // 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] | 5907 | final NetworkCapabilities nc = networkCapabilitiesRestrictedForCallerPermissions( |
Chalard Jean | b552c46 | 2018-02-21 18:43:54 +0900 | [diff] [blame] | 5908 | networkAgent.networkCapabilities, nri.mPid, nri.mUid); |
Chalard Jean | dda156a | 2018-01-10 21:19:32 +0900 | [diff] [blame] | 5909 | putParcelable(bundle, nc); |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5910 | break; |
| 5911 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5912 | case ConnectivityManager.CALLBACK_IP_CHANGED: { |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5913 | putParcelable(bundle, new LinkProperties(networkAgent.linkProperties)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5914 | break; |
| 5915 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5916 | case ConnectivityManager.CALLBACK_BLK_CHANGED: { |
junyulai | e6b3651 | 2018-10-24 22:38:06 +0800 | [diff] [blame] | 5917 | maybeLogBlockedStatusChanged(nri, networkAgent.network, arg1 != 0); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 5918 | msg.arg1 = arg1; |
| 5919 | break; |
| 5920 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5921 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5922 | msg.what = notificationType; |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 5923 | msg.setData(bundle); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5924 | try { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5925 | if (VDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 5926 | String notification = ConnectivityManager.getCallbackName(notificationType); |
| 5927 | log("sending notification " + notification + " for " + nri.request); |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5928 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 5929 | nri.messenger.send(msg); |
| 5930 | } catch (RemoteException e) { |
| 5931 | // may occur naturally in the race of binder death. |
| 5932 | loge("RemoteException caught trying to send a callback msg for " + nri.request); |
| 5933 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5934 | } |
| 5935 | |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 5936 | private static <T extends Parcelable> void putParcelable(Bundle bundle, T t) { |
| 5937 | bundle.putParcelable(t.getClass().getSimpleName(), t); |
| 5938 | } |
| 5939 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5940 | private void teardownUnneededNetwork(NetworkAgentInfo nai) { |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5941 | if (nai.numRequestNetworkRequests() != 0) { |
| 5942 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 5943 | NetworkRequest nr = nai.requestAt(i); |
| 5944 | // Ignore listening requests. |
Lorenzo Colitti | f4a45f4 | 2016-07-18 18:17:08 +0900 | [diff] [blame] | 5945 | if (nr.isListen()) continue; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 5946 | loge("Dead network still had at least " + nr); |
| 5947 | break; |
| 5948 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5949 | } |
| 5950 | nai.asyncChannel.disconnect(); |
| 5951 | } |
| 5952 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5953 | private void handleLingerComplete(NetworkAgentInfo oldNetwork) { |
| 5954 | if (oldNetwork == null) { |
| 5955 | loge("Unknown NetworkAgentInfo in handleLingerComplete"); |
| 5956 | return; |
| 5957 | } |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 5958 | if (DBG) log("handleLingerComplete for " + oldNetwork.name()); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5959 | |
| 5960 | // If we get here it means that the last linger timeout for this network expired. So there |
| 5961 | // must be no other active linger timers, and we must stop lingering. |
| 5962 | oldNetwork.clearLingerState(); |
| 5963 | |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 5964 | if (unneeded(oldNetwork, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5965 | // Tear the network down. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5966 | teardownUnneededNetwork(oldNetwork); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5967 | } else { |
| 5968 | // Put the network in the background. |
Lorenzo Colitti | b8167f6 | 2016-09-15 22:47:08 +0900 | [diff] [blame] | 5969 | updateCapabilities(oldNetwork.getCurrentScore(), oldNetwork, |
| 5970 | oldNetwork.networkCapabilities); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 5971 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 5972 | } |
| 5973 | |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 5974 | private void makeDefault(NetworkAgentInfo newNetwork) { |
Robert Greenwalt | fc0c689 | 2014-08-27 14:34:02 -0700 | [diff] [blame] | 5975 | if (DBG) log("Switching to new default network: " + newNetwork); |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 5976 | |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5977 | try { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 5978 | mNMS.setDefaultNetId(newNetwork.network.netId); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5979 | } catch (Exception e) { |
| 5980 | loge("Exception setting default network :" + e); |
| 5981 | } |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5982 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 5983 | notifyLockdownVpn(newNetwork); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5984 | handleApplyDefaultProxy(newNetwork.linkProperties.getHttpProxy()); |
lucaslin | 041a1af | 2018-11-28 19:27:52 +0800 | [diff] [blame] | 5985 | updateTcpBufferSizes(newNetwork.linkProperties.getTcpBufferSizes()); |
Erik Kline | 1742fe1 | 2017-12-13 19:40:49 +0900 | [diff] [blame] | 5986 | mDnsManager.setDefaultDnsSystemProperties(newNetwork.linkProperties.getDnsServers()); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 5987 | notifyIfacesChangedForNetworkStats(); |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 5988 | // Fix up the NetworkCapabilities of any VPNs that don't specify underlying networks. |
| 5989 | updateAllVpnsCapabilities(); |
Paul Jensen | 27b02b7 | 2014-07-14 12:03:33 -0400 | [diff] [blame] | 5990 | } |
| 5991 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 5992 | private void processListenRequests(NetworkAgentInfo nai, boolean capabilitiesChanged) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 5993 | // For consistency with previous behaviour, send onLost callbacks before onAvailable. |
| 5994 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 5995 | NetworkRequest nr = nri.request; |
| 5996 | if (!nr.isListen()) continue; |
| 5997 | if (nai.isSatisfyingRequest(nr.requestId) && !nai.satisfies(nr)) { |
| 5998 | nai.removeRequest(nri.request.requestId); |
| 5999 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_LOST, 0); |
| 6000 | } |
| 6001 | } |
| 6002 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6003 | if (capabilitiesChanged) { |
| 6004 | notifyNetworkCallbacks(nai, ConnectivityManager.CALLBACK_CAP_CHANGED); |
| 6005 | } |
| 6006 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6007 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
| 6008 | NetworkRequest nr = nri.request; |
| 6009 | if (!nr.isListen()) continue; |
| 6010 | if (nai.satisfies(nr) && !nai.isSatisfyingRequest(nr.requestId)) { |
| 6011 | nai.addRequest(nr); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6012 | notifyNetworkAvailable(nai, nri); |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6013 | } |
| 6014 | } |
| 6015 | } |
| 6016 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6017 | // Handles a network appearing or improving its score. |
| 6018 | // |
| 6019 | // - Evaluates all current NetworkRequests that can be |
| 6020 | // satisfied by newNetwork, and reassigns to newNetwork |
| 6021 | // any such requests for which newNetwork is the best. |
| 6022 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6023 | // - Lingers any validated Networks that as a result are no longer |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6024 | // needed. A network is needed if it is the best network for |
| 6025 | // one or more NetworkRequests, or if it is a VPN. |
| 6026 | // |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 6027 | // - Tears down newNetwork if it just became validated |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6028 | // but turns out to be unneeded. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6029 | // |
| 6030 | // - If reapUnvalidatedNetworks==REAP, tears down unvalidated |
| 6031 | // networks that have no chance (i.e. even if validated) |
| 6032 | // of becoming the highest scoring network. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6033 | // |
| 6034 | // NOTE: This function only adds NetworkRequests that "newNetwork" could satisfy, |
| 6035 | // it does not remove NetworkRequests that other Networks could better satisfy. |
| 6036 | // If you need to handle decreases in score, use {@link rematchAllNetworksAndRequests}. |
| 6037 | // This function should be used when possible instead of {@code rematchAllNetworksAndRequests} |
| 6038 | // as it performs better by a factor of the number of Networks. |
Paul Jensen | 4b9b2be | 2014-09-26 10:10:22 -0400 | [diff] [blame] | 6039 | // |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6040 | // @param newNetwork is the network to be matched against NetworkRequests. |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6041 | // @param reapUnvalidatedNetworks indicates if an additional pass over all networks should be |
| 6042 | // performed to tear down unvalidated networks that have no chance (i.e. even if |
| 6043 | // validated) of becoming the highest scoring network. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6044 | private void rematchNetworkAndRequests(NetworkAgentInfo newNetwork, |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6045 | ReapUnvalidatedNetworks reapUnvalidatedNetworks, long now) { |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6046 | if (!newNetwork.everConnected) return; |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6047 | boolean keep = newNetwork.isVPN(); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6048 | boolean isNewDefault = false; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 6049 | NetworkAgentInfo oldDefaultNetwork = null; |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6050 | |
| 6051 | final boolean wasBackgroundNetwork = newNetwork.isBackgroundNetwork(); |
| 6052 | final int score = newNetwork.getCurrentScore(); |
| 6053 | |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6054 | if (VDBG || DDBG) log("rematching " + newNetwork.name()); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6055 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6056 | // Find and migrate to this Network any NetworkRequests for |
| 6057 | // which this network is now the best. |
Chalard Jean | 4133a12 | 2018-06-04 13:33:12 +0900 | [diff] [blame] | 6058 | ArrayList<NetworkAgentInfo> affectedNetworks = new ArrayList<>(); |
| 6059 | ArrayList<NetworkRequestInfo> addedRequests = new ArrayList<>(); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6060 | NetworkCapabilities nc = newNetwork.networkCapabilities; |
| 6061 | if (VDBG) log(" network has: " + nc); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6062 | for (NetworkRequestInfo nri : mNetworkRequests.values()) { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6063 | // Process requests in the first pass and listens in the second pass. This allows us to |
| 6064 | // change a network's capabilities depending on which requests it has. This is only |
| 6065 | // correct if the change in capabilities doesn't affect whether the network satisfies |
| 6066 | // requests or not, and doesn't affect the network's score. |
| 6067 | if (nri.request.isListen()) continue; |
| 6068 | |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6069 | final NetworkAgentInfo currentNetwork = getNetworkForRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6070 | final boolean satisfies = newNetwork.satisfies(nri.request); |
| 6071 | if (newNetwork == currentNetwork && satisfies) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6072 | if (VDBG) { |
Robert Greenwalt | e73cc46 | 2014-09-07 16:50:01 -0700 | [diff] [blame] | 6073 | log("Network " + newNetwork.name() + " was already satisfying" + |
| 6074 | " request " + nri.request.requestId + ". No change."); |
| 6075 | } |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 6076 | keep = true; |
| 6077 | continue; |
| 6078 | } |
| 6079 | |
| 6080 | // check if it satisfies the NetworkCapabilities |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6081 | if (VDBG) log(" checking if request is satisfied: " + nri.request); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6082 | if (satisfies) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6083 | // next check if it's better than any current network we're using for |
| 6084 | // this request |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6085 | if (VDBG || DDBG) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6086 | log("currentScore = " + |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6087 | (currentNetwork != null ? currentNetwork.getCurrentScore() : 0) + |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6088 | ", newScore = " + score); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6089 | } |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6090 | if (currentNetwork == null || currentNetwork.getCurrentScore() < score) { |
Lorenzo Colitti | 39d2bb5 | 2016-04-08 23:09:09 +0900 | [diff] [blame] | 6091 | if (VDBG) log("rematch for " + newNetwork.name()); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6092 | if (currentNetwork != null) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6093 | if (VDBG || DDBG){ |
| 6094 | log(" accepting network in place of " + currentNetwork.name()); |
| 6095 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6096 | currentNetwork.removeRequest(nri.request.requestId); |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6097 | currentNetwork.lingerRequest(nri.request, now, mLingerDelayMs); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6098 | affectedNetworks.add(currentNetwork); |
| 6099 | } else { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6100 | if (VDBG || DDBG) log(" accepting network in place of null"); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6101 | } |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6102 | newNetwork.unlingerRequest(nri.request); |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6103 | setNetworkForRequest(nri.request.requestId, newNetwork); |
Paul Jensen | 3d91146 | 2015-06-12 06:40:24 -0400 | [diff] [blame] | 6104 | if (!newNetwork.addRequest(nri.request)) { |
| 6105 | Slog.wtf(TAG, "BUG: " + newNetwork.name() + " already has " + nri.request); |
| 6106 | } |
| 6107 | addedRequests.add(nri); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6108 | keep = true; |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6109 | // Tell NetworkFactories about the new score, so they can stop |
| 6110 | // trying to connect if they know they cannot match it. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6111 | // 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] | 6112 | // network. Think about if there is a way to reduce this. Push |
| 6113 | // netid->request mapping to each factory? |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 6114 | sendUpdatedScoreToFactories(nri.request, newNetwork); |
Lorenzo Colitti | 5526f9c | 2016-08-22 16:46:40 +0900 | [diff] [blame] | 6115 | if (isDefaultRequest(nri)) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6116 | isNewDefault = true; |
Paul Jensen | f4ffaa4 | 2014-12-15 11:56:18 -0500 | [diff] [blame] | 6117 | oldDefaultNetwork = currentNetwork; |
Lorenzo Colitti | c2e10bb | 2016-08-29 14:03:11 +0900 | [diff] [blame] | 6118 | if (currentNetwork != null) { |
| 6119 | mLingerMonitor.noteLingerDefaultNetwork(currentNetwork, newNetwork); |
| 6120 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6121 | } |
| 6122 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6123 | } else if (newNetwork.isSatisfyingRequest(nri.request.requestId)) { |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6124 | // If "newNetwork" is listed as satisfying "nri" but no longer satisfies "nri", |
| 6125 | // mark it as no longer satisfying "nri". Because networks are processed by |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 6126 | // rematchAllNetworksAndRequests() in descending score order, "currentNetwork" will |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6127 | // match "newNetwork" before this loop will encounter a "currentNetwork" with higher |
| 6128 | // score than "newNetwork" and where "currentNetwork" no longer satisfies "nri". |
| 6129 | // This means this code doesn't have to handle the case where "currentNetwork" no |
| 6130 | // longer satisfies "nri" when "currentNetwork" does not equal "newNetwork". |
| 6131 | if (DBG) { |
| 6132 | log("Network " + newNetwork.name() + " stopped satisfying" + |
| 6133 | " request " + nri.request.requestId); |
| 6134 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6135 | newNetwork.removeRequest(nri.request.requestId); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6136 | if (currentNetwork == newNetwork) { |
Hugo Benichi | cd95278 | 2017-09-20 11:20:14 +0900 | [diff] [blame] | 6137 | clearNetworkForRequest(nri.request.requestId); |
Chalard Jean | 08577fc | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 6138 | sendUpdatedScoreToFactories(nri.request, null); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6139 | } else { |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6140 | Slog.wtf(TAG, "BUG: Removing request " + nri.request.requestId + " from " + |
| 6141 | newNetwork.name() + |
| 6142 | " without updating mNetworkForRequestId or factories!"); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6143 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6144 | // TODO: Technically, sending CALLBACK_LOST here is |
| 6145 | // incorrect if there is a replacement network currently |
| 6146 | // connected that can satisfy nri, which is a request |
| 6147 | // (not a listen). However, the only capability that can both |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6148 | // a) be requested and b) change is NET_CAPABILITY_TRUSTED, |
| 6149 | // so this code is only incorrect for a network that loses |
| 6150 | // the TRUSTED capability, which is a rare case. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6151 | callCallbackForRequest(nri, newNetwork, ConnectivityManager.CALLBACK_LOST, 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6152 | } |
| 6153 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6154 | if (isNewDefault) { |
Chiachang Wang | a609304 | 2018-09-28 22:42:48 +0800 | [diff] [blame] | 6155 | updateDataActivityTracking(newNetwork, oldDefaultNetwork); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6156 | // Notify system services that this network is up. |
Hugo Benichi | 1654b1d | 2016-05-24 11:50:31 +0900 | [diff] [blame] | 6157 | makeDefault(newNetwork); |
| 6158 | // 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] | 6159 | metricsLogger().defaultNetworkMetrics().logDefaultNetworkEvent( |
Hugo Benichi | 380a063 | 2017-10-20 09:25:29 +0900 | [diff] [blame] | 6160 | now, newNetwork, oldDefaultNetwork); |
Hugo Benichi | 4c31b34 | 2017-03-30 23:18:10 +0900 | [diff] [blame] | 6161 | // Have a new default network, release the transition wakelock in |
| 6162 | scheduleReleaseNetworkTransitionWakelock(); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6163 | } |
| 6164 | |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6165 | if (!newNetwork.networkCapabilities.equalRequestableCapabilities(nc)) { |
| 6166 | Slog.wtf(TAG, String.format( |
| 6167 | "BUG: %s changed requestable capabilities during rematch: %s -> %s", |
Andreas Gampe | d4f64c4 | 2017-07-11 15:14:41 -0700 | [diff] [blame] | 6168 | newNetwork.name(), nc, newNetwork.networkCapabilities)); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6169 | } |
| 6170 | if (newNetwork.getCurrentScore() != score) { |
| 6171 | Slog.wtf(TAG, String.format( |
| 6172 | "BUG: %s changed score during rematch: %d -> %d", |
liangweikang@xiaomi.com | e9a7c26 | 2017-06-29 14:36:30 +0800 | [diff] [blame] | 6173 | newNetwork.name(), score, newNetwork.getCurrentScore())); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6174 | } |
| 6175 | |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6176 | // Second pass: process all listens. |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6177 | if (wasBackgroundNetwork != newNetwork.isBackgroundNetwork()) { |
| 6178 | // If the network went from background to foreground or vice versa, we need to update |
| 6179 | // its foreground state. It is safe to do this after rematching the requests because |
| 6180 | // NET_CAPABILITY_FOREGROUND does not affect requests, as is not a requestable |
| 6181 | // 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] | 6182 | updateCapabilities(score, newNetwork, newNetwork.networkCapabilities); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6183 | } else { |
| 6184 | processListenRequests(newNetwork, false); |
| 6185 | } |
Lorenzo Colitti | 72bbf48 | 2016-07-20 02:39:22 +0900 | [diff] [blame] | 6186 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6187 | // do this after the default net is switched, but |
| 6188 | // before LegacyTypeTracker sends legacy broadcasts |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6189 | for (NetworkRequestInfo nri : addedRequests) notifyNetworkAvailable(newNetwork, nri); |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6190 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6191 | // Linger any networks that are no longer needed. This should be done after sending the |
| 6192 | // available callback for newNetwork. |
| 6193 | for (NetworkAgentInfo nai : affectedNetworks) { |
| 6194 | updateLingerState(nai, now); |
| 6195 | } |
| 6196 | // Possibly unlinger newNetwork. Unlingering a network does not send any callbacks so it |
| 6197 | // does not need to be done in any particular order. |
| 6198 | updateLingerState(newNetwork, now); |
| 6199 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6200 | if (isNewDefault) { |
| 6201 | // Maintain the illusion: since the legacy API only |
| 6202 | // understands one network at a time, we must pretend |
| 6203 | // that the current default network disconnected before |
| 6204 | // the new one connected. |
| 6205 | if (oldDefaultNetwork != null) { |
| 6206 | mLegacyTypeTracker.remove(oldDefaultNetwork.networkInfo.getType(), |
| 6207 | oldDefaultNetwork, true); |
| 6208 | } |
| 6209 | mDefaultInetConditionPublished = newNetwork.lastValidated ? 100 : 0; |
| 6210 | mLegacyTypeTracker.add(newNetwork.networkInfo.getType(), newNetwork); |
| 6211 | notifyLockdownVpn(newNetwork); |
| 6212 | } |
| 6213 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6214 | if (keep) { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6215 | // Notify battery stats service about this network, both the normal |
| 6216 | // interface and any stacked links. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6217 | // 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] | 6218 | try { |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6219 | final IBatteryStats bs = BatteryStatsService.getService(); |
| 6220 | final int type = newNetwork.networkInfo.getType(); |
| 6221 | |
| 6222 | final String baseIface = newNetwork.linkProperties.getInterfaceName(); |
| 6223 | bs.noteNetworkInterfaceType(baseIface, type); |
| 6224 | for (LinkProperties stacked : newNetwork.linkProperties.getStackedLinks()) { |
| 6225 | final String stackedIface = stacked.getInterfaceName(); |
| 6226 | bs.noteNetworkInterfaceType(stackedIface, type); |
Jeff Sharkey | eb2c2c7 | 2014-08-11 15:22:51 -0700 | [diff] [blame] | 6227 | } |
| 6228 | } catch (RemoteException ignored) { |
| 6229 | } |
| 6230 | |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6231 | // This has to happen after the notifyNetworkCallbacks as that tickles each |
| 6232 | // ConnectivityManager instance so that legacy requests correctly bind dns |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6233 | // requests to this network. The legacy users are listening for this broadcast |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6234 | // and will generally do a dns request so they can ensureRouteToHost and if |
| 6235 | // they do that before the callbacks happen they'll use the default network. |
| 6236 | // |
| 6237 | // TODO: Is there still a race here? We send the broadcast |
| 6238 | // after sending the callback, but if the app can receive the |
| 6239 | // broadcast before the callback, it might still break. |
| 6240 | // |
| 6241 | // This *does* introduce a race where if the user uses the new api |
| 6242 | // (notification callbacks) and then uses the old api (getNetworkInfo(type)) |
| 6243 | // they may get old info. Reverse this after the old startUsing api is removed. |
| 6244 | // 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] | 6245 | for (int i = 0; i < newNetwork.numNetworkRequests(); i++) { |
| 6246 | NetworkRequest nr = newNetwork.requestAt(i); |
Lorenzo Colitti | b35d40d | 2016-07-01 13:19:21 +0900 | [diff] [blame] | 6247 | if (nr.legacyType != TYPE_NONE && nr.isRequest()) { |
Robert Greenwalt | 152ed37 | 2014-12-17 17:19:53 -0800 | [diff] [blame] | 6248 | // legacy type tracker filters out repeat adds |
| 6249 | mLegacyTypeTracker.add(nr.legacyType, newNetwork); |
| 6250 | } |
| 6251 | } |
Sreeram Ramachandran | 60c0c0d | 2014-10-30 14:55:29 -0700 | [diff] [blame] | 6252 | |
| 6253 | // A VPN generally won't get added to the legacy tracker in the "for (nri)" loop above, |
| 6254 | // because usually there are no NetworkRequests it satisfies (e.g., mDefaultRequest |
| 6255 | // wants the NOT_VPN capability, so it will never be satisfied by a VPN). So, add the |
| 6256 | // newNetwork to the tracker explicitly (it's a no-op if it has already been added). |
| 6257 | if (newNetwork.isVPN()) { |
| 6258 | mLegacyTypeTracker.add(TYPE_VPN, newNetwork); |
| 6259 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6260 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6261 | if (reapUnvalidatedNetworks == ReapUnvalidatedNetworks.REAP) { |
| 6262 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
Lorenzo Colitti | 3d4a106 | 2016-09-09 18:48:56 +0900 | [diff] [blame] | 6263 | if (unneeded(nai, UnneededFor.TEARDOWN)) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6264 | if (nai.getLingerExpiry() > 0) { |
| 6265 | // This network has active linger timers and no requests, but is not |
| 6266 | // lingering. Linger it. |
| 6267 | // |
| 6268 | // One way (the only way?) this can happen if this network is unvalidated |
| 6269 | // and became unneeded due to another network improving its score to the |
| 6270 | // point where this network will no longer be able to satisfy any requests |
| 6271 | // even if it validates. |
| 6272 | updateLingerState(nai, now); |
| 6273 | } else { |
| 6274 | if (DBG) log("Reaping " + nai.name()); |
| 6275 | teardownUnneededNetwork(nai); |
| 6276 | } |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6277 | } |
| 6278 | } |
| 6279 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6280 | } |
| 6281 | |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6282 | /** |
| 6283 | * Attempt to rematch all Networks with NetworkRequests. This may result in Networks |
| 6284 | * being disconnected. |
| 6285 | * @param changed If only one Network's score or capabilities have been modified since the last |
| 6286 | * time this function was called, pass this Network in this argument, otherwise pass |
| 6287 | * null. |
| 6288 | * @param oldScore If only one Network has been changed but its NetworkCapabilities have not |
| 6289 | * changed, pass in the Network's score (from getCurrentScore()) prior to the change via |
| 6290 | * this argument, otherwise pass {@code changed.getCurrentScore()} or 0 if |
| 6291 | * {@code changed} is {@code null}. This is because NetworkCapabilities influence a |
| 6292 | * network's score. |
Paul Jensen | 1c7ba02 | 2015-06-16 14:27:36 -0400 | [diff] [blame] | 6293 | */ |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6294 | private void rematchAllNetworksAndRequests(NetworkAgentInfo changed, int oldScore) { |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6295 | // TODO: This may get slow. The "changed" parameter is provided for future optimization |
| 6296 | // to avoid the slowness. It is not simply enough to process just "changed", for |
| 6297 | // 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] | 6298 | // satisfying a NetworkRequest that "changed" currently satisfies. |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6299 | |
| 6300 | // Optimization: Only reprocess "changed" if its score improved. This is safe because it |
| 6301 | // can only add more NetworkRequests satisfied by "changed", and this is exactly what |
| 6302 | // rematchNetworkAndRequests() handles. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6303 | final long now = SystemClock.elapsedRealtime(); |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6304 | if (changed != null && oldScore < changed.getCurrentScore()) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6305 | rematchNetworkAndRequests(changed, ReapUnvalidatedNetworks.REAP, now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6306 | } else { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6307 | final NetworkAgentInfo[] nais = mNetworkAgentInfos.values().toArray( |
| 6308 | new NetworkAgentInfo[mNetworkAgentInfos.size()]); |
| 6309 | // Rematch higher scoring networks first to prevent requests first matching a lower |
| 6310 | // scoring network and then a higher scoring network, which could produce multiple |
| 6311 | // callbacks and inadvertently unlinger networks. |
| 6312 | Arrays.sort(nais); |
| 6313 | for (NetworkAgentInfo nai : nais) { |
| 6314 | rematchNetworkAndRequests(nai, |
Paul Jensen | b10e37f | 2014-11-25 12:33:08 -0500 | [diff] [blame] | 6315 | // Only reap the last time through the loop. Reaping before all rematching |
| 6316 | // is complete could incorrectly teardown a network that hasn't yet been |
| 6317 | // rematched. |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6318 | (nai != nais[nais.length-1]) ? ReapUnvalidatedNetworks.DONT_REAP |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6319 | : ReapUnvalidatedNetworks.REAP, |
| 6320 | now); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6321 | } |
| 6322 | } |
| 6323 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6324 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6325 | private void updateInetCondition(NetworkAgentInfo nai) { |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6326 | // 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] | 6327 | if (!nai.everValidated) return; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6328 | // For now only update icons for default connection. |
| 6329 | // TODO: Update WiFi and cellular icons separately. b/17237507 |
| 6330 | if (!isDefaultNetwork(nai)) return; |
| 6331 | |
Lorenzo Colitti | 7b42f39 | 2014-12-17 11:26:49 +0900 | [diff] [blame] | 6332 | int newInetCondition = nai.lastValidated ? 100 : 0; |
Paul Jensen | ad50a1f | 2014-09-05 12:06:44 -0400 | [diff] [blame] | 6333 | // Don't repeat publish. |
| 6334 | if (newInetCondition == mDefaultInetConditionPublished) return; |
| 6335 | |
| 6336 | mDefaultInetConditionPublished = newInetCondition; |
| 6337 | sendInetConditionBroadcast(nai.networkInfo); |
| 6338 | } |
| 6339 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6340 | private void notifyLockdownVpn(NetworkAgentInfo nai) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6341 | synchronized (mVpns) { |
| 6342 | if (mLockdownTracker != null) { |
| 6343 | if (nai != null && nai.isVPN()) { |
| 6344 | mLockdownTracker.onVpnStateChanged(nai.networkInfo); |
| 6345 | } else { |
| 6346 | mLockdownTracker.onNetworkInfoChanged(); |
| 6347 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6348 | } |
| 6349 | } |
| 6350 | } |
| 6351 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6352 | private void updateNetworkInfo(NetworkAgentInfo networkAgent, NetworkInfo newInfo) { |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6353 | final NetworkInfo.State state = newInfo.getState(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6354 | NetworkInfo oldInfo = null; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6355 | final int oldScore = networkAgent.getCurrentScore(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 6356 | synchronized (networkAgent) { |
| 6357 | oldInfo = networkAgent.networkInfo; |
| 6358 | networkAgent.networkInfo = newInfo; |
| 6359 | } |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 6360 | notifyLockdownVpn(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6361 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6362 | if (DBG) { |
| 6363 | log(networkAgent.name() + " EVENT_NETWORK_INFO_CHANGED, going from " + |
| 6364 | (oldInfo == null ? "null" : oldInfo.getState()) + |
| 6365 | " to " + state); |
| 6366 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6367 | |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6368 | if (!networkAgent.created |
| 6369 | && (state == NetworkInfo.State.CONNECTED |
| 6370 | || (state == NetworkInfo.State.CONNECTING && networkAgent.isVPN()))) { |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6371 | |
| 6372 | // A network that has just connected has zero requests and is thus a foreground network. |
| 6373 | networkAgent.networkCapabilities.addCapability(NET_CAPABILITY_FOREGROUND); |
| 6374 | |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6375 | try { |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6376 | // 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] | 6377 | if (networkAgent.isVPN()) { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6378 | mNMS.createVirtualNetwork(networkAgent.network.netId, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 6379 | (networkAgent.networkMisc == null || |
| 6380 | !networkAgent.networkMisc.allowBypass)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6381 | } else { |
Luke Huang | 4e25ec6 | 2018-09-27 16:58:23 +0800 | [diff] [blame] | 6382 | mNMS.createPhysicalNetwork(networkAgent.network.netId, |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 6383 | getNetworkPermission(networkAgent.networkCapabilities)); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6384 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6385 | } catch (Exception e) { |
Lorenzo Colitti | bce0106 | 2014-05-29 14:05:41 +0900 | [diff] [blame] | 6386 | loge("Error creating network " + networkAgent.network.netId + ": " |
| 6387 | + e.getMessage()); |
| 6388 | return; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6389 | } |
Paul Jensen | eec7541 | 2014-08-04 12:21:19 -0400 | [diff] [blame] | 6390 | networkAgent.created = true; |
Robin Lee | 585e248 | 2016-05-01 23:00:00 +0100 | [diff] [blame] | 6391 | } |
| 6392 | |
| 6393 | if (!networkAgent.everConnected && state == NetworkInfo.State.CONNECTED) { |
| 6394 | networkAgent.everConnected = true; |
| 6395 | |
Lorenzo Colitti | 2df4c7d | 2018-02-27 22:47:01 +0900 | [diff] [blame] | 6396 | if (networkAgent.linkProperties == null) { |
| 6397 | Slog.wtf(TAG, networkAgent.name() + " connected with null LinkProperties"); |
| 6398 | } |
| 6399 | |
lucaslin | 8175ac99 | 2019-04-03 17:09:28 +0800 | [diff] [blame] | 6400 | // NetworkCapabilities need to be set before sending the private DNS config to |
| 6401 | // NetworkMonitor, otherwise NetworkMonitor cannot determine if validation is required. |
| 6402 | synchronized (networkAgent) { |
| 6403 | networkAgent.setNetworkCapabilities(networkAgent.networkCapabilities); |
| 6404 | } |
Erik Kline | 736353a | 2018-03-21 07:18:33 -0700 | [diff] [blame] | 6405 | handlePerNetworkPrivateDnsConfig(networkAgent, mDnsManager.getPrivateDnsConfig()); |
lucaslin | 25a4ec3 | 2018-11-28 12:51:55 +0800 | [diff] [blame] | 6406 | updateLinkProperties(networkAgent, new LinkProperties(networkAgent.linkProperties), |
| 6407 | null); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6408 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6409 | // Until parceled LinkProperties are sent directly to NetworkMonitor, the connect |
| 6410 | // command must be sent after updating LinkProperties to maximize chances of |
| 6411 | // NetworkMonitor seeing the correct LinkProperties when starting. |
| 6412 | // TODO: pass LinkProperties to the NetworkMonitor in the notifyNetworkConnected call. |
| 6413 | try { |
lucaslin | 4333899 | 2019-03-20 18:21:59 +0800 | [diff] [blame] | 6414 | if (networkAgent.networkMisc.acceptPartialConnectivity) { |
| 6415 | networkAgent.networkMonitor().setAcceptPartialConnectivity(); |
| 6416 | } |
Remi NGUYEN VAN | 3962f67 | 2019-03-27 15:42:53 +0900 | [diff] [blame] | 6417 | networkAgent.networkMonitor().notifyNetworkConnected( |
| 6418 | networkAgent.linkProperties, networkAgent.networkCapabilities); |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 6419 | } catch (RemoteException e) { |
| 6420 | e.rethrowFromSystemServer(); |
| 6421 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 6422 | scheduleUnvalidatedPrompt(networkAgent); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6423 | |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6424 | // Whether a particular NetworkRequest listen should cause signal strength thresholds to |
| 6425 | // be communicated to a particular NetworkAgent depends only on the network's immutable, |
| 6426 | // capabilities, so it only needs to be done once on initial connect, not every time the |
| 6427 | // network's capabilities change. Note that we do this before rematching the network, |
| 6428 | // so we could decide to tear it down immediately afterwards. That's fine though - on |
| 6429 | // disconnection NetworkAgents should stop any signal strength monitoring they have been |
| 6430 | // doing. |
Lorenzo Colitti | 6bc0a2b | 2015-09-15 15:56:01 +0900 | [diff] [blame] | 6431 | updateSignalStrengthThresholds(networkAgent, "CONNECT", null); |
Lorenzo Colitti | c3f21f3 | 2015-07-06 23:50:27 +0900 | [diff] [blame] | 6432 | |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6433 | if (networkAgent.isVPN()) { |
| 6434 | updateAllVpnsCapabilities(); |
| 6435 | } |
| 6436 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6437 | // Consider network even though it is not yet validated. |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6438 | final long now = SystemClock.elapsedRealtime(); |
| 6439 | rematchNetworkAndRequests(networkAgent, ReapUnvalidatedNetworks.REAP, now); |
Lorenzo Colitti | bdc4549 | 2015-04-09 14:35:26 +0900 | [diff] [blame] | 6440 | |
| 6441 | // This has to happen after matching the requests, because callbacks are just requests. |
| 6442 | notifyNetworkCallbacks(networkAgent, ConnectivityManager.CALLBACK_PRECHECK); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6443 | } else if (state == NetworkInfo.State.DISCONNECTED) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6444 | networkAgent.asyncChannel.disconnect(); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6445 | if (networkAgent.isVPN()) { |
Chalard Jean | f213ca1 | 2018-01-16 18:43:05 +0900 | [diff] [blame] | 6446 | updateUids(networkAgent, networkAgent.networkCapabilities, null); |
Paul Jensen | 6bc2c2c | 2014-05-07 15:27:40 -0400 | [diff] [blame] | 6447 | } |
Chalard Jean | 392971c | 2018-05-11 20:19:20 +0900 | [diff] [blame] | 6448 | disconnectAndDestroyNetwork(networkAgent); |
Irina Dumitrescu | 044a436 | 2018-12-05 16:19:47 +0000 | [diff] [blame] | 6449 | if (networkAgent.isVPN()) { |
| 6450 | // As the active or bound network changes for apps, broadcast the default proxy, as |
| 6451 | // apps may need to update their proxy data. This is called after disconnecting from |
| 6452 | // VPN to make sure we do not broadcast the old proxy data. |
| 6453 | // TODO(b/122649188): send the broadcast only to VPN users. |
| 6454 | mProxyTracker.sendProxyBroadcast(); |
| 6455 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6456 | } else if ((oldInfo != null && oldInfo.getState() == NetworkInfo.State.SUSPENDED) || |
| 6457 | state == NetworkInfo.State.SUSPENDED) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 6458 | // going into or coming out of SUSPEND: re-score and notify |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6459 | if (networkAgent.getCurrentScore() != oldScore) { |
Paul Jensen | 3b9ce37 | 2015-07-10 12:19:38 -0400 | [diff] [blame] | 6460 | rematchAllNetworksAndRequests(networkAgent, oldScore); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6461 | } |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 6462 | updateCapabilities(networkAgent.getCurrentScore(), networkAgent, |
| 6463 | networkAgent.networkCapabilities); |
| 6464 | // TODO (b/73132094) : remove this call once the few users of onSuspended and |
| 6465 | // onResumed have been removed. |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6466 | notifyNetworkCallbacks(networkAgent, (state == NetworkInfo.State.SUSPENDED ? |
| 6467 | ConnectivityManager.CALLBACK_SUSPENDED : |
| 6468 | ConnectivityManager.CALLBACK_RESUMED)); |
| 6469 | mLegacyTypeTracker.update(networkAgent); |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6470 | } |
| 6471 | } |
| 6472 | |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6473 | private void updateNetworkScore(NetworkAgentInfo nai, int score) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6474 | if (VDBG || DDBG) log("updateNetworkScore for " + nai.name() + " to " + score); |
Robert Greenwalt | 35f7a94 | 2014-09-09 14:46:37 -0700 | [diff] [blame] | 6475 | if (score < 0) { |
| 6476 | loge("updateNetworkScore for " + nai.name() + " got a negative score (" + score + |
| 6477 | "). Bumping score to min of 0"); |
| 6478 | score = 0; |
| 6479 | } |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6480 | |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6481 | final int oldScore = nai.getCurrentScore(); |
| 6482 | nai.setCurrentScore(score); |
Robert Greenwalt | ac96c52 | 2014-06-03 16:43:57 -0700 | [diff] [blame] | 6483 | |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6484 | rematchAllNetworksAndRequests(nai, oldScore); |
Paul Jensen | 2161a8e | 2014-09-11 11:00:39 -0400 | [diff] [blame] | 6485 | |
Paul Jensen | c8b9a74 | 2014-09-30 15:37:41 -0400 | [diff] [blame] | 6486 | sendUpdatedScoreToFactories(nai); |
Robert Greenwalt | 55691b8 | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 6487 | } |
| 6488 | |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6489 | // Notify only this one new request of the current state. Transfer all the |
| 6490 | // current state by calling NetworkCapabilities and LinkProperties callbacks |
| 6491 | // so that callers can be guaranteed to have as close to atomicity in state |
| 6492 | // transfer as can be supported by this current API. |
| 6493 | protected void notifyNetworkAvailable(NetworkAgentInfo nai, NetworkRequestInfo nri) { |
Etan Cohen | 681fcda | 2016-10-27 15:05:50 -0700 | [diff] [blame] | 6494 | mHandler.removeMessages(EVENT_TIMEOUT_NETWORK_REQUEST, nri); |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6495 | if (nri.mPendingIntent != null) { |
| 6496 | sendPendingIntentForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE); |
| 6497 | // Attempt no subsequent state pushes where intents are involved. |
| 6498 | return; |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6499 | } |
Erik Kline | c75d4fa | 2017-02-15 19:59:17 +0900 | [diff] [blame] | 6500 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 6501 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6502 | final boolean blocked = isUidNetworkingWithVpnBlocked(nri.mUid, mUidRules.get(nri.mUid), |
| 6503 | metered, mRestrictBackground); |
| 6504 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_AVAILABLE, blocked ? 1 : 0); |
| 6505 | } |
| 6506 | |
| 6507 | /** |
| 6508 | * Notify of the blocked state apps with a registered callback matching a given NAI. |
| 6509 | * |
| 6510 | * Unlike other callbacks, blocked status is different between each individual uid. So for |
| 6511 | * any given nai, all requests need to be considered according to the uid who filed it. |
| 6512 | * |
| 6513 | * @param nai The target NetworkAgentInfo. |
| 6514 | * @param oldMetered True if the previous network capabilities is metered. |
| 6515 | * @param newRestrictBackground True if data saver is enabled. |
| 6516 | */ |
| 6517 | private void maybeNotifyNetworkBlocked(NetworkAgentInfo nai, boolean oldMetered, |
| 6518 | boolean newMetered, boolean oldRestrictBackground, boolean newRestrictBackground) { |
| 6519 | |
| 6520 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6521 | NetworkRequest nr = nai.requestAt(i); |
| 6522 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6523 | final int uidRules = mUidRules.get(nri.mUid); |
| 6524 | final boolean oldBlocked, newBlocked; |
| 6525 | // mVpns lock needs to be hold here to ensure that the active VPN cannot be changed |
| 6526 | // between these two calls. |
| 6527 | synchronized (mVpns) { |
| 6528 | oldBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, oldMetered, |
| 6529 | oldRestrictBackground); |
| 6530 | newBlocked = isUidNetworkingWithVpnBlocked(nri.mUid, uidRules, newMetered, |
| 6531 | newRestrictBackground); |
| 6532 | } |
| 6533 | if (oldBlocked != newBlocked) { |
| 6534 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, |
| 6535 | encodeBool(newBlocked)); |
| 6536 | } |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | /** |
| 6541 | * Notify apps with a given UID of the new blocked state according to new uid rules. |
| 6542 | * @param uid The uid for which the rules changed. |
| 6543 | * @param newRules The new rules to apply. |
| 6544 | */ |
| 6545 | private void maybeNotifyNetworkBlockedForNewUidRules(int uid, int newRules) { |
| 6546 | for (final NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6547 | final boolean metered = nai.networkCapabilities.isMetered(); |
| 6548 | final boolean oldBlocked, newBlocked; |
| 6549 | // TODO: Consider that doze mode or turn on/off battery saver would deliver lots of uid |
| 6550 | // rules changed event. And this function actually loop through all connected nai and |
| 6551 | // its requests. It seems that mVpns lock will be grabbed frequently in this case. |
| 6552 | // Reduce the number of locking or optimize the use of lock are likely needed in future. |
| 6553 | synchronized (mVpns) { |
| 6554 | oldBlocked = isUidNetworkingWithVpnBlocked( |
| 6555 | uid, mUidRules.get(uid), metered, mRestrictBackground); |
| 6556 | newBlocked = isUidNetworkingWithVpnBlocked( |
| 6557 | uid, newRules, metered, mRestrictBackground); |
| 6558 | } |
| 6559 | if (oldBlocked == newBlocked) { |
Junyu Lai | c685f50 | 2019-04-09 08:59:00 -0700 | [diff] [blame] | 6560 | continue; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 6561 | } |
| 6562 | final int arg = encodeBool(newBlocked); |
| 6563 | for (int i = 0; i < nai.numNetworkRequests(); i++) { |
| 6564 | NetworkRequest nr = nai.requestAt(i); |
| 6565 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6566 | if (nri != null && nri.mUid == uid) { |
| 6567 | callCallbackForRequest(nri, nai, ConnectivityManager.CALLBACK_BLK_CHANGED, arg); |
| 6568 | } |
| 6569 | } |
| 6570 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6571 | } |
| 6572 | |
Chalard Jean | ff46a25 | 2019-04-10 23:07:55 +0900 | [diff] [blame^] | 6573 | @VisibleForTesting |
| 6574 | protected void sendLegacyNetworkBroadcast(NetworkAgentInfo nai, DetailedState state, int type) { |
Lorenzo Colitti | a793a67 | 2014-07-31 23:20:17 +0900 | [diff] [blame] | 6575 | // The NetworkInfo we actually send out has no bearing on the real |
| 6576 | // state of affairs. For example, if the default connection is mobile, |
| 6577 | // and a request for HIPRI has just gone away, we need to pretend that |
| 6578 | // HIPRI has just disconnected. So we need to set the type to HIPRI and |
| 6579 | // the state to DISCONNECTED, even though the network is of type MOBILE |
| 6580 | // and is still connected. |
| 6581 | NetworkInfo info = new NetworkInfo(nai.networkInfo); |
| 6582 | info.setType(type); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6583 | if (state != DetailedState.DISCONNECTED) { |
| 6584 | info.setDetailedState(state, null, info.getExtraInfo()); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6585 | sendConnectedBroadcast(info); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6586 | } else { |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 6587 | info.setDetailedState(state, info.getReason(), info.getExtraInfo()); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6588 | Intent intent = new Intent(ConnectivityManager.CONNECTIVITY_ACTION); |
| 6589 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_INFO, info); |
| 6590 | intent.putExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, info.getType()); |
| 6591 | if (info.isFailover()) { |
| 6592 | intent.putExtra(ConnectivityManager.EXTRA_IS_FAILOVER, true); |
| 6593 | nai.networkInfo.setFailover(false); |
| 6594 | } |
| 6595 | if (info.getReason() != null) { |
| 6596 | intent.putExtra(ConnectivityManager.EXTRA_REASON, info.getReason()); |
| 6597 | } |
| 6598 | if (info.getExtraInfo() != null) { |
| 6599 | intent.putExtra(ConnectivityManager.EXTRA_EXTRA_INFO, info.getExtraInfo()); |
| 6600 | } |
| 6601 | NetworkAgentInfo newDefaultAgent = null; |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6602 | if (nai.isSatisfyingRequest(mDefaultRequest.requestId)) { |
Paul Jensen | 85cf78e | 2015-06-25 13:25:07 -0400 | [diff] [blame] | 6603 | newDefaultAgent = getDefaultNetwork(); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6604 | if (newDefaultAgent != null) { |
| 6605 | intent.putExtra(ConnectivityManager.EXTRA_OTHER_NETWORK_INFO, |
| 6606 | newDefaultAgent.networkInfo); |
| 6607 | } else { |
| 6608 | intent.putExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, true); |
| 6609 | } |
| 6610 | } |
| 6611 | intent.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, |
| 6612 | mDefaultInetConditionPublished); |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6613 | sendStickyBroadcast(intent); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6614 | if (newDefaultAgent != null) { |
Erik Kline | 8f29dcf | 2014-12-08 16:25:20 +0900 | [diff] [blame] | 6615 | sendConnectedBroadcast(newDefaultAgent.networkInfo); |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 6616 | } |
| 6617 | } |
| 6618 | } |
| 6619 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6620 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType, int arg1) { |
hiroaki.yokoyama | eeeaab9 | 2018-10-16 12:50:33 +0900 | [diff] [blame] | 6621 | if (VDBG || DDBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 6622 | String notification = ConnectivityManager.getCallbackName(notifyType); |
| 6623 | log("notifyType " + notification + " for " + networkAgent.name()); |
| 6624 | } |
Lorenzo Colitti | 767708d | 2016-07-01 01:37:11 +0900 | [diff] [blame] | 6625 | for (int i = 0; i < networkAgent.numNetworkRequests(); i++) { |
| 6626 | NetworkRequest nr = networkAgent.requestAt(i); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 6627 | NetworkRequestInfo nri = mNetworkRequests.get(nr); |
| 6628 | if (VDBG) log(" sending notification for " + nr); |
Lorenzo Colitti | f0e9a33 | 2016-07-18 18:40:42 +0900 | [diff] [blame] | 6629 | // TODO: if we're in the middle of a rematch, can we send a CAP_CHANGED callback for |
| 6630 | // a network that no longer satisfies the listen? |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6631 | if (nri.mPendingIntent == null) { |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6632 | callCallbackForRequest(nri, networkAgent, notifyType, arg1); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 6633 | } else { |
| 6634 | sendPendingIntentForRequest(nri, networkAgent, notifyType); |
| 6635 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6636 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 6637 | } |
Robert Greenwalt | 12e6735 | 2014-05-13 21:41:06 -0700 | [diff] [blame] | 6638 | |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6639 | protected void notifyNetworkCallbacks(NetworkAgentInfo networkAgent, int notifyType) { |
| 6640 | notifyNetworkCallbacks(networkAgent, notifyType, 0); |
| 6641 | } |
| 6642 | |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6643 | /** |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6644 | * Returns the list of all interfaces that could be used by network traffic that does not |
| 6645 | * explicitly specify a network. This includes the default network, but also all VPNs that are |
| 6646 | * currently connected. |
| 6647 | * |
| 6648 | * Must be called on the handler thread. |
| 6649 | */ |
| 6650 | private Network[] getDefaultNetworks() { |
Varun Anand | 02c50ef | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6651 | ensureRunningOnConnectivityServiceThread(); |
Lorenzo Colitti | c78da29 | 2018-01-19 00:50:48 +0900 | [diff] [blame] | 6652 | ArrayList<Network> defaultNetworks = new ArrayList<>(); |
| 6653 | NetworkAgentInfo defaultNetwork = getDefaultNetwork(); |
| 6654 | for (NetworkAgentInfo nai : mNetworkAgentInfos.values()) { |
| 6655 | if (nai.everConnected && (nai == defaultNetwork || nai.isVPN())) { |
| 6656 | defaultNetworks.add(nai.network); |
| 6657 | } |
| 6658 | } |
| 6659 | return defaultNetworks.toArray(new Network[0]); |
| 6660 | } |
| 6661 | |
| 6662 | /** |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6663 | * Notify NetworkStatsService that the set of active ifaces has changed, or that one of the |
| 6664 | * properties tracked by NetworkStatsService on an active iface has changed. |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6665 | */ |
Jeff Davidson | 0b93c5d | 2016-01-20 11:35:38 -0800 | [diff] [blame] | 6666 | private void notifyIfacesChangedForNetworkStats() { |
Varun Anand | 02c50ef | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6667 | ensureRunningOnConnectivityServiceThread(); |
| 6668 | String activeIface = null; |
| 6669 | LinkProperties activeLinkProperties = getActiveLinkProperties(); |
| 6670 | if (activeLinkProperties != null) { |
| 6671 | activeIface = activeLinkProperties.getInterfaceName(); |
| 6672 | } |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6673 | try { |
Varun Anand | 02c50ef | 2019-02-07 14:13:13 -0800 | [diff] [blame] | 6674 | mStatsService.forceUpdateIfaces( |
| 6675 | getDefaultNetworks(), getAllVpnInfo(), getAllNetworkState(), activeIface); |
Jeff Sharkey | 6973634 | 2014-12-08 14:50:12 -0800 | [diff] [blame] | 6676 | } catch (Exception ignored) { |
| 6677 | } |
| 6678 | } |
| 6679 | |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6680 | @Override |
| 6681 | public boolean addVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6682 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6683 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6684 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6685 | return mVpns.get(user).addAddress(address, prefixLength); |
| 6686 | } |
| 6687 | } |
| 6688 | |
| 6689 | @Override |
| 6690 | public boolean removeVpnAddress(String address, int prefixLength) { |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6691 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
| 6692 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6693 | throwIfLockdownEnabled(); |
Sreeram Ramachandran | f4e0c0c | 2014-07-27 14:18:26 -0700 | [diff] [blame] | 6694 | return mVpns.get(user).removeAddress(address, prefixLength); |
| 6695 | } |
| 6696 | } |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6697 | |
| 6698 | @Override |
| 6699 | public boolean setUnderlyingNetworksForVpn(Network[] networks) { |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6700 | int user = UserHandle.getUserId(Binder.getCallingUid()); |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6701 | final boolean success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6702 | synchronized (mVpns) { |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6703 | throwIfLockdownEnabled(); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6704 | success = mVpns.get(user).setUnderlyingNetworks(networks); |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6705 | } |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6706 | if (success) { |
Varun Anand | 4fa80e8 | 2019-02-06 10:13:38 -0800 | [diff] [blame] | 6707 | mHandler.post(() -> { |
| 6708 | // Update VPN's capabilities based on updated underlying network set. |
| 6709 | updateAllVpnsCapabilities(); |
| 6710 | notifyIfacesChangedForNetworkStats(); |
| 6711 | }); |
Wenchao Tong | f5ea340 | 2015-03-04 13:26:38 -0800 | [diff] [blame] | 6712 | } |
| 6713 | return success; |
Sreeram Ramachandran | c2c0bea | 2014-11-11 16:09:21 -0800 | [diff] [blame] | 6714 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6715 | |
| 6716 | @Override |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6717 | public String getCaptivePortalServerUrl() { |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 6718 | enforceConnectivityInternalPermission(); |
Remi NGUYEN VAN | 9ca4c62 | 2019-04-09 02:04:54 -0700 | [diff] [blame] | 6719 | String settingUrl = mContext.getResources().getString( |
| 6720 | R.string.config_networkCaptivePortalServerUrl); |
| 6721 | |
| 6722 | if (!TextUtils.isEmpty(settingUrl)) { |
| 6723 | return settingUrl; |
| 6724 | } |
| 6725 | |
| 6726 | settingUrl = Settings.Global.getString(mContext.getContentResolver(), |
| 6727 | Settings.Global.CAPTIVE_PORTAL_HTTP_URL); |
| 6728 | if (!TextUtils.isEmpty(settingUrl)) { |
| 6729 | return settingUrl; |
| 6730 | } |
| 6731 | |
| 6732 | return DEFAULT_CAPTIVE_PORTAL_HTTP_URL; |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 6733 | } |
| 6734 | |
| 6735 | @Override |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6736 | public void startNattKeepalive(Network network, int intervalSeconds, |
| 6737 | ISocketKeepaliveCallback cb, String srcAddr, int srcPort, String dstAddr) { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6738 | enforceKeepalivePermission(); |
| 6739 | mKeepaliveTracker.startNattKeepalive( |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 6740 | getNetworkAgentInfoForNetwork(network), null /* fd */, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6741 | intervalSeconds, cb, |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6742 | srcAddr, srcPort, dstAddr, NattSocketKeepalive.NATT_PORT); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6743 | } |
| 6744 | |
| 6745 | @Override |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6746 | public void startNattKeepaliveWithFd(Network network, FileDescriptor fd, int resourceId, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6747 | int intervalSeconds, ISocketKeepaliveCallback cb, String srcAddr, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6748 | String dstAddr) { |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6749 | mKeepaliveTracker.startNattKeepalive( |
| 6750 | getNetworkAgentInfoForNetwork(network), fd, resourceId, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6751 | intervalSeconds, cb, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 6752 | srcAddr, dstAddr, NattSocketKeepalive.NATT_PORT); |
| 6753 | } |
| 6754 | |
| 6755 | @Override |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6756 | public void startTcpKeepalive(Network network, FileDescriptor fd, int intervalSeconds, |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6757 | ISocketKeepaliveCallback cb) { |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6758 | enforceKeepalivePermission(); |
| 6759 | mKeepaliveTracker.startTcpKeepalive( |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 6760 | getNetworkAgentInfoForNetwork(network), fd, intervalSeconds, cb); |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 6761 | } |
| 6762 | |
| 6763 | @Override |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6764 | public void stopKeepalive(Network network, int slot) { |
| 6765 | mHandler.sendMessage(mHandler.obtainMessage( |
junyulai | e413528 | 2019-01-03 18:50:15 +0800 | [diff] [blame] | 6766 | NetworkAgent.CMD_STOP_SOCKET_KEEPALIVE, slot, SocketKeepalive.SUCCESS, network)); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 6767 | } |
| 6768 | |
| 6769 | @Override |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6770 | public void factoryReset() { |
| 6771 | enforceConnectivityInternalPermission(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6772 | |
| 6773 | if (mUserManager.hasUserRestriction(UserManager.DISALLOW_NETWORK_RESET)) { |
| 6774 | return; |
| 6775 | } |
| 6776 | |
Robin Lee | 3b3dd94 | 2015-05-12 18:14:58 +0100 | [diff] [blame] | 6777 | final int userId = UserHandle.getCallingUserId(); |
| 6778 | |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6779 | // Turn airplane mode off |
| 6780 | setAirplaneMode(false); |
| 6781 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6782 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_TETHERING)) { |
| 6783 | // Untether |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6784 | String pkgName = mContext.getOpPackageName(); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6785 | for (String tether : getTetheredIfaces()) { |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 6786 | untether(tether, pkgName); |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6787 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6788 | } |
| 6789 | |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6790 | if (!mUserManager.hasUserRestriction(UserManager.DISALLOW_CONFIG_VPN)) { |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6791 | // Remove always-on package |
| 6792 | synchronized (mVpns) { |
| 6793 | final String alwaysOnPackage = getAlwaysOnVpnPackage(userId); |
| 6794 | if (alwaysOnPackage != null) { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 6795 | setAlwaysOnVpnPackage(userId, null, false, null); |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6796 | setVpnPackageAuthorization(alwaysOnPackage, userId, false); |
| 6797 | } |
Victor Chang | b04a5ea | 2016-05-30 20:36:30 +0100 | [diff] [blame] | 6798 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6799 | // Turn Always-on VPN off |
| 6800 | if (mLockdownEnabled && userId == UserHandle.USER_SYSTEM) { |
| 6801 | final long ident = Binder.clearCallingIdentity(); |
| 6802 | try { |
| 6803 | mKeyStore.delete(Credentials.LOCKDOWN_VPN); |
| 6804 | mLockdownEnabled = false; |
| 6805 | setLockdownTracker(null); |
| 6806 | } finally { |
| 6807 | Binder.restoreCallingIdentity(ident); |
| 6808 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6809 | } |
Koichi, Sugimoto | da1a7ac | 2016-01-27 18:48:58 +0900 | [diff] [blame] | 6810 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6811 | // Turn VPN off |
| 6812 | VpnConfig vpnConfig = getVpnConfig(userId); |
| 6813 | if (vpnConfig != null) { |
| 6814 | if (vpnConfig.legacy) { |
| 6815 | prepareVpn(VpnConfig.LEGACY_VPN, VpnConfig.LEGACY_VPN, userId); |
| 6816 | } else { |
| 6817 | // Prevent this app (packagename = vpnConfig.user) from initiating |
| 6818 | // VPN connections in the future without user intervention. |
| 6819 | setVpnPackageAuthorization(vpnConfig.user, userId, false); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6820 | |
Hugo Benichi | 6974434 | 2017-11-27 10:57:16 +0900 | [diff] [blame] | 6821 | prepareVpn(null, VpnConfig.LEGACY_VPN, userId); |
| 6822 | } |
Stuart Scott | e3e314d | 2015-04-20 14:07:45 -0700 | [diff] [blame] | 6823 | } |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6824 | } |
| 6825 | } |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 6826 | |
| 6827 | Settings.Global.putString(mContext.getContentResolver(), |
| 6828 | Settings.Global.NETWORK_AVOID_BAD_WIFI, null); |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 6829 | } |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6830 | |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 6831 | @Override |
| 6832 | public byte[] getNetworkWatchlistConfigHash() { |
| 6833 | NetworkWatchlistManager nwm = mContext.getSystemService(NetworkWatchlistManager.class); |
| 6834 | if (nwm == null) { |
| 6835 | loge("Unable to get NetworkWatchlistManager"); |
| 6836 | return null; |
| 6837 | } |
| 6838 | // Redirect it to network watchlist service to access watchlist file and calculate hash. |
| 6839 | return nwm.getWatchlistConfigHash(); |
| 6840 | } |
| 6841 | |
Paul Jensen | cf4c2c6 | 2015-07-01 14:16:32 -0400 | [diff] [blame] | 6842 | @VisibleForTesting |
Lorenzo Colitti | 58ebe1c | 2017-01-24 09:41:36 +0900 | [diff] [blame] | 6843 | MultinetworkPolicyTracker createMultinetworkPolicyTracker(Context c, Handler h, Runnable r) { |
| 6844 | return new MultinetworkPolicyTracker(c, h, r); |
Erik Kline | 065ab6e | 2016-10-02 18:02:14 +0900 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | @VisibleForTesting |
Lorenzo Colitti | b57578ca | 2016-07-01 01:53:25 +0900 | [diff] [blame] | 6848 | public WakeupMessage makeWakeupMessage(Context c, Handler h, String s, int cmd, Object obj) { |
| 6849 | return new WakeupMessage(c, h, s, cmd, 0, 0, obj); |
| 6850 | } |
| 6851 | |
Lorenzo Colitti | 7bbe3ee | 2017-08-24 22:35:10 +0900 | [diff] [blame] | 6852 | @VisibleForTesting |
| 6853 | public boolean hasService(String name) { |
| 6854 | return ServiceManager.checkService(name) != null; |
| 6855 | } |
| 6856 | |
Hugo Benichi | 64901e5 | 2017-10-19 14:42:40 +0900 | [diff] [blame] | 6857 | @VisibleForTesting |
| 6858 | protected IpConnectivityMetrics.Logger metricsLogger() { |
| 6859 | return checkNotNull(LocalServices.getService(IpConnectivityMetrics.Logger.class), |
| 6860 | "no IpConnectivityMetrics service"); |
Hugo Benichi | cfddd68 | 2016-05-31 16:28:06 +0900 | [diff] [blame] | 6861 | } |
| 6862 | |
| 6863 | private void logNetworkEvent(NetworkAgentInfo nai, int evtype) { |
Hugo Benichi | edf5c24 | 2017-11-11 08:06:43 +0900 | [diff] [blame] | 6864 | int[] transports = nai.networkCapabilities.getTransportTypes(); |
| 6865 | mMetricsLog.log(nai.network.netId, transports, new NetworkEvent(evtype)); |
Erik Kline | 48f12f2 | 2016-04-14 17:30:59 +0900 | [diff] [blame] | 6866 | } |
Hugo Benichi | ab7d2e6 | 2017-04-21 15:07:12 +0900 | [diff] [blame] | 6867 | |
| 6868 | private static boolean toBool(int encodedBoolean) { |
| 6869 | return encodedBoolean != 0; // Only 0 means false. |
| 6870 | } |
| 6871 | |
| 6872 | private static int encodeBool(boolean b) { |
| 6873 | return b ? 1 : 0; |
| 6874 | } |
mswest46 | 386886f | 2018-03-12 10:34:34 -0700 | [diff] [blame] | 6875 | |
| 6876 | @Override |
| 6877 | public void onShellCommand(FileDescriptor in, FileDescriptor out, |
| 6878 | FileDescriptor err, String[] args, ShellCallback callback, |
| 6879 | ResultReceiver resultReceiver) { |
| 6880 | (new ShellCmd()).exec(this, in, out, err, args, callback, resultReceiver); |
| 6881 | } |
| 6882 | |
| 6883 | private class ShellCmd extends ShellCommand { |
| 6884 | |
| 6885 | @Override |
| 6886 | public int onCommand(String cmd) { |
| 6887 | if (cmd == null) { |
| 6888 | return handleDefaultCommands(cmd); |
| 6889 | } |
| 6890 | final PrintWriter pw = getOutPrintWriter(); |
| 6891 | try { |
| 6892 | switch (cmd) { |
| 6893 | case "airplane-mode": |
| 6894 | final String action = getNextArg(); |
| 6895 | if ("enable".equals(action)) { |
| 6896 | setAirplaneMode(true); |
| 6897 | return 0; |
| 6898 | } else if ("disable".equals(action)) { |
| 6899 | setAirplaneMode(false); |
| 6900 | return 0; |
| 6901 | } else if (action == null) { |
| 6902 | final ContentResolver cr = mContext.getContentResolver(); |
| 6903 | final int enabled = Settings.Global.getInt(cr, |
| 6904 | Settings.Global.AIRPLANE_MODE_ON); |
| 6905 | pw.println(enabled == 0 ? "disabled" : "enabled"); |
| 6906 | return 0; |
| 6907 | } else { |
| 6908 | onHelp(); |
| 6909 | return -1; |
| 6910 | } |
| 6911 | default: |
| 6912 | return handleDefaultCommands(cmd); |
| 6913 | } |
| 6914 | } catch (Exception e) { |
| 6915 | pw.println(e); |
| 6916 | } |
| 6917 | return -1; |
| 6918 | } |
| 6919 | |
| 6920 | @Override |
| 6921 | public void onHelp() { |
| 6922 | PrintWriter pw = getOutPrintWriter(); |
| 6923 | pw.println("Connectivity service commands:"); |
| 6924 | pw.println(" help"); |
| 6925 | pw.println(" Print this help text."); |
| 6926 | pw.println(" airplane-mode [enable|disable]"); |
| 6927 | pw.println(" Turn airplane mode on or off."); |
| 6928 | pw.println(" airplane-mode"); |
| 6929 | pw.println(" Get airplane mode."); |
| 6930 | } |
| 6931 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6932 | |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6933 | @GuardedBy("mVpns") |
| 6934 | private Vpn getVpnIfOwner() { |
| 6935 | final int uid = Binder.getCallingUid(); |
| 6936 | final int user = UserHandle.getUserId(uid); |
| 6937 | |
| 6938 | final Vpn vpn = mVpns.get(user); |
| 6939 | if (vpn == null) { |
| 6940 | return null; |
| 6941 | } else { |
| 6942 | final VpnInfo info = vpn.getVpnInfo(); |
| 6943 | return (info == null || info.ownerUid != uid) ? null : vpn; |
| 6944 | } |
| 6945 | } |
| 6946 | |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6947 | /** |
| 6948 | * Caller either needs to be an active VPN, or hold the NETWORK_STACK permission |
| 6949 | * for testing. |
| 6950 | */ |
| 6951 | private Vpn enforceActiveVpnOrNetworkStackPermission() { |
| 6952 | if (checkNetworkStackPermission()) { |
| 6953 | return null; |
| 6954 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6955 | synchronized (mVpns) { |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6956 | Vpn vpn = getVpnIfOwner(); |
| 6957 | if (vpn != null) { |
| 6958 | return vpn; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 6959 | } |
| 6960 | } |
| 6961 | throw new SecurityException("App must either be an active VPN or have the NETWORK_STACK " |
| 6962 | + "permission"); |
| 6963 | } |
| 6964 | |
| 6965 | /** |
| 6966 | * @param connectionInfo the connection to resolve. |
| 6967 | * @return {@code uid} if the connection is found and the app has permission to observe it |
| 6968 | * (e.g., if it is associated with the calling VPN app's tunnel) or {@code INVALID_UID} if the |
| 6969 | * connection is not found. |
| 6970 | */ |
| 6971 | public int getConnectionOwnerUid(ConnectionInfo connectionInfo) { |
| 6972 | final Vpn vpn = enforceActiveVpnOrNetworkStackPermission(); |
| 6973 | if (connectionInfo.protocol != IPPROTO_TCP && connectionInfo.protocol != IPPROTO_UDP) { |
| 6974 | throw new IllegalArgumentException("Unsupported protocol " + connectionInfo.protocol); |
| 6975 | } |
| 6976 | |
| 6977 | final int uid = InetDiagMessage.getConnectionOwnerUid(connectionInfo.protocol, |
| 6978 | connectionInfo.local, connectionInfo.remote); |
| 6979 | |
| 6980 | /* Filter out Uids not associated with the VPN. */ |
| 6981 | if (vpn != null && !vpn.appliesToUid(uid)) { |
| 6982 | return INVALID_UID; |
| 6983 | } |
| 6984 | |
| 6985 | return uid; |
| 6986 | } |
Pavel Grafov | cb3b895 | 2018-12-14 13:51:07 +0000 | [diff] [blame] | 6987 | |
| 6988 | @Override |
| 6989 | public boolean isCallerCurrentAlwaysOnVpnApp() { |
| 6990 | synchronized (mVpns) { |
| 6991 | Vpn vpn = getVpnIfOwner(); |
| 6992 | return vpn != null && vpn.getAlwaysOn(); |
| 6993 | } |
| 6994 | } |
| 6995 | |
| 6996 | @Override |
| 6997 | public boolean isCallerCurrentAlwaysOnVpnLockdownApp() { |
| 6998 | synchronized (mVpns) { |
| 6999 | Vpn vpn = getVpnIfOwner(); |
| 7000 | return vpn != null && vpn.getLockdown(); |
| 7001 | } |
| 7002 | } |
Benedict Wong | 99a4841 | 2018-11-09 14:45:34 -0800 | [diff] [blame] | 7003 | |
| 7004 | /** |
| 7005 | * Returns a IBinder to a TestNetworkService. Will be lazily created as needed. |
| 7006 | * |
| 7007 | * <p>The TestNetworkService must be run in the system server due to TUN creation. |
| 7008 | */ |
| 7009 | @Override |
| 7010 | public IBinder startOrGetTestNetworkService() { |
| 7011 | synchronized (mTNSLock) { |
| 7012 | TestNetworkService.enforceTestNetworkPermissions(mContext); |
| 7013 | |
| 7014 | if (mTNS == null) { |
| 7015 | mTNS = new TestNetworkService(mContext, mNMS); |
| 7016 | } |
| 7017 | |
| 7018 | return mTNS; |
| 7019 | } |
| 7020 | } |
Nathan Harold | fd45e5f | 2018-07-30 13:38:01 -0700 | [diff] [blame] | 7021 | } |