Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.net; |
| 18 | |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 19 | import static android.Manifest.permission.NETWORK_STACK; |
Robin Lee | a249aee | 2016-02-03 13:42:56 +0000 | [diff] [blame] | 20 | import static android.provider.Settings.ACTION_VPN_SETTINGS; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 21 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 24 | import android.app.Notification; |
| 25 | import android.app.NotificationManager; |
| 26 | import android.app.PendingIntent; |
| 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.IntentFilter; |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 31 | import android.net.ConnectivityManager; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 32 | import android.net.LinkAddress; |
Remi NGUYEN VAN | dacee14 | 2019-02-13 18:28:35 +0900 | [diff] [blame] | 33 | import android.net.LinkProperties; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 34 | import android.net.NetworkInfo; |
| 35 | import android.net.NetworkInfo.DetailedState; |
| 36 | import android.net.NetworkInfo.State; |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 37 | import android.os.Handler; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 38 | import android.security.Credentials; |
| 39 | import android.security.KeyStore; |
| 40 | import android.text.TextUtils; |
| 41 | import android.util.Slog; |
| 42 | |
| 43 | import com.android.internal.R; |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 44 | import com.android.internal.messages.nano.SystemMessageProto.SystemMessage; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 45 | import com.android.internal.net.VpnConfig; |
| 46 | import com.android.internal.net.VpnProfile; |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 47 | import com.android.internal.notification.SystemNotificationChannels; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 48 | import com.android.server.ConnectivityService; |
Jeff Sharkey | 91c6a64 | 2012-09-06 18:33:14 -0700 | [diff] [blame] | 49 | import com.android.server.EventLogTags; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 50 | import com.android.server.connectivity.Vpn; |
| 51 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 52 | import java.util.List; |
Daulet Zhanguzin | 8d0b7f1 | 2020-01-03 13:37:24 +0000 | [diff] [blame] | 53 | import java.util.Objects; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 54 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 55 | /** |
| 56 | * State tracker for lockdown mode. Watches for normal {@link NetworkInfo} to be |
| 57 | * connected and kicks off VPN connection, managing any required {@code netd} |
| 58 | * firewall rules. |
| 59 | */ |
| 60 | public class LockdownVpnTracker { |
| 61 | private static final String TAG = "LockdownVpnTracker"; |
| 62 | |
| 63 | /** Number of VPN attempts before waiting for user intervention. */ |
| 64 | private static final int MAX_ERROR_COUNT = 4; |
| 65 | |
| 66 | private static final String ACTION_LOCKDOWN_RESET = "com.android.server.action.LOCKDOWN_RESET"; |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 67 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 68 | @NonNull private final Context mContext; |
| 69 | @NonNull private final ConnectivityService mConnService; |
| 70 | @NonNull private final Handler mHandler; |
| 71 | @NonNull private final Vpn mVpn; |
| 72 | @NonNull private final VpnProfile mProfile; |
Lorenzo Colitti | ad4cd0c | 2014-10-16 01:06:29 +0900 | [diff] [blame] | 73 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 74 | @NonNull private final Object mStateLock = new Object(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 75 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 76 | @NonNull private final PendingIntent mConfigIntent; |
| 77 | @NonNull private final PendingIntent mResetIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 78 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 79 | @Nullable |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 80 | private String mAcceptedEgressIface; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 81 | |
| 82 | private int mErrorCount; |
| 83 | |
| 84 | public static boolean isEnabled() { |
| 85 | return KeyStore.getInstance().contains(Credentials.LOCKDOWN_VPN); |
| 86 | } |
| 87 | |
junyulai | 465088e | 2019-08-30 16:44:45 +0800 | [diff] [blame] | 88 | public LockdownVpnTracker(@NonNull Context context, |
| 89 | @NonNull ConnectivityService connService, |
| 90 | @NonNull Handler handler, |
| 91 | @NonNull Vpn vpn, |
| 92 | @NonNull VpnProfile profile) { |
Daulet Zhanguzin | 8d0b7f1 | 2020-01-03 13:37:24 +0000 | [diff] [blame] | 93 | mContext = Objects.requireNonNull(context); |
| 94 | mConnService = Objects.requireNonNull(connService); |
| 95 | mHandler = Objects.requireNonNull(handler); |
| 96 | mVpn = Objects.requireNonNull(vpn); |
| 97 | mProfile = Objects.requireNonNull(profile); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 98 | |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 99 | final Intent configIntent = new Intent(ACTION_VPN_SETTINGS); |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 100 | mConfigIntent = PendingIntent.getActivity(mContext, 0, configIntent, 0); |
| 101 | |
Jeff Sharkey | 580dd31 | 2012-08-29 22:27:39 -0700 | [diff] [blame] | 102 | final Intent resetIntent = new Intent(ACTION_LOCKDOWN_RESET); |
| 103 | resetIntent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); |
| 104 | mResetIntent = PendingIntent.getBroadcast(mContext, 0, resetIntent, 0); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private BroadcastReceiver mResetReceiver = new BroadcastReceiver() { |
| 108 | @Override |
| 109 | public void onReceive(Context context, Intent intent) { |
| 110 | reset(); |
| 111 | } |
| 112 | }; |
| 113 | |
| 114 | /** |
| 115 | * Watch for state changes to both active egress network, kicking off a VPN |
| 116 | * connection when ready, or setting firewall rules once VPN is connected. |
| 117 | */ |
| 118 | private void handleStateChangedLocked() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 119 | |
| 120 | final NetworkInfo egressInfo = mConnService.getActiveNetworkInfoUnfiltered(); |
| 121 | final LinkProperties egressProp = mConnService.getActiveLinkProperties(); |
| 122 | |
| 123 | final NetworkInfo vpnInfo = mVpn.getNetworkInfo(); |
| 124 | final VpnConfig vpnConfig = mVpn.getLegacyVpnConfig(); |
| 125 | |
| 126 | // Restart VPN when egress network disconnected or changed |
| 127 | final boolean egressDisconnected = egressInfo == null |
| 128 | || State.DISCONNECTED.equals(egressInfo.getState()); |
| 129 | final boolean egressChanged = egressProp == null |
| 130 | || !TextUtils.equals(mAcceptedEgressIface, egressProp.getInterfaceName()); |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 131 | |
| 132 | final String egressTypeName = (egressInfo == null) ? |
| 133 | null : ConnectivityManager.getNetworkTypeName(egressInfo.getType()); |
| 134 | final String egressIface = (egressProp == null) ? |
| 135 | null : egressProp.getInterfaceName(); |
| 136 | Slog.d(TAG, "handleStateChanged: egress=" + egressTypeName + |
| 137 | " " + mAcceptedEgressIface + "->" + egressIface); |
| 138 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 139 | if (egressDisconnected || egressChanged) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 140 | mAcceptedEgressIface = null; |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 141 | mVpn.stopLegacyVpnPrivileged(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 142 | } |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 143 | if (egressDisconnected) { |
| 144 | hideNotification(); |
| 145 | return; |
| 146 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 147 | |
Jeff Sharkey | 91c6a64 | 2012-09-06 18:33:14 -0700 | [diff] [blame] | 148 | final int egressType = egressInfo.getType(); |
| 149 | if (vpnInfo.getDetailedState() == DetailedState.FAILED) { |
| 150 | EventLogTags.writeLockdownVpnError(egressType); |
| 151 | } |
| 152 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 153 | if (mErrorCount > MAX_ERROR_COUNT) { |
| 154 | showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected); |
| 155 | |
| 156 | } else if (egressInfo.isConnected() && !vpnInfo.isConnectedOrConnecting()) { |
| 157 | if (mProfile.isValidLockdownProfile()) { |
| 158 | Slog.d(TAG, "Active network connected; starting VPN"); |
Jeff Sharkey | 91c6a64 | 2012-09-06 18:33:14 -0700 | [diff] [blame] | 159 | EventLogTags.writeLockdownVpnConnecting(egressType); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 160 | showNotification(R.string.vpn_lockdown_connecting, R.drawable.vpn_disconnected); |
| 161 | |
| 162 | mAcceptedEgressIface = egressProp.getInterfaceName(); |
Jeff Sharkey | 421fab8 | 2013-06-27 10:57:45 -0700 | [diff] [blame] | 163 | try { |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 164 | // Use the privileged method because Lockdown VPN is initiated by the system, so |
| 165 | // no additional permission checks are necessary. |
| 166 | mVpn.startLegacyVpnPrivileged(mProfile, KeyStore.getInstance(), egressProp); |
Jeff Sharkey | 421fab8 | 2013-06-27 10:57:45 -0700 | [diff] [blame] | 167 | } catch (IllegalStateException e) { |
| 168 | mAcceptedEgressIface = null; |
| 169 | Slog.e(TAG, "Failed to start VPN", e); |
| 170 | showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected); |
| 171 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 172 | } else { |
| 173 | Slog.e(TAG, "Invalid VPN profile; requires IP-based server and DNS"); |
| 174 | showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected); |
| 175 | } |
| 176 | |
| 177 | } else if (vpnInfo.isConnected() && vpnConfig != null) { |
| 178 | final String iface = vpnConfig.interfaze; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 179 | final List<LinkAddress> sourceAddrs = vpnConfig.addresses; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 180 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 181 | Slog.d(TAG, "VPN connected using iface=" + iface + |
| 182 | ", sourceAddr=" + sourceAddrs.toString()); |
Jeff Sharkey | 91c6a64 | 2012-09-06 18:33:14 -0700 | [diff] [blame] | 183 | EventLogTags.writeLockdownVpnConnected(egressType); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 184 | showNotification(R.string.vpn_lockdown_connected, R.drawable.vpn_connected); |
| 185 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 186 | final NetworkInfo clone = new NetworkInfo(egressInfo); |
| 187 | augmentNetworkInfo(clone); |
| 188 | mConnService.sendConnectedBroadcast(clone); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | public void init() { |
Jeff Sharkey | 580dd31 | 2012-08-29 22:27:39 -0700 | [diff] [blame] | 193 | synchronized (mStateLock) { |
| 194 | initLocked(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | private void initLocked() { |
| 199 | Slog.d(TAG, "initLocked()"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 200 | |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 201 | mVpn.setEnableTeardown(false); |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 202 | mVpn.setLockdown(true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 203 | |
| 204 | final IntentFilter resetFilter = new IntentFilter(ACTION_LOCKDOWN_RESET); |
paulhu | 59148b7 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 205 | mContext.registerReceiver(mResetReceiver, resetFilter, NETWORK_STACK, mHandler); |
Robin Lee | aca5e7e3 | 2015-11-12 10:28:06 +0000 | [diff] [blame] | 206 | handleStateChangedLocked(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | public void shutdown() { |
Jeff Sharkey | 580dd31 | 2012-08-29 22:27:39 -0700 | [diff] [blame] | 210 | synchronized (mStateLock) { |
| 211 | shutdownLocked(); |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | private void shutdownLocked() { |
| 216 | Slog.d(TAG, "shutdownLocked()"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 217 | |
| 218 | mAcceptedEgressIface = null; |
| 219 | mErrorCount = 0; |
| 220 | |
Jeff Davidson | b21298a | 2015-02-10 10:02:11 -0800 | [diff] [blame] | 221 | mVpn.stopLegacyVpnPrivileged(); |
Robin Lee | c3736bc | 2017-03-10 16:19:54 +0000 | [diff] [blame] | 222 | mVpn.setLockdown(false); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 223 | hideNotification(); |
| 224 | |
| 225 | mContext.unregisterReceiver(mResetReceiver); |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 226 | mVpn.setEnableTeardown(true); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | public void reset() { |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 230 | Slog.d(TAG, "reset()"); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 231 | synchronized (mStateLock) { |
Jeff Sharkey | 580dd31 | 2012-08-29 22:27:39 -0700 | [diff] [blame] | 232 | // cycle tracker, reset error count, and trigger retry |
| 233 | shutdownLocked(); |
| 234 | initLocked(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 235 | handleStateChangedLocked(); |
| 236 | } |
| 237 | } |
| 238 | |
Lorenzo Colitti | 0cb7903 | 2014-10-15 16:06:07 +0900 | [diff] [blame] | 239 | public void onNetworkInfoChanged() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 240 | synchronized (mStateLock) { |
| 241 | handleStateChangedLocked(); |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | public void onVpnStateChanged(NetworkInfo info) { |
| 246 | if (info.getDetailedState() == DetailedState.FAILED) { |
| 247 | mErrorCount++; |
| 248 | } |
| 249 | synchronized (mStateLock) { |
| 250 | handleStateChangedLocked(); |
| 251 | } |
| 252 | } |
| 253 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 254 | public void augmentNetworkInfo(NetworkInfo info) { |
Jeff Sharkey | 0b81be6 | 2012-09-18 15:44:16 -0700 | [diff] [blame] | 255 | if (info.isConnected()) { |
| 256 | final NetworkInfo vpnInfo = mVpn.getNetworkInfo(); |
Jeff Sharkey | 0b81be6 | 2012-09-18 15:44:16 -0700 | [diff] [blame] | 257 | info.setDetailedState(vpnInfo.getDetailedState(), vpnInfo.getReason(), null); |
| 258 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | private void showNotification(int titleRes, int iconRes) { |
Geoffrey Pitsch | af759c5 | 2017-02-15 09:35:38 -0500 | [diff] [blame] | 262 | final Notification.Builder builder = |
| 263 | new Notification.Builder(mContext, SystemNotificationChannels.VPN) |
| 264 | .setWhen(0) |
| 265 | .setSmallIcon(iconRes) |
| 266 | .setContentTitle(mContext.getString(titleRes)) |
| 267 | .setContentText(mContext.getString(R.string.vpn_lockdown_config)) |
| 268 | .setContentIntent(mConfigIntent) |
| 269 | .setOngoing(true) |
| 270 | .addAction(R.drawable.ic_menu_refresh, mContext.getString(R.string.reset), |
| 271 | mResetIntent) |
| 272 | .setColor(mContext.getColor( |
| 273 | com.android.internal.R.color.system_notification_accent_color)); |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 274 | |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 275 | NotificationManager.from(mContext).notify(null, SystemMessage.NOTE_VPN_STATUS, |
| 276 | builder.build()); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | private void hideNotification() { |
Chris Wren | 282cfef | 2017-03-27 15:01:44 -0400 | [diff] [blame] | 280 | NotificationManager.from(mContext).cancel(null, SystemMessage.NOTE_VPN_STATUS); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 281 | } |
| 282 | } |