blob: b69cc31c1d26c7409f708b242c9bd92581a93340 [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 Yehff3bdca2011-05-23 17:26:46 -070021import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageManager;
25import android.content.res.Resources;
26import android.graphics.Bitmap;
27import android.graphics.Canvas;
28import android.graphics.drawable.Drawable;
29import android.net.INetworkManagementEventObserver;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070030import android.net.LocalSocket;
31import android.net.LocalSocketAddress;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070032import android.os.Binder;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070033import android.os.ParcelFileDescriptor;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070034import android.os.Process;
35import android.os.SystemClock;
36import android.os.SystemProperties;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070037import android.util.Log;
38
39import com.android.internal.R;
Chia-chi Yeh2e467642011-07-04 03:23:12 -070040import com.android.internal.net.LegacyVpnInfo;
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -070041import com.android.internal.net.VpnConfig;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070042import com.android.server.ConnectivityService.VpnCallback;
43
Chia-chi Yeh97a61562011-07-14 15:05:05 -070044import java.io.File;
45import java.io.FileInputStream;
46import java.io.InputStream;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070047import java.io.OutputStream;
48import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070049import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070050
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070051/**
52 * @hide
53 */
54public class Vpn extends INetworkManagementEventObserver.Stub {
55
56 private final static String TAG = "Vpn";
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070057
58 private final Context mContext;
59 private final VpnCallback mCallback;
60
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070061 private String mPackage = VpnConfig.LEGACY_VPN;
62 private String mInterface;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070063 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070064
65 public Vpn(Context context, VpnCallback callback) {
66 mContext = context;
67 mCallback = callback;
68 }
69
70 /**
Chia-chi Yeh100155a2011-07-03 16:52:38 -070071 * Prepare for a VPN application. This method is designed to solve
72 * race conditions. It first compares the current prepared package
73 * with {@code oldPackage}. If they are the same, the prepared
74 * package is revoked and replaced with {@code newPackage}. If
75 * {@code oldPackage} is {@code null}, the comparison is omitted.
76 * If {@code newPackage} is the same package or {@code null}, the
77 * revocation is omitted. This method returns {@code true} if the
78 * operation is succeeded.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070079 *
Chia-chi Yeh100155a2011-07-03 16:52:38 -070080 * Legacy VPN is handled specially since it is not a real package.
81 * It uses {@link VpnConfig#LEGACY_VPN} as its package name, and
82 * it can be revoked by itself.
83 *
84 * @param oldPackage The package name of the old VPN application.
85 * @param newPackage The package name of the new VPN application.
86 * @return true if the operation is succeeded.
Chia-chi Yehe9107902011-07-02 01:48:50 -070087 */
Chia-chi Yeh100155a2011-07-03 16:52:38 -070088 public synchronized boolean prepare(String oldPackage, String newPackage) {
89 // Return false if the package does not match.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070090 if (oldPackage != null && !oldPackage.equals(mPackage)) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -070091 return false;
Chia-chi Yehe9107902011-07-02 01:48:50 -070092 }
93
Chia-chi Yeh100155a2011-07-03 16:52:38 -070094 // Return true if we do not need to revoke.
95 if (newPackage == null ||
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -070096 (newPackage.equals(mPackage) && !newPackage.equals(VpnConfig.LEGACY_VPN))) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -070097 return true;
98 }
99
100 // Only system user can revoke a package.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700101 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
102 throw new SecurityException("Unauthorized Caller");
103 }
104
Chia-chi Yehe9107902011-07-02 01:48:50 -0700105 // Reset the interface and hide the notification.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700106 if (mInterface != null) {
107 jniReset(mInterface);
Chia-chi Yehe9107902011-07-02 01:48:50 -0700108 mCallback.restore();
109 hideNotification();
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700110 mInterface = null;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700111 }
112
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700113 // Revoke the connection or stop LegacyVpnRunner.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700114 if (!mPackage.equals(VpnConfig.LEGACY_VPN)) {
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700115 // TODO
Chia-chi Yehe9107902011-07-02 01:48:50 -0700116 } else if (mLegacyVpnRunner != null) {
117 mLegacyVpnRunner.exit();
118 mLegacyVpnRunner = null;
119 }
120
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700121 Log.i(TAG, "Switched from " + mPackage + " to " + newPackage);
122 mPackage = newPackage;
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700123 return true;
Chia-chi Yehe9107902011-07-02 01:48:50 -0700124 }
125
126 /**
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700127 * Protect a socket from routing changes by binding it to the given
128 * interface. The socket is NOT closed by this method.
129 *
130 * @param socket The socket to be bound.
131 * @param name The name of the interface.
132 */
133 public void protect(ParcelFileDescriptor socket, String interfaze) throws Exception {
134 PackageManager pm = mContext.getPackageManager();
135 ApplicationInfo app = pm.getApplicationInfo(mPackage, 0);
136 if (Binder.getCallingUid() != app.uid) {
137 throw new SecurityException("Unauthorized Caller");
138 }
139 jniProtect(socket.getFd(), interfaze);
140 }
141
142 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700143 * Establish a VPN network and return the file descriptor of the VPN
144 * interface. This methods returns {@code null} if the application is
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700145 * revoked or not prepared.
Chia-chi Yehe9107902011-07-02 01:48:50 -0700146 *
147 * @param config The parameters to configure the network.
148 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700149 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700150 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700151 // Check if the caller is already prepared.
152 PackageManager pm = mContext.getPackageManager();
153 ApplicationInfo app = null;
154 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700155 app = pm.getApplicationInfo(mPackage, 0);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700156 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700157 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700158 }
159 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700160 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700161 }
162
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700163 // Check if the service is properly declared.
164 // TODO
165
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700166 // Load the label.
167 String label = app.loadLabel(pm).toString();
168
169 // Load the icon and convert it into a bitmap.
170 Drawable icon = app.loadIcon(pm);
171 Bitmap bitmap = null;
172 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
173 int width = mContext.getResources().getDimensionPixelSize(
174 android.R.dimen.notification_large_icon_width);
175 int height = mContext.getResources().getDimensionPixelSize(
176 android.R.dimen.notification_large_icon_height);
177 icon.setBounds(0, 0, width, height);
178 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Dianne Hackborn6311d0a2011-08-02 16:37:58 -0700179 Canvas c = new Canvas(bitmap);
180 icon.draw(c);
181 c.setBitmap(null);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700182 }
183
Chia-chi Yehe9107902011-07-02 01:48:50 -0700184 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700185 ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700186 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700187 String interfaze = jniGetName(tun.getFd());
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700188 if (jniSetAddresses(interfaze, config.addresses) < 1) {
189 throw new IllegalArgumentException("At least one address must be specified");
190 }
191 if (config.routes != null) {
192 jniSetRoutes(interfaze, config.routes);
193 }
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700194 // TODO: bind the service
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700195 if (mInterface != null && !mInterface.equals(interfaze)) {
196 jniReset(mInterface);
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700197 }
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700198 mInterface = interfaze;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700199 } catch (RuntimeException e) {
200 try {
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700201 tun.close();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700202 } catch (Exception ex) {
203 // ignore
204 }
205 throw e;
206 }
207
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700208 // Fill more values.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700209 config.user = mPackage;
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700210 config.interfaze = mInterface;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700211
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700212 // Override DNS servers and show the notification.
213 long identity = Binder.clearCallingIdentity();
214 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700215 showNotification(config, label, bitmap);
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700216 Binder.restoreCallingIdentity(identity);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700217 return tun;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700218 }
219
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700220 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700221 @Override
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700222 public void interfaceAdded(String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700223 }
224
225 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700226 @Override
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700227 public synchronized void interfaceStatusChanged(String interfaze, boolean up) {
228 if (!up && mLegacyVpnRunner != null) {
229 mLegacyVpnRunner.check(interfaze);
230 }
231 }
232
233 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700234 @Override
235 public void interfaceLinkStateChanged(String interfaze, boolean up) {
236 interfaceStatusChanged(interfaze, up);
237 }
238
239 // INetworkManagementEventObserver.Stub
240 @Override
241 public synchronized void interfaceRemoved(String interfaze) {
242 if (interfaze.equals(mInterface) && jniCheck(interfaze) == 0) {
243 long identity = Binder.clearCallingIdentity();
244 mCallback.restore();
245 hideNotification();
246 Binder.restoreCallingIdentity(identity);
247 mInterface = null;
248 // TODO: unbind the service
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700249 }
250 }
251
252 // INetworkManagementEventObserver.Stub
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700253 @Override
254 public void limitReached(String limit, String interfaze) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700255 }
256
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700257 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700258 NotificationManager nm = (NotificationManager)
259 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
260
261 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700262 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
263 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700264 String text = (config.session == null) ? mContext.getString(R.string.vpn_text) :
265 mContext.getString(R.string.vpn_text_long, config.session);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700266 config.startTime = SystemClock.elapsedRealtime();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700267
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700268 Notification notification = new Notification.Builder(mContext)
269 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700270 .setLargeIcon(icon)
271 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700272 .setContentText(text)
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700273 .setContentIntent(VpnConfig.getIntentForStatusPanel(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700274 .setDefaults(Notification.DEFAULT_ALL)
275 .setOngoing(true)
276 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700277 nm.notify(R.drawable.vpn_connected, notification);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700278 }
279 }
280
281 private void hideNotification() {
282 NotificationManager nm = (NotificationManager)
283 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
284
285 if (nm != null) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700286 nm.cancel(R.drawable.vpn_connected);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700287 }
288 }
289
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700290 private native int jniCreate(int mtu);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700291 private native String jniGetName(int tun);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700292 private native int jniSetAddresses(String interfaze, String addresses);
293 private native int jniSetRoutes(String interfaze, String routes);
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700294 private native void jniReset(String interfaze);
295 private native int jniCheck(String interfaze);
296 private native void jniProtect(int socket, String interfaze);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700297
298 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700299 * Start legacy VPN. This method stops the daemons and restart them
300 * if arguments are not null. Heavy things are offloaded to another
Chia-chi Yehe9107902011-07-02 01:48:50 -0700301 * thread, so callers will not be blocked for a long time.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700302 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700303 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700304 * @param raoocn The arguments to be passed to racoon.
305 * @param mtpd The arguments to be passed to mtpd.
306 */
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700307 public synchronized void startLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700308 // Prepare for the new request. This also checks the caller.
309 prepare(null, VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700310
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700311 // Start a new LegacyVpnRunner and we are done!
Chia-chi Yeh100155a2011-07-03 16:52:38 -0700312 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
313 mLegacyVpnRunner.start();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700314 }
315
316 /**
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700317 * Return the information of the current ongoing legacy VPN.
318 */
319 public synchronized LegacyVpnInfo getLegacyVpnInfo() {
320 // Only system user can call this method.
321 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
322 throw new SecurityException("Unauthorized Caller");
323 }
324 return (mLegacyVpnRunner == null) ? null : mLegacyVpnRunner.getInfo();
325 }
326
327 /**
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700328 * Bringing up a VPN connection takes time, and that is all this thread
329 * does. Here we have plenty of time. The only thing we need to take
330 * care of is responding to interruptions as soon as possible. Otherwise
331 * requests will be piled up. This can be done in a Handler as a state
332 * machine, but it is much easier to read in the current form.
333 */
334 private class LegacyVpnRunner extends Thread {
335 private static final String TAG = "LegacyVpnRunner";
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700336
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700337 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700338 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700339 private final String[][] mArguments;
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700340 private final String mOuterInterface;
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700341 private final LegacyVpnInfo mInfo;
342
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700343 private long mTimer = -1;
344
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700345 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700346 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700347 mConfig = config;
348 mDaemons = new String[] {"racoon", "mtpd"};
349 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700350 mInfo = new LegacyVpnInfo();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700351
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700352 // This is the interface which VPN is running on.
353 mOuterInterface = mConfig.interfaze;
354
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700355 // Legacy VPN is not a real package, so we use it to carry the key.
Chia-chi Yehfcc1b412011-08-03 15:39:59 -0700356 mInfo.key = mConfig.user;
357 mConfig.user = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700358 }
359
Chia-chi Yehaa1727f2011-07-14 18:55:33 -0700360 public void check(String interfaze) {
361 if (interfaze.equals(mOuterInterface)) {
362 Log.i(TAG, "Legacy VPN is going down with " + interfaze);
363 exit();
364 }
365 }
366
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700367 public void exit() {
Chia-chi Yehe9107902011-07-02 01:48:50 -0700368 // We assume that everything is reset after the daemons die.
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700369 interrupt();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700370 for (String daemon : mDaemons) {
371 SystemProperties.set("ctl.stop", daemon);
372 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700373 }
374
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700375 public LegacyVpnInfo getInfo() {
376 // Update the info when VPN is disconnected.
377 if (mInfo.state == LegacyVpnInfo.STATE_CONNECTED && mInterface == null) {
378 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
379 mInfo.intent = null;
380 }
381 return mInfo;
382 }
383
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700384 @Override
385 public void run() {
386 // Wait for the previous thread since it has been interrupted.
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700387 Log.v(TAG, "Waiting");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700388 synchronized (TAG) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700389 Log.v(TAG, "Executing");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700390 execute();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700391 }
392 }
393
394 private void checkpoint(boolean yield) throws InterruptedException {
395 long now = SystemClock.elapsedRealtime();
396 if (mTimer == -1) {
397 mTimer = now;
398 Thread.sleep(1);
Chia-chi Yeh7ef86112011-07-22 15:46:52 -0700399 } else if (now - mTimer <= 60000) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700400 Thread.sleep(yield ? 200 : 1);
401 } else {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700402 mInfo.state = LegacyVpnInfo.STATE_TIMEOUT;
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700403 throw new IllegalStateException("Time is up");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700404 }
405 }
406
407 private void execute() {
408 // Catch all exceptions so we can clean up few things.
409 try {
410 // Initialize the timer.
411 checkpoint(false);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700412 mInfo.state = LegacyVpnInfo.STATE_INITIALIZING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700413
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700414 // First stop the daemons.
415 for (String daemon : mDaemons) {
416 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700417 }
418
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700419 // Wait for the daemons to stop.
420 for (String daemon : mDaemons) {
421 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700422 while (!"stopped".equals(SystemProperties.get(key))) {
423 checkpoint(true);
424 }
425 }
426
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700427 // Clear the previous state.
428 File state = new File("/data/misc/vpn/state");
429 state.delete();
430 if (state.exists()) {
431 throw new IllegalStateException("Cannot delete the state");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700432 }
433
Chia-chi Yehe9107902011-07-02 01:48:50 -0700434 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700435 boolean restart = false;
436 for (String[] arguments : mArguments) {
437 restart = restart || (arguments != null);
438 }
439 if (!restart) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700440 mInfo.state = LegacyVpnInfo.STATE_DISCONNECTED;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700441 return;
442 }
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700443 mInfo.state = LegacyVpnInfo.STATE_CONNECTING;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700444
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700445 // Start the daemon with arguments.
446 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700447 String[] arguments = mArguments[i];
448 if (arguments == null) {
449 continue;
450 }
451
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700452 // Start the daemon.
453 String daemon = mDaemons[i];
454 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700455
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700456 // Wait for the daemon to start.
457 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700458 while (!"running".equals(SystemProperties.get(key))) {
459 checkpoint(true);
460 }
461
462 // Create the control socket.
463 LocalSocket socket = new LocalSocket();
464 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700465 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700466
467 // Wait for the socket to connect.
468 while (true) {
469 try {
470 socket.connect(address);
471 break;
472 } catch (Exception e) {
473 // ignore
474 }
475 checkpoint(true);
476 }
477 socket.setSoTimeout(500);
478
479 // Send over the arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700480 OutputStream out = socket.getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700481 for (String argument : arguments) {
482 byte[] bytes = argument.getBytes(Charsets.UTF_8);
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700483 if (bytes.length > 0xFFFF) {
484 throw new IllegalArgumentException("Argument is too large");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700485 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700486 out.write(bytes.length >> 8);
487 out.write(bytes.length);
488 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700489 checkpoint(false);
490 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700491 out.flush();
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700492 socket.shutdownOutput();
493
494 // Wait for End-of-File.
495 InputStream in = socket.getInputStream();
496 while (true) {
497 try {
498 if (in.read() == -1) {
499 break;
500 }
501 } catch (Exception e) {
502 // ignore
503 }
504 checkpoint(true);
505 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700506 socket.close();
507 }
508
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700509 // Wait for the daemons to create the new state.
510 while (!state.exists()) {
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700511 // Check if a running daemon is dead.
512 for (int i = 0; i < mDaemons.length; ++i) {
513 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700514 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700515 SystemProperties.get("init.svc." + daemon))) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700516 throw new IllegalStateException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700517 }
518 }
519 checkpoint(true);
520 }
521
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700522 // Now we are connected. Read and parse the new state.
523 byte[] buffer = new byte[(int) state.length()];
524 if (new FileInputStream(state).read(buffer) != buffer.length) {
525 throw new IllegalStateException("Cannot read the state");
526 }
527 String[] parameters = new String(buffer, Charsets.UTF_8).split("\n", -1);
528 if (parameters.length != 6) {
529 throw new IllegalStateException("Cannot parse the state");
530 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700531
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700532 // Set the interface and the addresses in the config.
533 mConfig.interfaze = parameters[0].trim();
534 mConfig.addresses = parameters[1].trim();
535
536 // Set the routes if they are not set in the config.
537 if (mConfig.routes == null || mConfig.routes.isEmpty()) {
538 mConfig.routes = parameters[2].trim();
539 }
540
541 // Set the DNS servers if they are not set in the config.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700542 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700543 String dnsServers = parameters[3].trim();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700544 if (!dnsServers.isEmpty()) {
545 mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
546 }
547 }
548
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700549 // Set the search domains if they are not set in the config.
550 if (mConfig.searchDomains == null || mConfig.searchDomains.size() == 0) {
551 String searchDomains = parameters[4].trim();
552 if (!searchDomains.isEmpty()) {
553 mConfig.searchDomains = Arrays.asList(searchDomains.split(" "));
554 }
555 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700556
Chia-chi Yeh97a61562011-07-14 15:05:05 -0700557 // Set the routes.
558 jniSetRoutes(mConfig.interfaze, mConfig.routes);
559
560 // Here is the last step and it must be done synchronously.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700561 synchronized (Vpn.this) {
562 // Check if the thread is interrupted while we are waiting.
563 checkpoint(false);
564
Chia-chi Yehe9107902011-07-02 01:48:50 -0700565 // Check if the interface is gone while we are waiting.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700566 if (jniCheck(mConfig.interfaze) == 0) {
Chia-chi Yeh34e78132011-07-03 03:07:07 -0700567 throw new IllegalStateException(mConfig.interfaze + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700568 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700569
570 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yehc2b8aa02011-07-03 18:00:47 -0700571 mInterface = mConfig.interfaze;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700572 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
573 showNotification(mConfig, null, null);
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700574
575 Log.i(TAG, "Connected!");
576 mInfo.state = LegacyVpnInfo.STATE_CONNECTED;
577 mInfo.intent = VpnConfig.getIntentForStatusPanel(mContext, null);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700578 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700579 } catch (Exception e) {
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700580 Log.i(TAG, "Aborting", e);
Chia-chi Yehe9107902011-07-02 01:48:50 -0700581 exit();
Chia-chi Yeh2e467642011-07-04 03:23:12 -0700582 } finally {
583 // Do not leave an unstable state.
584 if (mInfo.state == LegacyVpnInfo.STATE_INITIALIZING ||
585 mInfo.state == LegacyVpnInfo.STATE_CONNECTING) {
586 mInfo.state = LegacyVpnInfo.STATE_FAILED;
587 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700588 }
589 }
590 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700591}