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