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 | |
| 19 | import android.app.Notification; |
| 20 | import android.app.NotificationManager; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 24 | import android.content.ServiceConnection; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 25 | import android.content.pm.ApplicationInfo; |
| 26 | import android.content.pm.PackageManager; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 27 | import android.content.pm.ResolveInfo; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 28 | import android.content.res.Resources; |
| 29 | import android.graphics.Bitmap; |
| 30 | import android.graphics.Canvas; |
| 31 | import android.graphics.drawable.Drawable; |
| 32 | import android.net.INetworkManagementEventObserver; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 33 | import android.net.LocalSocket; |
| 34 | import android.net.LocalSocketAddress; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 35 | import android.os.Binder; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 36 | import android.os.IBinder; |
| 37 | import android.os.Parcel; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 38 | import android.os.ParcelFileDescriptor; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 39 | import android.os.Process; |
| 40 | import android.os.SystemClock; |
| 41 | import android.os.SystemProperties; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 42 | import android.util.Log; |
| 43 | |
| 44 | import com.android.internal.R; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 45 | import com.android.internal.net.LegacyVpnInfo; |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 46 | import com.android.internal.net.VpnConfig; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 47 | import com.android.server.ConnectivityService.VpnCallback; |
| 48 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 49 | import java.io.File; |
| 50 | import java.io.FileInputStream; |
| 51 | import java.io.InputStream; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 52 | import java.io.OutputStream; |
| 53 | import java.nio.charset.Charsets; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 54 | import java.util.Arrays; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 55 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 56 | /** |
| 57 | * @hide |
| 58 | */ |
| 59 | public class Vpn extends INetworkManagementEventObserver.Stub { |
| 60 | |
| 61 | private final static String TAG = "Vpn"; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 62 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 63 | private final static String BIND_VPN_SERVICE = |
| 64 | android.Manifest.permission.BIND_VPN_SERVICE; |
| 65 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 66 | private final Context mContext; |
| 67 | private final VpnCallback mCallback; |
| 68 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 69 | private String mPackage = VpnConfig.LEGACY_VPN; |
| 70 | private String mInterface; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 71 | private Connection mConnection; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 72 | private LegacyVpnRunner mLegacyVpnRunner; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 73 | |
| 74 | public Vpn(Context context, VpnCallback callback) { |
| 75 | mContext = context; |
| 76 | mCallback = callback; |
| 77 | } |
| 78 | |
| 79 | /** |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 80 | * Prepare for a VPN application. This method is designed to solve |
| 81 | * race conditions. It first compares the current prepared package |
| 82 | * with {@code oldPackage}. If they are the same, the prepared |
| 83 | * package is revoked and replaced with {@code newPackage}. If |
| 84 | * {@code oldPackage} is {@code null}, the comparison is omitted. |
| 85 | * If {@code newPackage} is the same package or {@code null}, the |
| 86 | * revocation is omitted. This method returns {@code true} if the |
| 87 | * operation is succeeded. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 88 | * |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 89 | * Legacy VPN is handled specially since it is not a real package. |
| 90 | * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and |
| 91 | * it can be revoked by itself. |
| 92 | * |
| 93 | * @param oldPackage The package name of the old VPN application. |
| 94 | * @param newPackage The package name of the new VPN application. |
| 95 | * @return true if the operation is succeeded. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 96 | */ |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 97 | public synchronized boolean prepare(String oldPackage, String newPackage) { |
| 98 | // Return false if the package does not match. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 99 | if (oldPackage != null && !oldPackage.equals(mPackage)) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 100 | return false; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 101 | } |
| 102 | |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 103 | // Return true if we do not need to revoke. |
| 104 | if (newPackage == null || |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 105 | (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
| 109 | // Only system user can revoke a package. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 110 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 111 | throw new SecurityException("Unauthorized Caller"); |
| 112 | } |
| 113 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 114 | // Reset the interface and hide the notification. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 115 | if (mInterface != null) { |
| 116 | jniReset(mInterface); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 117 | mCallback.restore(); |
| 118 | hideNotification(); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 119 | mInterface = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 120 | } |
| 121 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 122 | // Revoke the connection or stop LegacyVpnRunner. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 123 | if (mConnection != null) { |
| 124 | try { |
| 125 | mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION, |
| 126 | Parcel.obtain(), null, IBinder.FLAG_ONEWAY); |
| 127 | } catch (Exception e) { |
| 128 | // ignore |
| 129 | } |
| 130 | mContext.unbindService(mConnection); |
| 131 | mConnection = null; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 132 | } else if (mLegacyVpnRunner != null) { |
| 133 | mLegacyVpnRunner.exit(); |
| 134 | mLegacyVpnRunner = null; |
| 135 | } |
| 136 | |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 137 | Log.i(TAG, "Switched from " + mPackage + " to " + newPackage); |
| 138 | mPackage = newPackage; |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 139 | return true; |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 143 | * Protect a socket from routing changes by binding it to the given |
| 144 | * interface. The socket is NOT closed by this method. |
| 145 | * |
| 146 | * @param socket The socket to be bound. |
| 147 | * @param name The name of the interface. |
| 148 | */ |
| 149 | public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception { |
| 150 | PackageManager pm = mContext.getPackageManager(); |
| 151 | ApplicationInfo app = pm.getApplicationInfo(mPackage, 0); |
| 152 | if (Binder.getCallingUid() != app.uid) { |
| 153 | throw new SecurityException("Unauthorized Caller"); |
| 154 | } |
| 155 | jniProtect(socket.getFd(), interfaze); |
| 156 | } |
| 157 | |
| 158 | /** |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 159 | * Establish a VPN network and return the file descriptor of the VPN |
| 160 | * interface. This methods returns {@code null} if the application is |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 161 | * revoked or not prepared. |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 162 | * |
| 163 | * @param config The parameters to configure the network. |
| 164 | * @return The file descriptor of the VPN interface. |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 165 | */ |
Chia-chi Yeh | 04ba25c | 2011-06-15 17:07:27 -0700 | [diff] [blame] | 166 | public synchronized ParcelFileDescriptor establish(VpnConfig config) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 167 | // Check if the caller is already prepared. |
| 168 | PackageManager pm = mContext.getPackageManager(); |
| 169 | ApplicationInfo app = null; |
| 170 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 171 | app = pm.getApplicationInfo(mPackage, 0); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 172 | } catch (Exception e) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 173 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 174 | } |
| 175 | if (Binder.getCallingUid() != app.uid) { |
Chia-chi Yeh | 7b0b834 | 2011-06-17 14:34:11 -0700 | [diff] [blame] | 176 | return null; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 179 | // Check if the service is properly declared. |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 180 | Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE); |
| 181 | intent.setClassName(mPackage, config.user); |
| 182 | ResolveInfo info = pm.resolveService(intent, 0); |
| 183 | if (info == null) { |
| 184 | throw new SecurityException("Cannot find " + config.user); |
| 185 | } |
| 186 | if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) { |
| 187 | throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE); |
| 188 | } |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 189 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 190 | // Load the label. |
| 191 | String label = app.loadLabel(pm).toString(); |
| 192 | |
| 193 | // Load the icon and convert it into a bitmap. |
| 194 | Drawable icon = app.loadIcon(pm); |
| 195 | Bitmap bitmap = null; |
| 196 | if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) { |
| 197 | int width = mContext.getResources().getDimensionPixelSize( |
| 198 | android.R.dimen.notification_large_icon_width); |
| 199 | int height = mContext.getResources().getDimensionPixelSize( |
| 200 | android.R.dimen.notification_large_icon_height); |
| 201 | icon.setBounds(0, 0, width, height); |
| 202 | bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
Dianne Hackborn | 6311d0a | 2011-08-02 16:37:58 -0700 | [diff] [blame] | 203 | Canvas c = new Canvas(bitmap); |
| 204 | icon.draw(c); |
| 205 | c.setBitmap(null); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 208 | // Configure the interface. Abort if any of these steps fails. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 209 | ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu)); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 210 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 211 | String interfaze = jniGetName(tun.getFd()); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 212 | if (jniSetAddresses(interfaze, config.addresses) < 1) { |
| 213 | throw new IllegalArgumentException("At least one address must be specified"); |
| 214 | } |
| 215 | if (config.routes != null) { |
| 216 | jniSetRoutes(interfaze, config.routes); |
| 217 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 218 | Connection connection = new Connection(); |
| 219 | if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) { |
| 220 | throw new IllegalStateException("Cannot bind " + config.user); |
| 221 | } |
| 222 | if (mConnection != null) { |
| 223 | mContext.unbindService(mConnection); |
| 224 | } |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 225 | if (mInterface != null && !mInterface.equals(interfaze)) { |
| 226 | jniReset(mInterface); |
Chia-chi Yeh | f4e3bf8 | 2011-06-30 12:33:17 -0700 | [diff] [blame] | 227 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 228 | mConnection = connection; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 229 | mInterface = interfaze; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 230 | } catch (RuntimeException e) { |
| 231 | try { |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 232 | tun.close(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 233 | } catch (Exception ex) { |
| 234 | // ignore |
| 235 | } |
| 236 | throw e; |
| 237 | } |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 238 | Log.i(TAG, "Established by " + config.user + " on " + mInterface); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 239 | |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 240 | // Fill more values. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 241 | config.user = mPackage; |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 242 | config.interfaze = mInterface; |
Chia-chi Yeh | 8909b10 | 2011-07-01 01:09:42 -0700 | [diff] [blame] | 243 | |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 244 | // Override DNS servers and show the notification. |
| 245 | long identity = Binder.clearCallingIdentity(); |
| 246 | mCallback.override(config.dnsServers, config.searchDomains); |
Chia-chi Yeh | 383e052 | 2011-07-01 00:13:25 -0700 | [diff] [blame] | 247 | showNotification(config, label, bitmap); |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 248 | Binder.restoreCallingIdentity(identity); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 249 | return tun; |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 252 | // INetworkManagementEventObserver.Stub |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 253 | @Override |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 254 | public void interfaceAdded(String interfaze) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // INetworkManagementEventObserver.Stub |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 258 | @Override |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 259 | public synchronized void interfaceStatusChanged(String interfaze, boolean up) { |
| 260 | if (!up && mLegacyVpnRunner != null) { |
| 261 | mLegacyVpnRunner.check(interfaze); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | // INetworkManagementEventObserver.Stub |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 266 | @Override |
| 267 | public void interfaceLinkStateChanged(String interfaze, boolean up) { |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // INetworkManagementEventObserver.Stub |
| 271 | @Override |
| 272 | public synchronized void interfaceRemoved(String interfaze) { |
| 273 | if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) { |
| 274 | long identity = Binder.clearCallingIdentity(); |
| 275 | mCallback.restore(); |
| 276 | hideNotification(); |
| 277 | Binder.restoreCallingIdentity(identity); |
| 278 | mInterface = null; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 279 | if (mConnection != null) { |
| 280 | mContext.unbindService(mConnection); |
| 281 | mConnection = null; |
Chia-chi Yeh | 0c074e6 | 2011-08-15 15:19:40 -0700 | [diff] [blame] | 282 | } else if (mLegacyVpnRunner != null) { |
| 283 | mLegacyVpnRunner.exit(); |
| 284 | mLegacyVpnRunner = null; |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 285 | } |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 286 | } |
| 287 | } |
| 288 | |
| 289 | // INetworkManagementEventObserver.Stub |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 290 | @Override |
| 291 | public void limitReached(String limit, String interfaze) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Chia-chi Yeh | 199ed6e | 2011-08-03 17:38:49 -0700 | [diff] [blame] | 294 | private class Connection implements ServiceConnection { |
| 295 | private IBinder mService; |
| 296 | |
| 297 | @Override |
| 298 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 299 | mService = service; |
| 300 | } |
| 301 | |
| 302 | @Override |
| 303 | public void onServiceDisconnected(ComponentName name) { |
| 304 | mService = null; |
| 305 | } |
| 306 | } |
| 307 | |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 308 | private void showNotification(VpnConfig config, String label, Bitmap icon) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 309 | NotificationManager nm = (NotificationManager) |
| 310 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 311 | |
| 312 | if (nm != null) { |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 313 | String title = (label == null) ? mContext.getString(R.string.vpn_title) : |
| 314 | mContext.getString(R.string.vpn_title_long, label); |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 315 | String text = (config.session == null) ? mContext.getString(R.string.vpn_text) : |
| 316 | mContext.getString(R.string.vpn_text_long, config.session); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 317 | config.startTime = SystemClock.elapsedRealtime(); |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 318 | |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 319 | Notification notification = new Notification.Builder(mContext) |
| 320 | .setSmallIcon(R.drawable.vpn_connected) |
Chia-chi Yeh | a4b87b5 | 2011-06-30 23:21:55 -0700 | [diff] [blame] | 321 | .setLargeIcon(icon) |
| 322 | .setContentTitle(title) |
Chia-chi Yeh | f8905fd | 2011-06-14 16:35:02 -0700 | [diff] [blame] | 323 | .setContentText(text) |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 324 | .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config)) |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 325 | .setDefaults(Notification.DEFAULT_ALL) |
| 326 | .setOngoing(true) |
| 327 | .getNotification(); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 328 | nm.notify(R.drawable.vpn_connected, notification); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 329 | } |
| 330 | } |
| 331 | |
| 332 | private void hideNotification() { |
| 333 | NotificationManager nm = (NotificationManager) |
| 334 | mContext.getSystemService(Context.NOTIFICATION_SERVICE); |
| 335 | |
| 336 | if (nm != null) { |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 337 | nm.cancel(R.drawable.vpn_connected); |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 341 | private native int jniCreate(int mtu); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 342 | private native String jniGetName(int tun); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 343 | private native int jniSetAddresses(String interfaze, String addresses); |
| 344 | private native int jniSetRoutes(String interfaze, String routes); |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 345 | private native void jniReset(String interfaze); |
| 346 | private native int jniCheck(String interfaze); |
| 347 | private native void jniProtect(int socket, String interfaze); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 348 | |
| 349 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 350 | * Start legacy VPN. This method stops the daemons and restart them |
| 351 | * if arguments are not null. Heavy things are offloaded to another |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 352 | * thread, so callers will not be blocked for a long time. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 353 | * |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 354 | * @param config The parameters to configure the network. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 355 | * @param raoocn The arguments to be passed to racoon. |
| 356 | * @param mtpd The arguments to be passed to mtpd. |
| 357 | */ |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 358 | public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 359 | // Prepare for the new request. This also checks the caller. |
| 360 | prepare(null, VpnConfig.LEGACY_VPN); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 361 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 362 | // Start a new LegacyVpnRunner and we are done! |
Chia-chi Yeh | 100155a | 2011-07-03 16:52:38 -0700 | [diff] [blame] | 363 | mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd); |
| 364 | mLegacyVpnRunner.start(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 368 | * Return the information of the current ongoing legacy VPN. |
| 369 | */ |
| 370 | public synchronized LegacyVpnInfo getLegacyVpnInfo() { |
| 371 | // Only system user can call this method. |
| 372 | if (Binder.getCallingUid() != Process.SYSTEM_UID) { |
| 373 | throw new SecurityException("Unauthorized Caller"); |
| 374 | } |
| 375 | return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo(); |
| 376 | } |
| 377 | |
| 378 | /** |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 379 | * Bringing up a VPN connection takes time, and that is all this thread |
| 380 | * does. Here we have plenty of time. The only thing we need to take |
| 381 | * care of is responding to interruptions as soon as possible. Otherwise |
| 382 | * requests will be piled up. This can be done in a Handler as a state |
| 383 | * machine, but it is much easier to read in the current form. |
| 384 | */ |
| 385 | private class LegacyVpnRunner extends Thread { |
| 386 | private static final String TAG = "LegacyVpnRunner"; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 387 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 388 | private final VpnConfig mConfig; |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 389 | private final String[] mDaemons; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 390 | private final String[][] mArguments; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 391 | private final LocalSocket[] mSockets; |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 392 | private final String mOuterInterface; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 393 | private final LegacyVpnInfo mInfo; |
| 394 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 395 | private long mTimer = -1; |
| 396 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 397 | public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 398 | super(TAG); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 399 | mConfig = config; |
| 400 | mDaemons = new String[] {"racoon", "mtpd"}; |
| 401 | mArguments = new String[][] {racoon, mtpd}; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 402 | mSockets = new LocalSocket[mDaemons.length]; |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 403 | mInfo = new LegacyVpnInfo(); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 404 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 405 | // This is the interface which VPN is running on. |
| 406 | mOuterInterface = mConfig.interfaze; |
| 407 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 408 | // Legacy VPN is not a real package, so we use it to carry the key. |
Chia-chi Yeh | fcc1b41 | 2011-08-03 15:39:59 -0700 | [diff] [blame] | 409 | mInfo.key = mConfig.user; |
| 410 | mConfig.user = VpnConfig.LEGACY_VPN; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 411 | } |
| 412 | |
Chia-chi Yeh | aa1727f | 2011-07-14 18:55:33 -0700 | [diff] [blame] | 413 | public void check(String interfaze) { |
| 414 | if (interfaze.equals(mOuterInterface)) { |
| 415 | Log.i(TAG, "Legacy VPN is going down with " + interfaze); |
| 416 | exit(); |
| 417 | } |
| 418 | } |
| 419 | |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 420 | public void exit() { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 421 | // We assume that everything is reset after stopping the daemons. |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 422 | interrupt(); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 423 | for (LocalSocket socket : mSockets) { |
| 424 | try { |
| 425 | socket.close(); |
| 426 | } catch (Exception e) { |
| 427 | // ignore |
| 428 | } |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 429 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 432 | public LegacyVpnInfo getInfo() { |
| 433 | // Update the info when VPN is disconnected. |
| 434 | if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) { |
| 435 | mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED; |
| 436 | mInfo.intent = null; |
| 437 | } |
| 438 | return mInfo; |
| 439 | } |
| 440 | |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 441 | @Override |
| 442 | public void run() { |
| 443 | // Wait for the previous thread since it has been interrupted. |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 444 | Log.v(TAG, "Waiting"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 445 | synchronized (TAG) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 446 | Log.v(TAG, "Executing"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 447 | execute(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 448 | } |
| 449 | } |
| 450 | |
| 451 | private void checkpoint(boolean yield) throws InterruptedException { |
| 452 | long now = SystemClock.elapsedRealtime(); |
| 453 | if (mTimer == -1) { |
| 454 | mTimer = now; |
| 455 | Thread.sleep(1); |
Chia-chi Yeh | 7ef8611 | 2011-07-22 15:46:52 -0700 | [diff] [blame] | 456 | } else if (now - mTimer <= 60000) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 457 | Thread.sleep(yield ? 200 : 1); |
| 458 | } else { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 459 | mInfo.state = LegacyVpnInfo.STATE_TIMEOUT; |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 460 | throw new IllegalStateException("Time is up"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | private void execute() { |
| 465 | // Catch all exceptions so we can clean up few things. |
| 466 | try { |
| 467 | // Initialize the timer. |
| 468 | checkpoint(false); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 469 | mInfo.state = LegacyVpnInfo.STATE_INITIALIZING; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 470 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 471 | // Wait for the daemons to stop. |
| 472 | for (String daemon : mDaemons) { |
| 473 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 474 | while (!"stopped".equals(SystemProperties.get(key, "stopped"))) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 475 | checkpoint(true); |
| 476 | } |
| 477 | } |
| 478 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 479 | // Clear the previous state. |
| 480 | File state = new File("/data/misc/vpn/state"); |
| 481 | state.delete(); |
| 482 | if (state.exists()) { |
| 483 | throw new IllegalStateException("Cannot delete the state"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 484 | } |
Chia-chi Yeh | c187273 | 2011-12-08 16:51:41 -0800 | [diff] [blame] | 485 | new File("/data/misc/vpn/abort").delete(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 486 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 487 | // Check if we need to restart any of the daemons. |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 488 | boolean restart = false; |
| 489 | for (String[] arguments : mArguments) { |
| 490 | restart = restart || (arguments != null); |
| 491 | } |
| 492 | if (!restart) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 493 | mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 494 | return; |
| 495 | } |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 496 | mInfo.state = LegacyVpnInfo.STATE_CONNECTING; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 497 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 498 | // Start the daemon with arguments. |
| 499 | for (int i = 0; i < mDaemons.length; ++i) { |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 500 | String[] arguments = mArguments[i]; |
| 501 | if (arguments == null) { |
| 502 | continue; |
| 503 | } |
| 504 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 505 | // Start the daemon. |
| 506 | String daemon = mDaemons[i]; |
| 507 | SystemProperties.set("ctl.start", daemon); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 508 | |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 509 | // Wait for the daemon to start. |
| 510 | String key = "init.svc." + daemon; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 511 | while (!"running".equals(SystemProperties.get(key))) { |
| 512 | checkpoint(true); |
| 513 | } |
| 514 | |
| 515 | // Create the control socket. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 516 | mSockets[i] = new LocalSocket(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 517 | LocalSocketAddress address = new LocalSocketAddress( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 518 | daemon, LocalSocketAddress.Namespace.RESERVED); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 519 | |
| 520 | // Wait for the socket to connect. |
| 521 | while (true) { |
| 522 | try { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 523 | mSockets[i].connect(address); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 524 | break; |
| 525 | } catch (Exception e) { |
| 526 | // ignore |
| 527 | } |
| 528 | checkpoint(true); |
| 529 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 530 | mSockets[i].setSoTimeout(500); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 531 | |
| 532 | // Send over the arguments. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 533 | OutputStream out = mSockets[i].getOutputStream(); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 534 | for (String argument : arguments) { |
| 535 | byte[] bytes = argument.getBytes(Charsets.UTF_8); |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 536 | if (bytes.length >= 0xFFFF) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 537 | throw new IllegalArgumentException("Argument is too large"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 538 | } |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 539 | out.write(bytes.length >> 8); |
| 540 | out.write(bytes.length); |
| 541 | out.write(bytes); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 542 | checkpoint(false); |
| 543 | } |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 544 | out.write(0xFF); |
| 545 | out.write(0xFF); |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 546 | out.flush(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 547 | |
| 548 | // Wait for End-of-File. |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 549 | InputStream in = mSockets[i].getInputStream(); |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 550 | while (true) { |
| 551 | try { |
| 552 | if (in.read() == -1) { |
| 553 | break; |
| 554 | } |
| 555 | } catch (Exception e) { |
| 556 | // ignore |
| 557 | } |
| 558 | checkpoint(true); |
| 559 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 560 | } |
| 561 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 562 | // Wait for the daemons to create the new state. |
| 563 | while (!state.exists()) { |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 564 | // Check if a running daemon is dead. |
| 565 | for (int i = 0; i < mDaemons.length; ++i) { |
| 566 | String daemon = mDaemons[i]; |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 567 | if (mArguments[i] != null && !"running".equals( |
Chia-chi Yeh | 1f7746b | 2011-07-01 00:29:06 -0700 | [diff] [blame] | 568 | SystemProperties.get("init.svc." + daemon))) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 569 | throw new IllegalStateException(daemon + " is dead"); |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | checkpoint(true); |
| 573 | } |
| 574 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 575 | // Now we are connected. Read and parse the new state. |
| 576 | byte[] buffer = new byte[(int) state.length()]; |
| 577 | if (new FileInputStream(state).read(buffer) != buffer.length) { |
| 578 | throw new IllegalStateException("Cannot read the state"); |
| 579 | } |
| 580 | String[] parameters = new String(buffer, Charsets.UTF_8).split("\n", -1); |
| 581 | if (parameters.length != 6) { |
| 582 | throw new IllegalStateException("Cannot parse the state"); |
| 583 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 584 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 585 | // Set the interface and the addresses in the config. |
| 586 | mConfig.interfaze = parameters[0].trim(); |
| 587 | mConfig.addresses = parameters[1].trim(); |
| 588 | |
| 589 | // Set the routes if they are not set in the config. |
| 590 | if (mConfig.routes == null || mConfig.routes.isEmpty()) { |
| 591 | mConfig.routes = parameters[2].trim(); |
| 592 | } |
| 593 | |
| 594 | // 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] | 595 | if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) { |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 596 | String dnsServers = parameters[3].trim(); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 597 | if (!dnsServers.isEmpty()) { |
| 598 | mConfig.dnsServers = Arrays.asList(dnsServers.split(" ")); |
| 599 | } |
| 600 | } |
| 601 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 602 | // Set the search domains if they are not set in the config. |
| 603 | if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) { |
| 604 | String searchDomains = parameters[4].trim(); |
| 605 | if (!searchDomains.isEmpty()) { |
| 606 | mConfig.searchDomains = Arrays.asList(searchDomains.split(" ")); |
| 607 | } |
| 608 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 609 | |
Chia-chi Yeh | 97a6156 | 2011-07-14 15:05:05 -0700 | [diff] [blame] | 610 | // Set the routes. |
| 611 | jniSetRoutes(mConfig.interfaze, mConfig.routes); |
| 612 | |
| 613 | // Here is the last step and it must be done synchronously. |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 614 | synchronized (Vpn.this) { |
| 615 | // Check if the thread is interrupted while we are waiting. |
| 616 | checkpoint(false); |
| 617 | |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 618 | // Check if the interface is gone while we are waiting. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 619 | if (jniCheck(mConfig.interfaze) == 0) { |
Chia-chi Yeh | 34e7813 | 2011-07-03 03:07:07 -0700 | [diff] [blame] | 620 | throw new IllegalStateException(mConfig.interfaze + " is gone"); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 621 | } |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 622 | |
| 623 | // Now INetworkManagementEventObserver is watching our back. |
Chia-chi Yeh | c2b8aa0 | 2011-07-03 18:00:47 -0700 | [diff] [blame] | 624 | mInterface = mConfig.interfaze; |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 625 | mCallback.override(mConfig.dnsServers, mConfig.searchDomains); |
| 626 | showNotification(mConfig, null, null); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 627 | |
| 628 | Log.i(TAG, "Connected!"); |
| 629 | mInfo.state = LegacyVpnInfo.STATE_CONNECTED; |
| 630 | mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null); |
Chia-chi Yeh | 41d1685 | 2011-07-01 02:12:06 -0700 | [diff] [blame] | 631 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 632 | } catch (Exception e) { |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 633 | Log.i(TAG, "Aborting", e); |
Chia-chi Yeh | e910790 | 2011-07-02 01:48:50 -0700 | [diff] [blame] | 634 | exit(); |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 635 | } finally { |
Chia-chi Yeh | 5317f03 | 2011-08-22 13:09:49 -0700 | [diff] [blame] | 636 | // Kill the daemons if they fail to stop. |
| 637 | if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING) { |
| 638 | for (String daemon : mDaemons) { |
| 639 | SystemProperties.set("ctl.stop", daemon); |
| 640 | } |
| 641 | } |
| 642 | |
Chia-chi Yeh | 2e46764 | 2011-07-04 03:23:12 -0700 | [diff] [blame] | 643 | // Do not leave an unstable state. |
| 644 | if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING || |
| 645 | mInfo.state == LegacyVpnInfo.STATE_CONNECTING) { |
| 646 | mInfo.state = LegacyVpnInfo.STATE_FAILED; |
| 647 | } |
Chia-chi Yeh | 85a7ce0 | 2011-06-29 16:05:58 -0700 | [diff] [blame] | 648 | } |
| 649 | } |
| 650 | } |
Chia-chi Yeh | ff3bdca | 2011-05-23 17:26:46 -0700 | [diff] [blame] | 651 | } |