blob: f5efda9bb8ce142f258e57c53ab0d8dad1be99ce [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.InputStream;
44import java.io.OutputStream;
45import java.nio.charset.Charsets;
46
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
58 private String mPackageName;
59 private String mInterfaceName;
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -070060
61 private LegacyVpnRunner mLegacyVpnRunner;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070062
63 public Vpn(Context context, VpnCallback callback) {
64 mContext = context;
65 mCallback = callback;
66 }
67
68 /**
69 * Prepare for a VPN application.
70 *
71 * @param packageName The package name of the new VPN application.
72 * @return The name of the current prepared package.
73 */
74 public synchronized String prepare(String packageName) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070075 // Return the current prepared package if the new one is null.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070076 if (packageName == null) {
77 return mPackageName;
78 }
79
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070080 // Check the permission of the caller.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070081 PackageManager pm = mContext.getPackageManager();
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070082 VpnConfig.enforceCallingPackage(pm.getNameForUid(Binder.getCallingUid()));
83
84 // Check the permission of the given package.
85 if (packageName.isEmpty()) {
86 packageName = null;
87 } else if (pm.checkPermission(VPN, packageName) != PackageManager.PERMISSION_GRANTED) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070088 throw new SecurityException(packageName + " does not have " + VPN);
89 }
90
91 // Reset the interface and hide the notification.
92 if (mInterfaceName != null) {
93 nativeReset(mInterfaceName);
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070094 mCallback.restore();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070095 hideNotification();
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070096 mInterfaceName = null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -070097 }
98
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -070099 // Notify the package being revoked.
100 if (mPackageName != null) {
101 Intent intent = new Intent(VpnConfig.ACTION_VPN_REVOKED);
102 intent.setPackage(mPackageName);
103 intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY);
104 mContext.sendBroadcast(intent);
105 }
106
107 Log.i(TAG, "Switched from " + mPackageName + " to " + packageName);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700108 mPackageName = packageName;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700109 return mPackageName;
110 }
111
112 /**
113 * Protect a socket from routing changes by binding it to the given
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -0700114 * interface. The socket IS closed by this method.
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700115 *
116 * @param socket The socket to be bound.
117 * @param name The name of the interface.
118 */
119 public void protect(ParcelFileDescriptor socket, String name) {
Chia-chi Yeh3f3337a2011-06-17 16:34:32 -0700120 try {
121 mContext.enforceCallingPermission(VPN, "protect");
122 nativeProtect(socket.getFd(), name);
123 } finally {
124 try {
125 socket.close();
126 } catch (Exception e) {
127 // ignore
128 }
129 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700130 }
131
132 /**
133 * Configure a TUN interface and return its file descriptor.
134 *
135 * @param configuration The parameters to configure the interface.
136 * @return The file descriptor of the interface.
137 */
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700138 public synchronized ParcelFileDescriptor establish(VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700139 // Check the permission of the caller.
140 mContext.enforceCallingPermission(VPN, "establish");
141
142 // Check if the caller is already prepared.
143 PackageManager pm = mContext.getPackageManager();
144 ApplicationInfo app = null;
145 try {
146 app = pm.getApplicationInfo(mPackageName, 0);
147 } catch (Exception e) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700148 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700149 }
150 if (Binder.getCallingUid() != app.uid) {
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700151 return null;
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700152 }
153
Chia-chi Yeh36673692011-06-13 14:24:13 -0700154 // Create and configure the interface.
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700155 ParcelFileDescriptor descriptor = ParcelFileDescriptor.adoptFd(
156 nativeEstablish(config.mtu, config.addresses, config.routes));
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700157
158 // Replace the interface and abort if it fails.
159 try {
160 String interfaceName = nativeGetName(descriptor.getFd());
161
162 if (mInterfaceName != null && !mInterfaceName.equals(interfaceName)) {
163 nativeReset(mInterfaceName);
164 }
165 mInterfaceName = interfaceName;
166 } catch (RuntimeException e) {
167 try {
168 descriptor.close();
169 } catch (Exception ex) {
170 // ignore
171 }
172 throw e;
173 }
174
Chia-chi Yeh04ba25c2011-06-15 17:07:27 -0700175 String dnsServers = (config.dnsServers == null) ? "" : config.dnsServers.trim();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700176 mCallback.override(dnsServers.isEmpty() ? null : dnsServers.split(" "));
177
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700178 config.packageName = mPackageName;
179 config.interfaceName = mInterfaceName;
180 showNotification(pm, app, config);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700181 return descriptor;
182 }
183
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700184 // INetworkManagementEventObserver.Stub
Mike J. Chenf59c7d02011-06-23 15:33:15 -0700185 public void interfaceStatusChanged(String name, boolean up) {
186 }
187
188 // INetworkManagementEventObserver.Stub
189 public void interfaceLinkStateChanged(String name, boolean up) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700190 }
191
192 // INetworkManagementEventObserver.Stub
193 public void interfaceAdded(String name) {
194 }
195
196 // INetworkManagementEventObserver.Stub
197 public synchronized void interfaceRemoved(String name) {
198 if (name.equals(mInterfaceName) && nativeCheck(name) == 0) {
199 hideNotification();
200 mInterfaceName = null;
201 mCallback.restore();
202 }
203 }
204
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700205 private void showNotification(PackageManager pm, ApplicationInfo app, VpnConfig config) {
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700206 NotificationManager nm = (NotificationManager)
207 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
208
209 if (nm != null) {
210 // Load the icon and convert it into a bitmap.
211 Drawable icon = app.loadIcon(pm);
212 Bitmap bitmap = null;
213 if (icon.getIntrinsicWidth() > 0 && icon.getIntrinsicHeight() > 0) {
214 int width = mContext.getResources().getDimensionPixelSize(
215 android.R.dimen.notification_large_icon_width);
216 int height = mContext.getResources().getDimensionPixelSize(
217 android.R.dimen.notification_large_icon_height);
218 icon.setBounds(0, 0, width, height);
219 bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
220 icon.draw(new Canvas(bitmap));
221 }
222
223 // Load the label.
224 String label = app.loadLabel(pm).toString();
225
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700226 // Build the notification.
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700227 String text = (config.sessionName == null) ? mContext.getString(R.string.vpn_text) :
228 mContext.getString(R.string.vpn_text_long, config.sessionName);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700229 long identity = Binder.clearCallingIdentity();
230 Notification notification = new Notification.Builder(mContext)
231 .setSmallIcon(R.drawable.vpn_connected)
232 .setLargeIcon(bitmap)
233 .setTicker(mContext.getString(R.string.vpn_ticker, label))
234 .setContentTitle(mContext.getString(R.string.vpn_title, label))
Chia-chi Yehf8905fd2011-06-14 16:35:02 -0700235 .setContentText(text)
Chia-chi Yeh7b0b8342011-06-17 14:34:11 -0700236 .setContentIntent(VpnConfig.getIntentForNotification(mContext, config))
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700237 .setDefaults(Notification.DEFAULT_ALL)
238 .setOngoing(true)
239 .getNotification();
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700240 nm.notify(R.drawable.vpn_connected, notification);
241 Binder.restoreCallingIdentity(identity);
242 }
243 }
244
245 private void hideNotification() {
246 NotificationManager nm = (NotificationManager)
247 mContext.getSystemService(Context.NOTIFICATION_SERVICE);
248
249 if (nm != null) {
250 long identity = Binder.clearCallingIdentity();
251 nm.cancel(R.drawable.vpn_connected);
252 Binder.restoreCallingIdentity(identity);
253 }
254 }
255
Chia-chi Yeh36632272011-06-13 15:05:37 -0700256 private native int nativeEstablish(int mtu, String addresses, String routes);
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700257 private native String nativeGetName(int fd);
258 private native void nativeReset(String name);
259 private native int nativeCheck(String name);
260 private native void nativeProtect(int fd, String name);
Chia-chi Yeh85a7ce02011-06-29 16:05:58 -0700261
262 /**
263 * Handle legacy VPN requests. This method stops the services and restart
264 * them if their arguments are not null. Heavy things are offloaded to
265 * another thread, so callers will not be blocked too long.
266 *
267 * @param raoocn The arguments to be passed to racoon.
268 * @param mtpd The arguments to be passed to mtpd.
269 */
270 public synchronized void doLegacyVpn(String[] racoon, String[] mtpd) {
271 // Currently only system user is allowed.
272 if (Binder.getCallingUid() != Process.SYSTEM_UID) {
273 throw new SecurityException("Unauthorized Caller");
274 }
275
276 // If the previous runner is still alive, interrupt it.
277 if (mLegacyVpnRunner != null && mLegacyVpnRunner.isAlive()) {
278 mLegacyVpnRunner.interrupt();
279 }
280
281 // Start a new runner and we are done!
282 mLegacyVpnRunner = new LegacyVpnRunner(
283 new String[] {"racoon", "mtpd"}, racoon, mtpd);
284 mLegacyVpnRunner.start();
285 }
286
287 /**
288 * Bringing up a VPN connection takes time, and that is all this thread
289 * does. Here we have plenty of time. The only thing we need to take
290 * care of is responding to interruptions as soon as possible. Otherwise
291 * requests will be piled up. This can be done in a Handler as a state
292 * machine, but it is much easier to read in the current form.
293 */
294 private class LegacyVpnRunner extends Thread {
295 private static final String TAG = "LegacyVpnRunner";
296
297 private static final String NONE = "--";
298
299 private final String[] mServices;
300 private final String[][] mArguments;
301 private long mTimer = -1;
302
303 public LegacyVpnRunner(String[] services, String[]... arguments) {
304 super(TAG);
305 mServices = services;
306 mArguments = arguments;
307 }
308
309 @Override
310 public void run() {
311 // Wait for the previous thread since it has been interrupted.
312 Log.v(TAG, "wait");
313 synchronized (TAG) {
314 Log.v(TAG, "run");
315 execute();
316 Log.v(TAG, "exit");
317 }
318 }
319
320 private void checkpoint(boolean yield) throws InterruptedException {
321 long now = SystemClock.elapsedRealtime();
322 if (mTimer == -1) {
323 mTimer = now;
324 Thread.sleep(1);
325 } else if (now - mTimer <= 30000) {
326 Thread.sleep(yield ? 200 : 1);
327 } else {
328 throw new InterruptedException("timeout");
329 }
330 }
331
332 private void execute() {
333 // Catch all exceptions so we can clean up few things.
334 try {
335 // Initialize the timer.
336 checkpoint(false);
337
338 // First stop the services.
339 for (String service : mServices) {
340 SystemProperties.set("ctl.stop", service);
341 }
342
343 // Wait for the services to stop.
344 for (String service : mServices) {
345 String key = "init.svc." + service;
346 while (!"stopped".equals(SystemProperties.get(key))) {
347 checkpoint(true);
348 }
349 }
350
351 // Reset the properties.
352 SystemProperties.set("vpn.dns", NONE);
353 SystemProperties.set("vpn.via", NONE);
354 while (!NONE.equals(SystemProperties.get("vpn.dns")) ||
355 !NONE.equals(SystemProperties.get("vpn.via"))) {
356 checkpoint(true);
357 }
358
359 // Check if we need to restart some services.
360 boolean restart = false;
361 for (String[] arguments : mArguments) {
362 restart = restart || (arguments != null);
363 }
364 if (!restart) {
365 return;
366 }
367
368 // Start the service with arguments.
369 for (int i = 0; i < mServices.length; ++i) {
370 String[] arguments = mArguments[i];
371 if (arguments == null) {
372 continue;
373 }
374
375 // Start the service.
376 String service = mServices[i];
377 SystemProperties.set("ctl.start", service);
378
379 // Wait for the service to start.
380 String key = "init.svc." + service;
381 while (!"running".equals(SystemProperties.get(key))) {
382 checkpoint(true);
383 }
384
385 // Create the control socket.
386 LocalSocket socket = new LocalSocket();
387 LocalSocketAddress address = new LocalSocketAddress(
388 service, LocalSocketAddress.Namespace.RESERVED);
389
390 // Wait for the socket to connect.
391 while (true) {
392 try {
393 socket.connect(address);
394 break;
395 } catch (Exception e) {
396 // ignore
397 }
398 checkpoint(true);
399 }
400 socket.setSoTimeout(500);
401
402 // Send over the arguments.
403 OutputStream output = socket.getOutputStream();
404 for (String argument : arguments) {
405 byte[] bytes = argument.getBytes(Charsets.UTF_8);
406 if (bytes.length >= 0xFFFF) {
407 throw new IllegalArgumentException("argument too large");
408 }
409 output.write(bytes.length >> 8);
410 output.write(bytes.length);
411 output.write(bytes);
412 checkpoint(false);
413 }
414
415 // Send End-Of-Arguments.
416 output.write(0xFF);
417 output.write(0xFF);
418 output.flush();
419 socket.close();
420 }
421
422 // Now here is the beast from the old days. We check few
423 // properties to figure out the current status. Ideally we
424 // can read things back from the sockets and get rid of the
425 // properties, but we have no time...
426 while (NONE.equals(SystemProperties.get("vpn.dns")) ||
427 NONE.equals(SystemProperties.get("vpn.via"))) {
428
429 // Check if a running service is dead.
430 for (int i = 0; i < mServices.length; ++i) {
431 String service = mServices[i];
432 if (mArguments[i] != null && !"running".equals(
433 SystemProperties.get("init.svc." + service))) {
434 throw new IllegalArgumentException(service + " is dead");
435 }
436 }
437 checkpoint(true);
438 }
439
440 // Great! Now we are connected!
441 Log.i(TAG, "connected!");
442 // TODO:
443
444 } catch (Exception e) {
445 Log.i(TAG, e.getMessage());
446 for (String service : mServices) {
447 SystemProperties.set("ctl.stop", service);
448 }
449 }
450 }
451 }
Chia-chi Yehff3bdca2011-05-23 17:26:46 -0700452}