blob: 760a5b69e93414aeba399e474f5128fe01840b3b [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 Yeh04ba25c2011-06-15 17:07:27 -070040import com.android.internal.net.VpnConfig;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070041import com.android.server.ConnectivityService.VpnCallback;
42
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070043import java.io.OutputStream;
44import java.nio.charset.Charsets;
Chia-chi Yeh41d16852011-07-01 02:12:06 -070045import java.util.Arrays;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070046
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070047/**
48 * @hide
49 */
50public class Vpn extends INetworkManagementEventObserver.Stub {
51
52 private final static String TAG = "Vpn";
53 private final static String VPN = android.Manifest.permission.VPN;
54
55 private final Context mContext;
56 private final VpnCallback mCallback;
57
Chia-chi Yehe9107902011-07-02 01:48:50 -070058 private String mPackageName = VpnConfig.LEGACY_VPN;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070059 private String mInterfaceName;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070060 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070061
62 public Vpn(Context context, VpnCallback callback) {
63 mContext = context;
64 mCallback = callback;
65 }
66
67 /**
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070068 * Protect a socket from routing changes by binding it to the given
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -070069 * interface. The socket IS closed by this method.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070070 *
71 * @param socket The socket to be bound.
72 * @param name The name of the interface.
73 */
74 public void protect(ParcelFileDescriptor socket, String name) {
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -070075 try {
76 mContext.enforceCallingPermission(VPN, "protect");
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -070077 jniProtectSocket(socket.getFd(), name);
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -070078 } finally {
79 try {
80 socket.close();
81 } catch (Exception e) {
82 // ignore
83 }
84 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070085 }
86
87 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -070088 * Prepare for a VPN application. If the new application is valid,
89 * the previous prepared application is revoked. Since legacy VPN
90 * is not a real application, it uses {@link VpnConfig#LEGACY_VPN}
91 * as its package name. Note that this method does not check if
92 * the applications are the same.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070093 *
Chia-chi Yehe9107902011-07-02 01:48:50 -070094 * @param packageName The package name of the VPN application.
95 * @return The package name of the current prepared application.
96 */
97 public synchronized String prepare(String packageName) {
98 // Return the current prepared application if the new one is null.
99 if (packageName == null) {
100 return mPackageName;
101 }
102
103 // Only system user can call this method.
104 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
105 throw new SecurityException("Unauthorized Caller");
106 }
107
108 // Check the permission of the given package.
109 PackageManager pm = mContext.getPackageManager();
110 if (!packageName.equals(VpnConfig.LEGACY_VPN) &&
111 pm.checkPermission(VPN, packageName) != PackageManager.PERMISSION_GRANTED) {
112 throw new SecurityException(packageName + " does not have " + VPN);
113 }
114
115 // Reset the interface and hide the notification.
116 if (mInterfaceName != null) {
117 jniResetInterface(mInterfaceName);
118 mCallback.restore();
119 hideNotification();
120 mInterfaceName = null;
121 }
122
123 // Send out the broadcast or stop LegacyVpnRunner.
124 if (!mPackageName.equals(VpnConfig.LEGACY_VPN)) {
125 Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED);
126 intent.setPackage(mPackageName);
127 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
128 mContext.sendBroadcast(intent);
129 } else if (mLegacyVpnRunner != null) {
130 mLegacyVpnRunner.exit();
131 mLegacyVpnRunner = null;
132 }
133
134 Log.i(TAG, "Switched from " + mPackageName + " to " + packageName);
135 mPackageName = packageName;
136 return mPackageName;
137 }
138
139 /**
140 * Establish a VPN network and return the file descriptor of the VPN
141 * interface. This methods returns {@code null} if the application is
142 * not prepared or revoked.
143 *
144 * @param config The parameters to configure the network.
145 * @return The file descriptor of the VPN interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700146 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700147 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700148 // Check the permission of the caller.
149 mContext.enforceCallingPermission(VPN, "establish");
150
151 // Check if the caller is already prepared.
152 PackageManager pm = mContext.getPackageManager();
153 ApplicationInfo app = null;
154 try {
155 app = pm.getApplicationInfo(mPackageName, 0);
156 } 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 Yeha4b87b52011-06-30 23:21:55 -0700163 // Load the label.
164 String label = app.loadLabel(pm).toString();
165
166 // Load the icon and convert it into a bitmap.
167 Drawable icon = app.loadIcon(pm);
168 Bitmap bitmap = null;
169 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
170 int width = mContext.getResources().getDimensionPixelSize(
171 android.R.dimen.notification_large_icon_width);
172 int height = mContext.getResources().getDimensionPixelSize(
173 android.R.dimen.notification_large_icon_height);
174 icon.setBounds(0, 0, width, height);
175 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
176 icon.draw(new Canvas(bitmap));
177 }
178
Chia-chi Yehe9107902011-07-02 01:48:50 -0700179 // Configure the interface. Abort if any of these steps fails.
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700180 ParcelFileDescriptor descriptor =
181 ParcelFileDescriptor.adoptFd(jniCreateInterface(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700182 try {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700183 String name = jniGetInterfaceName(descriptor.getFd());
184 if (jniSetAddresses(name, config.addresses) < 1) {
185 throw new IllegalArgumentException("At least one address must be specified");
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700186 }
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700187 if (config.routes != null) {
188 jniSetRoutes(name, config.routes);
189 }
190 if (mInterfaceName != null && !mInterfaceName.equals(name)) {
191 jniResetInterface(mInterfaceName);
192 }
193 mInterfaceName = name;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700194 } catch (RuntimeException e) {
195 try {
196 descriptor.close();
197 } catch (Exception ex) {
198 // ignore
199 }
200 throw e;
201 }
202
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700203 // Override DNS servers and search domains.
204 mCallback.override(config.dnsServers, config.searchDomains);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700205
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700206 // Fill more values.
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700207 config.packageName = mPackageName;
208 config.interfaceName = mInterfaceName;
Chia-chi Yeh8909b102011-07-01 01:09:42 -0700209
210 // Show the notification!
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700211 showNotification(config, label, bitmap);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700212 return descriptor;
213 }
214
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700215 // INetworkManagementEventObserver.Stub
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700216 public void interfaceStatusChanged(String name, boolean up) {
217 }
218
219 // INetworkManagementEventObserver.Stub
220 public void interfaceLinkStateChanged(String name, boolean up) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700221 }
222
223 // INetworkManagementEventObserver.Stub
224 public void interfaceAdded(String name) {
225 }
226
227 // INetworkManagementEventObserver.Stub
228 public synchronized void interfaceRemoved(String name) {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700229 if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700230 mCallback.restore();
Chia-chi Yehe9107902011-07-02 01:48:50 -0700231 hideNotification();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700232 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700233 }
234 }
235
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700236 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700237 NotificationManager nm = (NotificationManager)
238 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
239
240 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700241 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
242 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700243 String text = (config.sessionName == null) ? mContext.getString(R.string.vpn_text) :
244 mContext.getString(R.string.vpn_text_long, config.sessionName);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700245
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700246 long identity = Binder.clearCallingIdentity();
247 Notification notification = new Notification.Builder(mContext)
248 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700249 .setLargeIcon(icon)
250 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700251 .setContentText(text)
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700252 .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700253 .setDefaults(Notification.DEFAULT_ALL)
254 .setOngoing(true)
255 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700256 nm.notify(R.drawable.vpn_connected, notification);
257 Binder.restoreCallingIdentity(identity);
258 }
259 }
260
261 private void hideNotification() {
262 NotificationManager nm = (NotificationManager)
263 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
264
265 if (nm != null) {
266 long identity = Binder.clearCallingIdentity();
267 nm.cancel(R.drawable.vpn_connected);
268 Binder.restoreCallingIdentity(identity);
269 }
270 }
271
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700272 private native int jniCreateInterface(int mtu);
273 private native String jniGetInterfaceName(int fd);
274 private native int jniSetAddresses(String name, String addresses);
275 private native int jniSetRoutes(String name, String routes);
276 private native void jniResetInterface(String name);
277 private native int jniCheckInterface(String name);
278 private native void jniProtectSocket(int fd, String name);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700279
280 /**
Chia-chi Yehe9107902011-07-02 01:48:50 -0700281 * Handle a legacy VPN request. This method stops the daemons and restart
282 * them if arguments are not null. Heavy things are offloaded to another
283 * thread, so callers will not be blocked for a long time.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700284 *
Chia-chi Yehe9107902011-07-02 01:48:50 -0700285 * @param config The parameters to configure the network.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700286 * @param raoocn The arguments to be passed to racoon.
287 * @param mtpd The arguments to be passed to mtpd.
288 */
Chia-chi Yehe9107902011-07-02 01:48:50 -0700289 public synchronized void doLegacyVpn(VpnConfig config, String[] racoon, String[] mtpd) {
290 // There is nothing to stop if another VPN application is prepared.
291 if (config == null && !mPackageName.equals(VpnConfig.LEGACY_VPN)) {
292 return;
293 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700294
Chia-chi Yehe9107902011-07-02 01:48:50 -0700295 // Reset everything. This also checks the caller.
296 prepare(VpnConfig.LEGACY_VPN);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700297
298 // Start a new runner and we are done!
Chia-chi Yehe9107902011-07-02 01:48:50 -0700299 if (config != null) {
300 mLegacyVpnRunner = new LegacyVpnRunner(config, racoon, mtpd);
301 mLegacyVpnRunner.start();
302 }
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700303 }
304
305 /**
306 * Bringing up a VPN connection takes time, and that is all this thread
307 * does. Here we have plenty of time. The only thing we need to take
308 * care of is responding to interruptions as soon as possible. Otherwise
309 * requests will be piled up. This can be done in a Handler as a state
310 * machine, but it is much easier to read in the current form.
311 */
312 private class LegacyVpnRunner extends Thread {
313 private static final String TAG = "LegacyVpnRunner";
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700314 private static final String NONE = "--";
315
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700316 private final VpnConfig mConfig;
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700317 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700318 private final String[][] mArguments;
319 private long mTimer = -1;
320
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700321 public LegacyVpnRunner(VpnConfig config, String[] racoon, String[] mtpd) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700322 super(TAG);
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700323 mConfig = config;
324 mDaemons = new String[] {"racoon", "mtpd"};
325 mArguments = new String[][] {racoon, mtpd};
Chia-chi Yehe9107902011-07-02 01:48:50 -0700326
327 mConfig.packageName = VpnConfig.LEGACY_VPN;
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700328 }
329
330 public void exit() {
Chia-chi Yehe9107902011-07-02 01:48:50 -0700331 // We assume that everything is reset after the daemons die.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700332 for (String daemon : mDaemons) {
333 SystemProperties.set("ctl.stop", daemon);
334 }
335 interrupt();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700336 }
337
338 @Override
339 public void run() {
340 // Wait for the previous thread since it has been interrupted.
341 Log.v(TAG, "wait");
342 synchronized (TAG) {
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700343 Log.v(TAG, "begin");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700344 execute();
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700345 Log.v(TAG, "end");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700346 }
347 }
348
349 private void checkpoint(boolean yield) throws InterruptedException {
350 long now = SystemClock.elapsedRealtime();
351 if (mTimer == -1) {
352 mTimer = now;
353 Thread.sleep(1);
354 } else if (now - mTimer <= 30000) {
355 Thread.sleep(yield ? 200 : 1);
356 } else {
357 throw new InterruptedException("timeout");
358 }
359 }
360
361 private void execute() {
362 // Catch all exceptions so we can clean up few things.
363 try {
364 // Initialize the timer.
365 checkpoint(false);
366
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700367 // First stop the daemons.
368 for (String daemon : mDaemons) {
369 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700370 }
371
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700372 // Wait for the daemons to stop.
373 for (String daemon : mDaemons) {
374 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700375 while (!"stopped".equals(SystemProperties.get(key))) {
376 checkpoint(true);
377 }
378 }
379
380 // Reset the properties.
381 SystemProperties.set("vpn.dns", NONE);
382 SystemProperties.set("vpn.via", NONE);
383 while (!NONE.equals(SystemProperties.get("vpn.dns")) ||
384 !NONE.equals(SystemProperties.get("vpn.via"))) {
385 checkpoint(true);
386 }
387
Chia-chi Yehe9107902011-07-02 01:48:50 -0700388 // Check if we need to restart any of the daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700389 boolean restart = false;
390 for (String[] arguments : mArguments) {
391 restart = restart || (arguments != null);
392 }
393 if (!restart) {
394 return;
395 }
396
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700397 // Start the daemon with arguments.
398 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700399 String[] arguments = mArguments[i];
400 if (arguments == null) {
401 continue;
402 }
403
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700404 // Start the daemon.
405 String daemon = mDaemons[i];
406 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700407
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700408 // Wait for the daemon to start.
409 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700410 while (!"running".equals(SystemProperties.get(key))) {
411 checkpoint(true);
412 }
413
414 // Create the control socket.
415 LocalSocket socket = new LocalSocket();
416 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700417 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700418
419 // Wait for the socket to connect.
420 while (true) {
421 try {
422 socket.connect(address);
423 break;
424 } catch (Exception e) {
425 // ignore
426 }
427 checkpoint(true);
428 }
429 socket.setSoTimeout(500);
430
431 // Send over the arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700432 OutputStream out = socket.getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700433 for (String argument : arguments) {
434 byte[] bytes = argument.getBytes(Charsets.UTF_8);
435 if (bytes.length >= 0xFFFF) {
436 throw new IllegalArgumentException("argument too large");
437 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700438 out.write(bytes.length >> 8);
439 out.write(bytes.length);
440 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700441 checkpoint(false);
442 }
443
444 // Send End-Of-Arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700445 out.write(0xFF);
446 out.write(0xFF);
447 out.flush();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700448 socket.close();
449 }
450
451 // Now here is the beast from the old days. We check few
452 // properties to figure out the current status. Ideally we
453 // can read things back from the sockets and get rid of the
454 // properties, but we have no time...
455 while (NONE.equals(SystemProperties.get("vpn.dns")) ||
456 NONE.equals(SystemProperties.get("vpn.via"))) {
457
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700458 // Check if a running daemon is dead.
459 for (int i = 0; i < mDaemons.length; ++i) {
460 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700461 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700462 SystemProperties.get("init.svc." + daemon))) {
463 throw new IllegalArgumentException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700464 }
465 }
466 checkpoint(true);
467 }
468
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700469 // Now we are connected. Get the interface.
470 mConfig.interfaceName = SystemProperties.get("vpn.via");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700471
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700472 // Get the DNS servers if they are not set in config.
473 if (mConfig.dnsServers == null || mConfig.dnsServers.size() == 0) {
474 String dnsServers = SystemProperties.get("vpn.dns").trim();
475 if (!dnsServers.isEmpty()) {
476 mConfig.dnsServers = Arrays.asList(dnsServers.split(" "));
477 }
478 }
479
Chia-chi Yehe9107902011-07-02 01:48:50 -0700480 // TODO: support routes and search domains from IPSec Mode-CFG.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700481
Chia-chi Yehe9107902011-07-02 01:48:50 -0700482 // Set the routes as requested.
483 if (mConfig.routes != null) {
484 jniSetRoutes(mConfig.interfaceName, mConfig.routes);
485 }
486
487 // The final step must be synchronized.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700488 synchronized (Vpn.this) {
489 // Check if the thread is interrupted while we are waiting.
490 checkpoint(false);
491
Chia-chi Yehe9107902011-07-02 01:48:50 -0700492 // Check if the interface is gone while we are waiting.
493 if (jniCheckInterface(mConfig.interfaceName) == 0) {
494 throw new IllegalStateException(mConfig.interfaceName + " is gone");
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700495 }
Chia-chi Yehe9107902011-07-02 01:48:50 -0700496
497 // Now INetworkManagementEventObserver is watching our back.
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700498 mInterfaceName = mConfig.interfaceName;
499 mCallback.override(mConfig.dnsServers, mConfig.searchDomains);
500 showNotification(mConfig, null, null);
501 }
502 Log.i(TAG, "Connected!");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700503 } catch (Exception e) {
Chia-chi Yeh41d16852011-07-01 02:12:06 -0700504 Log.i(TAG, "Abort due to " + e.getMessage());
Chia-chi Yehe9107902011-07-02 01:48:50 -0700505 exit();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700506 }
507 }
508 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700509}