blob: 1232846674e561a5ae0cd9e02d7f9399e89b57f7 [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.graphics.Bitmap;
29import android.graphics.Canvas;
30import android.graphics.drawable.Drawable;
31import android.net.INetworkManagementEventObserver;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070032import android.net.LocalSocket;
33import android.net.LocalSocketAddress;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070034import android.os.Binder;
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -080035import android.os.FileUtils;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070036import android.os.IBinder;
37import android.os.Parcel;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070038import android.os.ParcelFileDescriptor;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070039import android.os.Process;
40import android.os.SystemClock;
Jeff Sharkey088f29f2012-08-05 14:55:04 -070041import android.os.SystemService;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070042import android.util.Log;
43
44import com.android.internal.R;
Chia-chi Yeh2e467642011-07-04 03:23:12 -070045import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070046import com.android.internal.net.VpnConfig;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070047import com.android.server.ConnectivityService.VpnCallback;
48
Chia-chi Yeh97a61562011-07-14 15:05:05 -070049import java.io.File;
Chia-chi Yeh97a61562011-07-14 15:05:05 -070050import java.io.InputStream;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070051import java.io.OutputStream;
52import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070053import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070054
Jeff Sharkey065b2992012-08-05 14:16:48 -070055import libcore.io.IoUtils;
56
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070057/**
58 * @hide
59 */
60public class Vpn extends INetworkManagementEventObserver.Stub {
61
62 private final static String TAG = "Vpn";
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070063
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070064 private final static String BIND_VPN_SERVICE =
65 android.Manifest.permission.BIND_VPN_SERVICE;
66
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070067 private final Context mContext;
68 private final VpnCallback mCallback;
69
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070070 private String mPackage = VpnConfig.LEGACY_VPN;
71 private String mInterface;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070072 private Connection mConnection;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070073 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070074
75 public Vpn(Context context, VpnCallback callback) {
76 mContext = context;
77 mCallback = callback;
78 }
79
80 /**
Chia-chi Yeh100155a2011-07-03 16:52:38 -070081 * Prepare for a VPN application. This method is designed to solve
82 * race conditions. It first compares the current prepared package
83 * with {@code oldPackage}. If they are the same, the prepared
84 * package is revoked and replaced with {@code newPackage}. If
85 * {@code oldPackage} is {@code null}, the comparison is omitted.
86 * If {@code newPackage} is the same package or {@code null}, the
87 * revocation is omitted. This method returns {@code true} if the
88 * operation is succeeded.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070089 *
Chia-chi Yeh100155a2011-07-03 16:52:38 -070090 * Legacy VPN is handled specially since it is not a real package.
91 * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and
92 * it can be revoked by itself.
93 *
94 * @param oldPackage The package name of the old VPN application.
95 * @param newPackage The package name of the new VPN application.
96 * @return true if the operation is succeeded.
Chia-chi Yehe9107902011-07-02 01:48:50 -070097 */
Chia-chi Yeh100155a2011-07-03 16:52:38 -070098 public synchronized boolean prepare(String oldPackage, String newPackage) {
99 // Return false if the package does not match.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700100 if (oldPackage != null && !oldPackage.equals(mPackage)) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700101 return false;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700102 }
103
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700104 // Return true if we do not need to revoke.
105 if (newPackage == null ||
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700106 (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700107 return true;
108 }
109
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700110 // Check if the caller is authorized.
111 enforceControlPermission();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700112
Chia-chi Yehe9107902011-07-02 01:48:50 -0700113 // Reset the interface and hide the notification.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700114 if (mInterface != null) {
115 jniReset(mInterface);
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700116 long identity = Binder.clearCallingIdentity();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700117 mCallback.restore();
118 hideNotification();
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700119 Binder.restoreCallingIdentity(identity);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700120 mInterface = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700121 }
122
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700123 // Revoke the connection or stop LegacyVpnRunner.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700124 if (mConnection != null) {
125 try {
126 mConnection.mService.transact(IBinder.LAST_CALL_TRANSACTION,
127 Parcel.obtain(), null, IBinder.FLAG_ONEWAY);
128 } catch (Exception e) {
129 // ignore
130 }
131 mContext.unbindService(mConnection);
132 mConnection = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700133 } else if (mLegacyVpnRunner != null) {
134 mLegacyVpnRunner.exit();
135 mLegacyVpnRunner = null;
136 }
137
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700138 Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
139 mPackage = newPackage;
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700140 return true;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700141 }
142
143 /**
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700144 * Protect a socket from routing changes by binding it to the given
145 * interface. The socket is NOT closed by this method.
146 *
147 * @param socket The socket to be bound.
148 * @param name The name of the interface.
149 */
150 public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception {
151 PackageManager pm = mContext.getPackageManager();
152 ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
153 if (Binder.getCallingUid() != app.uid) {
154 throw new SecurityException("Unauthorized Caller");
155 }
156 jniProtect(socket.getFd(), interfaze);
157 }
158
159 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700160 * Establish a VPN network and return the file descriptor of the VPN
161 * interface. This methods returns {@code null} if the application is
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700162 * revoked or not prepared.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700163 *
164 * @param config The parameters to configure the network.
165 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700166 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700167 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700168 // Check if the caller is already prepared.
169 PackageManager pm = mContext.getPackageManager();
170 ApplicationInfo app = null;
171 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700172 app = pm.getApplicationInfo(mPackage, 0);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700173 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700174 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700175 }
176 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700177 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700178 }
179
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700180 // Check if the service is properly declared.
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700181 Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
182 intent.setClassName(mPackage, config.user);
183 ResolveInfo info = pm.resolveService(intent, 0);
184 if (info == null) {
185 throw new SecurityException("Cannot find " + config.user);
186 }
187 if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
188 throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
189 }
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700190
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700191 // Load the label.
192 String label = app.loadLabel(pm).toString();
193
194 // Load the icon and convert it into a bitmap.
195 Drawable icon = app.loadIcon(pm);
196 Bitmap bitmap = null;
197 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
198 int width = mContext.getResources().getDimensionPixelSize(
199 android.R.dimen.notification_large_icon_width);
200 int height = mContext.getResources().getDimensionPixelSize(
201 android.R.dimen.notification_large_icon_height);
202 icon.setBounds(0, 0, width, height);
203 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -0700204 Canvas c = new Canvas(bitmap);
205 icon.draw(c);
206 c.setBitmap(null);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700207 }
208
Chia-chi Yehe9107902011-07-02 01:48:50 -0700209 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700210 ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700211 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700212 String interfaze = jniGetName(tun.getFd());
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700213 if (jniSetAddresses(interfaze, config.addresses) < 1) {
214 throw new IllegalArgumentException("At least one address must be specified");
215 }
216 if (config.routes != null) {
217 jniSetRoutes(interfaze, config.routes);
218 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700219 Connection connection = new Connection();
220 if (!mContext.bindService(intent, connection, Context.BIND_AUTO_CREATE)) {
221 throw new IllegalStateException("Cannot bind " + config.user);
222 }
223 if (mConnection != null) {
224 mContext.unbindService(mConnection);
225 }
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700226 if (mInterface != null && !mInterface.equals(interfaze)) {
227 jniReset(mInterface);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700228 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700229 mConnection = connection;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700230 mInterface = interfaze;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700231 } catch (RuntimeException e) {
Jeff Sharkey065b2992012-08-05 14:16:48 -0700232 IoUtils.closeQuietly(tun);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700233 throw e;
234 }
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700235 Log.i(TAG, "Established by " + config.user + " on " + mInterface);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700236
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700237 // Fill more values.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700238 config.user = mPackage;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700239 config.interfaze = mInterface;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700240
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700241 // Override DNS servers and show the notification.
242 long identity = Binder.clearCallingIdentity();
243 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700244 showNotification(config, label, bitmap);
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700245 Binder.restoreCallingIdentity(identity);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700246 return tun;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700247 }
248
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700249 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700250 @Override
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700251 public void interfaceAdded(String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700252 }
253
254 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700255 @Override
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700256 public synchronized void interfaceStatusChanged(String interfaze, boolean up) {
257 if (!up && mLegacyVpnRunner != null) {
258 mLegacyVpnRunner.check(interfaze);
259 }
260 }
261
262 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700263 @Override
264 public void interfaceLinkStateChanged(String interfaze, boolean up) {
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700265 }
266
267 // INetworkManagementEventObserver.Stub
268 @Override
269 public synchronized void interfaceRemoved(String interfaze) {
270 if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) {
271 long identity = Binder.clearCallingIdentity();
272 mCallback.restore();
273 hideNotification();
274 Binder.restoreCallingIdentity(identity);
275 mInterface = null;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700276 if (mConnection != null) {
277 mContext.unbindService(mConnection);
278 mConnection = null;
Chia-chi Yeh0c074e62011-08-15 15:19:40 -0700279 } else if (mLegacyVpnRunner != null) {
280 mLegacyVpnRunner.exit();
281 mLegacyVpnRunner = null;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700282 }
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700283 }
284 }
285
286 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700287 @Override
288 public void limitReached(String limit, String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700289 }
290
Haoyu Baidb3c8672012-06-20 14:29:57 -0700291 public void interfaceClassDataActivityChanged(String label, boolean active) {
292 }
293
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700294 private void enforceControlPermission() {
295 // System user is allowed to control VPN.
296 if (Binder.getCallingUid() == Process.SYSTEM_UID) {
297 return;
298 }
299
300 try {
301 // System dialogs are also allowed to control VPN.
302 PackageManager pm = mContext.getPackageManager();
303 ApplicationInfo app = pm.getApplicationInfo(VpnConfig.DIALOGS_PACKAGE, 0);
304 if (Binder.getCallingUid() == app.uid) {
305 return;
306 }
307 } catch (Exception e) {
308 // ignore
309 }
310
311 throw new SecurityException("Unauthorized Caller");
312 }
313
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700314 private class Connection implements ServiceConnection {
315 private IBinder mService;
316
317 @Override
318 public void onServiceConnected(ComponentName name, IBinder service) {
319 mService = service;
320 }
321
322 @Override
323 public void onServiceDisconnected(ComponentName name) {
324 mService = null;
325 }
326 }
327
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700328 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700329 NotificationManager nm = (NotificationManager)
330 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
331
332 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700333 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
334 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700335 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
336 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700337 config.startTime = SystemClock.elapsedRealtime();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700338
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700339 Notification notification = new Notification.Builder(mContext)
340 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700341 .setLargeIcon(icon)
342 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700343 .setContentText(text)
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700344 .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config))
Chia-chi Yeh50fe7092012-01-11 14:26:24 -0800345 .setDefaults(0)
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700346 .setOngoing(true)
347 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700348 nm.notify(R.drawable.vpn_connected, notification);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700349 }
350 }
351
352 private void hideNotification() {
353 NotificationManager nm = (NotificationManager)
354 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
355
356 if (nm != null) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700357 nm.cancel(R.drawable.vpn_connected);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700358 }
359 }
360
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700361 private native int jniCreate(int mtu);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700362 private native String jniGetName(int tun);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700363 private native int jniSetAddresses(String interfaze, String addresses);
364 private native int jniSetRoutes(String interfaze, String routes);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700365 private native void jniReset(String interfaze);
366 private native int jniCheck(String interfaze);
367 private native void jniProtect(int socket, String interfaze);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700368
369 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700370 * Start legacy VPN. This method stops the daemons and restart them
371 * if arguments are not null. Heavy things are offloaded to another
Chia-chi Yehe9107902011-07-02 01:48:50 -0700372 * thread, so callers will not be blocked for a long time.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700373 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700374 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700375 * @param raoocn The arguments to be passed to racoon.
376 * @param mtpd The arguments to be passed to mtpd.
377 */
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700378 public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700379 // Prepare for the new request. This also checks the caller.
380 prepare(null, VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700381
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700382 // Start a new LegacyVpnRunner and we are done!
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700383 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
384 mLegacyVpnRunner.start();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700385 }
386
387 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700388 * Return the information of the current ongoing legacy VPN.
389 */
390 public synchronized LegacyVpnInfo getLegacyVpnInfo() {
Chia-chi Yehdadc8572012-06-08 13:05:58 -0700391 // Check if the caller is authorized.
392 enforceControlPermission();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700393 return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
394 }
395
396 /**
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700397 * Bringing up a VPN connection takes time, and that is all this thread
398 * does. Here we have plenty of time. The only thing we need to take
399 * care of is responding to interruptions as soon as possible. Otherwise
400 * requests will be piled up. This can be done in a Handler as a state
401 * machine, but it is much easier to read in the current form.
402 */
403 private class LegacyVpnRunner extends Thread {
404 private static final String TAG = "LegacyVpnRunner";
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700405
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700406 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700407 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700408 private final String[][] mArguments;
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700409 private final LocalSocket[] mSockets;
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700410 private final String mOuterInterface;
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700411 private final LegacyVpnInfo mInfo;
412
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700413 private long mTimer = -1;
414
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700415 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700416 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700417 mConfig = config;
418 mDaemons = new String[] {"racoon", "mtpd"};
419 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700420 mSockets = new LocalSocket[mDaemons.length];
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700421 mInfo = new LegacyVpnInfo();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700422
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700423 // This is the interface which VPN is running on.
424 mOuterInterface = mConfig.interfaze;
425
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700426 // Legacy VPN is not a real package, so we use it to carry the key.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700427 mInfo.key = mConfig.user;
428 mConfig.user = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700429 }
430
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700431 public void check(String interfaze) {
432 if (interfaze.equals(mOuterInterface)) {
433 Log.i(TAG, "Legacy VPN is going down with " + interfaze);
434 exit();
435 }
436 }
437
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700438 public void exit() {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700439 // We assume that everything is reset after stopping the daemons.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700440 interrupt();
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700441 for (LocalSocket socket : mSockets) {
Jeff Sharkey065b2992012-08-05 14:16:48 -0700442 IoUtils.closeQuietly(socket);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700443 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700444 }
445
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700446 public LegacyVpnInfo getInfo() {
447 // Update the info when VPN is disconnected.
448 if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) {
449 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
450 mInfo.intent = null;
451 }
452 return mInfo;
453 }
454
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700455 @Override
456 public void run() {
457 // Wait for the previous thread since it has been interrupted.
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700458 Log.v(TAG, "Waiting");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700459 synchronized (TAG) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700460 Log.v(TAG, "Executing");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700461 execute();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700462 }
463 }
464
465 private void checkpoint(boolean yield) throws InterruptedException {
466 long now = SystemClock.elapsedRealtime();
467 if (mTimer == -1) {
468 mTimer = now;
469 Thread.sleep(1);
Chia-chi Yeh7ef86112011-07-22 15:46:52 -0700470 } else if (now - mTimer <= 60000) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700471 Thread.sleep(yield ? 200 : 1);
472 } else {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700473 mInfo.state = LegacyVpnInfo.STATE_TIMEOUT;
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700474 throw new IllegalStateException("Time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700475 }
476 }
477
478 private void execute() {
479 // Catch all exceptions so we can clean up few things.
480 try {
481 // Initialize the timer.
482 checkpoint(false);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700483 mInfo.state = LegacyVpnInfo.STATE_INITIALIZING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700484
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700485 // Wait for the daemons to stop.
486 for (String daemon : mDaemons) {
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700487 while (!SystemService.isStopped(daemon)) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700488 checkpoint(true);
489 }
490 }
491
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700492 // Clear the previous state.
493 File state = new File("/data/misc/vpn/state");
494 state.delete();
495 if (state.exists()) {
496 throw new IllegalStateException("Cannot delete the state");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700497 }
Chia-chi Yehc1872732011-12-08 16:51:41 -0800498 new File("/data/misc/vpn/abort").delete();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700499
Chia-chi Yehe9107902011-07-02 01:48:50 -0700500 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700501 boolean restart = false;
502 for (String[] arguments : mArguments) {
503 restart = restart || (arguments != null);
504 }
505 if (!restart) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700506 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700507 return;
508 }
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700509 mInfo.state = LegacyVpnInfo.STATE_CONNECTING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700510
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700511 // Start the daemon with arguments.
512 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700513 String[] arguments = mArguments[i];
514 if (arguments == null) {
515 continue;
516 }
517
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700518 // Start the daemon.
519 String daemon = mDaemons[i];
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700520 SystemService.start(daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700521
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700522 // Wait for the daemon to start.
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700523 while (!SystemService.isRunning(daemon)) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700524 checkpoint(true);
525 }
526
527 // Create the control socket.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700528 mSockets[i] = new LocalSocket();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700529 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700530 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700531
532 // Wait for the socket to connect.
533 while (true) {
534 try {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700535 mSockets[i].connect(address);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700536 break;
537 } catch (Exception e) {
538 // ignore
539 }
540 checkpoint(true);
541 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700542 mSockets[i].setSoTimeout(500);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700543
544 // Send over the arguments.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700545 OutputStream out = mSockets[i].getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700546 for (String argument : arguments) {
547 byte[] bytes = argument.getBytes(Charsets.UTF_8);
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700548 if (bytes.length >= 0xFFFF) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700549 throw new IllegalArgumentException("Argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700550 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700551 out.write(bytes.length >> 8);
552 out.write(bytes.length);
553 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700554 checkpoint(false);
555 }
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700556 out.write(0xFF);
557 out.write(0xFF);
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700558 out.flush();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700559
560 // Wait for End-of-File.
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700561 InputStream in = mSockets[i].getInputStream();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700562 while (true) {
563 try {
564 if (in.read() == -1) {
565 break;
566 }
567 } catch (Exception e) {
568 // ignore
569 }
570 checkpoint(true);
571 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700572 }
573
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700574 // Wait for the daemons to create the new state.
575 while (!state.exists()) {
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700576 // Check if a running daemon is dead.
577 for (int i = 0; i < mDaemons.length; ++i) {
578 String daemon = mDaemons[i];
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700579 if (mArguments[i] != null && !SystemService.isRunning(daemon)) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700580 throw new IllegalStateException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700581 }
582 }
583 checkpoint(true);
584 }
585
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700586 // Now we are connected. Read and parse the new state.
Chia-chi Yehc1bac3a2011-12-16 15:00:31 -0800587 String[] parameters = FileUtils.readTextFile(state, 0, null).split("\n", -1);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700588 if (parameters.length != 6) {
589 throw new IllegalStateException("Cannot parse the state");
590 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700591
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700592 // Set the interface and the addresses in the config.
593 mConfig.interfaze = parameters[0].trim();
594 mConfig.addresses = parameters[1].trim();
595
596 // Set the routes if they are not set in the config.
597 if (mConfig.routes == null || mConfig.routes.isEmpty()) {
598 mConfig.routes = parameters[2].trim();
599 }
600
601 // Set the DNS servers if they are not set in the config.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700602 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700603 String dnsServers = parameters[3].trim();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700604 if (!dnsServers.isEmpty()) {
605 mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
606 }
607 }
608
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700609 // Set the search domains if they are not set in the config.
610 if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) {
611 String searchDomains = parameters[4].trim();
612 if (!searchDomains.isEmpty()) {
613 mConfig.searchDomains = Arrays.asList(searchDomains.split(" "));
614 }
615 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700616
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700617 // Set the routes.
618 jniSetRoutes(mConfig.interfaze, mConfig.routes);
619
620 // Here is the last step and it must be done synchronously.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700621 synchronized (Vpn.this) {
622 // Check if the thread is interrupted while we are waiting.
623 checkpoint(false);
624
Chia-chi Yehe9107902011-07-02 01:48:50 -0700625 // Check if the interface is gone while we are waiting.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700626 if (jniCheck(mConfig.interfaze) == 0) {
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700627 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700628 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700629
630 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700631 mInterface = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700632 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
633 showNotification(mConfig, null, null);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700634
635 Log.i(TAG, "Connected!");
636 mInfo.state = LegacyVpnInfo.STATE_CONNECTED;
637 mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700638 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700639 } catch (Exception e) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700640 Log.i(TAG, "Aborting", e);
Chia-chi Yehe9107902011-07-02 01:48:50 -0700641 exit();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700642 } finally {
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700643 // Kill the daemons if they fail to stop.
644 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING) {
645 for (String daemon : mDaemons) {
Jeff Sharkey088f29f2012-08-05 14:55:04 -0700646 SystemService.stop(daemon);
Chia-chi Yeh5317f032011-08-22 13:09:49 -0700647 }
648 }
649
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700650 // Do not leave an unstable state.
651 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING ||
652 mInfo.state == LegacyVpnInfo.STATE_CONNECTING) {
653 mInfo.state = LegacyVpnInfo.STATE_FAILED;
654 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700655 }
656 }
657 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700658}