blob: 6402e07bddc3259ecbd684d82457647bd45d2880 [file] [log] [blame]
Nathan Harold1afbef42017-03-01 18:55:06 -08001/*
2 * Copyright (C) 2017 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;
18
19import static android.Manifest.permission.DUMP;
Nathan Harold93962f32017-03-07 13:23:36 -080020import static android.net.IpSecManager.INVALID_RESOURCE_ID;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070021import static android.system.OsConstants.AF_INET;
evitayane4259d32018-03-22 17:53:08 -070022import static android.system.OsConstants.AF_INET6;
23import static android.system.OsConstants.AF_UNSPEC;
manojboopathic4be79d2017-11-30 17:11:49 -080024import static android.system.OsConstants.EINVAL;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070025import static android.system.OsConstants.IPPROTO_UDP;
26import static android.system.OsConstants.SOCK_DGRAM;
Benedict Wongab80e1f2018-07-25 18:46:19 -070027
Nathan Harold21208ee2018-03-15 18:06:06 -070028import android.annotation.NonNull;
29import android.app.AppOpsManager;
Nathan Harold1afbef42017-03-01 18:55:06 -080030import android.content.Context;
Benedict Wong24452272019-02-25 12:33:22 -080031import android.content.pm.PackageManager;
Nathan Harold1afbef42017-03-01 18:55:06 -080032import android.net.IIpSecService;
33import android.net.INetd;
Nathan Harold93962f32017-03-07 13:23:36 -080034import android.net.IpSecAlgorithm;
35import android.net.IpSecConfig;
36import android.net.IpSecManager;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070037import android.net.IpSecSpiResponse;
Nathan Harold93962f32017-03-07 13:23:36 -080038import android.net.IpSecTransform;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070039import android.net.IpSecTransformResponse;
Benedict Wong8149f6e2018-01-18 18:31:45 -080040import android.net.IpSecTunnelInterfaceResponse;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070041import android.net.IpSecUdpEncapResponse;
Benedict Wongecc9f7c2018-03-01 18:53:07 -080042import android.net.LinkAddress;
Benedict Wong8149f6e2018-01-18 18:31:45 -080043import android.net.Network;
Nathan Harolda10003d2017-08-23 13:46:33 -070044import android.net.NetworkUtils;
Benedict Wongbabe5d72017-12-03 19:42:36 -080045import android.net.TrafficStats;
Remi NGUYEN VAN231b52b2019-01-29 15:38:52 +090046import android.net.util.NetdService;
Nathan Harold93962f32017-03-07 13:23:36 -080047import android.os.Binder;
Nathan Harold93962f32017-03-07 13:23:36 -080048import android.os.IBinder;
Benedict Wongd78ad3d2020-03-25 05:50:51 +000049import android.os.INetworkManagementService;
Nathan Harold93962f32017-03-07 13:23:36 -080050import android.os.ParcelFileDescriptor;
Nathan Harold1afbef42017-03-01 18:55:06 -080051import android.os.RemoteException;
Nathan Harold93962f32017-03-07 13:23:36 -080052import android.os.ServiceSpecificException;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070053import android.system.ErrnoException;
54import android.system.Os;
55import android.system.OsConstants;
Nathan Harolda10003d2017-08-23 13:46:33 -070056import android.text.TextUtils;
Nathan Harold1afbef42017-03-01 18:55:06 -080057import android.util.Log;
58import android.util.Slog;
Nathan Harold93962f32017-03-07 13:23:36 -080059import android.util.SparseArray;
Benedict Wong8149f6e2018-01-18 18:31:45 -080060import android.util.SparseBooleanArray;
Nathan Harolda10003d2017-08-23 13:46:33 -070061
Nathan Harold93962f32017-03-07 13:23:36 -080062import com.android.internal.annotations.GuardedBy;
ludi1a06aa72017-05-12 09:15:00 -070063import com.android.internal.annotations.VisibleForTesting;
Benedict Wong4f255702017-11-06 20:49:10 -080064import com.android.internal.util.Preconditions;
Nathan Harolda10003d2017-08-23 13:46:33 -070065
Benedict Wongab80e1f2018-07-25 18:46:19 -070066import libcore.io.IoUtils;
67
Nathan Harold1afbef42017-03-01 18:55:06 -080068import java.io.FileDescriptor;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070069import java.io.IOException;
Nathan Harold1afbef42017-03-01 18:55:06 -080070import java.io.PrintWriter;
evitayane4259d32018-03-22 17:53:08 -070071import java.net.Inet4Address;
72import java.net.Inet6Address;
Nathan Harold8dc1fd02017-04-04 19:37:48 -070073import java.net.InetAddress;
74import java.net.InetSocketAddress;
75import java.net.UnknownHostException;
Benedict Wong409c8ca2017-10-26 19:41:43 -070076import java.util.ArrayList;
77import java.util.List;
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +000078import java.util.Objects;
Nathan Harolda10003d2017-08-23 13:46:33 -070079
Benedict Wong409c8ca2017-10-26 19:41:43 -070080/**
81 * A service to manage multiple clients that want to access the IpSec API. The service is
82 * responsible for maintaining a list of clients and managing the resources (and related quotas)
83 * that each of them own.
84 *
85 * <p>Synchronization in IpSecService is done on all entrypoints due to potential race conditions at
86 * the kernel/xfrm level. Further, this allows the simplifying assumption to be made that only one
87 * thread is ever running at a time.
88 *
89 * @hide
90 */
Nathan Harold1afbef42017-03-01 18:55:06 -080091public class IpSecService extends IIpSecService.Stub {
92 private static final String TAG = "IpSecService";
93 private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG);
Nathan Harold8dc1fd02017-04-04 19:37:48 -070094
Nathan Harold1afbef42017-03-01 18:55:06 -080095 private static final String NETD_SERVICE_NAME = "netd";
Benedict Wong564e2aa2018-05-07 20:06:44 -070096 private static final int[] ADDRESS_FAMILIES =
97 new int[] {OsConstants.AF_INET, OsConstants.AF_INET6};
Nathan Harold1afbef42017-03-01 18:55:06 -080098
ludi1a06aa72017-05-12 09:15:00 -070099 private static final int NETD_FETCH_TIMEOUT_MS = 5000; // ms
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700100 private static final InetAddress INADDR_ANY;
101
Benedict Wongabcc6c02019-03-20 09:44:09 -0700102 @VisibleForTesting static final int MAX_PORT_BIND_ATTEMPTS = 10;
103
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700104 static {
105 try {
106 INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0});
107 } catch (UnknownHostException e) {
108 throw new RuntimeException(e);
109 }
110 }
111
112 static final int FREE_PORT_MIN = 1024; // ports 1-1023 are reserved
113 static final int PORT_MAX = 0xFFFF; // ports are an unsigned 16-bit integer
114
115 /* Binder context for this service */
Nathan Harold1afbef42017-03-01 18:55:06 -0800116 private final Context mContext;
117
Benedict Wongd78ad3d2020-03-25 05:50:51 +0000118 /* NetworkManager instance */
119 private final INetworkManagementService mNetworkManager;
120
Nathan Haroldd8c74292017-12-13 19:16:33 -0800121 /**
Nathan Harolda2523312018-01-05 19:25:13 -0800122 * The next non-repeating global ID for tracking resources between users, this service, and
123 * kernel data structures. Accessing this variable is not thread safe, so it is only read or
124 * modified within blocks synchronized on IpSecService.this. We want to avoid -1
125 * (INVALID_RESOURCE_ID) and 0 (we probably forgot to initialize it).
Nathan Haroldd8c74292017-12-13 19:16:33 -0800126 */
127 @GuardedBy("IpSecService.this")
128 private int mNextResourceId = 1;
Nathan Harold1afbef42017-03-01 18:55:06 -0800129
ludi1a06aa72017-05-12 09:15:00 -0700130 interface IpSecServiceConfiguration {
131 INetd getNetdInstance() throws RemoteException;
132
133 static IpSecServiceConfiguration GETSRVINSTANCE =
134 new IpSecServiceConfiguration() {
135 @Override
136 public INetd getNetdInstance() throws RemoteException {
137 final INetd netd = NetdService.getInstance();
138 if (netd == null) {
139 throw new RemoteException("Failed to Get Netd Instance");
140 }
141 return netd;
142 }
143 };
144 }
145
146 private final IpSecServiceConfiguration mSrvConfig;
Benedict Wongbabe5d72017-12-03 19:42:36 -0800147 final UidFdTagger mUidFdTagger;
ludi1a06aa72017-05-12 09:15:00 -0700148
Benedict Wong409c8ca2017-10-26 19:41:43 -0700149 /**
150 * Interface for user-reference and kernel-resource cleanup.
151 *
152 * <p>This interface must be implemented for a resource to be reference counted.
153 */
154 @VisibleForTesting
155 public interface IResource {
156 /**
157 * Invalidates a IResource object, ensuring it is invalid for the purposes of allocating new
158 * objects dependent on it.
159 *
160 * <p>Implementations of this method are expected to remove references to the IResource
161 * object from the IpSecService's tracking arrays. The removal from the arrays ensures that
162 * the resource is considered invalid for user access or allocation or use in other
163 * resources.
164 *
165 * <p>References to the IResource object may be held by other RefcountedResource objects,
Benedict Wong4f9fb812017-12-13 17:16:53 -0800166 * and as such, the underlying resources and quota may not be cleaned up.
Benedict Wong409c8ca2017-10-26 19:41:43 -0700167 */
168 void invalidate() throws RemoteException;
169
170 /**
171 * Releases underlying resources and related quotas.
172 *
173 * <p>Implementations of this method are expected to remove all system resources that are
174 * tracked by the IResource object. Due to other RefcountedResource objects potentially
Benedict Wong344bd622017-11-16 15:27:22 -0800175 * having references to the IResource object, freeUnderlyingResources may not always be
Benedict Wong409c8ca2017-10-26 19:41:43 -0700176 * called from releaseIfUnreferencedRecursively().
177 */
178 void freeUnderlyingResources() throws RemoteException;
179 }
180
181 /**
182 * RefcountedResource manages references and dependencies in an exclusively acyclic graph.
183 *
184 * <p>RefcountedResource implements both explicit and implicit resource management. Creating a
185 * RefcountedResource object creates an explicit reference that must be freed by calling
186 * userRelease(). Additionally, adding this object as a child of another RefcountedResource
187 * object will add an implicit reference.
188 *
189 * <p>Resources are cleaned up when all references, both implicit and explicit, are released
190 * (ie, when userRelease() is called and when all parents have called releaseReference() on this
191 * object.)
192 */
193 @VisibleForTesting
194 public class RefcountedResource<T extends IResource> implements IBinder.DeathRecipient {
Junyu Lai4e645f32019-04-26 01:38:04 +0000195 private final T mResource;
196 private final List<RefcountedResource> mChildren;
Benedict Wong24452272019-02-25 12:33:22 -0800197 int mRefCount = 1; // starts at 1 for user's reference.
Junyu Lai4e645f32019-04-26 01:38:04 +0000198 IBinder mBinder;
Benedict Wong24452272019-02-25 12:33:22 -0800199
Junyu Lai4e645f32019-04-26 01:38:04 +0000200 RefcountedResource(T resource, IBinder binder, RefcountedResource... children) {
Benedict Wong409c8ca2017-10-26 19:41:43 -0700201 synchronized (IpSecService.this) {
202 this.mResource = resource;
Junyu Lai4e645f32019-04-26 01:38:04 +0000203 this.mChildren = new ArrayList<>(children.length);
Benedict Wong409c8ca2017-10-26 19:41:43 -0700204 this.mBinder = binder;
Junyu Lai4e645f32019-04-26 01:38:04 +0000205
Benedict Wong409c8ca2017-10-26 19:41:43 -0700206 for (RefcountedResource child : children) {
Junyu Lai4e645f32019-04-26 01:38:04 +0000207 mChildren.add(child);
Benedict Wong409c8ca2017-10-26 19:41:43 -0700208 child.mRefCount++;
209 }
210
211 try {
Junyu Lai4e645f32019-04-26 01:38:04 +0000212 mBinder.linkToDeath(this, 0);
Benedict Wong409c8ca2017-10-26 19:41:43 -0700213 } catch (RemoteException e) {
214 binderDied();
Benedict Wongccfaa3c2019-02-28 20:28:48 -0800215 e.rethrowFromSystemServer();
Benedict Wong409c8ca2017-10-26 19:41:43 -0700216 }
217 }
218 }
219
220 /**
221 * If the Binder object dies, this function is called to free the system resources that are
Benedict Wong344bd622017-11-16 15:27:22 -0800222 * being tracked by this record and to subsequently release this record for garbage
Benedict Wong409c8ca2017-10-26 19:41:43 -0700223 * collection
224 */
225 @Override
226 public void binderDied() {
227 synchronized (IpSecService.this) {
228 try {
229 userRelease();
230 } catch (Exception e) {
231 Log.e(TAG, "Failed to release resource: " + e);
232 }
233 }
234 }
235
236 public T getResource() {
237 return mResource;
238 }
239
240 /**
241 * Unlinks from binder and performs IpSecService resource cleanup (removes from resource
242 * arrays)
243 *
244 * <p>If this method has been previously called, the RefcountedResource's binder field will
245 * be null, and the method will return without performing the cleanup a second time.
246 *
247 * <p>Note that calling this function does not imply that kernel resources will be freed at
248 * this time, or that the related quota will be returned. Such actions will only be
249 * performed upon the reference count reaching zero.
250 */
251 @GuardedBy("IpSecService.this")
252 public void userRelease() throws RemoteException {
253 // Prevent users from putting reference counts into a bad state by calling
254 // userRelease() multiple times.
255 if (mBinder == null) {
256 return;
257 }
258
Junyu Lai4e645f32019-04-26 01:38:04 +0000259 mBinder.unlinkToDeath(this, 0);
Benedict Wong409c8ca2017-10-26 19:41:43 -0700260 mBinder = null;
261
262 mResource.invalidate();
Junyu Lai4e645f32019-04-26 01:38:04 +0000263
Benedict Wong409c8ca2017-10-26 19:41:43 -0700264 releaseReference();
265 }
266
267 /**
268 * Removes a reference to this resource. If the resultant reference count is zero, the
269 * underlying resources are freed, and references to all child resources are also dropped
270 * recursively (resulting in them freeing their resources and children, etcetera)
271 *
272 * <p>This method also sets the reference count to an invalid value (-1) to signify that it
273 * has been fully released. Any subsequent calls to this method will result in an
274 * IllegalStateException being thrown due to resource already having been previously
275 * released
276 */
277 @VisibleForTesting
278 @GuardedBy("IpSecService.this")
279 public void releaseReference() throws RemoteException {
280 mRefCount--;
281
282 if (mRefCount > 0) {
283 return;
284 } else if (mRefCount < 0) {
285 throw new IllegalStateException(
286 "Invalid operation - resource has already been released.");
287 }
288
289 // Cleanup own resources
290 mResource.freeUnderlyingResources();
291
292 // Cleanup child resources as needed
293 for (RefcountedResource<? extends IResource> child : mChildren) {
294 child.releaseReference();
295 }
296
297 // Enforce that resource cleanup can only be called once
298 // By decrementing the refcount (from 0 to -1), the next call will throw an
299 // IllegalStateException - it has already been released fully.
300 mRefCount--;
301 }
302
303 @Override
304 public String toString() {
305 return new StringBuilder()
306 .append("{mResource=")
307 .append(mResource)
308 .append(", mRefCount=")
309 .append(mRefCount)
310 .append(", mChildren=")
311 .append(mChildren)
312 .append("}")
313 .toString();
314 }
315 }
316
Benedict Wong4f9fb812017-12-13 17:16:53 -0800317 /**
318 * Very simple counting class that looks much like a counting semaphore
319 *
320 * <p>This class is not thread-safe, and expects that that users of this class will ensure
321 * synchronization and thread safety by holding the IpSecService.this instance lock.
322 */
Benedict Wong344bd622017-11-16 15:27:22 -0800323 @VisibleForTesting
324 static class ResourceTracker {
Nathan Harolda1afbd82017-04-24 16:16:34 -0700325 private final int mMax;
326 int mCurrent;
327
328 ResourceTracker(int max) {
329 mMax = max;
330 mCurrent = 0;
331 }
332
Benedict Wong344bd622017-11-16 15:27:22 -0800333 boolean isAvailable() {
Nathan Harolda1afbd82017-04-24 16:16:34 -0700334 return (mCurrent < mMax);
335 }
336
Benedict Wong344bd622017-11-16 15:27:22 -0800337 void take() {
Nathan Harolda1afbd82017-04-24 16:16:34 -0700338 if (!isAvailable()) {
339 Log.wtf(TAG, "Too many resources allocated!");
340 }
341 mCurrent++;
342 }
343
Benedict Wong344bd622017-11-16 15:27:22 -0800344 void give() {
Nathan Harolda1afbd82017-04-24 16:16:34 -0700345 if (mCurrent <= 0) {
346 Log.wtf(TAG, "We've released this resource too many times");
347 }
348 mCurrent--;
349 }
ludi3e5ea232017-08-10 15:44:40 -0700350
351 @Override
352 public String toString() {
353 return new StringBuilder()
354 .append("{mCurrent=")
355 .append(mCurrent)
356 .append(", mMax=")
357 .append(mMax)
358 .append("}")
359 .toString();
360 }
Nathan Harolda1afbd82017-04-24 16:16:34 -0700361 }
362
Benedict Wong344bd622017-11-16 15:27:22 -0800363 @VisibleForTesting
364 static final class UserRecord {
Benedict Wong344bd622017-11-16 15:27:22 -0800365 /* Maximum number of each type of resource that a single UID may possess */
Automerger Merge Workera3769202020-03-25 00:49:05 +0000366
367 // Up to 4 active VPNs/IWLAN with potential soft handover.
368 public static final int MAX_NUM_TUNNEL_INTERFACES = 8;
369 public static final int MAX_NUM_ENCAP_SOCKETS = 16;
370
371 // SPIs and Transforms are both cheap, and are 1:1 correlated.
372 public static final int MAX_NUM_TRANSFORMS = 64;
373 public static final int MAX_NUM_SPIS = 64;
Nathan Harolda1afbd82017-04-24 16:16:34 -0700374
Benedict Wong4f9fb812017-12-13 17:16:53 -0800375 /**
376 * Store each of the OwnedResource types in an (thinly wrapped) sparse array for indexing
377 * and explicit (user) reference management.
378 *
379 * <p>These are stored in separate arrays to improve debuggability and dump output clarity.
380 *
381 * <p>Resources are removed from this array when the user releases their explicit reference
382 * by calling one of the releaseResource() methods.
383 */
Benedict Wong344bd622017-11-16 15:27:22 -0800384 final RefcountedResourceArray<SpiRecord> mSpiRecords =
Benedict Wong4f9fb812017-12-13 17:16:53 -0800385 new RefcountedResourceArray<>(SpiRecord.class.getSimpleName());
Benedict Wong344bd622017-11-16 15:27:22 -0800386 final RefcountedResourceArray<TransformRecord> mTransformRecords =
Benedict Wong4f9fb812017-12-13 17:16:53 -0800387 new RefcountedResourceArray<>(TransformRecord.class.getSimpleName());
Benedict Wong344bd622017-11-16 15:27:22 -0800388 final RefcountedResourceArray<EncapSocketRecord> mEncapSocketRecords =
Benedict Wong4f9fb812017-12-13 17:16:53 -0800389 new RefcountedResourceArray<>(EncapSocketRecord.class.getSimpleName());
Benedict Wong8149f6e2018-01-18 18:31:45 -0800390 final RefcountedResourceArray<TunnelInterfaceRecord> mTunnelInterfaceRecords =
391 new RefcountedResourceArray<>(TunnelInterfaceRecord.class.getSimpleName());
Benedict Wong4f9fb812017-12-13 17:16:53 -0800392
393 /**
394 * Trackers for quotas for each of the OwnedResource types.
395 *
396 * <p>These trackers are separate from the resource arrays, since they are incremented and
397 * decremented at different points in time. Specifically, quota is only returned upon final
398 * resource deallocation (after all explicit and implicit references are released). Note
399 * that it is possible that calls to releaseResource() will not return the used quota if
400 * there are other resources that depend on (are parents of) the resource being released.
401 */
402 final ResourceTracker mSpiQuotaTracker = new ResourceTracker(MAX_NUM_SPIS);
403 final ResourceTracker mTransformQuotaTracker = new ResourceTracker(MAX_NUM_TRANSFORMS);
Benedict Wong344bd622017-11-16 15:27:22 -0800404 final ResourceTracker mSocketQuotaTracker = new ResourceTracker(MAX_NUM_ENCAP_SOCKETS);
Benedict Wong8149f6e2018-01-18 18:31:45 -0800405 final ResourceTracker mTunnelQuotaTracker = new ResourceTracker(MAX_NUM_TUNNEL_INTERFACES);
Benedict Wong344bd622017-11-16 15:27:22 -0800406
407 void removeSpiRecord(int resourceId) {
408 mSpiRecords.remove(resourceId);
Nathan Harolda1afbd82017-04-24 16:16:34 -0700409 }
410
Benedict Wong344bd622017-11-16 15:27:22 -0800411 void removeTransformRecord(int resourceId) {
412 mTransformRecords.remove(resourceId);
413 }
414
Benedict Wong8149f6e2018-01-18 18:31:45 -0800415 void removeTunnelInterfaceRecord(int resourceId) {
416 mTunnelInterfaceRecords.remove(resourceId);
417 }
418
Benedict Wong344bd622017-11-16 15:27:22 -0800419 void removeEncapSocketRecord(int resourceId) {
420 mEncapSocketRecords.remove(resourceId);
421 }
422
423 @Override
424 public String toString() {
425 return new StringBuilder()
426 .append("{mSpiQuotaTracker=")
427 .append(mSpiQuotaTracker)
428 .append(", mTransformQuotaTracker=")
429 .append(mTransformQuotaTracker)
430 .append(", mSocketQuotaTracker=")
431 .append(mSocketQuotaTracker)
Benedict Wongb8ef5412018-01-24 15:31:39 -0800432 .append(", mTunnelQuotaTracker=")
433 .append(mTunnelQuotaTracker)
Benedict Wong344bd622017-11-16 15:27:22 -0800434 .append(", mSpiRecords=")
435 .append(mSpiRecords)
436 .append(", mTransformRecords=")
437 .append(mTransformRecords)
438 .append(", mEncapSocketRecords=")
439 .append(mEncapSocketRecords)
Benedict Wongb8ef5412018-01-24 15:31:39 -0800440 .append(", mTunnelInterfaceRecords=")
441 .append(mTunnelInterfaceRecords)
Benedict Wong344bd622017-11-16 15:27:22 -0800442 .append("}")
443 .toString();
444 }
445 }
446
Benedict Wong4f9fb812017-12-13 17:16:53 -0800447 /**
448 * This class is not thread-safe, and expects that that users of this class will ensure
449 * synchronization and thread safety by holding the IpSecService.this instance lock.
450 */
Benedict Wong344bd622017-11-16 15:27:22 -0800451 @VisibleForTesting
452 static final class UserResourceTracker {
Nathan Harolda1afbd82017-04-24 16:16:34 -0700453 private final SparseArray<UserRecord> mUserRecords = new SparseArray<>();
454
Benedict Wong4f9fb812017-12-13 17:16:53 -0800455 /** Lazy-initialization/getter that populates or retrieves the UserRecord as needed */
Benedict Wong344bd622017-11-16 15:27:22 -0800456 public UserRecord getUserRecord(int uid) {
457 checkCallerUid(uid);
458
Nathan Harolda1afbd82017-04-24 16:16:34 -0700459 UserRecord r = mUserRecords.get(uid);
460 if (r == null) {
461 r = new UserRecord();
462 mUserRecords.put(uid, r);
463 }
464 return r;
465 }
ludi3e5ea232017-08-10 15:44:40 -0700466
Benedict Wong344bd622017-11-16 15:27:22 -0800467 /** Safety method; guards against access of other user's UserRecords */
468 private void checkCallerUid(int uid) {
469 if (uid != Binder.getCallingUid()
470 && android.os.Process.SYSTEM_UID != Binder.getCallingUid()) {
471 throw new SecurityException("Attempted access of unowned resources");
472 }
473 }
474
ludi3e5ea232017-08-10 15:44:40 -0700475 @Override
476 public String toString() {
477 return mUserRecords.toString();
478 }
Nathan Harolda1afbd82017-04-24 16:16:34 -0700479 }
480
Benedict Wong344bd622017-11-16 15:27:22 -0800481 @VisibleForTesting final UserResourceTracker mUserResourceTracker = new UserResourceTracker();
Nathan Harolda1afbd82017-04-24 16:16:34 -0700482
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700483 /**
Benedict Wong4f9fb812017-12-13 17:16:53 -0800484 * The OwnedResourceRecord class provides a facility to cleanly and reliably track system
Benedict Wong344bd622017-11-16 15:27:22 -0800485 * resources. It relies on a provided resourceId that should uniquely identify the kernel
486 * resource. To use this class, the user should implement the invalidate() and
487 * freeUnderlyingResources() methods that are responsible for cleaning up IpSecService resource
Benedict Wong4f9fb812017-12-13 17:16:53 -0800488 * tracking arrays and kernel resources, respectively.
489 *
490 * <p>This class associates kernel resources with the UID that owns and controls them.
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700491 */
Benedict Wong4f9fb812017-12-13 17:16:53 -0800492 private abstract class OwnedResourceRecord implements IResource {
Nathan Harold93962f32017-03-07 13:23:36 -0800493 final int pid;
494 final int uid;
Benedict Wong344bd622017-11-16 15:27:22 -0800495 protected final int mResourceId;
Nathan Harold93962f32017-03-07 13:23:36 -0800496
Benedict Wong4f9fb812017-12-13 17:16:53 -0800497 OwnedResourceRecord(int resourceId) {
Nathan Harold93962f32017-03-07 13:23:36 -0800498 super();
Nathan Harolda1afbd82017-04-24 16:16:34 -0700499 if (resourceId == INVALID_RESOURCE_ID) {
500 throw new IllegalArgumentException("Resource ID must not be INVALID_RESOURCE_ID");
501 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700502 mResourceId = resourceId;
Nathan Harold93962f32017-03-07 13:23:36 -0800503 pid = Binder.getCallingPid();
504 uid = Binder.getCallingUid();
505
Nathan Harolda1afbd82017-04-24 16:16:34 -0700506 getResourceTracker().take();
Nathan Harold93962f32017-03-07 13:23:36 -0800507 }
508
Benedict Wong344bd622017-11-16 15:27:22 -0800509 @Override
510 public abstract void invalidate() throws RemoteException;
511
512 /** Convenience method; retrieves the user resource record for the stored UID. */
513 protected UserRecord getUserRecord() {
514 return mUserResourceTracker.getUserRecord(uid);
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700515 }
516
Benedict Wong344bd622017-11-16 15:27:22 -0800517 @Override
518 public abstract void freeUnderlyingResources() throws RemoteException;
ludib0c95b12017-05-22 10:52:23 -0700519
Nathan Harolda1afbd82017-04-24 16:16:34 -0700520 /** Get the resource tracker for this resource */
521 protected abstract ResourceTracker getResourceTracker();
522
ludib0c95b12017-05-22 10:52:23 -0700523 @Override
524 public String toString() {
525 return new StringBuilder()
526 .append("{mResourceId=")
527 .append(mResourceId)
528 .append(", pid=")
529 .append(pid)
530 .append(", uid=")
531 .append(uid)
ludib0c95b12017-05-22 10:52:23 -0700532 .append("}")
533 .toString();
534 }
Nathan Harold93962f32017-03-07 13:23:36 -0800535 };
536
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700537 /**
Benedict Wong344bd622017-11-16 15:27:22 -0800538 * Thin wrapper over SparseArray to ensure resources exist, and simplify generic typing.
539 *
540 * <p>RefcountedResourceArray prevents null insertions, and throws an IllegalArgumentException
541 * if a key is not found during a retrieval process.
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700542 */
Benedict Wong344bd622017-11-16 15:27:22 -0800543 static class RefcountedResourceArray<T extends IResource> {
544 SparseArray<RefcountedResource<T>> mArray = new SparseArray<>();
545 private final String mTypeName;
Nathan Harold93962f32017-03-07 13:23:36 -0800546
Benedict Wong344bd622017-11-16 15:27:22 -0800547 public RefcountedResourceArray(String typeName) {
548 this.mTypeName = typeName;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700549 }
550
Benedict Wong344bd622017-11-16 15:27:22 -0800551 /**
552 * Accessor method to get inner resource object.
553 *
554 * @throws IllegalArgumentException if no resource with provided key is found.
555 */
556 T getResourceOrThrow(int key) {
557 return getRefcountedResourceOrThrow(key).getResource();
558 }
559
560 /**
561 * Accessor method to get reference counting wrapper.
562 *
563 * @throws IllegalArgumentException if no resource with provided key is found.
564 */
565 RefcountedResource<T> getRefcountedResourceOrThrow(int key) {
566 RefcountedResource<T> resource = mArray.get(key);
567 if (resource == null) {
568 throw new IllegalArgumentException(
569 String.format("No such %s found for given id: %d", mTypeName, key));
570 }
571
572 return resource;
573 }
574
575 void put(int key, RefcountedResource<T> obj) {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +0000576 Objects.requireNonNull(obj, "Null resources cannot be added");
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700577 mArray.put(key, obj);
578 }
579
580 void remove(int key) {
581 mArray.remove(key);
582 }
ludib0c95b12017-05-22 10:52:23 -0700583
584 @Override
585 public String toString() {
586 return mArray.toString();
587 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700588 }
589
Benedict Wong4f9fb812017-12-13 17:16:53 -0800590 /**
591 * Tracks an SA in the kernel, and manages cleanup paths. Once a TransformRecord is
592 * created, the SpiRecord that originally tracked the SAs will reliquish the
593 * responsibility of freeing the underlying SA to this class via the mOwnedByTransform flag.
594 */
595 private final class TransformRecord extends OwnedResourceRecord {
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700596 private final IpSecConfig mConfig;
Nathan Harolda2523312018-01-05 19:25:13 -0800597 private final SpiRecord mSpi;
Benedict Wong344bd622017-11-16 15:27:22 -0800598 private final EncapSocketRecord mSocket;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700599
600 TransformRecord(
Nathan Harolda2523312018-01-05 19:25:13 -0800601 int resourceId, IpSecConfig config, SpiRecord spi, EncapSocketRecord socket) {
Benedict Wong344bd622017-11-16 15:27:22 -0800602 super(resourceId);
Nathan Harold93962f32017-03-07 13:23:36 -0800603 mConfig = config;
Nathan Harolda2523312018-01-05 19:25:13 -0800604 mSpi = spi;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700605 mSocket = socket;
Benedict Wonge6b42772017-12-13 18:26:40 -0800606
607 spi.setOwnedByTransform();
Nathan Harold93962f32017-03-07 13:23:36 -0800608 }
609
610 public IpSecConfig getConfig() {
611 return mConfig;
612 }
613
Nathan Harolda2523312018-01-05 19:25:13 -0800614 public SpiRecord getSpiRecord() {
615 return mSpi;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700616 }
617
Benedict Wong0fe58a92018-01-19 17:36:02 -0800618 public EncapSocketRecord getSocketRecord() {
619 return mSocket;
620 }
621
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700622 /** always guarded by IpSecService#this */
Nathan Harold93962f32017-03-07 13:23:36 -0800623 @Override
Benedict Wong344bd622017-11-16 15:27:22 -0800624 public void freeUnderlyingResources() {
Nathan Harolda2523312018-01-05 19:25:13 -0800625 int spi = mSpi.getSpi();
626 try {
627 mSrvConfig
628 .getNetdInstance()
629 .ipSecDeleteSecurityAssociation(
Benedict Wonga0989622018-07-25 13:06:29 -0700630 uid,
Nathan Harolda2523312018-01-05 19:25:13 -0800631 mConfig.getSourceAddress(),
632 mConfig.getDestinationAddress(),
Di Lu0b611f42018-01-11 11:35:25 -0800633 spi,
634 mConfig.getMarkValue(),
Benedict Wong781dae62018-09-06 11:31:25 -0700635 mConfig.getMarkMask(),
636 mConfig.getXfrmInterfaceId());
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800637 } catch (RemoteException | ServiceSpecificException e) {
638 Log.e(TAG, "Failed to delete SA with ID: " + mResourceId, e);
Nathan Harold93962f32017-03-07 13:23:36 -0800639 }
Nathan Harold93962f32017-03-07 13:23:36 -0800640
Benedict Wong344bd622017-11-16 15:27:22 -0800641 getResourceTracker().give();
Nathan Harold93962f32017-03-07 13:23:36 -0800642 }
ludib0c95b12017-05-22 10:52:23 -0700643
Benedict Wong344bd622017-11-16 15:27:22 -0800644 @Override
645 public void invalidate() throws RemoteException {
646 getUserRecord().removeTransformRecord(mResourceId);
647 }
648
649 @Override
Nathan Harolda1afbd82017-04-24 16:16:34 -0700650 protected ResourceTracker getResourceTracker() {
Benedict Wong344bd622017-11-16 15:27:22 -0800651 return getUserRecord().mTransformQuotaTracker;
Nathan Harolda1afbd82017-04-24 16:16:34 -0700652 }
653
ludib0c95b12017-05-22 10:52:23 -0700654 @Override
655 public String toString() {
656 StringBuilder strBuilder = new StringBuilder();
657 strBuilder
658 .append("{super=")
659 .append(super.toString())
660 .append(", mSocket=")
661 .append(mSocket)
Nathan Harolda2523312018-01-05 19:25:13 -0800662 .append(", mSpi.mResourceId=")
663 .append(mSpi.mResourceId)
ludib0c95b12017-05-22 10:52:23 -0700664 .append(", mConfig=")
665 .append(mConfig)
666 .append("}");
667 return strBuilder.toString();
668 }
Nathan Harold93962f32017-03-07 13:23:36 -0800669 }
670
Benedict Wong4f9fb812017-12-13 17:16:53 -0800671 /**
672 * Tracks a single SA in the kernel, and manages cleanup paths. Once used in a Transform, the
673 * responsibility for cleaning up underlying resources will be passed to the TransformRecord
674 * object
675 */
676 private final class SpiRecord extends OwnedResourceRecord {
Nathan Harolda2523312018-01-05 19:25:13 -0800677 private final String mSourceAddress;
678 private final String mDestinationAddress;
Nathan Harold93962f32017-03-07 13:23:36 -0800679 private int mSpi;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700680
681 private boolean mOwnedByTransform = false;
Nathan Harold93962f32017-03-07 13:23:36 -0800682
Nathan Harolda2523312018-01-05 19:25:13 -0800683 SpiRecord(int resourceId, String sourceAddress, String destinationAddress, int spi) {
Benedict Wong344bd622017-11-16 15:27:22 -0800684 super(resourceId);
Nathan Harolda2523312018-01-05 19:25:13 -0800685 mSourceAddress = sourceAddress;
686 mDestinationAddress = destinationAddress;
Nathan Harold93962f32017-03-07 13:23:36 -0800687 mSpi = spi;
Nathan Harold93962f32017-03-07 13:23:36 -0800688 }
689
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700690 /** always guarded by IpSecService#this */
691 @Override
Benedict Wong344bd622017-11-16 15:27:22 -0800692 public void freeUnderlyingResources() {
Nathan Harold93962f32017-03-07 13:23:36 -0800693 try {
Nathan Harold22795302018-02-27 19:19:40 -0800694 if (!mOwnedByTransform) {
695 mSrvConfig
696 .getNetdInstance()
697 .ipSecDeleteSecurityAssociation(
Benedict Wong781dae62018-09-06 11:31:25 -0700698 uid, mSourceAddress, mDestinationAddress, mSpi, 0 /* mark */,
699 0 /* mask */, 0 /* if_id */);
Nathan Harold22795302018-02-27 19:19:40 -0800700 }
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800701 } catch (ServiceSpecificException | RemoteException e) {
702 Log.e(TAG, "Failed to delete SPI reservation with ID: " + mResourceId, e);
Nathan Harold93962f32017-03-07 13:23:36 -0800703 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700704
705 mSpi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold93962f32017-03-07 13:23:36 -0800706
Benedict Wong344bd622017-11-16 15:27:22 -0800707 getResourceTracker().give();
Nathan Harolda1afbd82017-04-24 16:16:34 -0700708 }
709
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700710 public int getSpi() {
711 return mSpi;
712 }
713
Nathan Harolda2523312018-01-05 19:25:13 -0800714 public String getDestinationAddress() {
715 return mDestinationAddress;
716 }
717
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700718 public void setOwnedByTransform() {
719 if (mOwnedByTransform) {
720 // Programming error
Andreas Gamped6d8e452017-07-11 10:25:09 -0700721 throw new IllegalStateException("Cannot own an SPI twice!");
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700722 }
723
724 mOwnedByTransform = true;
Nathan Harold93962f32017-03-07 13:23:36 -0800725 }
ludib0c95b12017-05-22 10:52:23 -0700726
Benedict Wonge6b42772017-12-13 18:26:40 -0800727 public boolean getOwnedByTransform() {
728 return mOwnedByTransform;
729 }
730
ludib0c95b12017-05-22 10:52:23 -0700731 @Override
Benedict Wong344bd622017-11-16 15:27:22 -0800732 public void invalidate() throws RemoteException {
733 getUserRecord().removeSpiRecord(mResourceId);
734 }
735
736 @Override
737 protected ResourceTracker getResourceTracker() {
738 return getUserRecord().mSpiQuotaTracker;
739 }
740
741 @Override
ludib0c95b12017-05-22 10:52:23 -0700742 public String toString() {
743 StringBuilder strBuilder = new StringBuilder();
744 strBuilder
745 .append("{super=")
746 .append(super.toString())
747 .append(", mSpi=")
748 .append(mSpi)
Nathan Harolda2523312018-01-05 19:25:13 -0800749 .append(", mSourceAddress=")
750 .append(mSourceAddress)
751 .append(", mDestinationAddress=")
752 .append(mDestinationAddress)
ludib0c95b12017-05-22 10:52:23 -0700753 .append(", mOwnedByTransform=")
754 .append(mOwnedByTransform)
755 .append("}");
756 return strBuilder.toString();
757 }
Nathan Harold93962f32017-03-07 13:23:36 -0800758 }
759
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +0900760 // These values have been reserved in NetIdManager
Benedict Wong8149f6e2018-01-18 18:31:45 -0800761 @VisibleForTesting static final int TUN_INTF_NETID_START = 0xFC00;
762
Remi NGUYEN VAN95dc87e2019-06-13 16:12:02 +0900763 public static final int TUN_INTF_NETID_RANGE = 0x0400;
Benedict Wong8149f6e2018-01-18 18:31:45 -0800764
765 private final SparseBooleanArray mTunnelNetIds = new SparseBooleanArray();
766 private int mNextTunnelNetIdIndex = 0;
767
768 /**
769 * Reserves a netId within the range of netIds allocated for IPsec tunnel interfaces
770 *
771 * <p>This method should only be called from Binder threads. Do not call this from within the
772 * system server as it will crash the system on failure.
773 *
774 * @return an integer key within the netId range, if successful
775 * @throws IllegalStateException if unsuccessful (all netId are currently reserved)
776 */
777 @VisibleForTesting
778 int reserveNetId() {
779 synchronized (mTunnelNetIds) {
780 for (int i = 0; i < TUN_INTF_NETID_RANGE; i++) {
781 int index = mNextTunnelNetIdIndex;
782 int netId = index + TUN_INTF_NETID_START;
783 if (++mNextTunnelNetIdIndex >= TUN_INTF_NETID_RANGE) mNextTunnelNetIdIndex = 0;
784 if (!mTunnelNetIds.get(netId)) {
785 mTunnelNetIds.put(netId, true);
786 return netId;
787 }
788 }
789 }
790 throw new IllegalStateException("No free netIds to allocate");
791 }
792
793 @VisibleForTesting
794 void releaseNetId(int netId) {
795 synchronized (mTunnelNetIds) {
796 mTunnelNetIds.delete(netId);
797 }
798 }
799
800 private final class TunnelInterfaceRecord extends OwnedResourceRecord {
801 private final String mInterfaceName;
802 private final Network mUnderlyingNetwork;
803
804 // outer addresses
805 private final String mLocalAddress;
806 private final String mRemoteAddress;
807
808 private final int mIkey;
809 private final int mOkey;
810
Benedict Wong781dae62018-09-06 11:31:25 -0700811 private final int mIfId;
812
Benedict Wong8149f6e2018-01-18 18:31:45 -0800813 TunnelInterfaceRecord(
814 int resourceId,
815 String interfaceName,
816 Network underlyingNetwork,
817 String localAddr,
818 String remoteAddr,
819 int ikey,
Benedict Wong781dae62018-09-06 11:31:25 -0700820 int okey,
821 int intfId) {
Benedict Wong8149f6e2018-01-18 18:31:45 -0800822 super(resourceId);
823
824 mInterfaceName = interfaceName;
825 mUnderlyingNetwork = underlyingNetwork;
826 mLocalAddress = localAddr;
827 mRemoteAddress = remoteAddr;
828 mIkey = ikey;
829 mOkey = okey;
Benedict Wong781dae62018-09-06 11:31:25 -0700830 mIfId = intfId;
Benedict Wong8149f6e2018-01-18 18:31:45 -0800831 }
832
833 /** always guarded by IpSecService#this */
834 @Override
835 public void freeUnderlyingResources() {
Benedict Wong0fe58a92018-01-19 17:36:02 -0800836 // Calls to netd
Benedict Wong8149f6e2018-01-18 18:31:45 -0800837 // Teardown VTI
838 // Delete global policies
Benedict Wong0fe58a92018-01-19 17:36:02 -0800839 try {
Benedict Wong564e2aa2018-05-07 20:06:44 -0700840 final INetd netd = mSrvConfig.getNetdInstance();
Benedict Wong781dae62018-09-06 11:31:25 -0700841 netd.ipSecRemoveTunnelInterface(mInterfaceName);
Benedict Wong0fe58a92018-01-19 17:36:02 -0800842
Benedict Wong564e2aa2018-05-07 20:06:44 -0700843 for (int selAddrFamily : ADDRESS_FAMILIES) {
844 netd.ipSecDeleteSecurityPolicy(
Benedict Wonga0989622018-07-25 13:06:29 -0700845 uid,
Benedict Wong564e2aa2018-05-07 20:06:44 -0700846 selAddrFamily,
847 IpSecManager.DIRECTION_OUT,
848 mOkey,
Benedict Wong781dae62018-09-06 11:31:25 -0700849 0xffffffff,
850 mIfId);
Benedict Wong564e2aa2018-05-07 20:06:44 -0700851 netd.ipSecDeleteSecurityPolicy(
Benedict Wonga0989622018-07-25 13:06:29 -0700852 uid,
Benedict Wong564e2aa2018-05-07 20:06:44 -0700853 selAddrFamily,
854 IpSecManager.DIRECTION_IN,
855 mIkey,
Benedict Wong781dae62018-09-06 11:31:25 -0700856 0xffffffff,
857 mIfId);
Benedict Wong0fe58a92018-01-19 17:36:02 -0800858 }
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800859 } catch (ServiceSpecificException | RemoteException e) {
Benedict Wong0fe58a92018-01-19 17:36:02 -0800860 Log.e(
861 TAG,
862 "Failed to delete VTI with interface name: "
863 + mInterfaceName
864 + " and id: "
Benedict Wongecc9f7c2018-03-01 18:53:07 -0800865 + mResourceId, e);
Benedict Wong0fe58a92018-01-19 17:36:02 -0800866 }
Benedict Wong8149f6e2018-01-18 18:31:45 -0800867
868 getResourceTracker().give();
869 releaseNetId(mIkey);
870 releaseNetId(mOkey);
871 }
872
873 public String getInterfaceName() {
874 return mInterfaceName;
875 }
876
877 public Network getUnderlyingNetwork() {
878 return mUnderlyingNetwork;
879 }
880
881 /** Returns the local, outer address for the tunnelInterface */
882 public String getLocalAddress() {
883 return mLocalAddress;
884 }
885
886 /** Returns the remote, outer address for the tunnelInterface */
887 public String getRemoteAddress() {
888 return mRemoteAddress;
889 }
890
891 public int getIkey() {
892 return mIkey;
893 }
894
895 public int getOkey() {
896 return mOkey;
897 }
898
Benedict Wong781dae62018-09-06 11:31:25 -0700899 public int getIfId() {
900 return mIfId;
901 }
902
Benedict Wong8149f6e2018-01-18 18:31:45 -0800903 @Override
904 protected ResourceTracker getResourceTracker() {
905 return getUserRecord().mTunnelQuotaTracker;
906 }
907
908 @Override
909 public void invalidate() {
910 getUserRecord().removeTunnelInterfaceRecord(mResourceId);
911 }
912
913 @Override
914 public String toString() {
915 return new StringBuilder()
916 .append("{super=")
917 .append(super.toString())
918 .append(", mInterfaceName=")
919 .append(mInterfaceName)
920 .append(", mUnderlyingNetwork=")
921 .append(mUnderlyingNetwork)
922 .append(", mLocalAddress=")
923 .append(mLocalAddress)
924 .append(", mRemoteAddress=")
925 .append(mRemoteAddress)
926 .append(", mIkey=")
927 .append(mIkey)
928 .append(", mOkey=")
929 .append(mOkey)
930 .append("}")
931 .toString();
932 }
933 }
934
Benedict Wong4f9fb812017-12-13 17:16:53 -0800935 /**
936 * Tracks a UDP encap socket, and manages cleanup paths
937 *
938 * <p>While this class does not manage non-kernel resources, race conditions around socket
939 * binding require that the service creates the encap socket, binds it and applies the socket
940 * policy before handing it to a user.
941 */
942 private final class EncapSocketRecord extends OwnedResourceRecord {
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700943 private FileDescriptor mSocket;
944 private final int mPort;
Nathan Harold93962f32017-03-07 13:23:36 -0800945
Benedict Wong344bd622017-11-16 15:27:22 -0800946 EncapSocketRecord(int resourceId, FileDescriptor socket, int port) {
947 super(resourceId);
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700948 mSocket = socket;
949 mPort = port;
950 }
951
952 /** always guarded by IpSecService#this */
953 @Override
Benedict Wong344bd622017-11-16 15:27:22 -0800954 public void freeUnderlyingResources() {
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700955 Log.d(TAG, "Closing port " + mPort);
956 IoUtils.closeQuietly(mSocket);
957 mSocket = null;
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700958
Benedict Wong344bd622017-11-16 15:27:22 -0800959 getResourceTracker().give();
Nathan Harolda1afbd82017-04-24 16:16:34 -0700960 }
961
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700962 public int getPort() {
963 return mPort;
964 }
965
Benedict Wong4c987eb2018-03-27 16:55:48 -0700966 public FileDescriptor getFileDescriptor() {
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700967 return mSocket;
968 }
ludib0c95b12017-05-22 10:52:23 -0700969
970 @Override
Benedict Wong344bd622017-11-16 15:27:22 -0800971 protected ResourceTracker getResourceTracker() {
972 return getUserRecord().mSocketQuotaTracker;
973 }
974
975 @Override
976 public void invalidate() {
977 getUserRecord().removeEncapSocketRecord(mResourceId);
978 }
979
980 @Override
ludib0c95b12017-05-22 10:52:23 -0700981 public String toString() {
982 return new StringBuilder()
983 .append("{super=")
984 .append(super.toString())
985 .append(", mSocket=")
986 .append(mSocket)
987 .append(", mPort=")
988 .append(mPort)
989 .append("}")
990 .toString();
991 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -0700992 }
Nathan Harold93962f32017-03-07 13:23:36 -0800993
Nathan Harold1afbef42017-03-01 18:55:06 -0800994 /**
995 * Constructs a new IpSecService instance
996 *
997 * @param context Binder context for this service
998 */
Benedict Wongd78ad3d2020-03-25 05:50:51 +0000999 private IpSecService(Context context, INetworkManagementService networkManager) {
1000 this(context, networkManager, IpSecServiceConfiguration.GETSRVINSTANCE);
Nathan Harold1afbef42017-03-01 18:55:06 -08001001 }
1002
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001003 static IpSecService create(Context context, INetworkManagementService networkManager)
1004 throws InterruptedException {
1005 final IpSecService service = new IpSecService(context, networkManager);
Nathan Harold1afbef42017-03-01 18:55:06 -08001006 service.connectNativeNetdService();
1007 return service;
1008 }
1009
Nathan Harold21208ee2018-03-15 18:06:06 -07001010 @NonNull
1011 private AppOpsManager getAppOpsManager() {
1012 AppOpsManager appOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
1013 if(appOps == null) throw new RuntimeException("System Server couldn't get AppOps");
1014 return appOps;
1015 }
1016
ludi1a06aa72017-05-12 09:15:00 -07001017 /** @hide */
1018 @VisibleForTesting
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001019 public IpSecService(Context context, INetworkManagementService networkManager,
1020 IpSecServiceConfiguration config) {
Nathan Harolda2523312018-01-05 19:25:13 -08001021 this(
1022 context,
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001023 networkManager,
Nathan Harolda2523312018-01-05 19:25:13 -08001024 config,
1025 (fd, uid) -> {
1026 try {
1027 TrafficStats.setThreadStatsUid(uid);
1028 TrafficStats.tagFileDescriptor(fd);
1029 } finally {
1030 TrafficStats.clearThreadStatsUid();
1031 }
1032 });
Benedict Wongbabe5d72017-12-03 19:42:36 -08001033 }
1034
1035 /** @hide */
1036 @VisibleForTesting
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001037 public IpSecService(Context context, INetworkManagementService networkManager,
1038 IpSecServiceConfiguration config, UidFdTagger uidFdTagger) {
ludi1a06aa72017-05-12 09:15:00 -07001039 mContext = context;
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001040 mNetworkManager = Objects.requireNonNull(networkManager);
ludi1a06aa72017-05-12 09:15:00 -07001041 mSrvConfig = config;
Benedict Wongbabe5d72017-12-03 19:42:36 -08001042 mUidFdTagger = uidFdTagger;
ludi1a06aa72017-05-12 09:15:00 -07001043 }
1044
Nathan Harold1afbef42017-03-01 18:55:06 -08001045 public void systemReady() {
1046 if (isNetdAlive()) {
1047 Slog.d(TAG, "IpSecService is ready");
1048 } else {
1049 Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!");
1050 }
1051 }
1052
1053 private void connectNativeNetdService() {
1054 // Avoid blocking the system server to do this
Nathan Haroldb0e05082017-07-17 14:01:53 -07001055 new Thread() {
1056 @Override
1057 public void run() {
1058 synchronized (IpSecService.this) {
ludi1a06aa72017-05-12 09:15:00 -07001059 NetdService.get(NETD_FETCH_TIMEOUT_MS);
Nathan Haroldb0e05082017-07-17 14:01:53 -07001060 }
1061 }
1062 }.start();
Nathan Harold1afbef42017-03-01 18:55:06 -08001063 }
1064
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001065 synchronized boolean isNetdAlive() {
1066 try {
ludi1a06aa72017-05-12 09:15:00 -07001067 final INetd netd = mSrvConfig.getNetdInstance();
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001068 if (netd == null) {
Nathan Harold1afbef42017-03-01 18:55:06 -08001069 return false;
1070 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001071 return netd.isAlive();
1072 } catch (RemoteException re) {
1073 return false;
Nathan Harold1afbef42017-03-01 18:55:06 -08001074 }
1075 }
1076
Nathan Harolda10003d2017-08-23 13:46:33 -07001077 /**
1078 * Checks that the provided InetAddress is valid for use in an IPsec SA. The address must not be
1079 * a wildcard address and must be in a numeric form such as 1.2.3.4 or 2001::1.
1080 */
1081 private static void checkInetAddress(String inetAddress) {
1082 if (TextUtils.isEmpty(inetAddress)) {
1083 throw new IllegalArgumentException("Unspecified address");
1084 }
1085
1086 InetAddress checkAddr = NetworkUtils.numericToInetAddress(inetAddress);
1087
1088 if (checkAddr.isAnyLocalAddress()) {
1089 throw new IllegalArgumentException("Inappropriate wildcard address: " + inetAddress);
1090 }
1091 }
1092
1093 /**
1094 * Checks the user-provided direction field and throws an IllegalArgumentException if it is not
1095 * DIRECTION_IN or DIRECTION_OUT
1096 */
1097 private static void checkDirection(int direction) {
1098 switch (direction) {
Nathan Harolda2523312018-01-05 19:25:13 -08001099 case IpSecManager.DIRECTION_OUT:
1100 case IpSecManager.DIRECTION_IN:
Nathan Harolda10003d2017-08-23 13:46:33 -07001101 return;
1102 }
1103 throw new IllegalArgumentException("Invalid Direction: " + direction);
1104 }
1105
Nathan Harold93962f32017-03-07 13:23:36 -08001106 /** Get a new SPI and maintain the reservation in the system server */
Jonathan Basseri5fb92902017-11-16 10:58:01 -08001107 @Override
1108 public synchronized IpSecSpiResponse allocateSecurityParameterIndex(
Nathan Harolda2523312018-01-05 19:25:13 -08001109 String destinationAddress, int requestedSpi, IBinder binder) throws RemoteException {
1110 checkInetAddress(destinationAddress);
Nathan Harold7f606ee2018-03-28 08:52:51 -07001111 // RFC 4303 Section 2.1 - 0=local, 1-255=reserved.
1112 if (requestedSpi > 0 && requestedSpi < 256) {
1113 throw new IllegalArgumentException("ESP SPI must not be in the range of 0-255.");
1114 }
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001115 Objects.requireNonNull(binder, "Null Binder passed to allocateSecurityParameterIndex");
Nathan Harolda10003d2017-08-23 13:46:33 -07001116
Benedict Wonga0989622018-07-25 13:06:29 -07001117 int callingUid = Binder.getCallingUid();
1118 UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid);
Nathan Haroldd8c74292017-12-13 19:16:33 -08001119 final int resourceId = mNextResourceId++;
Nathan Harold93962f32017-03-07 13:23:36 -08001120
1121 int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX;
Nathan Harold93962f32017-03-07 13:23:36 -08001122 try {
Benedict Wong344bd622017-11-16 15:27:22 -08001123 if (!userRecord.mSpiQuotaTracker.isAvailable()) {
Nathan Harolda1afbd82017-04-24 16:16:34 -07001124 return new IpSecSpiResponse(
Nathan Harolda10003d2017-08-23 13:46:33 -07001125 IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi);
Nathan Harolda1afbd82017-04-24 16:16:34 -07001126 }
Nathan Harolda2523312018-01-05 19:25:13 -08001127
Nathan Harold93962f32017-03-07 13:23:36 -08001128 spi =
ludi1a06aa72017-05-12 09:15:00 -07001129 mSrvConfig
1130 .getNetdInstance()
Benedict Wonga0989622018-07-25 13:06:29 -07001131 .ipSecAllocateSpi(callingUid, "", destinationAddress, requestedSpi);
Nathan Harold93962f32017-03-07 13:23:36 -08001132 Log.d(TAG, "Allocated SPI " + spi);
Benedict Wong344bd622017-11-16 15:27:22 -08001133 userRecord.mSpiRecords.put(
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001134 resourceId,
Benedict Wong344bd622017-11-16 15:27:22 -08001135 new RefcountedResource<SpiRecord>(
Nathan Harolda2523312018-01-05 19:25:13 -08001136 new SpiRecord(resourceId, "", destinationAddress, spi), binder));
Nathan Harold93962f32017-03-07 13:23:36 -08001137 } catch (ServiceSpecificException e) {
Nathan Harold144ce0a2018-04-03 16:13:19 -07001138 if (e.errorCode == OsConstants.ENOENT) {
1139 return new IpSecSpiResponse(
1140 IpSecManager.Status.SPI_UNAVAILABLE, INVALID_RESOURCE_ID, spi);
1141 }
1142 throw e;
Nathan Harold93962f32017-03-07 13:23:36 -08001143 } catch (RemoteException e) {
1144 throw e.rethrowFromSystemServer();
1145 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001146 return new IpSecSpiResponse(IpSecManager.Status.OK, resourceId, spi);
1147 }
1148
1149 /* This method should only be called from Binder threads. Do not call this from
1150 * within the system server as it will crash the system on failure.
1151 */
Benedict Wong344bd622017-11-16 15:27:22 -08001152 private void releaseResource(RefcountedResourceArray resArray, int resourceId)
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001153 throws RemoteException {
Benedict Wong344bd622017-11-16 15:27:22 -08001154 resArray.getRefcountedResourceOrThrow(resourceId).userRelease();
Nathan Harold93962f32017-03-07 13:23:36 -08001155 }
1156
1157 /** Release a previously allocated SPI that has been registered with the system server */
1158 @Override
Benedict Wong344bd622017-11-16 15:27:22 -08001159 public synchronized void releaseSecurityParameterIndex(int resourceId) throws RemoteException {
1160 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1161 releaseResource(userRecord.mSpiRecords, resourceId);
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001162 }
1163
1164 /**
1165 * This function finds and forcibly binds to a random system port, ensuring that the port cannot
1166 * be unbound.
1167 *
1168 * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select
1169 * a random open port and then bind by number, this function creates a temp socket, binds to a
1170 * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP
1171 * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned
1172 * FileHandle.
1173 *
1174 * <p>The loop in this function handles the inherent race window between un-binding to a port
1175 * and re-binding, during which the system could *technically* hand that port out to someone
1176 * else.
1177 */
Benedict Wongf186d672017-10-10 20:44:28 -07001178 private int bindToRandomPort(FileDescriptor sockFd) throws IOException {
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001179 for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) {
1180 try {
1181 FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
1182 Os.bind(probeSocket, INADDR_ANY, 0);
1183 int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort();
1184 Os.close(probeSocket);
1185 Log.v(TAG, "Binding to port " + port);
1186 Os.bind(sockFd, INADDR_ANY, port);
Benedict Wongf186d672017-10-10 20:44:28 -07001187 return port;
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001188 } catch (ErrnoException e) {
1189 // Someone miraculously claimed the port just after we closed probeSocket.
1190 if (e.errno == OsConstants.EADDRINUSE) {
1191 continue;
1192 }
1193 throw e.rethrowAsIOException();
1194 }
1195 }
1196 throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port");
1197 }
Nathan Harold93962f32017-03-07 13:23:36 -08001198
1199 /**
Benedict Wongbabe5d72017-12-03 19:42:36 -08001200 * Functional interface to do traffic tagging of given sockets to UIDs.
1201 *
1202 * <p>Specifically used by openUdpEncapsulationSocket to ensure data usage on the UDP encap
1203 * sockets are billed to the UID that the UDP encap socket was created on behalf of.
1204 *
1205 * <p>Separate class so that the socket tagging logic can be mocked; TrafficStats uses static
1206 * methods that cannot be easily mocked/tested.
1207 */
1208 @VisibleForTesting
1209 public interface UidFdTagger {
1210 /**
1211 * Sets socket tag to assign all traffic to the provided UID.
1212 *
1213 * <p>Since the socket is created on behalf of an unprivileged application, all traffic
1214 * should be accounted to the UID of the unprivileged application.
1215 */
1216 public void tag(FileDescriptor fd, int uid) throws IOException;
1217 }
1218
1219 /**
Nathan Harold93962f32017-03-07 13:23:36 -08001220 * Open a socket via the system server and bind it to the specified port (random if port=0).
1221 * This will return a PFD to the user that represent a bound UDP socket. The system server will
1222 * cache the socket and a record of its owner so that it can and must be freed when no longer
1223 * needed.
1224 */
1225 @Override
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001226 public synchronized IpSecUdpEncapResponse openUdpEncapsulationSocket(int port, IBinder binder)
1227 throws RemoteException {
1228 if (port != 0 && (port < FREE_PORT_MIN || port > PORT_MAX)) {
1229 throw new IllegalArgumentException(
1230 "Specified port number must be a valid non-reserved UDP port");
1231 }
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001232 Objects.requireNonNull(binder, "Null Binder passed to openUdpEncapsulationSocket");
Nathan Harolda10003d2017-08-23 13:46:33 -07001233
Benedict Wongbabe5d72017-12-03 19:42:36 -08001234 int callingUid = Binder.getCallingUid();
1235 UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid);
Nathan Haroldd8c74292017-12-13 19:16:33 -08001236 final int resourceId = mNextResourceId++;
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001237 FileDescriptor sockFd = null;
1238 try {
Benedict Wong344bd622017-11-16 15:27:22 -08001239 if (!userRecord.mSocketQuotaTracker.isAvailable()) {
Nathan Harolda1afbd82017-04-24 16:16:34 -07001240 return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE);
1241 }
1242
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001243 sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
Benedict Wongbabe5d72017-12-03 19:42:36 -08001244 mUidFdTagger.tag(sockFd, callingUid);
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001245
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001246 // This code is common to both the unspecified and specified port cases
1247 Os.setsockoptInt(
1248 sockFd,
1249 OsConstants.IPPROTO_UDP,
1250 OsConstants.UDP_ENCAP,
1251 OsConstants.UDP_ENCAP_ESPINUDP);
1252
Luke Huange8e522b2018-11-23 12:01:41 +08001253 mSrvConfig.getNetdInstance().ipSecSetEncapSocketOwner(
1254 new ParcelFileDescriptor(sockFd), callingUid);
Benedict Wongba8d3132017-12-06 21:56:35 -08001255 if (port != 0) {
1256 Log.v(TAG, "Binding to port " + port);
1257 Os.bind(sockFd, INADDR_ANY, port);
1258 } else {
1259 port = bindToRandomPort(sockFd);
1260 }
1261
Benedict Wong344bd622017-11-16 15:27:22 -08001262 userRecord.mEncapSocketRecords.put(
1263 resourceId,
1264 new RefcountedResource<EncapSocketRecord>(
1265 new EncapSocketRecord(resourceId, sockFd, port), binder));
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001266 return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd);
1267 } catch (IOException | ErrnoException e) {
1268 IoUtils.closeQuietly(sockFd);
1269 }
1270 // If we make it to here, then something has gone wrong and we couldn't open a socket.
1271 // The only reasonable condition that would cause that is resource unavailable.
1272 return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE);
Nathan Harold93962f32017-03-07 13:23:36 -08001273 }
1274
1275 /** close a socket that has been been allocated by and registered with the system server */
1276 @Override
Benedict Wong344bd622017-11-16 15:27:22 -08001277 public synchronized void closeUdpEncapsulationSocket(int resourceId) throws RemoteException {
1278 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1279 releaseResource(userRecord.mEncapSocketRecords, resourceId);
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001280 }
Nathan Harold93962f32017-03-07 13:23:36 -08001281
Benedict Wong8149f6e2018-01-18 18:31:45 -08001282 /**
1283 * Create a tunnel interface for use in IPSec tunnel mode. The system server will cache the
1284 * tunnel interface and a record of its owner so that it can and must be freed when no longer
1285 * needed.
1286 */
1287 @Override
1288 public synchronized IpSecTunnelInterfaceResponse createTunnelInterface(
Nathan Harold21208ee2018-03-15 18:06:06 -07001289 String localAddr, String remoteAddr, Network underlyingNetwork, IBinder binder,
1290 String callingPackage) {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001291 enforceTunnelFeatureAndPermissions(callingPackage);
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001292 Objects.requireNonNull(binder, "Null Binder passed to createTunnelInterface");
1293 Objects.requireNonNull(underlyingNetwork, "No underlying network was specified");
Benedict Wong8149f6e2018-01-18 18:31:45 -08001294 checkInetAddress(localAddr);
1295 checkInetAddress(remoteAddr);
1296
1297 // TODO: Check that underlying network exists, and IP addresses not assigned to a different
1298 // network (b/72316676).
1299
Benedict Wonga0989622018-07-25 13:06:29 -07001300 int callerUid = Binder.getCallingUid();
1301 UserRecord userRecord = mUserResourceTracker.getUserRecord(callerUid);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001302 if (!userRecord.mTunnelQuotaTracker.isAvailable()) {
1303 return new IpSecTunnelInterfaceResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE);
1304 }
1305
1306 final int resourceId = mNextResourceId++;
1307 final int ikey = reserveNetId();
1308 final int okey = reserveNetId();
Nathan Harold4fd7c322018-04-26 11:47:14 -07001309 String intfName = String.format("%s%d", INetd.IPSEC_INTERFACE_PREFIX, resourceId);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001310
Benedict Wong0fe58a92018-01-19 17:36:02 -08001311 try {
1312 // Calls to netd:
1313 // Create VTI
1314 // Add inbound/outbound global policies
1315 // (use reqid = 0)
Benedict Wong564e2aa2018-05-07 20:06:44 -07001316 final INetd netd = mSrvConfig.getNetdInstance();
Benedict Wong781dae62018-09-06 11:31:25 -07001317 netd.ipSecAddTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey, resourceId);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001318
Benedict Wongd78ad3d2020-03-25 05:50:51 +00001319 Binder.withCleanCallingIdentity(() -> {
1320 mNetworkManager.setInterfaceUp(intfName);
1321 });
1322
Benedict Wong564e2aa2018-05-07 20:06:44 -07001323 for (int selAddrFamily : ADDRESS_FAMILIES) {
1324 // Always send down correct local/remote addresses for template.
1325 netd.ipSecAddSecurityPolicy(
Benedict Wonga0989622018-07-25 13:06:29 -07001326 callerUid,
Benedict Wong564e2aa2018-05-07 20:06:44 -07001327 selAddrFamily,
1328 IpSecManager.DIRECTION_OUT,
1329 localAddr,
1330 remoteAddr,
1331 0,
1332 okey,
Benedict Wong781dae62018-09-06 11:31:25 -07001333 0xffffffff,
1334 resourceId);
Benedict Wong564e2aa2018-05-07 20:06:44 -07001335 netd.ipSecAddSecurityPolicy(
Benedict Wonga0989622018-07-25 13:06:29 -07001336 callerUid,
Benedict Wong564e2aa2018-05-07 20:06:44 -07001337 selAddrFamily,
1338 IpSecManager.DIRECTION_IN,
1339 remoteAddr,
1340 localAddr,
1341 0,
1342 ikey,
Benedict Wong781dae62018-09-06 11:31:25 -07001343 0xffffffff,
1344 resourceId);
Benedict Wong0fe58a92018-01-19 17:36:02 -08001345 }
1346
1347 userRecord.mTunnelInterfaceRecords.put(
1348 resourceId,
1349 new RefcountedResource<TunnelInterfaceRecord>(
1350 new TunnelInterfaceRecord(
1351 resourceId,
1352 intfName,
1353 underlyingNetwork,
1354 localAddr,
1355 remoteAddr,
1356 ikey,
Benedict Wong781dae62018-09-06 11:31:25 -07001357 okey,
1358 resourceId),
Benedict Wong0fe58a92018-01-19 17:36:02 -08001359 binder));
1360 return new IpSecTunnelInterfaceResponse(IpSecManager.Status.OK, resourceId, intfName);
1361 } catch (RemoteException e) {
1362 // Release keys if we got an error.
1363 releaseNetId(ikey);
1364 releaseNetId(okey);
1365 throw e.rethrowFromSystemServer();
Nathan Harold144ce0a2018-04-03 16:13:19 -07001366 } catch (Throwable t) {
1367 // Release keys if we got an error.
1368 releaseNetId(ikey);
1369 releaseNetId(okey);
1370 throw t;
Benedict Wong0fe58a92018-01-19 17:36:02 -08001371 }
Benedict Wong8149f6e2018-01-18 18:31:45 -08001372 }
1373
1374 /**
1375 * Adds a new local address to the tunnel interface. This allows packets to be sent and received
1376 * from multiple local IP addresses over the same tunnel.
1377 */
1378 @Override
Benedict Wongecc9f7c2018-03-01 18:53:07 -08001379 public synchronized void addAddressToTunnelInterface(
Nathan Harold21208ee2018-03-15 18:06:06 -07001380 int tunnelResourceId, LinkAddress localAddr, String callingPackage) {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001381 enforceTunnelFeatureAndPermissions(callingPackage);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001382 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1383
1384 // Get tunnelInterface record; if no such interface is found, will throw
1385 // IllegalArgumentException
1386 TunnelInterfaceRecord tunnelInterfaceInfo =
1387 userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId);
1388
Benedict Wongecc9f7c2018-03-01 18:53:07 -08001389 try {
1390 // We can assume general validity of the IP address, since we get them as a
1391 // LinkAddress, which does some validation.
1392 mSrvConfig
1393 .getNetdInstance()
1394 .interfaceAddAddress(
1395 tunnelInterfaceInfo.mInterfaceName,
1396 localAddr.getAddress().getHostAddress(),
1397 localAddr.getPrefixLength());
1398 } catch (RemoteException e) {
1399 throw e.rethrowFromSystemServer();
Benedict Wongecc9f7c2018-03-01 18:53:07 -08001400 }
Benedict Wong8149f6e2018-01-18 18:31:45 -08001401 }
1402
1403 /**
1404 * Remove a new local address from the tunnel interface. After removal, the address will no
1405 * longer be available to send from, or receive on.
1406 */
1407 @Override
1408 public synchronized void removeAddressFromTunnelInterface(
Nathan Harold21208ee2018-03-15 18:06:06 -07001409 int tunnelResourceId, LinkAddress localAddr, String callingPackage) {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001410 enforceTunnelFeatureAndPermissions(callingPackage);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001411
Nathan Harold21208ee2018-03-15 18:06:06 -07001412 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
Benedict Wong8149f6e2018-01-18 18:31:45 -08001413 // Get tunnelInterface record; if no such interface is found, will throw
1414 // IllegalArgumentException
1415 TunnelInterfaceRecord tunnelInterfaceInfo =
1416 userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId);
1417
Benedict Wongecc9f7c2018-03-01 18:53:07 -08001418 try {
1419 // We can assume general validity of the IP address, since we get them as a
1420 // LinkAddress, which does some validation.
1421 mSrvConfig
1422 .getNetdInstance()
1423 .interfaceDelAddress(
1424 tunnelInterfaceInfo.mInterfaceName,
1425 localAddr.getAddress().getHostAddress(),
1426 localAddr.getPrefixLength());
1427 } catch (RemoteException e) {
1428 throw e.rethrowFromSystemServer();
Benedict Wongecc9f7c2018-03-01 18:53:07 -08001429 }
Benedict Wong8149f6e2018-01-18 18:31:45 -08001430 }
1431
1432 /**
1433 * Delete a TunnelInterface that has been been allocated by and registered with the system
1434 * server
1435 */
1436 @Override
Nathan Harold21208ee2018-03-15 18:06:06 -07001437 public synchronized void deleteTunnelInterface(
1438 int resourceId, String callingPackage) throws RemoteException {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001439 enforceTunnelFeatureAndPermissions(callingPackage);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001440 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1441 releaseResource(userRecord.mTunnelInterfaceRecords, resourceId);
1442 }
1443
Benedict Wong4f255702017-11-06 20:49:10 -08001444 @VisibleForTesting
Nathan Harolda2523312018-01-05 19:25:13 -08001445 void validateAlgorithms(IpSecConfig config) throws IllegalArgumentException {
1446 IpSecAlgorithm auth = config.getAuthentication();
1447 IpSecAlgorithm crypt = config.getEncryption();
1448 IpSecAlgorithm aead = config.getAuthenticatedEncryption();
Benedict Wong4f255702017-11-06 20:49:10 -08001449
Nathan Harolda2523312018-01-05 19:25:13 -08001450 // Validate the algorithm set
1451 Preconditions.checkArgument(
1452 aead != null || crypt != null || auth != null,
1453 "No Encryption or Authentication algorithms specified");
1454 Preconditions.checkArgument(
1455 auth == null || auth.isAuthentication(),
1456 "Unsupported algorithm for Authentication");
1457 Preconditions.checkArgument(
Benedict Wong4f255702017-11-06 20:49:10 -08001458 crypt == null || crypt.isEncryption(), "Unsupported algorithm for Encryption");
Nathan Harolda2523312018-01-05 19:25:13 -08001459 Preconditions.checkArgument(
1460 aead == null || aead.isAead(),
1461 "Unsupported algorithm for Authenticated Encryption");
1462 Preconditions.checkArgument(
1463 aead == null || (auth == null && crypt == null),
1464 "Authenticated Encryption is mutually exclusive with other Authentication "
1465 + "or Encryption algorithms");
Benedict Wong4f255702017-11-06 20:49:10 -08001466 }
1467
evitayane4259d32018-03-22 17:53:08 -07001468 private int getFamily(String inetAddress) {
1469 int family = AF_UNSPEC;
1470 InetAddress checkAddress = NetworkUtils.numericToInetAddress(inetAddress);
1471 if (checkAddress instanceof Inet4Address) {
1472 family = AF_INET;
1473 } else if (checkAddress instanceof Inet6Address) {
1474 family = AF_INET6;
1475 }
1476 return family;
1477 }
1478
Nathan Harold93962f32017-03-07 13:23:36 -08001479 /**
Nathan Harolda10003d2017-08-23 13:46:33 -07001480 * Checks an IpSecConfig parcel to ensure that the contents are sane and throws an
1481 * IllegalArgumentException if they are not.
1482 */
1483 private void checkIpSecConfig(IpSecConfig config) {
Benedict Wong344bd622017-11-16 15:27:22 -08001484 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1485
Nathan Harolda10003d2017-08-23 13:46:33 -07001486 switch (config.getEncapType()) {
1487 case IpSecTransform.ENCAP_NONE:
1488 break;
1489 case IpSecTransform.ENCAP_ESPINUDP:
1490 case IpSecTransform.ENCAP_ESPINUDP_NON_IKE:
Benedict Wong344bd622017-11-16 15:27:22 -08001491 // Retrieve encap socket record; will throw IllegalArgumentException if not found
1492 userRecord.mEncapSocketRecords.getResourceOrThrow(
1493 config.getEncapSocketResourceId());
Nathan Harolda10003d2017-08-23 13:46:33 -07001494
1495 int port = config.getEncapRemotePort();
1496 if (port <= 0 || port > 0xFFFF) {
1497 throw new IllegalArgumentException("Invalid remote UDP port: " + port);
1498 }
1499 break;
1500 default:
1501 throw new IllegalArgumentException("Invalid Encap Type: " + config.getEncapType());
1502 }
1503
Nathan Harolda2523312018-01-05 19:25:13 -08001504 validateAlgorithms(config);
Nathan Harolda10003d2017-08-23 13:46:33 -07001505
Nathan Harolda2523312018-01-05 19:25:13 -08001506 // Retrieve SPI record; will throw IllegalArgumentException if not found
1507 SpiRecord s = userRecord.mSpiRecords.getResourceOrThrow(config.getSpiResourceId());
1508
Benedict Wonge6b42772017-12-13 18:26:40 -08001509 // Check to ensure that SPI has not already been used.
1510 if (s.getOwnedByTransform()) {
1511 throw new IllegalStateException("SPI already in use; cannot be used in new Transforms");
1512 }
1513
Nathan Harolda2523312018-01-05 19:25:13 -08001514 // If no remote address is supplied, then use one from the SPI.
1515 if (TextUtils.isEmpty(config.getDestinationAddress())) {
1516 config.setDestinationAddress(s.getDestinationAddress());
1517 }
1518
1519 // All remote addresses must match
1520 if (!config.getDestinationAddress().equals(s.getDestinationAddress())) {
1521 throw new IllegalArgumentException("Mismatched remote addresseses.");
1522 }
1523
1524 // This check is technically redundant due to the chain of custody between the SPI and
1525 // the IpSecConfig, but in the future if the dest is allowed to be set explicitly in
1526 // the transform, this will prevent us from messing up.
1527 checkInetAddress(config.getDestinationAddress());
1528
1529 // Require a valid source address for all transforms.
1530 checkInetAddress(config.getSourceAddress());
1531
evitayane4259d32018-03-22 17:53:08 -07001532 // Check to ensure source and destination have the same address family.
1533 String sourceAddress = config.getSourceAddress();
1534 String destinationAddress = config.getDestinationAddress();
1535 int sourceFamily = getFamily(sourceAddress);
1536 int destinationFamily = getFamily(destinationAddress);
1537 if (sourceFamily != destinationFamily) {
1538 throw new IllegalArgumentException(
1539 "Source address ("
1540 + sourceAddress
1541 + ") and destination address ("
1542 + destinationAddress
1543 + ") have different address families.");
1544 }
1545
1546 // Throw an error if UDP Encapsulation is not used in IPv4.
1547 if (config.getEncapType() != IpSecTransform.ENCAP_NONE && sourceFamily != AF_INET) {
1548 throw new IllegalArgumentException(
1549 "UDP Encapsulation is not supported for this address family");
1550 }
1551
Nathan Harolda2523312018-01-05 19:25:13 -08001552 switch (config.getMode()) {
1553 case IpSecTransform.MODE_TRANSPORT:
Nathan Harold5a920ca2018-02-02 18:34:25 -08001554 break;
Nathan Harolda2523312018-01-05 19:25:13 -08001555 case IpSecTransform.MODE_TUNNEL:
1556 break;
1557 default:
1558 throw new IllegalArgumentException(
1559 "Invalid IpSecTransform.mode: " + config.getMode());
Nathan Harolda10003d2017-08-23 13:46:33 -07001560 }
Benedict Wongab80e1f2018-07-25 18:46:19 -07001561
1562 config.setMarkValue(0);
1563 config.setMarkMask(0);
Nathan Harolda10003d2017-08-23 13:46:33 -07001564 }
1565
Benedict Wongd8db9182018-09-13 16:45:12 -07001566 private static final String TUNNEL_OP = AppOpsManager.OPSTR_MANAGE_IPSEC_TUNNELS;
Nathan Harold8c69bcb2018-05-15 19:18:38 -07001567
Benedict Wonge265d5f2018-11-08 19:45:34 -08001568 private void enforceTunnelFeatureAndPermissions(String callingPackage) {
1569 if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_IPSEC_TUNNELS)) {
1570 throw new UnsupportedOperationException(
1571 "IPsec Tunnel Mode requires PackageManager.FEATURE_IPSEC_TUNNELS");
1572 }
1573
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001574 Objects.requireNonNull(callingPackage, "Null calling package cannot create IpSec tunnels");
Benedict Wong64e8db82019-11-12 22:31:51 -08001575
1576 // OP_MANAGE_IPSEC_TUNNELS will return MODE_ERRORED by default, including for the system
1577 // server. If the appop is not granted, require that the caller has the MANAGE_IPSEC_TUNNELS
1578 // permission or is the System Server.
1579 if (AppOpsManager.MODE_ALLOWED == getAppOpsManager().noteOpNoThrow(
1580 TUNNEL_OP, Binder.getCallingUid(), callingPackage)) {
1581 return;
Nathan Harold21208ee2018-03-15 18:06:06 -07001582 }
Benedict Wong64e8db82019-11-12 22:31:51 -08001583 mContext.enforceCallingOrSelfPermission(
1584 android.Manifest.permission.MANAGE_IPSEC_TUNNELS, "IpSecService");
Nathan Harold5a920ca2018-02-02 18:34:25 -08001585 }
1586
Benedict Wong0fe58a92018-01-19 17:36:02 -08001587 private void createOrUpdateTransform(
1588 IpSecConfig c, int resourceId, SpiRecord spiRecord, EncapSocketRecord socketRecord)
1589 throws RemoteException {
1590
1591 int encapType = c.getEncapType(), encapLocalPort = 0, encapRemotePort = 0;
1592 if (encapType != IpSecTransform.ENCAP_NONE) {
1593 encapLocalPort = socketRecord.getPort();
1594 encapRemotePort = c.getEncapRemotePort();
1595 }
1596
1597 IpSecAlgorithm auth = c.getAuthentication();
1598 IpSecAlgorithm crypt = c.getEncryption();
1599 IpSecAlgorithm authCrypt = c.getAuthenticatedEncryption();
1600
Benedict Wongbf013a32018-03-15 19:41:41 -07001601 String cryptName;
1602 if (crypt == null) {
1603 cryptName = (authCrypt == null) ? IpSecAlgorithm.CRYPT_NULL : "";
1604 } else {
1605 cryptName = crypt.getName();
1606 }
1607
Benedict Wong0fe58a92018-01-19 17:36:02 -08001608 mSrvConfig
1609 .getNetdInstance()
1610 .ipSecAddSecurityAssociation(
Benedict Wonga0989622018-07-25 13:06:29 -07001611 Binder.getCallingUid(),
Benedict Wong0fe58a92018-01-19 17:36:02 -08001612 c.getMode(),
1613 c.getSourceAddress(),
1614 c.getDestinationAddress(),
1615 (c.getNetwork() != null) ? c.getNetwork().netId : 0,
1616 spiRecord.getSpi(),
1617 c.getMarkValue(),
1618 c.getMarkMask(),
1619 (auth != null) ? auth.getName() : "",
1620 (auth != null) ? auth.getKey() : new byte[] {},
1621 (auth != null) ? auth.getTruncationLengthBits() : 0,
Benedict Wongbf013a32018-03-15 19:41:41 -07001622 cryptName,
Benedict Wong0fe58a92018-01-19 17:36:02 -08001623 (crypt != null) ? crypt.getKey() : new byte[] {},
1624 (crypt != null) ? crypt.getTruncationLengthBits() : 0,
1625 (authCrypt != null) ? authCrypt.getName() : "",
1626 (authCrypt != null) ? authCrypt.getKey() : new byte[] {},
1627 (authCrypt != null) ? authCrypt.getTruncationLengthBits() : 0,
1628 encapType,
1629 encapLocalPort,
Benedict Wong781dae62018-09-06 11:31:25 -07001630 encapRemotePort,
1631 c.getXfrmInterfaceId());
Benedict Wong0fe58a92018-01-19 17:36:02 -08001632 }
1633
Nathan Harolda10003d2017-08-23 13:46:33 -07001634 /**
Benedict Wong0fe58a92018-01-19 17:36:02 -08001635 * Create a IPsec transform, which represents a single security association in the kernel. The
1636 * transform will be cached by the system server and must be freed when no longer needed. It is
1637 * possible to free one, deleting the SA from underneath sockets that are using it, which will
1638 * result in all of those sockets becoming unable to send or receive data.
Nathan Harold93962f32017-03-07 13:23:36 -08001639 */
1640 @Override
Nathan Harold21208ee2018-03-15 18:06:06 -07001641 public synchronized IpSecTransformResponse createTransform(
1642 IpSecConfig c, IBinder binder, String callingPackage) throws RemoteException {
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001643 Objects.requireNonNull(c);
Nathan Harold21208ee2018-03-15 18:06:06 -07001644 if (c.getMode() == IpSecTransform.MODE_TUNNEL) {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001645 enforceTunnelFeatureAndPermissions(callingPackage);
Nathan Harold21208ee2018-03-15 18:06:06 -07001646 }
Nathan Harolda10003d2017-08-23 13:46:33 -07001647 checkIpSecConfig(c);
Daulet Zhanguzinea1a7ca2020-01-03 09:46:50 +00001648 Objects.requireNonNull(binder, "Null Binder passed to createTransform");
Nathan Haroldd8c74292017-12-13 19:16:33 -08001649 final int resourceId = mNextResourceId++;
Benedict Wong344bd622017-11-16 15:27:22 -08001650
1651 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
Benedict Wong4f9fb812017-12-13 17:16:53 -08001652 List<RefcountedResource> dependencies = new ArrayList<>();
Benedict Wong344bd622017-11-16 15:27:22 -08001653
1654 if (!userRecord.mTransformQuotaTracker.isAvailable()) {
Nathan Harolda1afbd82017-04-24 16:16:34 -07001655 return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE);
1656 }
Nathan Harolda10003d2017-08-23 13:46:33 -07001657
Benedict Wong344bd622017-11-16 15:27:22 -08001658 EncapSocketRecord socketRecord = null;
Benedict Wong0fe58a92018-01-19 17:36:02 -08001659 if (c.getEncapType() != IpSecTransform.ENCAP_NONE) {
Benedict Wong344bd622017-11-16 15:27:22 -08001660 RefcountedResource<EncapSocketRecord> refcountedSocketRecord =
1661 userRecord.mEncapSocketRecords.getRefcountedResourceOrThrow(
1662 c.getEncapSocketResourceId());
1663 dependencies.add(refcountedSocketRecord);
Benedict Wong344bd622017-11-16 15:27:22 -08001664 socketRecord = refcountedSocketRecord.getResource();
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001665 }
1666
Nathan Harolda2523312018-01-05 19:25:13 -08001667 RefcountedResource<SpiRecord> refcountedSpiRecord =
1668 userRecord.mSpiRecords.getRefcountedResourceOrThrow(c.getSpiResourceId());
1669 dependencies.add(refcountedSpiRecord);
1670 SpiRecord spiRecord = refcountedSpiRecord.getResource();
Benedict Wong344bd622017-11-16 15:27:22 -08001671
Nathan Harold144ce0a2018-04-03 16:13:19 -07001672 createOrUpdateTransform(c, resourceId, spiRecord, socketRecord);
Benedict Wong0fe58a92018-01-19 17:36:02 -08001673
1674 // SA was created successfully, time to construct a record and lock it away
Benedict Wong344bd622017-11-16 15:27:22 -08001675 userRecord.mTransformRecords.put(
1676 resourceId,
1677 new RefcountedResource<TransformRecord>(
Nathan Harolda2523312018-01-05 19:25:13 -08001678 new TransformRecord(resourceId, c, spiRecord, socketRecord),
Benedict Wong344bd622017-11-16 15:27:22 -08001679 binder,
1680 dependencies.toArray(new RefcountedResource[dependencies.size()])));
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001681 return new IpSecTransformResponse(IpSecManager.Status.OK, resourceId);
Nathan Harold93962f32017-03-07 13:23:36 -08001682 }
1683
1684 /**
1685 * Delete a transport mode transform that was previously allocated by + registered with the
1686 * system server. If this is called on an inactive (or non-existent) transform, it will not
1687 * return an error. It's safe to de-allocate transforms that may have already been deleted for
1688 * other reasons.
1689 */
1690 @Override
Benedict Wongf33f03132018-01-18 14:38:16 -08001691 public synchronized void deleteTransform(int resourceId) throws RemoteException {
Benedict Wong344bd622017-11-16 15:27:22 -08001692 UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid());
1693 releaseResource(userRecord.mTransformRecords, resourceId);
Nathan Harold93962f32017-03-07 13:23:36 -08001694 }
1695
1696 /**
1697 * Apply an active transport mode transform to a socket, which will apply the IPsec security
1698 * association as a correspondent policy to the provided socket
1699 */
1700 @Override
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001701 public synchronized void applyTransportModeTransform(
Nathan Harolda2523312018-01-05 19:25:13 -08001702 ParcelFileDescriptor socket, int direction, int resourceId) throws RemoteException {
Benedict Wonga0989622018-07-25 13:06:29 -07001703 int callingUid = Binder.getCallingUid();
1704 UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid);
Nathan Harolda2523312018-01-05 19:25:13 -08001705 checkDirection(direction);
Benedict Wong344bd622017-11-16 15:27:22 -08001706 // Get transform record; if no transform is found, will throw IllegalArgumentException
1707 TransformRecord info = userRecord.mTransformRecords.getResourceOrThrow(resourceId);
Nathan Harold93962f32017-03-07 13:23:36 -08001708
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001709 // TODO: make this a function.
Benedict Wonga0989622018-07-25 13:06:29 -07001710 if (info.pid != getCallingPid() || info.uid != callingUid) {
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001711 throw new SecurityException("Only the owner of an IpSec Transform may apply it!");
1712 }
1713
Benedict Wong8149f6e2018-01-18 18:31:45 -08001714 // Get config and check that to-be-applied transform has the correct mode
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001715 IpSecConfig c = info.getConfig();
Benedict Wong8149f6e2018-01-18 18:31:45 -08001716 Preconditions.checkArgument(
1717 c.getMode() == IpSecTransform.MODE_TRANSPORT,
1718 "Transform mode was not Transport mode; cannot be applied to a socket");
1719
Nathan Harold144ce0a2018-04-03 16:13:19 -07001720 mSrvConfig
1721 .getNetdInstance()
1722 .ipSecApplyTransportModeTransform(
Luke Huange8e522b2018-11-23 12:01:41 +08001723 socket,
Benedict Wonga0989622018-07-25 13:06:29 -07001724 callingUid,
Nathan Harold144ce0a2018-04-03 16:13:19 -07001725 direction,
1726 c.getSourceAddress(),
1727 c.getDestinationAddress(),
1728 info.getSpiRecord().getSpi());
Nathan Harold93962f32017-03-07 13:23:36 -08001729 }
Nathan Harold8dc1fd02017-04-04 19:37:48 -07001730
Nathan Harold93962f32017-03-07 13:23:36 -08001731 /**
Nathan Harolda2523312018-01-05 19:25:13 -08001732 * Remove transport mode transforms from a socket, applying the default (empty) policy. This
1733 * ensures that NO IPsec policy is applied to the socket (would be the equivalent of applying a
1734 * policy that performs no IPsec). Today the resourceId parameter is passed but not used:
1735 * reserved for future improved input validation.
Nathan Harold93962f32017-03-07 13:23:36 -08001736 */
1737 @Override
Nathan Haroldf73d2522018-01-17 01:00:20 -08001738 public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket)
1739 throws RemoteException {
Nathan Harold144ce0a2018-04-03 16:13:19 -07001740 mSrvConfig
1741 .getNetdInstance()
Luke Huange8e522b2018-11-23 12:01:41 +08001742 .ipSecRemoveTransportModeTransform(socket);
Nathan Harold93962f32017-03-07 13:23:36 -08001743 }
1744
Benedict Wong8149f6e2018-01-18 18:31:45 -08001745 /**
1746 * Apply an active tunnel mode transform to a TunnelInterface, which will apply the IPsec
1747 * security association as a correspondent policy to the provided interface
1748 */
1749 @Override
1750 public synchronized void applyTunnelModeTransform(
Nathan Harold21208ee2018-03-15 18:06:06 -07001751 int tunnelResourceId, int direction,
1752 int transformResourceId, String callingPackage) throws RemoteException {
Benedict Wonge265d5f2018-11-08 19:45:34 -08001753 enforceTunnelFeatureAndPermissions(callingPackage);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001754 checkDirection(direction);
1755
Benedict Wonga0989622018-07-25 13:06:29 -07001756 int callingUid = Binder.getCallingUid();
1757 UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid);
Benedict Wong8149f6e2018-01-18 18:31:45 -08001758
1759 // Get transform record; if no transform is found, will throw IllegalArgumentException
1760 TransformRecord transformInfo =
1761 userRecord.mTransformRecords.getResourceOrThrow(transformResourceId);
1762
1763 // Get tunnelInterface record; if no such interface is found, will throw
1764 // IllegalArgumentException
1765 TunnelInterfaceRecord tunnelInterfaceInfo =
1766 userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId);
1767
1768 // Get config and check that to-be-applied transform has the correct mode
1769 IpSecConfig c = transformInfo.getConfig();
1770 Preconditions.checkArgument(
1771 c.getMode() == IpSecTransform.MODE_TUNNEL,
1772 "Transform mode was not Tunnel mode; cannot be applied to a tunnel interface");
1773
Benedict Wong0fe58a92018-01-19 17:36:02 -08001774 EncapSocketRecord socketRecord = null;
1775 if (c.getEncapType() != IpSecTransform.ENCAP_NONE) {
1776 socketRecord =
1777 userRecord.mEncapSocketRecords.getResourceOrThrow(c.getEncapSocketResourceId());
1778 }
Benedict Wong963502f2019-10-03 11:09:00 -07001779 SpiRecord spiRecord = transformInfo.getSpiRecord();
Benedict Wong0fe58a92018-01-19 17:36:02 -08001780
Benedict Wong8149f6e2018-01-18 18:31:45 -08001781 int mark =
Benedict Wong564e2aa2018-05-07 20:06:44 -07001782 (direction == IpSecManager.DIRECTION_OUT)
1783 ? tunnelInterfaceInfo.getOkey()
1784 : tunnelInterfaceInfo.getIkey();
Benedict Wong8149f6e2018-01-18 18:31:45 -08001785
Benedict Wong0fe58a92018-01-19 17:36:02 -08001786 try {
Benedict Wong781dae62018-09-06 11:31:25 -07001787 // Default to using the invalid SPI of 0 for inbound SAs. This allows policies to skip
1788 // SPI matching as part of the template resolution.
1789 int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX;
1790 c.setXfrmInterfaceId(tunnelInterfaceInfo.getIfId());
1791
Benedict Wongab80e1f2018-07-25 18:46:19 -07001792 // TODO: enable this when UPDSA supports updating marks. Adding kernel support upstream
1793 // (and backporting) would allow us to narrow the mark space, and ensure that the SA
1794 // and SPs have matching marks (as VTI are meant to be built).
1795 // Currently update does nothing with marks. Leave empty (defaulting to 0) to ensure the
1796 // config matches the actual allocated resources in the kernel.
Benedict Wongb2920572018-11-21 21:24:55 -08001797 // All SAs will have zero marks (from creation time), and any policy that matches the
1798 // same src/dst could match these SAs. Non-IpSecService governed processes that
1799 // establish floating policies with the same src/dst may result in undefined
1800 // behavior. This is generally limited to vendor code due to the permissions
1801 // (CAP_NET_ADMIN) required.
Benedict Wongab80e1f2018-07-25 18:46:19 -07001802 //
1803 // c.setMarkValue(mark);
1804 // c.setMarkMask(0xffffffff);
Benedict Wong0fe58a92018-01-19 17:36:02 -08001805
1806 if (direction == IpSecManager.DIRECTION_OUT) {
1807 // Set output mark via underlying network (output only)
1808 c.setNetwork(tunnelInterfaceInfo.getUnderlyingNetwork());
1809
Benedict Wong781dae62018-09-06 11:31:25 -07001810 // Set outbound SPI only. We want inbound to use any valid SA (old, new) on rekeys,
1811 // but want to guarantee outbound packets are sent over the new SA.
Benedict Wong963502f2019-10-03 11:09:00 -07001812 spi = spiRecord.getSpi();
Benedict Wong781dae62018-09-06 11:31:25 -07001813 }
1814
1815 // Always update the policy with the relevant XFRM_IF_ID
1816 for (int selAddrFamily : ADDRESS_FAMILIES) {
1817 mSrvConfig
1818 .getNetdInstance()
1819 .ipSecUpdateSecurityPolicy(
1820 callingUid,
1821 selAddrFamily,
1822 direction,
1823 transformInfo.getConfig().getSourceAddress(),
1824 transformInfo.getConfig().getDestinationAddress(),
1825 spi, // If outbound, also add SPI to the policy.
1826 mark, // Must always set policy mark; ikey/okey for VTIs
1827 0xffffffff,
1828 c.getXfrmInterfaceId());
Benedict Wong0fe58a92018-01-19 17:36:02 -08001829 }
1830
1831 // Update SA with tunnel mark (ikey or okey based on direction)
1832 createOrUpdateTransform(c, transformResourceId, spiRecord, socketRecord);
1833 } catch (ServiceSpecificException e) {
1834 if (e.errorCode == EINVAL) {
1835 throw new IllegalArgumentException(e.toString());
1836 } else {
1837 throw e;
1838 }
1839 }
Benedict Wong8149f6e2018-01-18 18:31:45 -08001840 }
1841
Nathan Harold93962f32017-03-07 13:23:36 -08001842 @Override
ludib0c95b12017-05-22 10:52:23 -07001843 protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
Nathan Harold1afbef42017-03-01 18:55:06 -08001844 mContext.enforceCallingOrSelfPermission(DUMP, TAG);
ludib0c95b12017-05-22 10:52:23 -07001845
1846 pw.println("IpSecService dump:");
Nathan Harold1afbef42017-03-01 18:55:06 -08001847 pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead"));
1848 pw.println();
ludib0c95b12017-05-22 10:52:23 -07001849
Benedict Wong344bd622017-11-16 15:27:22 -08001850 pw.println("mUserResourceTracker:");
1851 pw.println(mUserResourceTracker);
Nathan Harold1afbef42017-03-01 18:55:06 -08001852 }
1853}