blob: 9a6d8ee635a2dc5df9586ad28023dbe19e58fc06 [file] [log] [blame]
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -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 android.net;
18
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -070019import static android.system.OsConstants.AF_INET;
20import static android.system.OsConstants.AF_INET6;
21
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070022import android.app.Activity;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070023import android.app.PendingIntent;
Jeff Sharkeyfea17de2013-06-11 14:13:09 -070024import android.app.Service;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070025import android.content.Context;
26import android.content.Intent;
Paul Jensen0784eea2014-08-19 16:00:24 -040027import android.content.pm.IPackageManager;
Sreeram Ramachandran633f0e82014-07-09 21:11:12 -070028import android.content.pm.PackageManager;
Paul Jensen6bc2c2c2014-05-07 15:27:40 -040029import android.net.NetworkUtils;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070030import android.os.Binder;
31import android.os.IBinder;
32import android.os.Parcel;
33import android.os.ParcelFileDescriptor;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Paul Jensen0784eea2014-08-19 16:00:24 -040036import android.os.UserHandle;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070037
38import com.android.internal.net.VpnConfig;
39
Jeff Sharkeyfea17de2013-06-11 14:13:09 -070040import java.net.DatagramSocket;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070041import java.net.Inet4Address;
42import java.net.Inet6Address;
Jeff Sharkeyfea17de2013-06-11 14:13:09 -070043import java.net.InetAddress;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070044import java.net.Socket;
45import java.util.ArrayList;
Chad Brubaker4ca19e82013-06-14 11:16:51 -070046import java.util.List;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -070047
48/**
49 * VpnService is a base class for applications to extend and build their
50 * own VPN solutions. In general, it creates a virtual network interface,
51 * configures addresses and routing rules, and returns a file descriptor
52 * to the application. Each read from the descriptor retrieves an outgoing
53 * packet which was routed to the interface. Each write to the descriptor
54 * injects an incoming packet just like it was received from the interface.
55 * The interface is running on Internet Protocol (IP), so packets are
56 * always started with IP headers. The application then completes a VPN
57 * connection by processing and exchanging packets with the remote server
58 * over a tunnel.
59 *
60 * <p>Letting applications intercept packets raises huge security concerns.
61 * A VPN application can easily break the network. Besides, two of them may
62 * conflict with each other. The system takes several actions to address
63 * these issues. Here are some key points:
64 * <ul>
65 * <li>User action is required to create a VPN connection.</li>
66 * <li>There can be only one VPN connection running at the same time. The
67 * existing interface is deactivated when a new one is created.</li>
68 * <li>A system-managed notification is shown during the lifetime of a
69 * VPN connection.</li>
70 * <li>A system-managed dialog gives the information of the current VPN
71 * connection. It also provides a button to disconnect.</li>
72 * <li>The network is restored automatically when the file descriptor is
73 * closed. It also covers the cases when a VPN application is crashed
74 * or killed by the system.</li>
75 * </ul>
76 *
77 * <p>There are two primary methods in this class: {@link #prepare} and
78 * {@link Builder#establish}. The former deals with user action and stops
79 * the VPN connection created by another application. The latter creates
80 * a VPN interface using the parameters supplied to the {@link Builder}.
81 * An application must call {@link #prepare} to grant the right to use
82 * other methods in this class, and the right can be revoked at any time.
83 * Here are the general steps to create a VPN connection:
84 * <ol>
85 * <li>When the user press the button to connect, call {@link #prepare}
86 * and launch the returned intent.</li>
87 * <li>When the application becomes prepared, start the service.</li>
88 * <li>Create a tunnel to the remote server and negotiate the network
89 * parameters for the VPN connection.</li>
90 * <li>Supply those parameters to a {@link Builder} and create a VPN
91 * interface by calling {@link Builder#establish}.</li>
92 * <li>Process and exchange packets between the tunnel and the returned
93 * file descriptor.</li>
94 * <li>When {@link #onRevoke} is invoked, close the file descriptor and
95 * shut down the tunnel gracefully.</li>
96 * </ol>
97 *
98 * <p>Services extended this class need to be declared with appropriate
99 * permission and intent filter. Their access must be secured by
100 * {@link android.Manifest.permission#BIND_VPN_SERVICE} permission, and
101 * their intent filter must match {@link #SERVICE_INTERFACE} action. Here
102 * is an example of declaring a VPN service in {@code AndroidManifest.xml}:
103 * <pre>
104 * &lt;service android:name=".ExampleVpnService"
105 * android:permission="android.permission.BIND_VPN_SERVICE"&gt;
106 * &lt;intent-filter&gt;
107 * &lt;action android:name="android.net.VpnService"/&gt;
108 * &lt;/intent-filter&gt;
109 * &lt;/service&gt;</pre>
110 *
111 * @see Builder
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700112 */
113public class VpnService extends Service {
114
115 /**
116 * The action must be matched by the intent filter of this service. It also
117 * needs to require {@link android.Manifest.permission#BIND_VPN_SERVICE}
118 * permission so that other applications cannot abuse it.
119 */
120 public static final String SERVICE_INTERFACE = VpnConfig.SERVICE_INTERFACE;
121
122 /**
123 * Use IConnectivityManager since those methods are hidden and not
124 * available in ConnectivityManager.
125 */
126 private static IConnectivityManager getService() {
127 return IConnectivityManager.Stub.asInterface(
128 ServiceManager.getService(Context.CONNECTIVITY_SERVICE));
129 }
130
131 /**
132 * Prepare to establish a VPN connection. This method returns {@code null}
133 * if the VPN application is already prepared. Otherwise, it returns an
134 * {@link Intent} to a system activity. The application should launch the
135 * activity using {@link Activity#startActivityForResult} to get itself
136 * prepared. The activity may pop up a dialog to require user action, and
137 * the result will come back via its {@link Activity#onActivityResult}.
138 * If the result is {@link Activity#RESULT_OK}, the application becomes
139 * prepared and is granted to use other methods in this class.
140 *
141 * <p>Only one application can be granted at the same time. The right
142 * is revoked when another application is granted. The application
143 * losing the right will be notified via its {@link #onRevoke}. Unless
144 * it becomes prepared again, subsequent calls to other methods in this
145 * class will fail.
146 *
147 * @see #onRevoke
148 */
149 public static Intent prepare(Context context) {
150 try {
151 if (getService().prepareVpn(context.getPackageName(), null)) {
152 return null;
153 }
154 } catch (RemoteException e) {
155 // ignore
156 }
157 return VpnConfig.getIntentForConfirmation();
158 }
159
160 /**
Chad Brubakerbcf12b32014-02-11 14:18:56 -0800161 * Protect a socket from VPN connections. After protecting, data sent
162 * through this socket will go directly to the underlying network,
163 * so its traffic will not be forwarded through the VPN.
164 * This method is useful if some connections need to be kept
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700165 * outside of VPN. For example, a VPN tunnel should protect itself if its
166 * destination is covered by VPN routes. Otherwise its outgoing packets
167 * will be sent back to the VPN interface and cause an infinite loop. This
168 * method will fail if the application is not prepared or is revoked.
169 *
170 * <p class="note">The socket is NOT closed by this method.
171 *
172 * @return {@code true} on success.
173 */
174 public boolean protect(int socket) {
Paul Jensen6bc2c2c2014-05-07 15:27:40 -0400175 return NetworkUtils.protectFromVpn(socket);
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700176 }
177
178 /**
179 * Convenience method to protect a {@link Socket} from VPN connections.
180 *
181 * @return {@code true} on success.
182 * @see #protect(int)
183 */
184 public boolean protect(Socket socket) {
185 return protect(socket.getFileDescriptor$().getInt$());
186 }
187
188 /**
189 * Convenience method to protect a {@link DatagramSocket} from VPN
190 * connections.
191 *
192 * @return {@code true} on success.
193 * @see #protect(int)
194 */
195 public boolean protect(DatagramSocket socket) {
196 return protect(socket.getFileDescriptor$().getInt$());
197 }
198
199 /**
Sreeram Ramachandran81c295e2014-07-09 23:21:25 -0700200 * Adds a network address to the VPN interface.
201 *
202 * Both IPv4 and IPv6 addresses are supported. The VPN must already be established. Fails if the
203 * address is already in use or cannot be assigned to the interface for any other reason.
204 *
Sreeram Ramachandran13846052014-07-10 12:35:23 -0700205 * Adding an address implicitly allows traffic from that address family (i.e., IPv4 or IPv6) to
206 * be routed over the VPN. @see Builder#allowFamily
207 *
Sreeram Ramachandran81c295e2014-07-09 23:21:25 -0700208 * @throws {@link IllegalArgumentException} if the address is invalid.
209 *
210 * @param address The IP address (IPv4 or IPv6) to assign to the VPN interface.
211 * @param prefixLength The prefix length of the address.
212 *
213 * @return {@code true} on success.
214 * @see Builder#addAddress
215 */
216 public boolean addAddress(InetAddress address, int prefixLength) {
Sreeram Ramachandranf4e0c0c2014-07-27 14:18:26 -0700217 check(address, prefixLength);
218 try {
219 return getService().addVpnAddress(address.getHostAddress(), prefixLength);
220 } catch (RemoteException e) {
221 throw new IllegalStateException(e);
222 }
Sreeram Ramachandran81c295e2014-07-09 23:21:25 -0700223 }
224
225 /**
226 * Removes a network address from the VPN interface.
227 *
228 * Both IPv4 and IPv6 addresses are supported. The VPN must already be established. Fails if the
229 * address is not assigned to the VPN interface, or if it is the only address assigned (thus
230 * cannot be removed), or if the address cannot be removed for any other reason.
231 *
Sreeram Ramachandran13846052014-07-10 12:35:23 -0700232 * After removing an address, if there are no addresses, routes or DNS servers of a particular
233 * address family (i.e., IPv4 or IPv6) configured on the VPN, that <b>DOES NOT</b> block that
234 * family from being routed. In other words, once an address family has been allowed, it stays
235 * allowed for the rest of the VPN's session. @see Builder#allowFamily
236 *
Sreeram Ramachandran81c295e2014-07-09 23:21:25 -0700237 * @throws {@link IllegalArgumentException} if the address is invalid.
238 *
239 * @param address The IP address (IPv4 or IPv6) to assign to the VPN interface.
240 * @param prefixLength The prefix length of the address.
241 *
242 * @return {@code true} on success.
243 */
244 public boolean removeAddress(InetAddress address, int prefixLength) {
Sreeram Ramachandranf4e0c0c2014-07-27 14:18:26 -0700245 check(address, prefixLength);
246 try {
247 return getService().removeVpnAddress(address.getHostAddress(), prefixLength);
248 } catch (RemoteException e) {
249 throw new IllegalStateException(e);
250 }
Sreeram Ramachandran81c295e2014-07-09 23:21:25 -0700251 }
252
253 /**
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700254 * Return the communication interface to the service. This method returns
255 * {@code null} on {@link Intent}s other than {@link #SERVICE_INTERFACE}
256 * action. Applications overriding this method must identify the intent
257 * and return the corresponding interface accordingly.
258 *
259 * @see Service#onBind
260 */
261 @Override
262 public IBinder onBind(Intent intent) {
263 if (intent != null && SERVICE_INTERFACE.equals(intent.getAction())) {
264 return new Callback();
265 }
266 return null;
267 }
268
269 /**
270 * Invoked when the application is revoked. At this moment, the VPN
271 * interface is already deactivated by the system. The application should
272 * close the file descriptor and shut down gracefully. The default
273 * implementation of this method is calling {@link Service#stopSelf()}.
274 *
275 * <p class="note">Calls to this method may not happen on the main thread
276 * of the process.
277 *
278 * @see #prepare
279 */
280 public void onRevoke() {
281 stopSelf();
282 }
283
284 /**
285 * Use raw Binder instead of AIDL since now there is only one usage.
286 */
287 private class Callback extends Binder {
288 @Override
289 protected boolean onTransact(int code, Parcel data, Parcel reply, int flags) {
290 if (code == IBinder.LAST_CALL_TRANSACTION) {
291 onRevoke();
292 return true;
293 }
294 return false;
295 }
296 }
297
298 /**
Sreeram Ramachandranf4e0c0c2014-07-27 14:18:26 -0700299 * Private method to validate address and prefixLength.
300 */
301 private static void check(InetAddress address, int prefixLength) {
302 if (address.isLoopbackAddress()) {
303 throw new IllegalArgumentException("Bad address");
304 }
305 if (address instanceof Inet4Address) {
306 if (prefixLength < 0 || prefixLength > 32) {
307 throw new IllegalArgumentException("Bad prefixLength");
308 }
309 } else if (address instanceof Inet6Address) {
310 if (prefixLength < 0 || prefixLength > 128) {
311 throw new IllegalArgumentException("Bad prefixLength");
312 }
313 } else {
314 throw new IllegalArgumentException("Unsupported family");
315 }
316 }
317
318 /**
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700319 * Helper class to create a VPN interface. This class should be always
320 * used within the scope of the outer {@link VpnService}.
321 *
322 * @see VpnService
323 */
324 public class Builder {
325
326 private final VpnConfig mConfig = new VpnConfig();
Chad Brubaker4ca19e82013-06-14 11:16:51 -0700327 private final List<LinkAddress> mAddresses = new ArrayList<LinkAddress>();
328 private final List<RouteInfo> mRoutes = new ArrayList<RouteInfo>();
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700329
330 public Builder() {
331 mConfig.user = VpnService.this.getClass().getName();
332 }
333
334 /**
335 * Set the name of this session. It will be displayed in
336 * system-managed dialogs and notifications. This is recommended
337 * not required.
338 */
339 public Builder setSession(String session) {
340 mConfig.session = session;
341 return this;
342 }
343
344 /**
345 * Set the {@link PendingIntent} to an activity for users to
346 * configure the VPN connection. If it is not set, the button
347 * to configure will not be shown in system-managed dialogs.
348 */
349 public Builder setConfigureIntent(PendingIntent intent) {
350 mConfig.configureIntent = intent;
351 return this;
352 }
353
354 /**
355 * Set the maximum transmission unit (MTU) of the VPN interface. If
356 * it is not set, the default value in the operating system will be
357 * used.
358 *
359 * @throws IllegalArgumentException if the value is not positive.
360 */
361 public Builder setMtu(int mtu) {
362 if (mtu <= 0) {
363 throw new IllegalArgumentException("Bad mtu");
364 }
365 mConfig.mtu = mtu;
366 return this;
367 }
368
369 /**
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700370 * Add a network address to the VPN interface. Both IPv4 and IPv6
371 * addresses are supported. At least one address must be set before
372 * calling {@link #establish}.
373 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700374 * Adding an address implicitly allows traffic from that address family
375 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
376 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700377 * @throws IllegalArgumentException if the address is invalid.
378 */
379 public Builder addAddress(InetAddress address, int prefixLength) {
380 check(address, prefixLength);
381
382 if (address.isAnyLocalAddress()) {
383 throw new IllegalArgumentException("Bad address");
384 }
Chad Brubaker4ca19e82013-06-14 11:16:51 -0700385 mAddresses.add(new LinkAddress(address, prefixLength));
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -0700386 mConfig.updateAllowedFamilies(address);
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700387 return this;
388 }
389
390 /**
391 * Convenience method to add a network address to the VPN interface
392 * using a numeric address string. See {@link InetAddress} for the
393 * definitions of numeric address formats.
394 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700395 * Adding an address implicitly allows traffic from that address family
396 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
397 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700398 * @throws IllegalArgumentException if the address is invalid.
399 * @see #addAddress(InetAddress, int)
400 */
401 public Builder addAddress(String address, int prefixLength) {
402 return addAddress(InetAddress.parseNumericAddress(address), prefixLength);
403 }
404
405 /**
406 * Add a network route to the VPN interface. Both IPv4 and IPv6
407 * routes are supported.
408 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700409 * Adding a route implicitly allows traffic from that address family
410 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
411 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700412 * @throws IllegalArgumentException if the route is invalid.
413 */
414 public Builder addRoute(InetAddress address, int prefixLength) {
415 check(address, prefixLength);
416
417 int offset = prefixLength / 8;
418 byte[] bytes = address.getAddress();
419 if (offset < bytes.length) {
420 for (bytes[offset] <<= prefixLength % 8; offset < bytes.length; ++offset) {
421 if (bytes[offset] != 0) {
422 throw new IllegalArgumentException("Bad address");
423 }
424 }
425 }
Chad Brubaker4ca19e82013-06-14 11:16:51 -0700426 mRoutes.add(new RouteInfo(new LinkAddress(address, prefixLength), null));
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -0700427 mConfig.updateAllowedFamilies(address);
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700428 return this;
429 }
430
431 /**
432 * Convenience method to add a network route to the VPN interface
433 * using a numeric address string. See {@link InetAddress} for the
434 * definitions of numeric address formats.
435 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700436 * Adding a route implicitly allows traffic from that address family
437 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
438 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700439 * @throws IllegalArgumentException if the route is invalid.
440 * @see #addRoute(InetAddress, int)
441 */
442 public Builder addRoute(String address, int prefixLength) {
443 return addRoute(InetAddress.parseNumericAddress(address), prefixLength);
444 }
445
446 /**
447 * Add a DNS server to the VPN connection. Both IPv4 and IPv6
448 * addresses are supported. If none is set, the DNS servers of
449 * the default network will be used.
450 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700451 * Adding a server implicitly allows traffic from that address family
452 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
453 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700454 * @throws IllegalArgumentException if the address is invalid.
455 */
456 public Builder addDnsServer(InetAddress address) {
457 if (address.isLoopbackAddress() || address.isAnyLocalAddress()) {
458 throw new IllegalArgumentException("Bad address");
459 }
460 if (mConfig.dnsServers == null) {
461 mConfig.dnsServers = new ArrayList<String>();
462 }
463 mConfig.dnsServers.add(address.getHostAddress());
464 return this;
465 }
466
467 /**
468 * Convenience method to add a DNS server to the VPN connection
469 * using a numeric address string. See {@link InetAddress} for the
470 * definitions of numeric address formats.
471 *
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700472 * Adding a server implicitly allows traffic from that address family
473 * (i.e., IPv4 or IPv6) to be routed over the VPN. @see #allowFamily
474 *
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700475 * @throws IllegalArgumentException if the address is invalid.
476 * @see #addDnsServer(InetAddress)
477 */
478 public Builder addDnsServer(String address) {
479 return addDnsServer(InetAddress.parseNumericAddress(address));
480 }
481
482 /**
483 * Add a search domain to the DNS resolver.
484 */
485 public Builder addSearchDomain(String domain) {
486 if (mConfig.searchDomains == null) {
487 mConfig.searchDomains = new ArrayList<String>();
488 }
489 mConfig.searchDomains.add(domain);
490 return this;
491 }
492
493 /**
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700494 * Allows traffic from the specified address family.
495 *
496 * By default, if no address, route or DNS server of a specific family (IPv4 or IPv6) is
497 * added to this VPN, then all outgoing traffic of that family is blocked. If any address,
498 * route or DNS server is added, that family is allowed.
499 *
500 * This method allows an address family to be unblocked even without adding an address,
501 * route or DNS server of that family. Traffic of that family will then typically
502 * fall-through to the underlying network if it's supported.
503 *
504 * {@code family} must be either {@code AF_INET} (for IPv4) or {@code AF_INET6} (for IPv6).
505 * {@link IllegalArgumentException} is thrown if it's neither.
506 *
507 * @param family The address family ({@code AF_INET} or {@code AF_INET6}) to allow.
508 *
509 * @return this {@link Builder} object to facilitate chaining of method calls.
510 */
511 public Builder allowFamily(int family) {
Sreeram Ramachandran42065ac2014-07-27 00:37:35 -0700512 if (family == AF_INET) {
513 mConfig.allowIPv4 = true;
514 } else if (family == AF_INET6) {
515 mConfig.allowIPv6 = true;
516 } else {
517 throw new IllegalArgumentException(family + " is neither " + AF_INET + " nor " +
518 AF_INET6);
519 }
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700520 return this;
521 }
522
Paul Jensen0784eea2014-08-19 16:00:24 -0400523 private void verifyApp(String packageName) throws PackageManager.NameNotFoundException {
524 IPackageManager pm = IPackageManager.Stub.asInterface(
525 ServiceManager.getService("package"));
526 try {
527 pm.getApplicationInfo(packageName, 0, UserHandle.getCallingUserId());
528 } catch (RemoteException e) {
529 throw new IllegalStateException(e);
530 }
531 }
532
Sreeram Ramachandrand7e71642014-07-09 23:01:30 -0700533 /**
Sreeram Ramachandran633f0e82014-07-09 21:11:12 -0700534 * Adds an application that's allowed to access the VPN connection.
535 *
536 * If this method is called at least once, only applications added through this method (and
537 * no others) are allowed access. Else (if this method is never called), all applications
538 * are allowed by default.
539 *
540 * A {@link Builder} may have only a set of allowed applications OR a set of disallowed
541 * ones, but not both. Calling this method after {@link #addDisallowedApplication} has
542 * already been called, or vice versa, will throw an {@link UnsupportedOperationException}.
543 *
544 * {@code packageName} must be the canonical name of a currently installed application.
545 * {@link PackageManager.NameNotFoundException} is thrown if there's no such application.
546 *
547 * @throws {@link PackageManager.NameNotFoundException} If the application isn't installed.
548 *
549 * @param packageName The full name (e.g.: "com.google.apps.contacts") of an application.
550 *
551 * @return this {@link Builder} object to facilitate chaining method calls.
552 */
553 public Builder addAllowedApplication(String packageName)
554 throws PackageManager.NameNotFoundException {
Paul Jensen0784eea2014-08-19 16:00:24 -0400555 if (mConfig.disallowedApplications != null) {
556 throw new UnsupportedOperationException("addDisallowedApplication already called");
557 }
558 verifyApp(packageName);
559 if (mConfig.allowedApplications == null) {
560 mConfig.allowedApplications = new ArrayList<String>();
561 }
562 mConfig.allowedApplications.add(packageName);
Sreeram Ramachandran633f0e82014-07-09 21:11:12 -0700563 return this;
564 }
565
566 /**
567 * Adds an application that's denied access to the VPN connection.
568 *
569 * By default, all applications are allowed access, except for those denied through this
570 * method.
571 *
572 * A {@link Builder} may have only a set of allowed applications OR a set of disallowed
573 * ones, but not both. Calling this method after {@link #addAllowedApplication} has already
574 * been called, or vice versa, will throw an {@link UnsupportedOperationException}.
575 *
576 * {@code packageName} must be the canonical name of a currently installed application.
577 * {@link PackageManager.NameNotFoundException} is thrown if there's no such application.
578 *
579 * @throws {@link PackageManager.NameNotFoundException} If the application isn't installed.
580 *
581 * @param packageName The full name (e.g.: "com.google.apps.contacts") of an application.
582 *
583 * @return this {@link Builder} object to facilitate chaining method calls.
584 */
585 public Builder addDisallowedApplication(String packageName)
586 throws PackageManager.NameNotFoundException {
Paul Jensen0784eea2014-08-19 16:00:24 -0400587 if (mConfig.allowedApplications != null) {
588 throw new UnsupportedOperationException("addAllowedApplication already called");
589 }
590 verifyApp(packageName);
591 if (mConfig.disallowedApplications == null) {
592 mConfig.disallowedApplications = new ArrayList<String>();
593 }
594 mConfig.disallowedApplications.add(packageName);
Sreeram Ramachandran633f0e82014-07-09 21:11:12 -0700595 return this;
596 }
597
598 /**
Sreeram Ramachandrana9294eb2014-07-09 21:43:03 -0700599 * Allows all apps to bypass this VPN connection.
600 *
601 * By default, all traffic from apps is forwarded through the VPN interface and it is not
602 * possible for apps to side-step the VPN. If this method is called, apps may use methods
603 * such as {@link ConnectivityManager#setProcessDefaultNetwork} to instead send/receive
604 * directly over the underlying network or any other network they have permissions for.
605 *
606 * @return this {@link Builder} object to facilitate chaining of method calls.
607 */
608 public Builder allowBypass() {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -0700609 mConfig.allowBypass = true;
Sreeram Ramachandrana9294eb2014-07-09 21:43:03 -0700610 return this;
611 }
612
613 /**
Sreeram Ramachandrancc26b4c2014-07-18 16:41:25 -0700614 * Sets the VPN interface's file descriptor to be in blocking/non-blocking mode.
615 *
616 * By default, the file descriptor returned by {@link #establish} is non-blocking.
617 *
618 * @param blocking True to put the descriptor into blocking mode; false for non-blocking.
619 *
620 * @return this {@link Builder} object to facilitate chaining method calls.
621 */
622 public Builder setBlocking(boolean blocking) {
Jeff Davidson6bbf39c2014-07-23 10:14:53 -0700623 mConfig.blocking = blocking;
Sreeram Ramachandrancc26b4c2014-07-18 16:41:25 -0700624 return this;
625 }
626
627 /**
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700628 * Create a VPN interface using the parameters supplied to this
629 * builder. The interface works on IP packets, and a file descriptor
630 * is returned for the application to access them. Each read
631 * retrieves an outgoing packet which was routed to the interface.
632 * Each write injects an incoming packet just like it was received
633 * from the interface. The file descriptor is put into non-blocking
634 * mode by default to avoid blocking Java threads. To use the file
635 * descriptor completely in native space, see
636 * {@link ParcelFileDescriptor#detachFd()}. The application MUST
637 * close the file descriptor when the VPN connection is terminated.
638 * The VPN interface will be removed and the network will be
639 * restored by the system automatically.
640 *
641 * <p>To avoid conflicts, there can be only one active VPN interface
642 * at the same time. Usually network parameters are never changed
643 * during the lifetime of a VPN connection. It is also common for an
644 * application to create a new file descriptor after closing the
645 * previous one. However, it is rare but not impossible to have two
646 * interfaces while performing a seamless handover. In this case, the
647 * old interface will be deactivated when the new one is created
648 * successfully. Both file descriptors are valid but now outgoing
649 * packets will be routed to the new interface. Therefore, after
650 * draining the old file descriptor, the application MUST close it
651 * and start using the new file descriptor. If the new interface
652 * cannot be created, the existing interface and its file descriptor
653 * remain untouched.
654 *
655 * <p>An exception will be thrown if the interface cannot be created
656 * for any reason. However, this method returns {@code null} if the
657 * application is not prepared or is revoked. This helps solve
658 * possible race conditions between other VPN applications.
659 *
660 * @return {@link ParcelFileDescriptor} of the VPN interface, or
661 * {@code null} if the application is not prepared.
662 * @throws IllegalArgumentException if a parameter is not accepted
663 * by the operating system.
664 * @throws IllegalStateException if a parameter cannot be applied
665 * by the operating system.
666 * @throws SecurityException if the service is not properly declared
667 * in {@code AndroidManifest.xml}.
668 * @see VpnService
669 */
670 public ParcelFileDescriptor establish() {
Chad Brubaker4ca19e82013-06-14 11:16:51 -0700671 mConfig.addresses = mAddresses;
672 mConfig.routes = mRoutes;
Chia-chi Yeh199ed6e2011-08-03 17:38:49 -0700673
674 try {
675 return getService().establishVpn(mConfig);
676 } catch (RemoteException e) {
677 throw new IllegalStateException(e);
678 }
679 }
680 }
681}