Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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.connectivity; |
| 18 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 19 | import static android.Manifest.permission.BIND_VPN_SERVICE; |
| 20 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 21 | import android.app.AppGlobals; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 22 | import android.app.Notification; |
| 23 | import android.app.NotificationManager; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 24 | import android.app.PendingIntent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 25 | import android.content.BroadcastReceiver; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 27 | import android.content.Context; |
| 28 | import android.content.Intent; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 29 | import android.content.IntentFilter; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 30 | import android.content.ServiceConnection; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 31 | import android.content.pm.ApplicationInfo; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 32 | import android.content.pm.IPackageManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 34 | import android.content.pm.ResolveInfo; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 35 | import android.content.pm.UserInfo; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 36 | import android.graphics.Bitmap; |
| 37 | import android.graphics.Canvas; |
| 38 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 39 | import android.net.BaseNetworkStateTracker; |
| 40 | import android.net.ConnectivityManager; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 41 | import android.net.IConnectivityManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 42 | import android.net.INetworkManagementEventObserver; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 43 | import android.net.LinkAddress; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 44 | import android.net.LinkProperties; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 45 | import android.net.LocalSocket; |
| 46 | import android.net.LocalSocketAddress; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 47 | import android.net.NetworkInfo; |
Chad Brubaker | 74f99a4 | 2014-03-13 18:18:40 +0000 | [diff] [blame] | 48 | import android.net.NetworkUtils; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 49 | import android.net.RouteInfo; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 50 | import android.net.NetworkInfo.DetailedState; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 51 | import android.os.Binder; |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 52 | import android.os.FileUtils; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 53 | import android.os.IBinder; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 54 | import android.os.INetworkManagementService; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 55 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 56 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 57 | import android.os.Process; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 58 | import android.os.RemoteException; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 59 | import android.os.SystemClock; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 60 | import android.os.SystemService; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 61 | import android.os.UserHandle; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 62 | import android.os.UserManager; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 63 | import android.security.Credentials; |
| 64 | import android.security.KeyStore; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 65 | import android.util.Log; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 66 | import android.util.SparseBooleanArray; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 67 | import android.widget.Toast; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 68 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 69 | import com.android.internal.annotations.GuardedBy; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 70 | import com.android.internal.R; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 71 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 72 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 73 | import com.android.internal.net.VpnProfile; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 74 | import com.android.internal.util.Preconditions; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 75 | import com.android.server.ConnectivityService.VpnCallback; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 76 | import com.android.server.net.BaseNetworkObserver; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 77 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 78 | import java.io.File; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 79 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 80 | import java.io.OutputStream; |
Chad Brubaker | 74f99a4 | 2014-03-13 18:18:40 +0000 | [diff] [blame] | 81 | import java.net.InetAddress; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 82 | import java.net.Inet4Address; |
| 83 | import java.net.InetAddress; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 84 | import java.nio.charset.StandardCharsets; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 85 | import java.util.Arrays; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 86 | import java.util.List; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 87 | import java.util.concurrent.atomic.AtomicInteger; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 88 | |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 89 | import libcore.io.IoUtils; |
| 90 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 91 | /** |
| 92 | * @hide |
| 93 | */ |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 94 | public class Vpn extends BaseNetworkStateTracker { |
| 95 | private static final String TAG = "Vpn"; |
| 96 | private static final boolean LOGD = true; |
| 97 | |
| 98 | // TODO: create separate trackers for each unique VPN to support |
| 99 | // automated reconnection |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 100 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 101 | private final VpnCallback mCallback; |
| 102 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 103 | private String mPackage = VpnConfig.LEGACY_VPN; |
| 104 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 105 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 106 | private LegacyVpnRunner mLegacyVpnRunner; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 107 | private PendingIntent mStatusIntent; |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 108 | private volatile boolean mEnableNotif = true; |
| 109 | private volatile boolean mEnableTeardown = true; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 110 | private final IConnectivityManager mConnService; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 111 | private VpnConfig mConfig; |
| 112 | |
| 113 | /* list of users using this VPN. */ |
| 114 | @GuardedBy("this") |
| 115 | private SparseBooleanArray mVpnUsers = null; |
| 116 | private BroadcastReceiver mUserIntentReceiver = null; |
| 117 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 118 | private final int mUserId; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 119 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 120 | public Vpn(Context context, VpnCallback callback, INetworkManagementService netService, |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 121 | IConnectivityManager connService, int userId) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 122 | // TODO: create dedicated TYPE_VPN network type |
| 123 | super(ConnectivityManager.TYPE_DUMMY); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 124 | mContext = context; |
| 125 | mCallback = callback; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 126 | mConnService = connService; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 127 | mUserId = userId; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 128 | |
| 129 | try { |
| 130 | netService.registerObserver(mObserver); |
| 131 | } catch (RemoteException e) { |
| 132 | Log.wtf(TAG, "Problem registering observer", e); |
| 133 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 134 | if (userId == UserHandle.USER_OWNER) { |
| 135 | // Owner's VPN also needs to handle restricted users |
| 136 | mUserIntentReceiver = new BroadcastReceiver() { |
| 137 | @Override |
| 138 | public void onReceive(Context context, Intent intent) { |
| 139 | final String action = intent.getAction(); |
| 140 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, |
| 141 | UserHandle.USER_NULL); |
| 142 | if (userId == UserHandle.USER_NULL) return; |
| 143 | |
| 144 | if (Intent.ACTION_USER_ADDED.equals(action)) { |
| 145 | onUserAdded(userId); |
| 146 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
| 147 | onUserRemoved(userId); |
| 148 | } |
| 149 | } |
| 150 | }; |
| 151 | |
| 152 | IntentFilter intentFilter = new IntentFilter(); |
| 153 | intentFilter.addAction(Intent.ACTION_USER_ADDED); |
| 154 | intentFilter.addAction(Intent.ACTION_USER_REMOVED); |
| 155 | mContext.registerReceiverAsUser( |
| 156 | mUserIntentReceiver, UserHandle.ALL, intentFilter, null, null); |
| 157 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 160 | /** |
| 161 | * Set if this object is responsible for showing its own notifications. When |
| 162 | * {@code false}, notifications are handled externally by someone else. |
| 163 | */ |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 164 | public void setEnableNotifications(boolean enableNotif) { |
| 165 | mEnableNotif = enableNotif; |
| 166 | } |
| 167 | |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 168 | /** |
| 169 | * Set if this object is responsible for watching for {@link NetworkInfo} |
| 170 | * teardown. When {@code false}, teardown is handled externally by someone |
| 171 | * else. |
| 172 | */ |
| 173 | public void setEnableTeardown(boolean enableTeardown) { |
| 174 | mEnableTeardown = enableTeardown; |
| 175 | } |
| 176 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 177 | @Override |
| 178 | protected void startMonitoringInternal() { |
| 179 | // Ignored; events are sent through callbacks for now |
| 180 | } |
| 181 | |
| 182 | @Override |
| 183 | public boolean teardown() { |
| 184 | // TODO: finish migration to unique tracker for each VPN |
| 185 | throw new UnsupportedOperationException(); |
| 186 | } |
| 187 | |
| 188 | @Override |
| 189 | public boolean reconnect() { |
| 190 | // TODO: finish migration to unique tracker for each VPN |
| 191 | throw new UnsupportedOperationException(); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public String getTcpBufferSizesPropName() { |
| 196 | return PROP_TCP_BUFFER_UNKNOWN; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Update current state, dispaching event to listeners. |
| 201 | */ |
| 202 | private void updateState(DetailedState detailedState, String reason) { |
| 203 | if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); |
| 204 | mNetworkInfo.setDetailedState(detailedState, reason, null); |
| 205 | mCallback.onStateChanged(new NetworkInfo(mNetworkInfo)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 209 | * Prepare for a VPN application. This method is designed to solve |
| 210 | * race conditions. It first compares the current prepared package |
| 211 | * with {@code oldPackage}. If they are the same, the prepared |
| 212 | * package is revoked and replaced with {@code newPackage}. If |
| 213 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 214 | * If {@code newPackage} is the same package or {@code null}, the |
| 215 | * revocation is omitted. This method returns {@code true} if the |
| 216 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 217 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 218 | * Legacy VPN is handled specially since it is not a real package. |
| 219 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 220 | * it can be revoked by itself. |
| 221 | * |
| 222 | * @param oldPackage The package name of the old VPN application. |
| 223 | * @param newPackage The package name of the new VPN application. |
| 224 | * @return true if the operation is succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 225 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 226 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
| 227 | // Return false if the package does not match. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 228 | if (oldPackage != null && !oldPackage.equals(mPackage)) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 229 | return false; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 232 | // Return true if we do not need to revoke. |
| 233 | if (newPackage == null || |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 234 | (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 235 | return true; |
| 236 | } |
| 237 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 238 | // Check if the caller is authorized. |
| 239 | enforceControlPermission(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 240 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 241 | // Reset the interface and hide the notification. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 242 | if (mInterface != null) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 243 | final long token = Binder.clearCallingIdentity(); |
| 244 | try { |
| 245 | mCallback.restore(); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 246 | final int size = mVpnUsers.size(); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 247 | final boolean forwardDns = (mConfig.dnsServers != null && |
| 248 | mConfig.dnsServers.size() != 0); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 249 | for (int i = 0; i < size; i++) { |
| 250 | int user = mVpnUsers.keyAt(i); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 251 | mCallback.clearUserForwarding(mInterface, user, forwardDns); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 252 | hideNotification(user); |
| 253 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 254 | |
| 255 | mCallback.clearMarkedForwarding(mInterface); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 256 | } finally { |
| 257 | Binder.restoreCallingIdentity(token); |
| 258 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 259 | jniReset(mInterface); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 260 | mInterface = null; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 261 | mVpnUsers = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 264 | // Revoke the connection or stop LegacyVpnRunner. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 265 | if (mConnection != null) { |
| 266 | try { |
| 267 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 268 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 269 | } catch (Exception e) { |
| 270 | // ignore |
| 271 | } |
| 272 | mContext.unbindService(mConnection); |
| 273 | mConnection = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 274 | } else if (mLegacyVpnRunner != null) { |
| 275 | mLegacyVpnRunner.exit(); |
| 276 | mLegacyVpnRunner = null; |
| 277 | } |
| 278 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 279 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 280 | mPackage = newPackage; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 281 | mConfig = null; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 282 | updateState(DetailedState.IDLE, "prepare"); |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 283 | return true; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
Chad Brubaker | 7c2b162 | 2014-03-14 16:27:58 +0000 | [diff] [blame] | 287 | * Protect a socket from VPN rules by binding it to the main routing table. |
| 288 | * The socket is NOT closed by this method. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 289 | * |
| 290 | * @param socket The socket to be bound. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 291 | */ |
Chad Brubaker | 7c2b162 | 2014-03-14 16:27:58 +0000 | [diff] [blame] | 292 | public void protect(ParcelFileDescriptor socket) throws Exception { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 293 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 294 | PackageManager pm = mContext.getPackageManager(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 295 | int appUid = pm.getPackageUid(mPackage, mUserId); |
| 296 | if (Binder.getCallingUid() != appUid) { |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 297 | throw new SecurityException("Unauthorized Caller"); |
| 298 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 299 | // protect the socket from routing rules |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 300 | final long token = Binder.clearCallingIdentity(); |
| 301 | try { |
| 302 | mCallback.protect(socket); |
| 303 | } finally { |
| 304 | Binder.restoreCallingIdentity(token); |
| 305 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 306 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 310 | * Establish a VPN network and return the file descriptor of the VPN |
| 311 | * interface. This methods returns {@code null} if the application is |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 312 | * revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 313 | * |
| 314 | * @param config The parameters to configure the network. |
| 315 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 316 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 317 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 318 | // Check if the caller is already prepared. |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 319 | UserManager mgr = UserManager.get(mContext); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 320 | PackageManager pm = mContext.getPackageManager(); |
| 321 | ApplicationInfo app = null; |
| 322 | try { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 323 | app = AppGlobals.getPackageManager().getApplicationInfo(mPackage, 0, mUserId); |
| 324 | if (Binder.getCallingUid() != app.uid) { |
| 325 | return null; |
| 326 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 327 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 328 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 329 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 330 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 331 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 332 | intent.setClassName(mPackage, config.user); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 333 | long token = Binder.clearCallingIdentity(); |
| 334 | try { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 335 | // Restricted users are not allowed to create VPNs, they are tied to Owner |
| 336 | UserInfo user = mgr.getUserInfo(mUserId); |
| 337 | if (user.isRestricted()) { |
| 338 | throw new SecurityException("Restricted users cannot establish VPNs"); |
| 339 | } |
| 340 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 341 | ResolveInfo info = AppGlobals.getPackageManager().resolveService(intent, |
| 342 | null, 0, mUserId); |
| 343 | if (info == null) { |
| 344 | throw new SecurityException("Cannot find " + config.user); |
| 345 | } |
| 346 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 347 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 348 | } |
| 349 | } catch (RemoteException e) { |
| 350 | throw new SecurityException("Cannot find " + config.user); |
| 351 | } finally { |
| 352 | Binder.restoreCallingIdentity(token); |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 353 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 354 | |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 355 | // Save the old config in case we need to go back. |
| 356 | VpnConfig oldConfig = mConfig; |
| 357 | String oldInterface = mInterface; |
| 358 | Connection oldConnection = mConnection; |
| 359 | SparseBooleanArray oldUsers = mVpnUsers; |
| 360 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 361 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 362 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 363 | try { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 364 | updateState(DetailedState.CONNECTING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 365 | String interfaze = jniGetName(tun.getFd()); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 366 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 367 | // TEMP use the old jni calls until there is support for netd address setting |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 368 | StringBuilder builder = new StringBuilder(); |
| 369 | for (LinkAddress address : config.addresses) { |
| 370 | builder.append(" " + address); |
| 371 | } |
| 372 | if (jniSetAddresses(interfaze, builder.toString()) < 1) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 373 | throw new IllegalArgumentException("At least one address must be specified"); |
| 374 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 375 | Connection connection = new Connection(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 376 | if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE, |
| 377 | new UserHandle(mUserId))) { |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 378 | throw new IllegalStateException("Cannot bind " + config.user); |
| 379 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 380 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 381 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 382 | mInterface = interfaze; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 383 | |
| 384 | // Fill more values. |
| 385 | config.user = mPackage; |
| 386 | config.interfaze = mInterface; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 387 | config.startTime = SystemClock.elapsedRealtime(); |
| 388 | mConfig = config; |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 389 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 390 | // Set up forwarding and DNS rules. |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 391 | mVpnUsers = new SparseBooleanArray(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 392 | token = Binder.clearCallingIdentity(); |
| 393 | try { |
| 394 | mCallback.setMarkedForwarding(mInterface); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 395 | mCallback.setRoutes(mInterface, config.routes); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 396 | mCallback.override(mInterface, config.dnsServers, config.searchDomains); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 397 | addVpnUserLocked(mUserId); |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 398 | // If we are owner assign all Restricted Users to this VPN |
| 399 | if (mUserId == UserHandle.USER_OWNER) { |
| 400 | for (UserInfo user : mgr.getUsers()) { |
| 401 | if (user.isRestricted()) { |
| 402 | try { |
| 403 | addVpnUserLocked(user.id); |
| 404 | } catch (Exception e) { |
| 405 | Log.wtf(TAG, "Failed to add user " + user.id + " to owner's VPN"); |
| 406 | } |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | } finally { |
| 411 | Binder.restoreCallingIdentity(token); |
| 412 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 413 | |
| 414 | if (oldConnection != null) { |
| 415 | mContext.unbindService(oldConnection); |
| 416 | } |
| 417 | if (oldInterface != null && !oldInterface.equals(interfaze)) { |
| 418 | // Remove the old tun's user forwarding rules |
| 419 | // The new tun's user rules have already been added so they will take over |
| 420 | // as rules are deleted. This prevents data leakage as the rules are moved over. |
| 421 | token = Binder.clearCallingIdentity(); |
| 422 | try { |
| 423 | final int size = oldUsers.size(); |
| 424 | final boolean forwardDns = (oldConfig.dnsServers != null && |
| 425 | oldConfig.dnsServers.size() != 0); |
| 426 | for (int i = 0; i < size; i++) { |
| 427 | int user = oldUsers.keyAt(i); |
| 428 | mCallback.clearUserForwarding(oldInterface, user, forwardDns); |
| 429 | } |
| 430 | mCallback.clearMarkedForwarding(oldInterface); |
| 431 | } finally { |
| 432 | Binder.restoreCallingIdentity(token); |
| 433 | } |
| 434 | jniReset(oldInterface); |
| 435 | } |
| 436 | } catch (RuntimeException e) { |
| 437 | updateState(DetailedState.FAILED, "establish"); |
| 438 | IoUtils.closeQuietly(tun); |
| 439 | // make sure marked forwarding is cleared if it was set |
| 440 | token = Binder.clearCallingIdentity(); |
| 441 | try { |
| 442 | mCallback.clearMarkedForwarding(mInterface); |
| 443 | } catch (Exception ingored) { |
| 444 | // ignored |
| 445 | } finally { |
| 446 | Binder.restoreCallingIdentity(token); |
| 447 | } |
| 448 | // restore old state |
| 449 | mConfig = oldConfig; |
| 450 | mConnection = oldConnection; |
| 451 | mVpnUsers = oldUsers; |
| 452 | mInterface = oldInterface; |
| 453 | throw e; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 454 | } |
Chad Brubaker | 850eb67 | 2014-03-21 21:02:47 +0000 | [diff] [blame^] | 455 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
| 456 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 457 | // TODO: ensure that contract class eventually marks as connected |
| 458 | updateState(DetailedState.AUTHENTICATING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 459 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Chad Brubaker | 74f99a4 | 2014-03-13 18:18:40 +0000 | [diff] [blame] | 462 | /** |
| 463 | * Check if a given address is covered by the VPN's routing rules. |
| 464 | */ |
| 465 | public boolean isAddressCovered(InetAddress address) { |
| 466 | synchronized (Vpn.this) { |
| 467 | if (!isRunningLocked()) { |
| 468 | return false; |
| 469 | } |
| 470 | return RouteInfo.selectBestRoute(mConfig.routes, address) != null; |
| 471 | } |
| 472 | } |
| 473 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 474 | private boolean isRunningLocked() { |
| 475 | return mVpnUsers != null; |
| 476 | } |
| 477 | |
| 478 | private void addVpnUserLocked(int user) { |
| 479 | enforceControlPermission(); |
| 480 | |
| 481 | if (!isRunningLocked()) { |
| 482 | throw new IllegalStateException("VPN is not active"); |
| 483 | } |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 484 | |
| 485 | final boolean forwardDns = (mConfig.dnsServers != null && |
| 486 | mConfig.dnsServers.size() != 0); |
| 487 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 488 | // add the user |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 489 | mCallback.addUserForwarding(mInterface, user, forwardDns); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 490 | mVpnUsers.put(user, true); |
| 491 | |
| 492 | // show the notification |
| 493 | if (!mPackage.equals(VpnConfig.LEGACY_VPN)) { |
| 494 | // Load everything for the user's notification |
| 495 | PackageManager pm = mContext.getPackageManager(); |
| 496 | ApplicationInfo app = null; |
| 497 | try { |
| 498 | app = AppGlobals.getPackageManager().getApplicationInfo(mPackage, 0, mUserId); |
| 499 | } catch (RemoteException e) { |
| 500 | throw new IllegalStateException("Invalid application"); |
| 501 | } |
| 502 | String label = app.loadLabel(pm).toString(); |
| 503 | // Load the icon and convert it into a bitmap. |
| 504 | Drawable icon = app.loadIcon(pm); |
| 505 | Bitmap bitmap = null; |
| 506 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 507 | int width = mContext.getResources().getDimensionPixelSize( |
| 508 | android.R.dimen.notification_large_icon_width); |
| 509 | int height = mContext.getResources().getDimensionPixelSize( |
| 510 | android.R.dimen.notification_large_icon_height); |
| 511 | icon.setBounds(0, 0, width, height); |
| 512 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 513 | Canvas c = new Canvas(bitmap); |
| 514 | icon.draw(c); |
| 515 | c.setBitmap(null); |
| 516 | } |
| 517 | showNotification(label, bitmap, user); |
| 518 | } else { |
| 519 | showNotification(null, null, user); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | private void removeVpnUserLocked(int user) { |
| 524 | enforceControlPermission(); |
| 525 | |
| 526 | if (!isRunningLocked()) { |
| 527 | throw new IllegalStateException("VPN is not active"); |
| 528 | } |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 529 | final boolean forwardDns = (mConfig.dnsServers != null && |
| 530 | mConfig.dnsServers.size() != 0); |
| 531 | mCallback.clearUserForwarding(mInterface, user, forwardDns); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 532 | mVpnUsers.delete(user); |
| 533 | hideNotification(user); |
| 534 | } |
| 535 | |
| 536 | private void onUserAdded(int userId) { |
| 537 | // If the user is restricted tie them to the owner's VPN |
| 538 | synchronized(Vpn.this) { |
| 539 | UserManager mgr = UserManager.get(mContext); |
| 540 | UserInfo user = mgr.getUserInfo(userId); |
| 541 | if (user.isRestricted()) { |
| 542 | try { |
| 543 | addVpnUserLocked(userId); |
| 544 | } catch (Exception e) { |
| 545 | Log.wtf(TAG, "Failed to add restricted user to owner", e); |
| 546 | } |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | private void onUserRemoved(int userId) { |
| 552 | // clean up if restricted |
| 553 | synchronized(Vpn.this) { |
| 554 | UserManager mgr = UserManager.get(mContext); |
| 555 | UserInfo user = mgr.getUserInfo(userId); |
| 556 | if (user.isRestricted()) { |
| 557 | try { |
| 558 | removeVpnUserLocked(userId); |
| 559 | } catch (Exception e) { |
| 560 | Log.wtf(TAG, "Failed to remove restricted user to owner", e); |
| 561 | } |
| 562 | } |
| 563 | } |
| 564 | } |
| 565 | |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 566 | /** |
| 567 | * Return the configuration of the currently running VPN. |
| 568 | */ |
| 569 | public VpnConfig getVpnConfig() { |
| 570 | enforceControlPermission(); |
| 571 | return mConfig; |
| 572 | } |
| 573 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 574 | @Deprecated |
| 575 | public synchronized void interfaceStatusChanged(String iface, boolean up) { |
| 576 | try { |
| 577 | mObserver.interfaceStatusChanged(iface, up); |
| 578 | } catch (RemoteException e) { |
| 579 | // ignored; target is local |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 580 | } |
| 581 | } |
| 582 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 583 | private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() { |
| 584 | @Override |
| 585 | public void interfaceStatusChanged(String interfaze, boolean up) { |
| 586 | synchronized (Vpn.this) { |
| 587 | if (!up && mLegacyVpnRunner != null) { |
| 588 | mLegacyVpnRunner.check(interfaze); |
| 589 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 590 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 591 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 592 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 593 | @Override |
| 594 | public void interfaceRemoved(String interfaze) { |
| 595 | synchronized (Vpn.this) { |
| 596 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
| 597 | final long token = Binder.clearCallingIdentity(); |
| 598 | try { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 599 | final int size = mVpnUsers.size(); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 600 | final boolean forwardDns = (mConfig.dnsServers != null && |
| 601 | mConfig.dnsServers.size() != 0); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 602 | for (int i = 0; i < size; i++) { |
| 603 | int user = mVpnUsers.keyAt(i); |
Robert Greenwalt | 69887e8 | 2013-09-24 11:05:57 -0700 | [diff] [blame] | 604 | mCallback.clearUserForwarding(mInterface, user, forwardDns); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 605 | hideNotification(user); |
| 606 | } |
| 607 | mVpnUsers = null; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 608 | mCallback.clearMarkedForwarding(mInterface); |
| 609 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 610 | mCallback.restore(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 611 | } finally { |
| 612 | Binder.restoreCallingIdentity(token); |
| 613 | } |
| 614 | mInterface = null; |
| 615 | if (mConnection != null) { |
| 616 | mContext.unbindService(mConnection); |
| 617 | mConnection = null; |
| 618 | updateState(DetailedState.DISCONNECTED, "interfaceRemoved"); |
| 619 | } else if (mLegacyVpnRunner != null) { |
| 620 | mLegacyVpnRunner.exit(); |
| 621 | mLegacyVpnRunner = null; |
| 622 | } |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 627 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 628 | private void enforceControlPermission() { |
| 629 | // System user is allowed to control VPN. |
| 630 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 631 | return; |
| 632 | } |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 633 | int appId = UserHandle.getAppId(Binder.getCallingUid()); |
| 634 | final long token = Binder.clearCallingIdentity(); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 635 | try { |
Nick Kralevich | 212a195 | 2013-10-18 17:48:39 -0700 | [diff] [blame] | 636 | // System VPN dialogs are also allowed to control VPN. |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 637 | PackageManager pm = mContext.getPackageManager(); |
| 638 | ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0); |
Nick Kralevich | 212a195 | 2013-10-18 17:48:39 -0700 | [diff] [blame] | 639 | if (((app.flags & ApplicationInfo.FLAG_SYSTEM) != 0) && (appId == app.uid)) { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 640 | return; |
| 641 | } |
| 642 | } catch (Exception e) { |
| 643 | // ignore |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 644 | } finally { |
| 645 | Binder.restoreCallingIdentity(token); |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 646 | } |
| 647 | |
| 648 | throw new SecurityException("Unauthorized Caller"); |
| 649 | } |
| 650 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 651 | private class Connection implements ServiceConnection { |
| 652 | private IBinder mService; |
| 653 | |
| 654 | @Override |
| 655 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 656 | mService = service; |
| 657 | } |
| 658 | |
| 659 | @Override |
| 660 | public void onServiceDisconnected(ComponentName name) { |
| 661 | mService = null; |
| 662 | } |
| 663 | } |
| 664 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 665 | private void showNotification(String label, Bitmap icon, int user) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 666 | if (!mEnableNotif) return; |
Chad Brubaker | bf6ff2c | 2013-07-16 18:59:12 -0700 | [diff] [blame] | 667 | mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 668 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 669 | NotificationManager nm = (NotificationManager) |
| 670 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 671 | |
| 672 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 673 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 674 | mContext.getString(R.string.vpn_title_long, label); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 675 | String text = (mConfig.session == null) ? mContext.getString(R.string.vpn_text) : |
| 676 | mContext.getString(R.string.vpn_text_long, mConfig.session); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 677 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 678 | Notification notification = new Notification.Builder(mContext) |
| 679 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 680 | .setLargeIcon(icon) |
| 681 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 682 | .setContentText(text) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 683 | .setContentIntent(mStatusIntent) |
Chia-chi Yeh | 50fe709 | 2012-01-11 14:26:24 -0800 | [diff] [blame] | 684 | .setDefaults(0) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 685 | .setOngoing(true) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 686 | .build(); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 687 | nm.notifyAsUser(null, R.drawable.vpn_connected, notification, new UserHandle(user)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 688 | } |
| 689 | } |
| 690 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 691 | private void hideNotification(int user) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 692 | if (!mEnableNotif) return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 693 | mStatusIntent = null; |
| 694 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 695 | NotificationManager nm = (NotificationManager) |
| 696 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 697 | |
| 698 | if (nm != null) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 699 | nm.cancelAsUser(null, R.drawable.vpn_connected, new UserHandle(user)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 703 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 704 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 705 | private native int jniSetAddresses(String interfaze, String addresses); |
| 706 | private native int jniSetRoutes(String interfaze, String routes); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 707 | private native void jniReset(String interfaze); |
| 708 | private native int jniCheck(String interfaze); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 709 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 710 | private static RouteInfo findIPv4DefaultRoute(LinkProperties prop) { |
| 711 | for (RouteInfo route : prop.getAllRoutes()) { |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 712 | // Currently legacy VPN only works on IPv4. |
| 713 | if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) { |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 714 | return route; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 715 | } |
| 716 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 717 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 718 | throw new IllegalStateException("Unable to find IPv4 default gateway"); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 723 | * secondary thread to perform connection work, returning quickly. |
| 724 | */ |
| 725 | public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) { |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 726 | enforceControlPermission(); |
Kenny Root | b9594ce | 2013-02-14 10:18:38 -0800 | [diff] [blame] | 727 | if (!keyStore.isUnlocked()) { |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 728 | throw new IllegalStateException("KeyStore isn't unlocked"); |
| 729 | } |
| 730 | |
Lorenzo Colitti | 41fb98c | 2013-06-28 17:26:21 +0900 | [diff] [blame] | 731 | final RouteInfo ipv4DefaultRoute = findIPv4DefaultRoute(egress); |
| 732 | final String gateway = ipv4DefaultRoute.getGateway().getHostAddress(); |
| 733 | final String iface = ipv4DefaultRoute.getInterface(); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 734 | |
| 735 | // Load certificates. |
| 736 | String privateKey = ""; |
| 737 | String userCert = ""; |
| 738 | String caCert = ""; |
| 739 | String serverCert = ""; |
| 740 | if (!profile.ipsecUserCert.isEmpty()) { |
| 741 | privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert; |
| 742 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 743 | userCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 744 | } |
| 745 | if (!profile.ipsecCaCert.isEmpty()) { |
| 746 | byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 747 | caCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 748 | } |
| 749 | if (!profile.ipsecServerCert.isEmpty()) { |
| 750 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 751 | serverCert = (value == null) ? null : new String(value, StandardCharsets.UTF_8); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 752 | } |
| 753 | if (privateKey == null || userCert == null || caCert == null || serverCert == null) { |
| 754 | throw new IllegalStateException("Cannot load credentials"); |
| 755 | } |
| 756 | |
| 757 | // Prepare arguments for racoon. |
| 758 | String[] racoon = null; |
| 759 | switch (profile.type) { |
| 760 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 761 | racoon = new String[] { |
| 762 | iface, profile.server, "udppsk", profile.ipsecIdentifier, |
| 763 | profile.ipsecSecret, "1701", |
| 764 | }; |
| 765 | break; |
| 766 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 767 | racoon = new String[] { |
| 768 | iface, profile.server, "udprsa", privateKey, userCert, |
| 769 | caCert, serverCert, "1701", |
| 770 | }; |
| 771 | break; |
| 772 | case VpnProfile.TYPE_IPSEC_XAUTH_PSK: |
| 773 | racoon = new String[] { |
| 774 | iface, profile.server, "xauthpsk", profile.ipsecIdentifier, |
| 775 | profile.ipsecSecret, profile.username, profile.password, "", gateway, |
| 776 | }; |
| 777 | break; |
| 778 | case VpnProfile.TYPE_IPSEC_XAUTH_RSA: |
| 779 | racoon = new String[] { |
| 780 | iface, profile.server, "xauthrsa", privateKey, userCert, |
| 781 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 782 | }; |
| 783 | break; |
| 784 | case VpnProfile.TYPE_IPSEC_HYBRID_RSA: |
| 785 | racoon = new String[] { |
| 786 | iface, profile.server, "hybridrsa", |
| 787 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 788 | }; |
| 789 | break; |
| 790 | } |
| 791 | |
| 792 | // Prepare arguments for mtpd. |
| 793 | String[] mtpd = null; |
| 794 | switch (profile.type) { |
| 795 | case VpnProfile.TYPE_PPTP: |
| 796 | mtpd = new String[] { |
| 797 | iface, "pptp", profile.server, "1723", |
| 798 | "name", profile.username, "password", profile.password, |
| 799 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 800 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 801 | (profile.mppe ? "+mppe" : "nomppe"), |
| 802 | }; |
| 803 | break; |
| 804 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 805 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 806 | mtpd = new String[] { |
| 807 | iface, "l2tp", profile.server, "1701", profile.l2tpSecret, |
| 808 | "name", profile.username, "password", profile.password, |
| 809 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 810 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 811 | }; |
| 812 | break; |
| 813 | } |
| 814 | |
| 815 | VpnConfig config = new VpnConfig(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 816 | config.legacy = true; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 817 | config.user = profile.key; |
| 818 | config.interfaze = iface; |
| 819 | config.session = profile.name; |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 820 | |
| 821 | config.addLegacyRoutes(profile.routes); |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 822 | if (!profile.dnsServers.isEmpty()) { |
| 823 | config.dnsServers = Arrays.asList(profile.dnsServers.split(" +")); |
| 824 | } |
| 825 | if (!profile.searchDomains.isEmpty()) { |
| 826 | config.searchDomains = Arrays.asList(profile.searchDomains.split(" +")); |
| 827 | } |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 828 | startLegacyVpn(config, racoon, mtpd); |
| 829 | } |
| 830 | |
| 831 | private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
| 832 | stopLegacyVpn(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 833 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 834 | // Prepare for the new request. This also checks the caller. |
| 835 | prepare(null, VpnConfig.LEGACY_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 836 | updateState(DetailedState.CONNECTING, "startLegacyVpn"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 837 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 838 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 839 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 840 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 841 | } |
| 842 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 843 | public synchronized void stopLegacyVpn() { |
| 844 | if (mLegacyVpnRunner != null) { |
| 845 | mLegacyVpnRunner.exit(); |
| 846 | mLegacyVpnRunner = null; |
| 847 | |
| 848 | synchronized (LegacyVpnRunner.TAG) { |
| 849 | // wait for old thread to completely finish before spinning up |
| 850 | // new instance, otherwise state updates can be out of order. |
| 851 | } |
| 852 | } |
| 853 | } |
| 854 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 855 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 856 | * Return the information of the current ongoing legacy VPN. |
| 857 | */ |
| 858 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 859 | // Check if the caller is authorized. |
| 860 | enforceControlPermission(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 861 | if (mLegacyVpnRunner == null) return null; |
| 862 | |
| 863 | final LegacyVpnInfo info = new LegacyVpnInfo(); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 864 | info.key = mConfig.user; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 865 | info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); |
| 866 | if (mNetworkInfo.isConnected()) { |
| 867 | info.intent = mStatusIntent; |
| 868 | } |
| 869 | return info; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 870 | } |
| 871 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 872 | public VpnConfig getLegacyVpnConfig() { |
| 873 | if (mLegacyVpnRunner != null) { |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 874 | return mConfig; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 875 | } else { |
| 876 | return null; |
| 877 | } |
| 878 | } |
| 879 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 880 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 881 | * Bringing up a VPN connection takes time, and that is all this thread |
| 882 | * does. Here we have plenty of time. The only thing we need to take |
| 883 | * care of is responding to interruptions as soon as possible. Otherwise |
| 884 | * requests will be piled up. This can be done in a Handler as a state |
| 885 | * machine, but it is much easier to read in the current form. |
| 886 | */ |
| 887 | private class LegacyVpnRunner extends Thread { |
| 888 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 889 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 890 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 891 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 892 | private final LocalSocket[] mSockets; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 893 | private final String mOuterInterface; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 894 | private final AtomicInteger mOuterConnection = |
| 895 | new AtomicInteger(ConnectivityManager.TYPE_NONE); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 896 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 897 | private long mTimer = -1; |
| 898 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 899 | /** |
| 900 | * Watch for the outer connection (passing in the constructor) going away. |
| 901 | */ |
| 902 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 903 | @Override |
| 904 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 5766693 | 2013-04-30 17:01:57 -0700 | [diff] [blame] | 905 | if (!mEnableTeardown) return; |
| 906 | |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 907 | if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) { |
| 908 | if (intent.getIntExtra(ConnectivityManager.EXTRA_NETWORK_TYPE, |
| 909 | ConnectivityManager.TYPE_NONE) == mOuterConnection.get()) { |
| 910 | NetworkInfo info = (NetworkInfo)intent.getExtra( |
| 911 | ConnectivityManager.EXTRA_NETWORK_INFO); |
| 912 | if (info != null && !info.isConnectedOrConnecting()) { |
| 913 | try { |
| 914 | mObserver.interfaceStatusChanged(mOuterInterface, false); |
| 915 | } catch (RemoteException e) {} |
| 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | }; |
| 921 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 922 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 923 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 924 | mConfig = config; |
| 925 | mDaemons = new String[] {"racoon", "mtpd"}; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 926 | // TODO: clear arguments from memory once launched |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 927 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 928 | mSockets = new LocalSocket[mDaemons.length]; |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 929 | |
| 930 | // This is the interface which VPN is running on, |
| 931 | // mConfig.interfaze will change to point to OUR |
| 932 | // internal interface soon. TODO - add inner/outer to mconfig |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 933 | // TODO - we have a race - if the outer iface goes away/disconnects before we hit this |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 934 | // we will leave the VPN up. We should check that it's still there/connected after |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 935 | // registering |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 936 | mOuterInterface = mConfig.interfaze; |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 937 | |
| 938 | try { |
| 939 | mOuterConnection.set( |
| 940 | mConnService.findConnectionTypeForIface(mOuterInterface)); |
| 941 | } catch (Exception e) { |
| 942 | mOuterConnection.set(ConnectivityManager.TYPE_NONE); |
| 943 | } |
| 944 | |
| 945 | IntentFilter filter = new IntentFilter(); |
| 946 | filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); |
| 947 | mContext.registerReceiver(mBroadcastReceiver, filter); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 950 | public void check(String interfaze) { |
Robert Greenwalt | 53c04bd | 2012-10-12 17:02:45 -0700 | [diff] [blame] | 951 | if (interfaze.equals(mOuterInterface)) { |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 952 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 953 | exit(); |
| 954 | } |
| 955 | } |
| 956 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 957 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 958 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 959 | interrupt(); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 960 | for (LocalSocket socket : mSockets) { |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 961 | IoUtils.closeQuietly(socket); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 962 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 963 | updateState(DetailedState.DISCONNECTED, "exit"); |
Robert Greenwalt | 1b0ca9d | 2013-04-22 11:13:02 -0700 | [diff] [blame] | 964 | try { |
| 965 | mContext.unregisterReceiver(mBroadcastReceiver); |
| 966 | } catch (IllegalArgumentException e) {} |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 967 | } |
| 968 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 969 | @Override |
| 970 | public void run() { |
| 971 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 972 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 973 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 974 | Log.v(TAG, "Executing"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 975 | execute(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 976 | monitorDaemons(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 977 | } |
| 978 | } |
| 979 | |
| 980 | private void checkpoint(boolean yield) throws InterruptedException { |
| 981 | long now = SystemClock.elapsedRealtime(); |
| 982 | if (mTimer == -1) { |
| 983 | mTimer = now; |
| 984 | Thread.sleep(1); |
Chia-chi Yeh | 7ef8611 | 2011-07-22 15:46:52 -0700 | [diff] [blame] | 985 | } else if (now - mTimer <= 60000) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 986 | Thread.sleep(yield ? 200 : 1); |
| 987 | } else { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 988 | updateState(DetailedState.FAILED, "checkpoint"); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 989 | throw new IllegalStateException("Time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| 993 | private void execute() { |
| 994 | // Catch all exceptions so we can clean up few things. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 995 | boolean initFinished = false; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 996 | try { |
| 997 | // Initialize the timer. |
| 998 | checkpoint(false); |
| 999 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1000 | // Wait for the daemons to stop. |
| 1001 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1002 | while (!SystemService.isStopped(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1003 | checkpoint(true); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1007 | // Clear the previous state. |
| 1008 | File state = new File("/data/misc/vpn/state"); |
| 1009 | state.delete(); |
| 1010 | if (state.exists()) { |
| 1011 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1012 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 1013 | new File("/data/misc/vpn/abort").delete(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1014 | initFinished = true; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1015 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1016 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1017 | boolean restart = false; |
| 1018 | for (String[] arguments : mArguments) { |
| 1019 | restart = restart || (arguments != null); |
| 1020 | } |
| 1021 | if (!restart) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1022 | updateState(DetailedState.DISCONNECTED, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1023 | return; |
| 1024 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1025 | updateState(DetailedState.CONNECTING, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1026 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1027 | // Start the daemon with arguments. |
| 1028 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1029 | String[] arguments = mArguments[i]; |
| 1030 | if (arguments == null) { |
| 1031 | continue; |
| 1032 | } |
| 1033 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1034 | // Start the daemon. |
| 1035 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1036 | SystemService.start(daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1037 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1038 | // Wait for the daemon to start. |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1039 | while (!SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1040 | checkpoint(true); |
| 1041 | } |
| 1042 | |
| 1043 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1044 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1045 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1046 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1047 | |
| 1048 | // Wait for the socket to connect. |
| 1049 | while (true) { |
| 1050 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1051 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1052 | break; |
| 1053 | } catch (Exception e) { |
| 1054 | // ignore |
| 1055 | } |
| 1056 | checkpoint(true); |
| 1057 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1058 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1059 | |
| 1060 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1061 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1062 | for (String argument : arguments) { |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 1063 | byte[] bytes = argument.getBytes(StandardCharsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1064 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1065 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1066 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1067 | out.write(bytes.length >> 8); |
| 1068 | out.write(bytes.length); |
| 1069 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1070 | checkpoint(false); |
| 1071 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1072 | out.write(0xFF); |
| 1073 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1074 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1075 | |
| 1076 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1077 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1078 | while (true) { |
| 1079 | try { |
| 1080 | if (in.read() == -1) { |
| 1081 | break; |
| 1082 | } |
| 1083 | } catch (Exception e) { |
| 1084 | // ignore |
| 1085 | } |
| 1086 | checkpoint(true); |
| 1087 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1088 | } |
| 1089 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1090 | // Wait for the daemons to create the new state. |
| 1091 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 1092 | // Check if a running daemon is dead. |
| 1093 | for (int i = 0; i < mDaemons.length; ++i) { |
| 1094 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1095 | if (mArguments[i] != null && !SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1096 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1097 | } |
| 1098 | } |
| 1099 | checkpoint(true); |
| 1100 | } |
| 1101 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1102 | // Now we are connected. Read and parse the new state. |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 1103 | String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1104 | if (parameters.length != 6) { |
| 1105 | throw new IllegalStateException("Cannot parse the state"); |
| 1106 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1107 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1108 | // Set the interface and the addresses in the config. |
| 1109 | mConfig.interfaze = parameters[0].trim(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1110 | |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1111 | mConfig.addLegacyAddresses(parameters[1]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1112 | // Set the routes if they are not set in the config. |
| 1113 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1114 | mConfig.addLegacyRoutes(parameters[2]); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | // Set the DNS servers if they are not set in the config. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1118 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1119 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1120 | if (!dnsServers.isEmpty()) { |
| 1121 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 1122 | } |
| 1123 | } |
| 1124 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1125 | // Set the search domains if they are not set in the config. |
| 1126 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 1127 | String searchDomains = parameters[4].trim(); |
| 1128 | if (!searchDomains.isEmpty()) { |
| 1129 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 1130 | } |
| 1131 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1132 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1133 | // Set the routes. |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1134 | long token = Binder.clearCallingIdentity(); |
| 1135 | try { |
| 1136 | mCallback.setMarkedForwarding(mConfig.interfaze); |
| 1137 | mCallback.setRoutes(mConfig.interfaze, mConfig.routes); |
| 1138 | } finally { |
| 1139 | Binder.restoreCallingIdentity(token); |
| 1140 | } |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 1141 | |
| 1142 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1143 | synchronized (Vpn.this) { |
Vinit Deshapnde | 2b862e5 | 2013-10-02 11:50:39 -0700 | [diff] [blame] | 1144 | // Set the start time |
| 1145 | mConfig.startTime = SystemClock.elapsedRealtime(); |
| 1146 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1147 | // Check if the thread is interrupted while we are waiting. |
| 1148 | checkpoint(false); |
| 1149 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1150 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1151 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 1152 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1153 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1154 | |
| 1155 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 1156 | mInterface = mConfig.interfaze; |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1157 | mVpnUsers = new SparseBooleanArray(); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1158 | |
| 1159 | token = Binder.clearCallingIdentity(); |
| 1160 | try { |
| 1161 | mCallback.override(mInterface, mConfig.dnsServers, mConfig.searchDomains); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1162 | addVpnUserLocked(mUserId); |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1163 | } finally { |
| 1164 | Binder.restoreCallingIdentity(token); |
| 1165 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1166 | |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1167 | // Assign all restircted users to this VPN |
| 1168 | // (Legacy VPNs are Owner only) |
| 1169 | UserManager mgr = UserManager.get(mContext); |
| 1170 | token = Binder.clearCallingIdentity(); |
| 1171 | try { |
| 1172 | for (UserInfo user : mgr.getUsers()) { |
| 1173 | if (user.isRestricted()) { |
| 1174 | try { |
| 1175 | addVpnUserLocked(user.id); |
| 1176 | } catch (Exception e) { |
| 1177 | Log.wtf(TAG, "Failed to add user " + user.id |
| 1178 | + " to owner's VPN"); |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | } finally { |
| 1183 | Binder.restoreCallingIdentity(token); |
| 1184 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1185 | Log.i(TAG, "Connected!"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1186 | updateState(DetailedState.CONNECTED, "execute"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 1187 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1188 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1189 | Log.i(TAG, "Aborting", e); |
Chad Brubaker | c286519 | 2013-07-10 14:46:23 -0700 | [diff] [blame] | 1190 | // make sure the routing is cleared |
Chad Brubaker | 4ca19e8 | 2013-06-14 11:16:51 -0700 | [diff] [blame] | 1191 | try { |
| 1192 | mCallback.clearMarkedForwarding(mConfig.interfaze); |
| 1193 | } catch (Exception ignored) { |
| 1194 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 1195 | exit(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1196 | } finally { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1197 | // Kill the daemons if they fail to stop. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1198 | if (!initFinished) { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1199 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 1200 | SystemService.stop(daemon); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1204 | // Do not leave an unstable state. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1205 | if (!initFinished || mNetworkInfo.getDetailedState() == DetailedState.CONNECTING) { |
| 1206 | updateState(DetailedState.FAILED, "execute"); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 1207 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1208 | } |
| 1209 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1210 | |
| 1211 | /** |
| 1212 | * Monitor the daemons we started, moving to disconnected state if the |
| 1213 | * underlying services fail. |
| 1214 | */ |
| 1215 | private void monitorDaemons() { |
| 1216 | if (!mNetworkInfo.isConnected()) { |
| 1217 | return; |
| 1218 | } |
| 1219 | |
| 1220 | try { |
| 1221 | while (true) { |
| 1222 | Thread.sleep(2000); |
| 1223 | for (int i = 0; i < mDaemons.length; i++) { |
| 1224 | if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) { |
| 1225 | return; |
| 1226 | } |
| 1227 | } |
| 1228 | } |
| 1229 | } catch (InterruptedException e) { |
| 1230 | Log.d(TAG, "interrupted during monitorDaemons(); stopping services"); |
| 1231 | } finally { |
| 1232 | for (String daemon : mDaemons) { |
| 1233 | SystemService.stop(daemon); |
| 1234 | } |
| 1235 | |
| 1236 | updateState(DetailedState.DISCONNECTED, "babysit"); |
| 1237 | } |
| 1238 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 1239 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 1240 | } |