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