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