blob: b12d5976bf91a90ae465707937cc17ea64f9775f [file] [log] [blame]
Chia-chi Yehff3bdca2011-05-23 17:26:46 -07001/*
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
17package com.android.server.connectivity;
18
19import android.app.Notification;
20import android.app.NotificationManager;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070021import android.content.ComponentName;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070022import android.content.Context;
23import android.content.Intent;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070024import android.content.ServiceConnection;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070025import android.content.pm.ApplicationInfo;
26import android.content.pm.PackageManager;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070027import android.content.pm.ResolveInfo;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070028import android.content.res.Resources;
29import android.graphics.Bitmap;
30import android.graphics.Canvas;
31import android.graphics.drawable.Drawable;
32import android.net.INetworkManagementEventObserver;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070033import android.net.LocalSocket;
34import android.net.LocalSocketAddress;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070035import android.os.Binder;
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -080036import android.os.FileUtils;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070037import android.os.IBinder;
38import android.os.Parcel;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070039import android.os.ParcelFileDescriptor;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070040import android.os.Process;
41import android.os.SystemClock;
42import android.os.SystemProperties;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070043import android.util.Log;
44
45import com.android.internal.R;
Chia-chi Yeh2e467642011-07-04 03:23:12 -070046import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070047import com.android.internal.net.VpnConfig;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070048import com.android.server.ConnectivityService.VpnCallback;
49
Chia-chi Yeh97a61562011-07-14 15:05:05 -070050import java.io.File;
Chia-chi Yeh97a61562011-07-14 15:05:05 -070051import java.io.InputStream;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070052import java.io.OutputStream;
53import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070054import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070055
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070056/**
57 * @hide
58 */
59public class Vpn extends INetworkManagementEventObserver.Stub {
60
61 private final static String TAG = "Vpn";
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070062
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070063 private final static String BIND_VPN_SERVICE =
64 android.Manifest.permission.BIND_VPN_SERVICE;
65
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070066 private final Context mContext;
67 private final VpnCallback mCallback;
68
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070069 private String mPackage = VpnConfig.LEGACY_VPN;
70 private String mInterface;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070071 private Connection mConnection;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070072 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070073
74 public Vpn(Context context, VpnCallback callback) {
75 mContext = context;
76 mCallback = callback;
77 }
78
79 /**
Chia-chi Yeh100155a2011-07-03 16:52:38 -070080 * 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 Yehff3bdca2011-05-23 17:26:46 -070088 *
Chia-chi Yeh100155a2011-07-03 16:52:38 -070089 * 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 Yehe9107902011-07-02 01:48:50 -070096 */
Chia-chi Yeh100155a2011-07-03 16:52:38 -070097 public synchronized boolean prepare(String oldPackage, String newPackage) {
98 // Return false if the package does not match.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070099 if (oldPackage != null && !oldPackage.equals(mPackage)) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700100 return false;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700101 }
102
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700103 // Return true if we do not need to revoke.
104 if (newPackage == null ||
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700105 (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700106 return true;
107 }
108
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700109 // Check if the caller is authorized.
110 enforceControlPermission();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700111
Chia-chi Yehe9107902011-07-02 01:48:50 -0700112 // Reset the interface and hide the notification.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700113 if (mInterface != null) {
114 jniReset(mInterface);
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700115 long identity = Binder.clearCallingIdentity();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700116 mCallback.restore();
117 hideNotification();
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700118 Binder.restoreCallingIdentity(identity);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700119 mInterface = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700120 }
121
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700122 // Revoke the connection or stop LegacyVpnRunner.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700123 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 Yehe9107902011-07-02 01:48:50 -0700132 } else if (mLegacyVpnRunner != null) {
133 mLegacyVpnRunner.exit();
134 mLegacyVpnRunner = null;
135 }
136
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700137 Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
138 mPackage = newPackage;
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700139 return true;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700140 }
141
142 /**
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700143 * 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 Yehe9107902011-07-02 01:48:50 -0700159 * 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 Yeh100155a2011-07-03 16:52:38 -0700161 * revoked or not prepared.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700162 *
163 * @param config The parameters to configure the network.
164 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700165 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700166 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700167 // Check if the caller is already prepared.
168 PackageManager pm = mContext.getPackageManager();
169 ApplicationInfo app = null;
170 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700171 app = pm.getApplicationInfo(mPackage, 0);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700172 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700173 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700174 }
175 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700176 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700177 }
178
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700179 // Check if the service is properly declared.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700180 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 Yehfcc1b412011-08-03 15:39:59 -0700189
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700190 // 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 Hackborn6311d0a2011-08-02 16:37:58 -0700203 Canvas c = new Canvas(bitmap);
204 icon.draw(c);
205 c.setBitmap(null);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700206 }
207
Chia-chi Yehe9107902011-07-02 01:48:50 -0700208 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700209 ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700210 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700211 String interfaze = jniGetName(tun.getFd());
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700212 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 Yeh199ed6e2011-08-03 17:38:49 -0700218 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 Yehc2b8aa02011-07-03 18:00:47 -0700225 if (mInterface != null && !mInterface.equals(interfaze)) {
226 jniReset(mInterface);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700227 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700228 mConnection = connection;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700229 mInterface = interfaze;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700230 } catch (RuntimeException e) {
231 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700232 tun.close();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700233 } catch (Exception ex) {
234 // ignore
235 }
236 throw e;
237 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700238 Log.i(TAG, "Established by " + config.user + " on " + mInterface);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700239
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700240 // Fill more values.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700241 config.user = mPackage;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700242 config.interfaze = mInterface;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700243
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700244 // Override DNS servers and show the notification.
245 long identity = Binder.clearCallingIdentity();
246 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700247 showNotification(config, label, bitmap);
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700248 Binder.restoreCallingIdentity(identity);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700249 return tun;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700250 }
251
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700252 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700253 @Override
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700254 public void interfaceAdded(String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700255 }
256
257 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700258 @Override
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700259 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 Yehfcc1b412011-08-03 15:39:59 -0700266 @Override
267 public void interfaceLinkStateChanged(String interfaze, boolean up) {
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700268 }
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 Yeh199ed6e2011-08-03 17:38:49 -0700279 if (mConnection != null) {
280 mContext.unbindService(mConnection);
281 mConnection = null;
Chia-chi Yeh0c074e62011-08-15 15:19:40 -0700282 } else if (mLegacyVpnRunner != null) {
283 mLegacyVpnRunner.exit();
284 mLegacyVpnRunner = null;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700285 }
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700286 }
287 }
288
289 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700290 @Override
291 public void limitReached(String limit, String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700292 }
293
Haoyu Baidb3c8672012-06-20 14:29:57 -0700294 public void interfaceClassDataActivityChanged(String label, boolean active) {
295 }
296
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700297 private void enforceControlPermission() {
298 // System user is allowed to control VPN.
299 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
300 return;
301 }
302
303 try {
304 // System dialogs are also allowed to control VPN.
305 PackageManager pm = mContext.getPackageManager();
306 ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0);
307 if (Binder.getCallingUid() == app.uid) {
308 return;
309 }
310 } catch (Exception e) {
311 // ignore
312 }
313
314 throw new SecurityException("Unauthorized Caller");
315 }
316
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700317 private class Connection implements ServiceConnection {
318 private IBinder mService;
319
320 @Override
321 public void onServiceConnected(ComponentName name, IBinder service) {
322 mService = service;
323 }
324
325 @Override
326 public void onServiceDisconnected(ComponentName name) {
327 mService = null;
328 }
329 }
330
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700331 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700332 NotificationManager nm = (NotificationManager)
333 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
334
335 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700336 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
337 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700338 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
339 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700340 config.startTime = SystemClock.elapsedRealtime();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700341
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700342 Notification notification = new Notification.Builder(mContext)
343 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700344 .setLargeIcon(icon)
345 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700346 .setContentText(text)
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700347 .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config))
Chia-chi Yeh50fe7092012-01-11 14:26:24 -0800348 .setDefaults(0)
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700349 .setOngoing(true)
350 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700351 nm.notify(R.drawable.vpn_connected, notification);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700352 }
353 }
354
355 private void hideNotification() {
356 NotificationManager nm = (NotificationManager)
357 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
358
359 if (nm != null) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700360 nm.cancel(R.drawable.vpn_connected);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700361 }
362 }
363
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700364 private native int jniCreate(int mtu);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700365 private native String jniGetName(int tun);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700366 private native int jniSetAddresses(String interfaze, String addresses);
367 private native int jniSetRoutes(String interfaze, String routes);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700368 private native void jniReset(String interfaze);
369 private native int jniCheck(String interfaze);
370 private native void jniProtect(int socket, String interfaze);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700371
372 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700373 * Start legacy VPN. This method stops the daemons and restart them
374 * if arguments are not null. Heavy things are offloaded to another
Chia-chi Yehe9107902011-07-02 01:48:50 -0700375 * thread, so callers will not be blocked for a long time.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700376 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700377 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700378 * @param raoocn The arguments to be passed to racoon.
379 * @param mtpd The arguments to be passed to mtpd.
380 */
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700381 public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700382 // Prepare for the new request. This also checks the caller.
383 prepare(null, VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700384
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700385 // Start a new LegacyVpnRunner and we are done!
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700386 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
387 mLegacyVpnRunner.start();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700388 }
389
390 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700391 * Return the information of the current ongoing legacy VPN.
392 */
393 public synchronized LegacyVpnInfo getLegacyVpnInfo() {
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700394 // Check if the caller is authorized.
395 enforceControlPermission();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700396 return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
397 }
398
399 /**
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700400 * Bringing up a VPN connection takes time, and that is all this thread
401 * does. Here we have plenty of time. The only thing we need to take
402 * care of is responding to interruptions as soon as possible. Otherwise
403 * requests will be piled up. This can be done in a Handler as a state
404 * machine, but it is much easier to read in the current form.
405 */
406 private class LegacyVpnRunner extends Thread {
407 private static final String TAG = "LegacyVpnRunner";
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700408
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700409 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700410 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700411 private final String[][] mArguments;
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700412 private final LocalSocket[] mSockets;
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700413 private final String mOuterInterface;
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700414 private final LegacyVpnInfo mInfo;
415
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700416 private long mTimer = -1;
417
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700418 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700419 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700420 mConfig = config;
421 mDaemons = new String[] {"racoon", "mtpd"};
422 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700423 mSockets = new LocalSocket[mDaemons.length];
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700424 mInfo = new LegacyVpnInfo();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700425
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700426 // This is the interface which VPN is running on.
427 mOuterInterface = mConfig.interfaze;
428
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700429 // Legacy VPN is not a real package, so we use it to carry the key.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700430 mInfo.key = mConfig.user;
431 mConfig.user = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700432 }
433
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700434 public void check(String interfaze) {
435 if (interfaze.equals(mOuterInterface)) {
436 Log.i(TAG, "Legacy VPN is going down with " + interfaze);
437 exit();
438 }
439 }
440
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700441 public void exit() {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700442 // We assume that everything is reset after stopping the daemons.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700443 interrupt();
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700444 for (LocalSocket socket : mSockets) {
445 try {
446 socket.close();
447 } catch (Exception e) {
448 // ignore
449 }
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700450 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700451 }
452
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700453 public LegacyVpnInfo getInfo() {
454 // Update the info when VPN is disconnected.
455 if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) {
456 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
457 mInfo.intent = null;
458 }
459 return mInfo;
460 }
461
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700462 @Override
463 public void run() {
464 // Wait for the previous thread since it has been interrupted.
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700465 Log.v(TAG, "Waiting");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700466 synchronized (TAG) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700467 Log.v(TAG, "Executing");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700468 execute();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700469 }
470 }
471
472 private void checkpoint(boolean yield) throws InterruptedException {
473 long now = SystemClock.elapsedRealtime();
474 if (mTimer == -1) {
475 mTimer = now;
476 Thread.sleep(1);
Chia-chi Yeh7ef86112011-07-22 15:46:52 -0700477 } else if (now - mTimer <= 60000) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700478 Thread.sleep(yield ? 200 : 1);
479 } else {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700480 mInfo.state = LegacyVpnInfo.STATE_TIMEOUT;
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700481 throw new IllegalStateException("Time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700482 }
483 }
484
485 private void execute() {
486 // Catch all exceptions so we can clean up few things.
487 try {
488 // Initialize the timer.
489 checkpoint(false);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700490 mInfo.state = LegacyVpnInfo.STATE_INITIALIZING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700491
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700492 // Wait for the daemons to stop.
493 for (String daemon : mDaemons) {
494 String key = "init.svc." + daemon;
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700495 while (!"stopped".equals(SystemProperties.get(key, "stopped"))) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700496 checkpoint(true);
497 }
498 }
499
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700500 // Clear the previous state.
501 File state = new File("/data/misc/vpn/state");
502 state.delete();
503 if (state.exists()) {
504 throw new IllegalStateException("Cannot delete the state");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700505 }
Chia-chi Yehc1872732011-12-08 16:51:41 -0800506 new File("/data/misc/vpn/abort").delete();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700507
Chia-chi Yehe9107902011-07-02 01:48:50 -0700508 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700509 boolean restart = false;
510 for (String[] arguments : mArguments) {
511 restart = restart || (arguments != null);
512 }
513 if (!restart) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700514 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700515 return;
516 }
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700517 mInfo.state = LegacyVpnInfo.STATE_CONNECTING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700518
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700519 // Start the daemon with arguments.
520 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700521 String[] arguments = mArguments[i];
522 if (arguments == null) {
523 continue;
524 }
525
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700526 // Start the daemon.
527 String daemon = mDaemons[i];
528 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700529
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700530 // Wait for the daemon to start.
531 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700532 while (!"running".equals(SystemProperties.get(key))) {
533 checkpoint(true);
534 }
535
536 // Create the control socket.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700537 mSockets[i] = new LocalSocket();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700538 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700539 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700540
541 // Wait for the socket to connect.
542 while (true) {
543 try {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700544 mSockets[i].connect(address);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700545 break;
546 } catch (Exception e) {
547 // ignore
548 }
549 checkpoint(true);
550 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700551 mSockets[i].setSoTimeout(500);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700552
553 // Send over the arguments.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700554 OutputStream out = mSockets[i].getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700555 for (String argument : arguments) {
556 byte[] bytes = argument.getBytes(Charsets.UTF_8);
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700557 if (bytes.length >= 0xFFFF) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700558 throw new IllegalArgumentException("Argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700559 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700560 out.write(bytes.length >> 8);
561 out.write(bytes.length);
562 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700563 checkpoint(false);
564 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700565 out.write(0xFF);
566 out.write(0xFF);
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700567 out.flush();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700568
569 // Wait for End-of-File.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700570 InputStream in = mSockets[i].getInputStream();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700571 while (true) {
572 try {
573 if (in.read() == -1) {
574 break;
575 }
576 } catch (Exception e) {
577 // ignore
578 }
579 checkpoint(true);
580 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700581 }
582
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700583 // Wait for the daemons to create the new state.
584 while (!state.exists()) {
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700585 // Check if a running daemon is dead.
586 for (int i = 0; i < mDaemons.length; ++i) {
587 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700588 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700589 SystemProperties.get("init.svc." + daemon))) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700590 throw new IllegalStateException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700591 }
592 }
593 checkpoint(true);
594 }
595
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700596 // Now we are connected. Read and parse the new state.
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -0800597 String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700598 if (parameters.length != 6) {
599 throw new IllegalStateException("Cannot parse the state");
600 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700601
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700602 // Set the interface and the addresses in the config.
603 mConfig.interfaze = parameters[0].trim();
604 mConfig.addresses = parameters[1].trim();
605
606 // Set the routes if they are not set in the config.
607 if (mConfig.routes == null || mConfig.routes.isEmpty()) {
608 mConfig.routes = parameters[2].trim();
609 }
610
611 // Set the DNS servers if they are not set in the config.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700612 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700613 String dnsServers = parameters[3].trim();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700614 if (!dnsServers.isEmpty()) {
615 mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
616 }
617 }
618
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700619 // Set the search domains if they are not set in the config.
620 if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) {
621 String searchDomains = parameters[4].trim();
622 if (!searchDomains.isEmpty()) {
623 mConfig.searchDomains = Arrays.asList(searchDomains.split(" "));
624 }
625 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700626
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700627 // Set the routes.
628 jniSetRoutes(mConfig.interfaze, mConfig.routes);
629
630 // Here is the last step and it must be done synchronously.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700631 synchronized (Vpn.this) {
632 // Check if the thread is interrupted while we are waiting.
633 checkpoint(false);
634
Chia-chi Yehe9107902011-07-02 01:48:50 -0700635 // Check if the interface is gone while we are waiting.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700636 if (jniCheck(mConfig.interfaze) == 0) {
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700637 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700638 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700639
640 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700641 mInterface = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700642 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
643 showNotification(mConfig, null, null);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700644
645 Log.i(TAG, "Connected!");
646 mInfo.state = LegacyVpnInfo.STATE_CONNECTED;
647 mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700648 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700649 } catch (Exception e) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700650 Log.i(TAG, "Aborting", e);
Chia-chi Yehe9107902011-07-02 01:48:50 -0700651 exit();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700652 } finally {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700653 // Kill the daemons if they fail to stop.
654 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING) {
655 for (String daemon : mDaemons) {
656 SystemProperties.set("ctl.stop", daemon);
657 }
658 }
659
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700660 // Do not leave an unstable state.
661 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING ||
662 mInfo.state == LegacyVpnInfo.STATE_CONNECTING) {
663 mInfo.state = LegacyVpnInfo.STATE_FAILED;
664 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700665 }
666 }
667 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700668}