blob: 5c0fa37ece44028dec88da7a512d1c2bc36c3d3f [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;
45
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070046/**
47 * @hide
48 */
49public class Vpn extends INetworkManagementEventObserver.Stub {
50
51 private final static String TAG = "Vpn";
52 private final static String VPN = android.Manifest.permission.VPN;
53
54 private final Context mContext;
55 private final VpnCallback mCallback;
56
57 private String mPackageName;
58 private String mInterfaceName;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070059
60 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 /**
68 * Prepare for a VPN application.
69 *
70 * @param packageName The package name of the new VPN application.
71 * @return The name of the current prepared package.
72 */
73 public synchronized String prepare(String packageName) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070074 // Return the current prepared package if the new one is null.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070075 if (packageName == null) {
76 return mPackageName;
77 }
78
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070079 // Check the permission of the caller.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070080 PackageManager pm = mContext.getPackageManager();
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070081 VpnConfig.enforceCallingPackage(pm.getNameForUid(Binder.getCallingUid()));
82
83 // Check the permission of the given package.
84 if (packageName.isEmpty()) {
85 packageName = null;
86 } else if (pm.checkPermission(VPN, packageName) != PackageManager.PERMISSION_GRANTED) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070087 throw new SecurityException(packageName + " does not have " + VPN);
88 }
89
90 // Reset the interface and hide the notification.
91 if (mInterfaceName != null) {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -070092 jniResetInterface(mInterfaceName);
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070093 mCallback.restore();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070094 hideNotification();
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070095 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070096 }
97
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070098 // Notify the package being revoked.
99 if (mPackageName != null) {
100 Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED);
101 intent.setPackage(mPackageName);
102 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
103 mContext.sendBroadcast(intent);
104 }
105
106 Log.i(TAG, "Switched from " + mPackageName + " to " + packageName);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700107 mPackageName = packageName;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700108 return mPackageName;
109 }
110
111 /**
112 * Protect a socket from routing changes by binding it to the given
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -0700113 * interface. The socket IS closed by this method.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700114 *
115 * @param socket The socket to be bound.
116 * @param name The name of the interface.
117 */
118 public void protect(ParcelFileDescriptor socket, String name) {
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -0700119 try {
120 mContext.enforceCallingPermission(VPN, "protect");
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700121 jniProtectSocket(socket.getFd(), name);
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -0700122 } finally {
123 try {
124 socket.close();
125 } catch (Exception e) {
126 // ignore
127 }
128 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700129 }
130
131 /**
132 * Configure a TUN interface and return its file descriptor.
133 *
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700134 * @param config The parameters to configure the interface.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700135 * @return The file descriptor of the interface.
136 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700137 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700138 // Check the permission of the caller.
139 mContext.enforceCallingPermission(VPN, "establish");
140
141 // Check if the caller is already prepared.
142 PackageManager pm = mContext.getPackageManager();
143 ApplicationInfo app = null;
144 try {
145 app = pm.getApplicationInfo(mPackageName, 0);
146 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700147 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700148 }
149 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700150 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700151 }
152
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700153 // Load the label.
154 String label = app.loadLabel(pm).toString();
155
156 // Load the icon and convert it into a bitmap.
157 Drawable icon = app.loadIcon(pm);
158 Bitmap bitmap = null;
159 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
160 int width = mContext.getResources().getDimensionPixelSize(
161 android.R.dimen.notification_large_icon_width);
162 int height = mContext.getResources().getDimensionPixelSize(
163 android.R.dimen.notification_large_icon_height);
164 icon.setBounds(0, 0, width, height);
165 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
166 icon.draw(new Canvas(bitmap));
167 }
168
169 // Create the interface and abort if any of the following steps fails.
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700170 ParcelFileDescriptor descriptor =
171 ParcelFileDescriptor.adoptFd(jniCreateInterface(config.mtu));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700172 try {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700173 String name = jniGetInterfaceName(descriptor.getFd());
174 if (jniSetAddresses(name, config.addresses) < 1) {
175 throw new IllegalArgumentException("At least one address must be specified");
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700176 }
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700177 if (config.routes != null) {
178 jniSetRoutes(name, config.routes);
179 }
180 if (mInterfaceName != null && !mInterfaceName.equals(name)) {
181 jniResetInterface(mInterfaceName);
182 }
183 mInterfaceName = name;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700184 } catch (RuntimeException e) {
185 try {
186 descriptor.close();
187 } catch (Exception ex) {
188 // ignore
189 }
190 throw e;
191 }
192
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700193 String dnsServers = (config.dnsServers == null) ? "" : config.dnsServers.trim();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700194 mCallback.override(dnsServers.isEmpty() ? null : dnsServers.split(" "));
195
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700196 config.packageName = mPackageName;
197 config.interfaceName = mInterfaceName;
Chia-chi Yeh383e0522011-07-01 00:13:25 -0700198 showNotification(config, label, bitmap);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700199 return descriptor;
200 }
201
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700202 // INetworkManagementEventObserver.Stub
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700203 public void interfaceStatusChanged(String name, boolean up) {
204 }
205
206 // INetworkManagementEventObserver.Stub
207 public void interfaceLinkStateChanged(String name, boolean up) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700208 }
209
210 // INetworkManagementEventObserver.Stub
211 public void interfaceAdded(String name) {
212 }
213
214 // INetworkManagementEventObserver.Stub
215 public synchronized void interfaceRemoved(String name) {
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700216 if (name.equals(mInterfaceName) && jniCheckInterface(name) == 0) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700217 hideNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700218 mCallback.restore();
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700219 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700220 }
221 }
222
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700223 private void showNotification(VpnConfig config, String label, Bitmap icon) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700224 NotificationManager nm = (NotificationManager)
225 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
226
227 if (nm != null) {
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700228 String title = (label == null) ? mContext.getString(R.string.vpn_title) :
229 mContext.getString(R.string.vpn_title_long, label);
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700230 String text = (config.sessionName == null) ? mContext.getString(R.string.vpn_text) :
231 mContext.getString(R.string.vpn_text_long, config.sessionName);
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700232
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700233 long identity = Binder.clearCallingIdentity();
234 Notification notification = new Notification.Builder(mContext)
235 .setSmallIcon(R.drawable.vpn_connected)
Chia-chi Yeha4b87b52011-06-30 23:21:55 -0700236 .setLargeIcon(icon)
237 .setContentTitle(title)
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700238 .setContentText(text)
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700239 .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700240 .setDefaults(Notification.DEFAULT_ALL)
241 .setOngoing(true)
242 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700243 nm.notify(R.drawable.vpn_connected, notification);
244 Binder.restoreCallingIdentity(identity);
245 }
246 }
247
248 private void hideNotification() {
249 NotificationManager nm = (NotificationManager)
250 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
251
252 if (nm != null) {
253 long identity = Binder.clearCallingIdentity();
254 nm.cancel(R.drawable.vpn_connected);
255 Binder.restoreCallingIdentity(identity);
256 }
257 }
258
Chia-chi Yehf4e3bf82011-06-30 12:33:17 -0700259 private native int jniCreateInterface(int mtu);
260 private native String jniGetInterfaceName(int fd);
261 private native int jniSetAddresses(String name, String addresses);
262 private native int jniSetRoutes(String name, String routes);
263 private native void jniResetInterface(String name);
264 private native int jniCheckInterface(String name);
265 private native void jniProtectSocket(int fd, String name);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700266
267 /**
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700268 * Handle legacy VPN requests. This method stops the daemons and restart
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700269 * them if their arguments are not null. Heavy things are offloaded to
270 * another thread, so callers will not be blocked too long.
271 *
272 * @param raoocn The arguments to be passed to racoon.
273 * @param mtpd The arguments to be passed to mtpd.
274 */
275 public synchronized void doLegacyVpn(String[] racoon, String[] mtpd) {
276 // Currently only system user is allowed.
277 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
278 throw new SecurityException("Unauthorized Caller");
279 }
280
281 // If the previous runner is still alive, interrupt it.
282 if (mLegacyVpnRunner != null && mLegacyVpnRunner.isAlive()) {
283 mLegacyVpnRunner.interrupt();
284 }
285
286 // Start a new runner and we are done!
287 mLegacyVpnRunner = new LegacyVpnRunner(
288 new String[] {"racoon", "mtpd"}, racoon, mtpd);
289 mLegacyVpnRunner.start();
290 }
291
292 /**
293 * Bringing up a VPN connection takes time, and that is all this thread
294 * does. Here we have plenty of time. The only thing we need to take
295 * care of is responding to interruptions as soon as possible. Otherwise
296 * requests will be piled up. This can be done in a Handler as a state
297 * machine, but it is much easier to read in the current form.
298 */
299 private class LegacyVpnRunner extends Thread {
300 private static final String TAG = "LegacyVpnRunner";
301
302 private static final String NONE = "--";
303
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700304 private final String[] mDaemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700305 private final String[][] mArguments;
306 private long mTimer = -1;
307
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700308 public LegacyVpnRunner(String[] daemons, String[]... arguments) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700309 super(TAG);
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700310 mDaemons = daemons;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700311 mArguments = arguments;
312 }
313
314 @Override
315 public void run() {
316 // Wait for the previous thread since it has been interrupted.
317 Log.v(TAG, "wait");
318 synchronized (TAG) {
319 Log.v(TAG, "run");
320 execute();
321 Log.v(TAG, "exit");
322 }
323 }
324
325 private void checkpoint(boolean yield) throws InterruptedException {
326 long now = SystemClock.elapsedRealtime();
327 if (mTimer == -1) {
328 mTimer = now;
329 Thread.sleep(1);
330 } else if (now - mTimer <= 30000) {
331 Thread.sleep(yield ? 200 : 1);
332 } else {
333 throw new InterruptedException("timeout");
334 }
335 }
336
337 private void execute() {
338 // Catch all exceptions so we can clean up few things.
339 try {
340 // Initialize the timer.
341 checkpoint(false);
342
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700343 // First stop the daemons.
344 for (String daemon : mDaemons) {
345 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700346 }
347
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700348 // Wait for the daemons to stop.
349 for (String daemon : mDaemons) {
350 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700351 while (!"stopped".equals(SystemProperties.get(key))) {
352 checkpoint(true);
353 }
354 }
355
356 // Reset the properties.
357 SystemProperties.set("vpn.dns", NONE);
358 SystemProperties.set("vpn.via", NONE);
359 while (!NONE.equals(SystemProperties.get("vpn.dns")) ||
360 !NONE.equals(SystemProperties.get("vpn.via"))) {
361 checkpoint(true);
362 }
363
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700364 // Check if we need to restart some daemons.
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700365 boolean restart = false;
366 for (String[] arguments : mArguments) {
367 restart = restart || (arguments != null);
368 }
369 if (!restart) {
370 return;
371 }
372
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700373 // Start the daemon with arguments.
374 for (int i = 0; i < mDaemons.length; ++i) {
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700375 String[] arguments = mArguments[i];
376 if (arguments == null) {
377 continue;
378 }
379
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700380 // Start the daemon.
381 String daemon = mDaemons[i];
382 SystemProperties.set("ctl.start", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700383
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700384 // Wait for the daemon to start.
385 String key = "init.svc." + daemon;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700386 while (!"running".equals(SystemProperties.get(key))) {
387 checkpoint(true);
388 }
389
390 // Create the control socket.
391 LocalSocket socket = new LocalSocket();
392 LocalSocketAddress address = new LocalSocketAddress(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700393 daemon, LocalSocketAddress.Namespace.RESERVED);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700394
395 // Wait for the socket to connect.
396 while (true) {
397 try {
398 socket.connect(address);
399 break;
400 } catch (Exception e) {
401 // ignore
402 }
403 checkpoint(true);
404 }
405 socket.setSoTimeout(500);
406
407 // Send over the arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700408 OutputStream out = socket.getOutputStream();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700409 for (String argument : arguments) {
410 byte[] bytes = argument.getBytes(Charsets.UTF_8);
411 if (bytes.length >= 0xFFFF) {
412 throw new IllegalArgumentException("argument too large");
413 }
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700414 out.write(bytes.length >> 8);
415 out.write(bytes.length);
416 out.write(bytes);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700417 checkpoint(false);
418 }
419
420 // Send End-Of-Arguments.
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700421 out.write(0xFF);
422 out.write(0xFF);
423 out.flush();
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700424 socket.close();
425 }
426
427 // Now here is the beast from the old days. We check few
428 // properties to figure out the current status. Ideally we
429 // can read things back from the sockets and get rid of the
430 // properties, but we have no time...
431 while (NONE.equals(SystemProperties.get("vpn.dns")) ||
432 NONE.equals(SystemProperties.get("vpn.via"))) {
433
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700434 // Check if a running daemon is dead.
435 for (int i = 0; i < mDaemons.length; ++i) {
436 String daemon = mDaemons[i];
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700437 if (mArguments[i] != null && !"running".equals(
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700438 SystemProperties.get("init.svc." + daemon))) {
439 throw new IllegalArgumentException(daemon + " is dead");
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700440 }
441 }
442 checkpoint(true);
443 }
444
445 // Great! Now we are connected!
446 Log.i(TAG, "connected!");
447 // TODO:
448
449 } catch (Exception e) {
450 Log.i(TAG, e.getMessage());
Chia-chi Yeh1f7746b2011-07-01 00:29:06 -0700451 for (String daemon : mDaemons) {
452 SystemProperties.set("ctl.stop", daemon);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700453 }
454 }
455 }
456 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700457}