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 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 21 | import android.app.Notification; |
| 22 | import android.app.NotificationManager; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 23 | import android.app.PendingIntent; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 24 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 25 | import android.content.Context; |
| 26 | import android.content.Intent; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 27 | import android.content.ServiceConnection; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 28 | import android.content.pm.ApplicationInfo; |
| 29 | import android.content.pm.PackageManager; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 30 | import android.content.pm.ResolveInfo; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 31 | import android.graphics.Bitmap; |
| 32 | import android.graphics.Canvas; |
| 33 | import android.graphics.drawable.Drawable; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 34 | import android.net.BaseNetworkStateTracker; |
| 35 | import android.net.ConnectivityManager; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 36 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 37 | import android.net.LinkProperties; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 38 | import android.net.LocalSocket; |
| 39 | import android.net.LocalSocketAddress; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 40 | import android.net.NetworkInfo; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 41 | import android.net.RouteInfo; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 42 | import android.net.NetworkInfo.DetailedState; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 43 | import android.os.Binder; |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 44 | import android.os.FileUtils; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 45 | import android.os.IBinder; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 46 | import android.os.INetworkManagementService; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 47 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 48 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 49 | import android.os.Process; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 50 | import android.os.RemoteException; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 51 | import android.os.SystemClock; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 52 | import android.os.SystemService; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 53 | import android.security.Credentials; |
| 54 | import android.security.KeyStore; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 55 | import android.util.Log; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 56 | import android.widget.Toast; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 57 | |
| 58 | import com.android.internal.R; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 59 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 60 | import com.android.internal.net.VpnConfig; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 61 | import com.android.internal.net.VpnProfile; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 62 | import com.android.internal.util.Preconditions; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 63 | import com.android.server.ConnectivityService.VpnCallback; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 64 | import com.android.server.net.BaseNetworkObserver; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 65 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 66 | import java.io.File; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 67 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 68 | import java.io.OutputStream; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 69 | import java.net.Inet4Address; |
| 70 | import java.net.InetAddress; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 71 | import java.nio.charset.Charsets; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 72 | import java.util.Arrays; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 73 | |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 74 | import libcore.io.IoUtils; |
| 75 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 76 | /** |
| 77 | * @hide |
| 78 | */ |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 79 | public class Vpn extends BaseNetworkStateTracker { |
| 80 | private static final String TAG = "Vpn"; |
| 81 | private static final boolean LOGD = true; |
| 82 | |
| 83 | // TODO: create separate trackers for each unique VPN to support |
| 84 | // automated reconnection |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 85 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 86 | private final VpnCallback mCallback; |
| 87 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 88 | private String mPackage = VpnConfig.LEGACY_VPN; |
| 89 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 90 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 91 | private LegacyVpnRunner mLegacyVpnRunner; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 92 | private PendingIntent mStatusIntent; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame^] | 93 | private boolean mEnableNotif = true; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 94 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 95 | public Vpn(Context context, VpnCallback callback, INetworkManagementService netService) { |
| 96 | // TODO: create dedicated TYPE_VPN network type |
| 97 | super(ConnectivityManager.TYPE_DUMMY); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 98 | mContext = context; |
| 99 | mCallback = callback; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 100 | |
| 101 | try { |
| 102 | netService.registerObserver(mObserver); |
| 103 | } catch (RemoteException e) { |
| 104 | Log.wtf(TAG, "Problem registering observer", e); |
| 105 | } |
| 106 | } |
| 107 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame^] | 108 | public void setEnableNotifications(boolean enableNotif) { |
| 109 | mEnableNotif = enableNotif; |
| 110 | } |
| 111 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 112 | @Override |
| 113 | protected void startMonitoringInternal() { |
| 114 | // Ignored; events are sent through callbacks for now |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public boolean teardown() { |
| 119 | // TODO: finish migration to unique tracker for each VPN |
| 120 | throw new UnsupportedOperationException(); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean reconnect() { |
| 125 | // TODO: finish migration to unique tracker for each VPN |
| 126 | throw new UnsupportedOperationException(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public String getTcpBufferSizesPropName() { |
| 131 | return PROP_TCP_BUFFER_UNKNOWN; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Update current state, dispaching event to listeners. |
| 136 | */ |
| 137 | private void updateState(DetailedState detailedState, String reason) { |
| 138 | if (LOGD) Log.d(TAG, "setting state=" + detailedState + ", reason=" + reason); |
| 139 | mNetworkInfo.setDetailedState(detailedState, reason, null); |
| 140 | mCallback.onStateChanged(new NetworkInfo(mNetworkInfo)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 144 | * Prepare for a VPN application. This method is designed to solve |
| 145 | * race conditions. It first compares the current prepared package |
| 146 | * with {@code oldPackage}. If they are the same, the prepared |
| 147 | * package is revoked and replaced with {@code newPackage}. If |
| 148 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 149 | * If {@code newPackage} is the same package or {@code null}, the |
| 150 | * revocation is omitted. This method returns {@code true} if the |
| 151 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 152 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 153 | * Legacy VPN is handled specially since it is not a real package. |
| 154 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 155 | * it can be revoked by itself. |
| 156 | * |
| 157 | * @param oldPackage The package name of the old VPN application. |
| 158 | * @param newPackage The package name of the new VPN application. |
| 159 | * @return true if the operation is succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 160 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 161 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
| 162 | // Return false if the package does not match. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 163 | if (oldPackage != null && !oldPackage.equals(mPackage)) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 164 | return false; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 167 | // Return true if we do not need to revoke. |
| 168 | if (newPackage == null || |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 169 | (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 170 | return true; |
| 171 | } |
| 172 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 173 | // Check if the caller is authorized. |
| 174 | enforceControlPermission(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 175 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 176 | // Reset the interface and hide the notification. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 177 | if (mInterface != null) { |
| 178 | jniReset(mInterface); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 179 | final long token = Binder.clearCallingIdentity(); |
| 180 | try { |
| 181 | mCallback.restore(); |
| 182 | hideNotification(); |
| 183 | } finally { |
| 184 | Binder.restoreCallingIdentity(token); |
| 185 | } |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 186 | mInterface = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 189 | // Revoke the connection or stop LegacyVpnRunner. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 190 | if (mConnection != null) { |
| 191 | try { |
| 192 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 193 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 194 | } catch (Exception e) { |
| 195 | // ignore |
| 196 | } |
| 197 | mContext.unbindService(mConnection); |
| 198 | mConnection = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 199 | } else if (mLegacyVpnRunner != null) { |
| 200 | mLegacyVpnRunner.exit(); |
| 201 | mLegacyVpnRunner = null; |
| 202 | } |
| 203 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 204 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 205 | mPackage = newPackage; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 206 | updateState(DetailedState.IDLE, "prepare"); |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 207 | return true; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /** |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 211 | * Protect a socket from routing changes by binding it to the given |
| 212 | * interface. The socket is NOT closed by this method. |
| 213 | * |
| 214 | * @param socket The socket to be bound. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 215 | * @param interfaze The name of the interface. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 216 | */ |
| 217 | public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception { |
| 218 | PackageManager pm = mContext.getPackageManager(); |
| 219 | ApplicationInfo app = pm.getApplicationInfo(mPackage, 0); |
| 220 | if (Binder.getCallingUid() != app.uid) { |
| 221 | throw new SecurityException("Unauthorized Caller"); |
| 222 | } |
| 223 | jniProtect(socket.getFd(), interfaze); |
| 224 | } |
| 225 | |
| 226 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 227 | * Establish a VPN network and return the file descriptor of the VPN |
| 228 | * interface. This methods returns {@code null} if the application is |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 229 | * revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 230 | * |
| 231 | * @param config The parameters to configure the network. |
| 232 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 233 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 234 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 235 | // Check if the caller is already prepared. |
| 236 | PackageManager pm = mContext.getPackageManager(); |
| 237 | ApplicationInfo app = null; |
| 238 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 239 | app = pm.getApplicationInfo(mPackage, 0); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 240 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 241 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 242 | } |
| 243 | if (Binder.getCallingUid() != app.uid) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 244 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 245 | } |
| 246 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 247 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 248 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 249 | intent.setClassName(mPackage, config.user); |
| 250 | ResolveInfo info = pm.resolveService(intent, 0); |
| 251 | if (info == null) { |
| 252 | throw new SecurityException("Cannot find " + config.user); |
| 253 | } |
| 254 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 255 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 256 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 257 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 258 | // Load the label. |
| 259 | String label = app.loadLabel(pm).toString(); |
| 260 | |
| 261 | // Load the icon and convert it into a bitmap. |
| 262 | Drawable icon = app.loadIcon(pm); |
| 263 | Bitmap bitmap = null; |
| 264 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 265 | int width = mContext.getResources().getDimensionPixelSize( |
| 266 | android.R.dimen.notification_large_icon_width); |
| 267 | int height = mContext.getResources().getDimensionPixelSize( |
| 268 | android.R.dimen.notification_large_icon_height); |
| 269 | icon.setBounds(0, 0, width, height); |
| 270 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 271 | Canvas c = new Canvas(bitmap); |
| 272 | icon.draw(c); |
| 273 | c.setBitmap(null); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 276 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 277 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 278 | try { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 279 | updateState(DetailedState.CONNECTING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 280 | String interfaze = jniGetName(tun.getFd()); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 281 | if (jniSetAddresses(interfaze, config.addresses) < 1) { |
| 282 | throw new IllegalArgumentException("At least one address must be specified"); |
| 283 | } |
| 284 | if (config.routes != null) { |
| 285 | jniSetRoutes(interfaze, config.routes); |
| 286 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 287 | Connection connection = new Connection(); |
| 288 | if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) { |
| 289 | throw new IllegalStateException("Cannot bind " + config.user); |
| 290 | } |
| 291 | if (mConnection != null) { |
| 292 | mContext.unbindService(mConnection); |
| 293 | } |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 294 | if (mInterface != null && !mInterface.equals(interfaze)) { |
| 295 | jniReset(mInterface); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 296 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 297 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 298 | mInterface = interfaze; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 299 | } catch (RuntimeException e) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 300 | updateState(DetailedState.FAILED, "establish"); |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 301 | IoUtils.closeQuietly(tun); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 302 | throw e; |
| 303 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 304 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 305 | |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 306 | // Fill more values. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 307 | config.user = mPackage; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 308 | config.interfaze = mInterface; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 309 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 310 | // Override DNS servers and show the notification. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 311 | final long token = Binder.clearCallingIdentity(); |
| 312 | try { |
| 313 | mCallback.override(config.dnsServers, config.searchDomains); |
| 314 | showNotification(config, label, bitmap); |
| 315 | } finally { |
| 316 | Binder.restoreCallingIdentity(token); |
| 317 | } |
| 318 | // TODO: ensure that contract class eventually marks as connected |
| 319 | updateState(DetailedState.AUTHENTICATING, "establish"); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 320 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 321 | } |
| 322 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 323 | @Deprecated |
| 324 | public synchronized void interfaceStatusChanged(String iface, boolean up) { |
| 325 | try { |
| 326 | mObserver.interfaceStatusChanged(iface, up); |
| 327 | } catch (RemoteException e) { |
| 328 | // ignored; target is local |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 332 | private INetworkManagementEventObserver mObserver = new BaseNetworkObserver() { |
| 333 | @Override |
| 334 | public void interfaceStatusChanged(String interfaze, boolean up) { |
| 335 | synchronized (Vpn.this) { |
| 336 | if (!up && mLegacyVpnRunner != null) { |
| 337 | mLegacyVpnRunner.check(interfaze); |
| 338 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 339 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 340 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 341 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 342 | @Override |
| 343 | public void interfaceRemoved(String interfaze) { |
| 344 | synchronized (Vpn.this) { |
| 345 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
| 346 | final long token = Binder.clearCallingIdentity(); |
| 347 | try { |
| 348 | mCallback.restore(); |
| 349 | hideNotification(); |
| 350 | } finally { |
| 351 | Binder.restoreCallingIdentity(token); |
| 352 | } |
| 353 | mInterface = null; |
| 354 | if (mConnection != null) { |
| 355 | mContext.unbindService(mConnection); |
| 356 | mConnection = null; |
| 357 | updateState(DetailedState.DISCONNECTED, "interfaceRemoved"); |
| 358 | } else if (mLegacyVpnRunner != null) { |
| 359 | mLegacyVpnRunner.exit(); |
| 360 | mLegacyVpnRunner = null; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | }; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 366 | |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 367 | private void enforceControlPermission() { |
| 368 | // System user is allowed to control VPN. |
| 369 | if (Binder.getCallingUid() == Process.SYSTEM_UID) { |
| 370 | return; |
| 371 | } |
| 372 | |
| 373 | try { |
| 374 | // System dialogs are also allowed to control VPN. |
| 375 | PackageManager pm = mContext.getPackageManager(); |
| 376 | ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0); |
| 377 | if (Binder.getCallingUid() == app.uid) { |
| 378 | return; |
| 379 | } |
| 380 | } catch (Exception e) { |
| 381 | // ignore |
| 382 | } |
| 383 | |
| 384 | throw new SecurityException("Unauthorized Caller"); |
| 385 | } |
| 386 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 387 | private class Connection implements ServiceConnection { |
| 388 | private IBinder mService; |
| 389 | |
| 390 | @Override |
| 391 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 392 | mService = service; |
| 393 | } |
| 394 | |
| 395 | @Override |
| 396 | public void onServiceDisconnected(ComponentName name) { |
| 397 | mService = null; |
| 398 | } |
| 399 | } |
| 400 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 401 | private void showNotification(VpnConfig config, String label, Bitmap icon) { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame^] | 402 | if (!mEnableNotif) return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 403 | mStatusIntent = VpnConfig.getIntentForStatusPanel(mContext, config); |
| 404 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 405 | NotificationManager nm = (NotificationManager) |
| 406 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 407 | |
| 408 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 409 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 410 | mContext.getString(R.string.vpn_title_long, label); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 411 | String text = (config.session == null) ? mContext.getString(R.string.vpn_text) : |
| 412 | mContext.getString(R.string.vpn_text_long, config.session); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 413 | config.startTime = SystemClock.elapsedRealtime(); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 414 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 415 | Notification notification = new Notification.Builder(mContext) |
| 416 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 417 | .setLargeIcon(icon) |
| 418 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 419 | .setContentText(text) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 420 | .setContentIntent(mStatusIntent) |
Chia-chi Yeh | 50fe709 | 2012-01-11 14:26:24 -0800 | [diff] [blame] | 421 | .setDefaults(0) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 422 | .setOngoing(true) |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 423 | .build(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 424 | nm.notify(R.drawable.vpn_connected, notification); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 425 | } |
| 426 | } |
| 427 | |
| 428 | private void hideNotification() { |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame^] | 429 | if (!mEnableNotif) return; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 430 | mStatusIntent = null; |
| 431 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 432 | NotificationManager nm = (NotificationManager) |
| 433 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 434 | |
| 435 | if (nm != null) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 436 | nm.cancel(R.drawable.vpn_connected); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 440 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 441 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 442 | private native int jniSetAddresses(String interfaze, String addresses); |
| 443 | private native int jniSetRoutes(String interfaze, String routes); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 444 | private native void jniReset(String interfaze); |
| 445 | private native int jniCheck(String interfaze); |
| 446 | private native void jniProtect(int socket, String interfaze); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 447 | |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 448 | private static String findLegacyVpnGateway(LinkProperties prop) { |
| 449 | for (RouteInfo route : prop.getRoutes()) { |
| 450 | // Currently legacy VPN only works on IPv4. |
| 451 | if (route.isDefaultRoute() && route.getGateway() instanceof Inet4Address) { |
| 452 | return route.getGateway().getHostAddress(); |
| 453 | } |
| 454 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 455 | |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 456 | throw new IllegalStateException("Unable to find suitable gateway"); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Start legacy VPN, controlling native daemons as needed. Creates a |
| 461 | * secondary thread to perform connection work, returning quickly. |
| 462 | */ |
| 463 | public void startLegacyVpn(VpnProfile profile, KeyStore keyStore, LinkProperties egress) { |
| 464 | if (keyStore.state() != KeyStore.State.UNLOCKED) { |
| 465 | throw new IllegalStateException("KeyStore isn't unlocked"); |
| 466 | } |
| 467 | |
| 468 | final String iface = egress.getInterfaceName(); |
| 469 | final String gateway = findLegacyVpnGateway(egress); |
| 470 | |
| 471 | // Load certificates. |
| 472 | String privateKey = ""; |
| 473 | String userCert = ""; |
| 474 | String caCert = ""; |
| 475 | String serverCert = ""; |
| 476 | if (!profile.ipsecUserCert.isEmpty()) { |
| 477 | privateKey = Credentials.USER_PRIVATE_KEY + profile.ipsecUserCert; |
| 478 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecUserCert); |
| 479 | userCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 480 | } |
| 481 | if (!profile.ipsecCaCert.isEmpty()) { |
| 482 | byte[] value = keyStore.get(Credentials.CA_CERTIFICATE + profile.ipsecCaCert); |
| 483 | caCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 484 | } |
| 485 | if (!profile.ipsecServerCert.isEmpty()) { |
| 486 | byte[] value = keyStore.get(Credentials.USER_CERTIFICATE + profile.ipsecServerCert); |
| 487 | serverCert = (value == null) ? null : new String(value, Charsets.UTF_8); |
| 488 | } |
| 489 | if (privateKey == null || userCert == null || caCert == null || serverCert == null) { |
| 490 | throw new IllegalStateException("Cannot load credentials"); |
| 491 | } |
| 492 | |
| 493 | // Prepare arguments for racoon. |
| 494 | String[] racoon = null; |
| 495 | switch (profile.type) { |
| 496 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 497 | racoon = new String[] { |
| 498 | iface, profile.server, "udppsk", profile.ipsecIdentifier, |
| 499 | profile.ipsecSecret, "1701", |
| 500 | }; |
| 501 | break; |
| 502 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 503 | racoon = new String[] { |
| 504 | iface, profile.server, "udprsa", privateKey, userCert, |
| 505 | caCert, serverCert, "1701", |
| 506 | }; |
| 507 | break; |
| 508 | case VpnProfile.TYPE_IPSEC_XAUTH_PSK: |
| 509 | racoon = new String[] { |
| 510 | iface, profile.server, "xauthpsk", profile.ipsecIdentifier, |
| 511 | profile.ipsecSecret, profile.username, profile.password, "", gateway, |
| 512 | }; |
| 513 | break; |
| 514 | case VpnProfile.TYPE_IPSEC_XAUTH_RSA: |
| 515 | racoon = new String[] { |
| 516 | iface, profile.server, "xauthrsa", privateKey, userCert, |
| 517 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 518 | }; |
| 519 | break; |
| 520 | case VpnProfile.TYPE_IPSEC_HYBRID_RSA: |
| 521 | racoon = new String[] { |
| 522 | iface, profile.server, "hybridrsa", |
| 523 | caCert, serverCert, profile.username, profile.password, "", gateway, |
| 524 | }; |
| 525 | break; |
| 526 | } |
| 527 | |
| 528 | // Prepare arguments for mtpd. |
| 529 | String[] mtpd = null; |
| 530 | switch (profile.type) { |
| 531 | case VpnProfile.TYPE_PPTP: |
| 532 | mtpd = new String[] { |
| 533 | iface, "pptp", profile.server, "1723", |
| 534 | "name", profile.username, "password", profile.password, |
| 535 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 536 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 537 | (profile.mppe ? "+mppe" : "nomppe"), |
| 538 | }; |
| 539 | break; |
| 540 | case VpnProfile.TYPE_L2TP_IPSEC_PSK: |
| 541 | case VpnProfile.TYPE_L2TP_IPSEC_RSA: |
| 542 | mtpd = new String[] { |
| 543 | iface, "l2tp", profile.server, "1701", profile.l2tpSecret, |
| 544 | "name", profile.username, "password", profile.password, |
| 545 | "linkname", "vpn", "refuse-eap", "nodefaultroute", |
| 546 | "usepeerdns", "idle", "1800", "mtu", "1400", "mru", "1400", |
| 547 | }; |
| 548 | break; |
| 549 | } |
| 550 | |
| 551 | VpnConfig config = new VpnConfig(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 552 | config.legacy = true; |
Jeff Sharkey | 82f8521 | 2012-08-24 11:17:25 -0700 | [diff] [blame] | 553 | config.user = profile.key; |
| 554 | config.interfaze = iface; |
| 555 | config.session = profile.name; |
| 556 | config.routes = profile.routes; |
| 557 | if (!profile.dnsServers.isEmpty()) { |
| 558 | config.dnsServers = Arrays.asList(profile.dnsServers.split(" +")); |
| 559 | } |
| 560 | if (!profile.searchDomains.isEmpty()) { |
| 561 | config.searchDomains = Arrays.asList(profile.searchDomains.split(" +")); |
| 562 | } |
| 563 | |
| 564 | startLegacyVpn(config, racoon, mtpd); |
| 565 | } |
| 566 | |
| 567 | private synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
| 568 | stopLegacyVpn(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 569 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 570 | // Prepare for the new request. This also checks the caller. |
| 571 | prepare(null, VpnConfig.LEGACY_VPN); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 572 | updateState(DetailedState.CONNECTING, "startLegacyVpn"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 573 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 574 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 575 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 576 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 579 | public synchronized void stopLegacyVpn() { |
| 580 | if (mLegacyVpnRunner != null) { |
| 581 | mLegacyVpnRunner.exit(); |
| 582 | mLegacyVpnRunner = null; |
| 583 | |
| 584 | synchronized (LegacyVpnRunner.TAG) { |
| 585 | // wait for old thread to completely finish before spinning up |
| 586 | // new instance, otherwise state updates can be out of order. |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 591 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 592 | * Return the information of the current ongoing legacy VPN. |
| 593 | */ |
| 594 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
Chia-chi Yeh | dadc857 | 2012-06-08 13:05:58 -0700 | [diff] [blame] | 595 | // Check if the caller is authorized. |
| 596 | enforceControlPermission(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 597 | if (mLegacyVpnRunner == null) return null; |
| 598 | |
| 599 | final LegacyVpnInfo info = new LegacyVpnInfo(); |
| 600 | info.key = mLegacyVpnRunner.mConfig.user; |
| 601 | info.state = LegacyVpnInfo.stateFromNetworkInfo(mNetworkInfo); |
| 602 | if (mNetworkInfo.isConnected()) { |
| 603 | info.intent = mStatusIntent; |
| 604 | } |
| 605 | return info; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame^] | 608 | public VpnConfig getLegacyVpnConfig() { |
| 609 | if (mLegacyVpnRunner != null) { |
| 610 | return mLegacyVpnRunner.mConfig; |
| 611 | } else { |
| 612 | return null; |
| 613 | } |
| 614 | } |
| 615 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 616 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 617 | * Bringing up a VPN connection takes time, and that is all this thread |
| 618 | * does. Here we have plenty of time. The only thing we need to take |
| 619 | * care of is responding to interruptions as soon as possible. Otherwise |
| 620 | * requests will be piled up. This can be done in a Handler as a state |
| 621 | * machine, but it is much easier to read in the current form. |
| 622 | */ |
| 623 | private class LegacyVpnRunner extends Thread { |
| 624 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 625 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 626 | private final VpnConfig mConfig; |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 627 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 628 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 629 | private final LocalSocket[] mSockets; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 630 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 631 | private long mTimer = -1; |
| 632 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 633 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 634 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 635 | mConfig = config; |
| 636 | mDaemons = new String[] {"racoon", "mtpd"}; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 637 | // TODO: clear arguments from memory once launched |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 638 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 639 | mSockets = new LocalSocket[mDaemons.length]; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 642 | public void check(String interfaze) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 643 | if (interfaze.equals(mConfig.interfaze)) { |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 644 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 645 | exit(); |
| 646 | } |
| 647 | } |
| 648 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 649 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 650 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 651 | interrupt(); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 652 | for (LocalSocket socket : mSockets) { |
Jeff Sharkey | 065b299 | 2012-08-05 14:16:48 -0700 | [diff] [blame] | 653 | IoUtils.closeQuietly(socket); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 654 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 655 | updateState(DetailedState.DISCONNECTED, "exit"); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 656 | } |
| 657 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 658 | @Override |
| 659 | public void run() { |
| 660 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 661 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 662 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 663 | Log.v(TAG, "Executing"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 664 | execute(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 665 | monitorDaemons(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 666 | } |
| 667 | } |
| 668 | |
| 669 | private void checkpoint(boolean yield) throws InterruptedException { |
| 670 | long now = SystemClock.elapsedRealtime(); |
| 671 | if (mTimer == -1) { |
| 672 | mTimer = now; |
| 673 | Thread.sleep(1); |
Chia-chi Yeh | 7ef8611 | 2011-07-22 15:46:52 -0700 | [diff] [blame] | 674 | } else if (now - mTimer <= 60000) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 675 | Thread.sleep(yield ? 200 : 1); |
| 676 | } else { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 677 | updateState(DetailedState.FAILED, "checkpoint"); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 678 | throw new IllegalStateException("Time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 679 | } |
| 680 | } |
| 681 | |
| 682 | private void execute() { |
| 683 | // Catch all exceptions so we can clean up few things. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 684 | boolean initFinished = false; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 685 | try { |
| 686 | // Initialize the timer. |
| 687 | checkpoint(false); |
| 688 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 689 | // Wait for the daemons to stop. |
| 690 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 691 | while (!SystemService.isStopped(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 692 | checkpoint(true); |
| 693 | } |
| 694 | } |
| 695 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 696 | // Clear the previous state. |
| 697 | File state = new File("/data/misc/vpn/state"); |
| 698 | state.delete(); |
| 699 | if (state.exists()) { |
| 700 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 701 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 702 | new File("/data/misc/vpn/abort").delete(); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 703 | initFinished = true; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 704 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 705 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 706 | boolean restart = false; |
| 707 | for (String[] arguments : mArguments) { |
| 708 | restart = restart || (arguments != null); |
| 709 | } |
| 710 | if (!restart) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 711 | updateState(DetailedState.DISCONNECTED, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 712 | return; |
| 713 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 714 | updateState(DetailedState.CONNECTING, "execute"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 715 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 716 | // Start the daemon with arguments. |
| 717 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 718 | String[] arguments = mArguments[i]; |
| 719 | if (arguments == null) { |
| 720 | continue; |
| 721 | } |
| 722 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 723 | // Start the daemon. |
| 724 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 725 | SystemService.start(daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 726 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 727 | // Wait for the daemon to start. |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 728 | while (!SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 729 | checkpoint(true); |
| 730 | } |
| 731 | |
| 732 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 733 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 734 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 735 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 736 | |
| 737 | // Wait for the socket to connect. |
| 738 | while (true) { |
| 739 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 740 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 741 | break; |
| 742 | } catch (Exception e) { |
| 743 | // ignore |
| 744 | } |
| 745 | checkpoint(true); |
| 746 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 747 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 748 | |
| 749 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 750 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 751 | for (String argument : arguments) { |
| 752 | byte[] bytes = argument.getBytes(Charsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 753 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 754 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 755 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 756 | out.write(bytes.length >> 8); |
| 757 | out.write(bytes.length); |
| 758 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 759 | checkpoint(false); |
| 760 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 761 | out.write(0xFF); |
| 762 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 763 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 764 | |
| 765 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 766 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 767 | while (true) { |
| 768 | try { |
| 769 | if (in.read() == -1) { |
| 770 | break; |
| 771 | } |
| 772 | } catch (Exception e) { |
| 773 | // ignore |
| 774 | } |
| 775 | checkpoint(true); |
| 776 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 777 | } |
| 778 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 779 | // Wait for the daemons to create the new state. |
| 780 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 781 | // Check if a running daemon is dead. |
| 782 | for (int i = 0; i < mDaemons.length; ++i) { |
| 783 | String daemon = mDaemons[i]; |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 784 | if (mArguments[i] != null && !SystemService.isRunning(daemon)) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 785 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 786 | } |
| 787 | } |
| 788 | checkpoint(true); |
| 789 | } |
| 790 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 791 | // Now we are connected. Read and parse the new state. |
Chia-chi Yeh | c1bac3a | 2011-12-16 15:00:31 -0800 | [diff] [blame] | 792 | String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 793 | if (parameters.length != 6) { |
| 794 | throw new IllegalStateException("Cannot parse the state"); |
| 795 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 796 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 797 | // Set the interface and the addresses in the config. |
| 798 | mConfig.interfaze = parameters[0].trim(); |
| 799 | mConfig.addresses = parameters[1].trim(); |
| 800 | |
| 801 | // Set the routes if they are not set in the config. |
| 802 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
| 803 | mConfig.routes = parameters[2].trim(); |
| 804 | } |
| 805 | |
| 806 | // 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] | 807 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 808 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 809 | if (!dnsServers.isEmpty()) { |
| 810 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 811 | } |
| 812 | } |
| 813 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 814 | // Set the search domains if they are not set in the config. |
| 815 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 816 | String searchDomains = parameters[4].trim(); |
| 817 | if (!searchDomains.isEmpty()) { |
| 818 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 819 | } |
| 820 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 821 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 822 | // Set the routes. |
| 823 | jniSetRoutes(mConfig.interfaze, mConfig.routes); |
| 824 | |
| 825 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 826 | synchronized (Vpn.this) { |
| 827 | // Check if the thread is interrupted while we are waiting. |
| 828 | checkpoint(false); |
| 829 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 830 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 831 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 832 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 833 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 834 | |
| 835 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 836 | mInterface = mConfig.interfaze; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 837 | mCallback.override(mConfig.dnsServers, mConfig.searchDomains); |
| 838 | showNotification(mConfig, null, null); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 839 | |
| 840 | Log.i(TAG, "Connected!"); |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 841 | updateState(DetailedState.CONNECTED, "execute"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 842 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 843 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 844 | Log.i(TAG, "Aborting", e); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 845 | exit(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 846 | } finally { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 847 | // Kill the daemons if they fail to stop. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 848 | if (!initFinished) { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 849 | for (String daemon : mDaemons) { |
Jeff Sharkey | 088f29f | 2012-08-05 14:55:04 -0700 | [diff] [blame] | 850 | SystemService.stop(daemon); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 854 | // Do not leave an unstable state. |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 855 | if (!initFinished || mNetworkInfo.getDetailedState() == DetailedState.CONNECTING) { |
| 856 | updateState(DetailedState.FAILED, "execute"); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 857 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 858 | } |
| 859 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 860 | |
| 861 | /** |
| 862 | * Monitor the daemons we started, moving to disconnected state if the |
| 863 | * underlying services fail. |
| 864 | */ |
| 865 | private void monitorDaemons() { |
| 866 | if (!mNetworkInfo.isConnected()) { |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | try { |
| 871 | while (true) { |
| 872 | Thread.sleep(2000); |
| 873 | for (int i = 0; i < mDaemons.length; i++) { |
| 874 | if (mArguments[i] != null && SystemService.isStopped(mDaemons[i])) { |
| 875 | return; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | } catch (InterruptedException e) { |
| 880 | Log.d(TAG, "interrupted during monitorDaemons(); stopping services"); |
| 881 | } finally { |
| 882 | for (String daemon : mDaemons) { |
| 883 | SystemService.stop(daemon); |
| 884 | } |
| 885 | |
| 886 | updateState(DetailedState.DISCONNECTED, "babysit"); |
| 887 | } |
| 888 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 889 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 890 | } |