Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import static android.Manifest.permission.DUMP; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 20 | import static android.net.IpSecManager.INVALID_RESOURCE_ID; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 21 | import static android.system.OsConstants.AF_INET; |
manojboopathi | c4be79d | 2017-11-30 17:11:49 -0800 | [diff] [blame] | 22 | import static android.system.OsConstants.EINVAL; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 23 | import static android.system.OsConstants.IPPROTO_UDP; |
| 24 | import static android.system.OsConstants.SOCK_DGRAM; |
| 25 | import static com.android.internal.util.Preconditions.checkNotNull; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 26 | |
| 27 | import android.content.Context; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 28 | import android.net.ConnectivityManager; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 29 | import android.net.IIpSecService; |
| 30 | import android.net.INetd; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 31 | import android.net.IpSecAlgorithm; |
| 32 | import android.net.IpSecConfig; |
| 33 | import android.net.IpSecManager; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 34 | import android.net.IpSecSpiResponse; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 35 | import android.net.IpSecTransform; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 36 | import android.net.IpSecTransformResponse; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 37 | import android.net.IpSecTunnelInterfaceResponse; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 38 | import android.net.IpSecUdpEncapResponse; |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 39 | import android.net.LinkAddress; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 40 | import android.net.Network; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 41 | import android.net.NetworkUtils; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 42 | import android.net.TrafficStats; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 43 | import android.net.util.NetdService; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 44 | import android.os.Binder; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 45 | import android.os.IBinder; |
| 46 | import android.os.ParcelFileDescriptor; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 47 | import android.os.RemoteException; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 48 | import android.os.ServiceSpecificException; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 49 | import android.system.ErrnoException; |
| 50 | import android.system.Os; |
| 51 | import android.system.OsConstants; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 52 | import android.text.TextUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 53 | import android.util.Log; |
| 54 | import android.util.Slog; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 55 | import android.util.SparseArray; |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 56 | import android.util.SparseBooleanArray; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 57 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 58 | import com.android.internal.annotations.GuardedBy; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 59 | import com.android.internal.annotations.VisibleForTesting; |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 60 | import com.android.internal.util.Preconditions; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 61 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 62 | import java.io.FileDescriptor; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 63 | import java.io.IOException; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 64 | import java.io.PrintWriter; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 65 | import java.net.InetAddress; |
| 66 | import java.net.InetSocketAddress; |
| 67 | import java.net.UnknownHostException; |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 68 | import java.util.ArrayList; |
| 69 | import java.util.List; |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 70 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 71 | import libcore.io.IoUtils; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 72 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 73 | /** |
| 74 | * A service to manage multiple clients that want to access the IpSec API. The service is |
| 75 | * responsible for maintaining a list of clients and managing the resources (and related quotas) |
| 76 | * that each of them own. |
| 77 | * |
| 78 | * <p>Synchronization in IpSecService is done on all entrypoints due to potential race conditions at |
| 79 | * the kernel/xfrm level. Further, this allows the simplifying assumption to be made that only one |
| 80 | * thread is ever running at a time. |
| 81 | * |
| 82 | * @hide |
| 83 | */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 84 | public class IpSecService extends IIpSecService.Stub { |
| 85 | private static final String TAG = "IpSecService"; |
| 86 | private static final boolean DBG = Log.isLoggable(TAG, Log.DEBUG); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 87 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 88 | private static final String NETD_SERVICE_NAME = "netd"; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 89 | private static final int[] DIRECTIONS = |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 90 | new int[] {IpSecManager.DIRECTION_OUT, IpSecManager.DIRECTION_IN}; |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 91 | private static final String[] WILDCARD_ADDRESSES = new String[]{"0.0.0.0", "::"}; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 92 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 93 | private static final int NETD_FETCH_TIMEOUT_MS = 5000; // ms |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 94 | private static final int MAX_PORT_BIND_ATTEMPTS = 10; |
| 95 | private static final InetAddress INADDR_ANY; |
| 96 | |
| 97 | static { |
| 98 | try { |
| 99 | INADDR_ANY = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); |
| 100 | } catch (UnknownHostException e) { |
| 101 | throw new RuntimeException(e); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | static final int FREE_PORT_MIN = 1024; // ports 1-1023 are reserved |
| 106 | static final int PORT_MAX = 0xFFFF; // ports are an unsigned 16-bit integer |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 107 | static final String TUNNEL_INTERFACE_PREFIX = "ipsec"; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 108 | |
| 109 | /* Binder context for this service */ |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 110 | private final Context mContext; |
| 111 | |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 112 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 113 | * The next non-repeating global ID for tracking resources between users, this service, and |
| 114 | * kernel data structures. Accessing this variable is not thread safe, so it is only read or |
| 115 | * modified within blocks synchronized on IpSecService.this. We want to avoid -1 |
| 116 | * (INVALID_RESOURCE_ID) and 0 (we probably forgot to initialize it). |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 117 | */ |
| 118 | @GuardedBy("IpSecService.this") |
| 119 | private int mNextResourceId = 1; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 120 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 121 | interface IpSecServiceConfiguration { |
| 122 | INetd getNetdInstance() throws RemoteException; |
| 123 | |
| 124 | static IpSecServiceConfiguration GETSRVINSTANCE = |
| 125 | new IpSecServiceConfiguration() { |
| 126 | @Override |
| 127 | public INetd getNetdInstance() throws RemoteException { |
| 128 | final INetd netd = NetdService.getInstance(); |
| 129 | if (netd == null) { |
| 130 | throw new RemoteException("Failed to Get Netd Instance"); |
| 131 | } |
| 132 | return netd; |
| 133 | } |
| 134 | }; |
| 135 | } |
| 136 | |
| 137 | private final IpSecServiceConfiguration mSrvConfig; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 138 | final UidFdTagger mUidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 139 | |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 140 | /** |
| 141 | * Interface for user-reference and kernel-resource cleanup. |
| 142 | * |
| 143 | * <p>This interface must be implemented for a resource to be reference counted. |
| 144 | */ |
| 145 | @VisibleForTesting |
| 146 | public interface IResource { |
| 147 | /** |
| 148 | * Invalidates a IResource object, ensuring it is invalid for the purposes of allocating new |
| 149 | * objects dependent on it. |
| 150 | * |
| 151 | * <p>Implementations of this method are expected to remove references to the IResource |
| 152 | * object from the IpSecService's tracking arrays. The removal from the arrays ensures that |
| 153 | * the resource is considered invalid for user access or allocation or use in other |
| 154 | * resources. |
| 155 | * |
| 156 | * <p>References to the IResource object may be held by other RefcountedResource objects, |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 157 | * and as such, the underlying resources and quota may not be cleaned up. |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 158 | */ |
| 159 | void invalidate() throws RemoteException; |
| 160 | |
| 161 | /** |
| 162 | * Releases underlying resources and related quotas. |
| 163 | * |
| 164 | * <p>Implementations of this method are expected to remove all system resources that are |
| 165 | * tracked by the IResource object. Due to other RefcountedResource objects potentially |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 166 | * having references to the IResource object, freeUnderlyingResources may not always be |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 167 | * called from releaseIfUnreferencedRecursively(). |
| 168 | */ |
| 169 | void freeUnderlyingResources() throws RemoteException; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * RefcountedResource manages references and dependencies in an exclusively acyclic graph. |
| 174 | * |
| 175 | * <p>RefcountedResource implements both explicit and implicit resource management. Creating a |
| 176 | * RefcountedResource object creates an explicit reference that must be freed by calling |
| 177 | * userRelease(). Additionally, adding this object as a child of another RefcountedResource |
| 178 | * object will add an implicit reference. |
| 179 | * |
| 180 | * <p>Resources are cleaned up when all references, both implicit and explicit, are released |
| 181 | * (ie, when userRelease() is called and when all parents have called releaseReference() on this |
| 182 | * object.) |
| 183 | */ |
| 184 | @VisibleForTesting |
| 185 | public class RefcountedResource<T extends IResource> implements IBinder.DeathRecipient { |
| 186 | private final T mResource; |
| 187 | private final List<RefcountedResource> mChildren; |
| 188 | int mRefCount = 1; // starts at 1 for user's reference. |
| 189 | IBinder mBinder; |
| 190 | |
| 191 | RefcountedResource(T resource, IBinder binder, RefcountedResource... children) { |
| 192 | synchronized (IpSecService.this) { |
| 193 | this.mResource = resource; |
| 194 | this.mChildren = new ArrayList<>(children.length); |
| 195 | this.mBinder = binder; |
| 196 | |
| 197 | for (RefcountedResource child : children) { |
| 198 | mChildren.add(child); |
| 199 | child.mRefCount++; |
| 200 | } |
| 201 | |
| 202 | try { |
| 203 | mBinder.linkToDeath(this, 0); |
| 204 | } catch (RemoteException e) { |
| 205 | binderDied(); |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * If the Binder object dies, this function is called to free the system resources that are |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 212 | * being tracked by this record and to subsequently release this record for garbage |
Benedict Wong | 409c8ca | 2017-10-26 19:41:43 -0700 | [diff] [blame] | 213 | * collection |
| 214 | */ |
| 215 | @Override |
| 216 | public void binderDied() { |
| 217 | synchronized (IpSecService.this) { |
| 218 | try { |
| 219 | userRelease(); |
| 220 | } catch (Exception e) { |
| 221 | Log.e(TAG, "Failed to release resource: " + e); |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | public T getResource() { |
| 227 | return mResource; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Unlinks from binder and performs IpSecService resource cleanup (removes from resource |
| 232 | * arrays) |
| 233 | * |
| 234 | * <p>If this method has been previously called, the RefcountedResource's binder field will |
| 235 | * be null, and the method will return without performing the cleanup a second time. |
| 236 | * |
| 237 | * <p>Note that calling this function does not imply that kernel resources will be freed at |
| 238 | * this time, or that the related quota will be returned. Such actions will only be |
| 239 | * performed upon the reference count reaching zero. |
| 240 | */ |
| 241 | @GuardedBy("IpSecService.this") |
| 242 | public void userRelease() throws RemoteException { |
| 243 | // Prevent users from putting reference counts into a bad state by calling |
| 244 | // userRelease() multiple times. |
| 245 | if (mBinder == null) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | mBinder.unlinkToDeath(this, 0); |
| 250 | mBinder = null; |
| 251 | |
| 252 | mResource.invalidate(); |
| 253 | |
| 254 | releaseReference(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Removes a reference to this resource. If the resultant reference count is zero, the |
| 259 | * underlying resources are freed, and references to all child resources are also dropped |
| 260 | * recursively (resulting in them freeing their resources and children, etcetera) |
| 261 | * |
| 262 | * <p>This method also sets the reference count to an invalid value (-1) to signify that it |
| 263 | * has been fully released. Any subsequent calls to this method will result in an |
| 264 | * IllegalStateException being thrown due to resource already having been previously |
| 265 | * released |
| 266 | */ |
| 267 | @VisibleForTesting |
| 268 | @GuardedBy("IpSecService.this") |
| 269 | public void releaseReference() throws RemoteException { |
| 270 | mRefCount--; |
| 271 | |
| 272 | if (mRefCount > 0) { |
| 273 | return; |
| 274 | } else if (mRefCount < 0) { |
| 275 | throw new IllegalStateException( |
| 276 | "Invalid operation - resource has already been released."); |
| 277 | } |
| 278 | |
| 279 | // Cleanup own resources |
| 280 | mResource.freeUnderlyingResources(); |
| 281 | |
| 282 | // Cleanup child resources as needed |
| 283 | for (RefcountedResource<? extends IResource> child : mChildren) { |
| 284 | child.releaseReference(); |
| 285 | } |
| 286 | |
| 287 | // Enforce that resource cleanup can only be called once |
| 288 | // By decrementing the refcount (from 0 to -1), the next call will throw an |
| 289 | // IllegalStateException - it has already been released fully. |
| 290 | mRefCount--; |
| 291 | } |
| 292 | |
| 293 | @Override |
| 294 | public String toString() { |
| 295 | return new StringBuilder() |
| 296 | .append("{mResource=") |
| 297 | .append(mResource) |
| 298 | .append(", mRefCount=") |
| 299 | .append(mRefCount) |
| 300 | .append(", mChildren=") |
| 301 | .append(mChildren) |
| 302 | .append("}") |
| 303 | .toString(); |
| 304 | } |
| 305 | } |
| 306 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 307 | /** |
| 308 | * Very simple counting class that looks much like a counting semaphore |
| 309 | * |
| 310 | * <p>This class is not thread-safe, and expects that that users of this class will ensure |
| 311 | * synchronization and thread safety by holding the IpSecService.this instance lock. |
| 312 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 313 | @VisibleForTesting |
| 314 | static class ResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 315 | private final int mMax; |
| 316 | int mCurrent; |
| 317 | |
| 318 | ResourceTracker(int max) { |
| 319 | mMax = max; |
| 320 | mCurrent = 0; |
| 321 | } |
| 322 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 323 | boolean isAvailable() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 324 | return (mCurrent < mMax); |
| 325 | } |
| 326 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 327 | void take() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 328 | if (!isAvailable()) { |
| 329 | Log.wtf(TAG, "Too many resources allocated!"); |
| 330 | } |
| 331 | mCurrent++; |
| 332 | } |
| 333 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 334 | void give() { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 335 | if (mCurrent <= 0) { |
| 336 | Log.wtf(TAG, "We've released this resource too many times"); |
| 337 | } |
| 338 | mCurrent--; |
| 339 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 340 | |
| 341 | @Override |
| 342 | public String toString() { |
| 343 | return new StringBuilder() |
| 344 | .append("{mCurrent=") |
| 345 | .append(mCurrent) |
| 346 | .append(", mMax=") |
| 347 | .append(mMax) |
| 348 | .append("}") |
| 349 | .toString(); |
| 350 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 353 | @VisibleForTesting |
| 354 | static final class UserRecord { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 355 | /* Maximum number of each type of resource that a single UID may possess */ |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 356 | public static final int MAX_NUM_TUNNEL_INTERFACES = 2; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 357 | public static final int MAX_NUM_ENCAP_SOCKETS = 2; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 358 | public static final int MAX_NUM_TRANSFORMS = 4; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 359 | public static final int MAX_NUM_SPIS = 8; |
| 360 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 361 | /** |
| 362 | * Store each of the OwnedResource types in an (thinly wrapped) sparse array for indexing |
| 363 | * and explicit (user) reference management. |
| 364 | * |
| 365 | * <p>These are stored in separate arrays to improve debuggability and dump output clarity. |
| 366 | * |
| 367 | * <p>Resources are removed from this array when the user releases their explicit reference |
| 368 | * by calling one of the releaseResource() methods. |
| 369 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 370 | final RefcountedResourceArray<SpiRecord> mSpiRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 371 | new RefcountedResourceArray<>(SpiRecord.class.getSimpleName()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 372 | final RefcountedResourceArray<TransformRecord> mTransformRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 373 | new RefcountedResourceArray<>(TransformRecord.class.getSimpleName()); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 374 | final RefcountedResourceArray<EncapSocketRecord> mEncapSocketRecords = |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 375 | new RefcountedResourceArray<>(EncapSocketRecord.class.getSimpleName()); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 376 | final RefcountedResourceArray<TunnelInterfaceRecord> mTunnelInterfaceRecords = |
| 377 | new RefcountedResourceArray<>(TunnelInterfaceRecord.class.getSimpleName()); |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 378 | |
| 379 | /** |
| 380 | * Trackers for quotas for each of the OwnedResource types. |
| 381 | * |
| 382 | * <p>These trackers are separate from the resource arrays, since they are incremented and |
| 383 | * decremented at different points in time. Specifically, quota is only returned upon final |
| 384 | * resource deallocation (after all explicit and implicit references are released). Note |
| 385 | * that it is possible that calls to releaseResource() will not return the used quota if |
| 386 | * there are other resources that depend on (are parents of) the resource being released. |
| 387 | */ |
| 388 | final ResourceTracker mSpiQuotaTracker = new ResourceTracker(MAX_NUM_SPIS); |
| 389 | final ResourceTracker mTransformQuotaTracker = new ResourceTracker(MAX_NUM_TRANSFORMS); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 390 | final ResourceTracker mSocketQuotaTracker = new ResourceTracker(MAX_NUM_ENCAP_SOCKETS); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 391 | final ResourceTracker mTunnelQuotaTracker = new ResourceTracker(MAX_NUM_TUNNEL_INTERFACES); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 392 | |
| 393 | void removeSpiRecord(int resourceId) { |
| 394 | mSpiRecords.remove(resourceId); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 397 | void removeTransformRecord(int resourceId) { |
| 398 | mTransformRecords.remove(resourceId); |
| 399 | } |
| 400 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 401 | void removeTunnelInterfaceRecord(int resourceId) { |
| 402 | mTunnelInterfaceRecords.remove(resourceId); |
| 403 | } |
| 404 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 405 | void removeEncapSocketRecord(int resourceId) { |
| 406 | mEncapSocketRecords.remove(resourceId); |
| 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public String toString() { |
| 411 | return new StringBuilder() |
| 412 | .append("{mSpiQuotaTracker=") |
| 413 | .append(mSpiQuotaTracker) |
| 414 | .append(", mTransformQuotaTracker=") |
| 415 | .append(mTransformQuotaTracker) |
| 416 | .append(", mSocketQuotaTracker=") |
| 417 | .append(mSocketQuotaTracker) |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 418 | .append(", mTunnelQuotaTracker=") |
| 419 | .append(mTunnelQuotaTracker) |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 420 | .append(", mSpiRecords=") |
| 421 | .append(mSpiRecords) |
| 422 | .append(", mTransformRecords=") |
| 423 | .append(mTransformRecords) |
| 424 | .append(", mEncapSocketRecords=") |
| 425 | .append(mEncapSocketRecords) |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 426 | .append(", mTunnelInterfaceRecords=") |
| 427 | .append(mTunnelInterfaceRecords) |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 428 | .append("}") |
| 429 | .toString(); |
| 430 | } |
| 431 | } |
| 432 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 433 | /** |
| 434 | * This class is not thread-safe, and expects that that users of this class will ensure |
| 435 | * synchronization and thread safety by holding the IpSecService.this instance lock. |
| 436 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 437 | @VisibleForTesting |
| 438 | static final class UserResourceTracker { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 439 | private final SparseArray<UserRecord> mUserRecords = new SparseArray<>(); |
| 440 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 441 | /** Lazy-initialization/getter that populates or retrieves the UserRecord as needed */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 442 | public UserRecord getUserRecord(int uid) { |
| 443 | checkCallerUid(uid); |
| 444 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 445 | UserRecord r = mUserRecords.get(uid); |
| 446 | if (r == null) { |
| 447 | r = new UserRecord(); |
| 448 | mUserRecords.put(uid, r); |
| 449 | } |
| 450 | return r; |
| 451 | } |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 452 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 453 | /** Safety method; guards against access of other user's UserRecords */ |
| 454 | private void checkCallerUid(int uid) { |
| 455 | if (uid != Binder.getCallingUid() |
| 456 | && android.os.Process.SYSTEM_UID != Binder.getCallingUid()) { |
| 457 | throw new SecurityException("Attempted access of unowned resources"); |
| 458 | } |
| 459 | } |
| 460 | |
ludi | 3e5ea23 | 2017-08-10 15:44:40 -0700 | [diff] [blame] | 461 | @Override |
| 462 | public String toString() { |
| 463 | return mUserRecords.toString(); |
| 464 | } |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 467 | @VisibleForTesting final UserResourceTracker mUserResourceTracker = new UserResourceTracker(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 468 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 469 | /** |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 470 | * The OwnedResourceRecord class provides a facility to cleanly and reliably track system |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 471 | * resources. It relies on a provided resourceId that should uniquely identify the kernel |
| 472 | * resource. To use this class, the user should implement the invalidate() and |
| 473 | * freeUnderlyingResources() methods that are responsible for cleaning up IpSecService resource |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 474 | * tracking arrays and kernel resources, respectively. |
| 475 | * |
| 476 | * <p>This class associates kernel resources with the UID that owns and controls them. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 477 | */ |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 478 | private abstract class OwnedResourceRecord implements IResource { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 479 | final int pid; |
| 480 | final int uid; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 481 | protected final int mResourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 482 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 483 | OwnedResourceRecord(int resourceId) { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 484 | super(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 485 | if (resourceId == INVALID_RESOURCE_ID) { |
| 486 | throw new IllegalArgumentException("Resource ID must not be INVALID_RESOURCE_ID"); |
| 487 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 488 | mResourceId = resourceId; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 489 | pid = Binder.getCallingPid(); |
| 490 | uid = Binder.getCallingUid(); |
| 491 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 492 | getResourceTracker().take(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 495 | @Override |
| 496 | public abstract void invalidate() throws RemoteException; |
| 497 | |
| 498 | /** Convenience method; retrieves the user resource record for the stored UID. */ |
| 499 | protected UserRecord getUserRecord() { |
| 500 | return mUserResourceTracker.getUserRecord(uid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 501 | } |
| 502 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 503 | @Override |
| 504 | public abstract void freeUnderlyingResources() throws RemoteException; |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 505 | |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 506 | /** Get the resource tracker for this resource */ |
| 507 | protected abstract ResourceTracker getResourceTracker(); |
| 508 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 509 | @Override |
| 510 | public String toString() { |
| 511 | return new StringBuilder() |
| 512 | .append("{mResourceId=") |
| 513 | .append(mResourceId) |
| 514 | .append(", pid=") |
| 515 | .append(pid) |
| 516 | .append(", uid=") |
| 517 | .append(uid) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 518 | .append("}") |
| 519 | .toString(); |
| 520 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 521 | }; |
| 522 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 523 | /** |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 524 | * Thin wrapper over SparseArray to ensure resources exist, and simplify generic typing. |
| 525 | * |
| 526 | * <p>RefcountedResourceArray prevents null insertions, and throws an IllegalArgumentException |
| 527 | * if a key is not found during a retrieval process. |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 528 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 529 | static class RefcountedResourceArray<T extends IResource> { |
| 530 | SparseArray<RefcountedResource<T>> mArray = new SparseArray<>(); |
| 531 | private final String mTypeName; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 532 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 533 | public RefcountedResourceArray(String typeName) { |
| 534 | this.mTypeName = typeName; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 535 | } |
| 536 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 537 | /** |
| 538 | * Accessor method to get inner resource object. |
| 539 | * |
| 540 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 541 | */ |
| 542 | T getResourceOrThrow(int key) { |
| 543 | return getRefcountedResourceOrThrow(key).getResource(); |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * Accessor method to get reference counting wrapper. |
| 548 | * |
| 549 | * @throws IllegalArgumentException if no resource with provided key is found. |
| 550 | */ |
| 551 | RefcountedResource<T> getRefcountedResourceOrThrow(int key) { |
| 552 | RefcountedResource<T> resource = mArray.get(key); |
| 553 | if (resource == null) { |
| 554 | throw new IllegalArgumentException( |
| 555 | String.format("No such %s found for given id: %d", mTypeName, key)); |
| 556 | } |
| 557 | |
| 558 | return resource; |
| 559 | } |
| 560 | |
| 561 | void put(int key, RefcountedResource<T> obj) { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 562 | checkNotNull(obj, "Null resources cannot be added"); |
| 563 | mArray.put(key, obj); |
| 564 | } |
| 565 | |
| 566 | void remove(int key) { |
| 567 | mArray.remove(key); |
| 568 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 569 | |
| 570 | @Override |
| 571 | public String toString() { |
| 572 | return mArray.toString(); |
| 573 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 576 | /** |
| 577 | * Tracks an SA in the kernel, and manages cleanup paths. Once a TransformRecord is |
| 578 | * created, the SpiRecord that originally tracked the SAs will reliquish the |
| 579 | * responsibility of freeing the underlying SA to this class via the mOwnedByTransform flag. |
| 580 | */ |
| 581 | private final class TransformRecord extends OwnedResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 582 | private final IpSecConfig mConfig; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 583 | private final SpiRecord mSpi; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 584 | private final EncapSocketRecord mSocket; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 585 | |
| 586 | TransformRecord( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 587 | int resourceId, IpSecConfig config, SpiRecord spi, EncapSocketRecord socket) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 588 | super(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 589 | mConfig = config; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 590 | mSpi = spi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 591 | mSocket = socket; |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 592 | |
| 593 | spi.setOwnedByTransform(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | public IpSecConfig getConfig() { |
| 597 | return mConfig; |
| 598 | } |
| 599 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 600 | public SpiRecord getSpiRecord() { |
| 601 | return mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 604 | public EncapSocketRecord getSocketRecord() { |
| 605 | return mSocket; |
| 606 | } |
| 607 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 608 | /** always guarded by IpSecService#this */ |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 609 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 610 | public void freeUnderlyingResources() { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 611 | int spi = mSpi.getSpi(); |
| 612 | try { |
| 613 | mSrvConfig |
| 614 | .getNetdInstance() |
| 615 | .ipSecDeleteSecurityAssociation( |
| 616 | mResourceId, |
| 617 | mConfig.getSourceAddress(), |
| 618 | mConfig.getDestinationAddress(), |
Di Lu | 0b611f4 | 2018-01-11 11:35:25 -0800 | [diff] [blame] | 619 | spi, |
| 620 | mConfig.getMarkValue(), |
| 621 | mConfig.getMarkMask()); |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 622 | } catch (RemoteException | ServiceSpecificException e) { |
| 623 | Log.e(TAG, "Failed to delete SA with ID: " + mResourceId, e); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 624 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 625 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 626 | getResourceTracker().give(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 627 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 628 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 629 | @Override |
| 630 | public void invalidate() throws RemoteException { |
| 631 | getUserRecord().removeTransformRecord(mResourceId); |
| 632 | } |
| 633 | |
| 634 | @Override |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 635 | protected ResourceTracker getResourceTracker() { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 636 | return getUserRecord().mTransformQuotaTracker; |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 637 | } |
| 638 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 639 | @Override |
| 640 | public String toString() { |
| 641 | StringBuilder strBuilder = new StringBuilder(); |
| 642 | strBuilder |
| 643 | .append("{super=") |
| 644 | .append(super.toString()) |
| 645 | .append(", mSocket=") |
| 646 | .append(mSocket) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 647 | .append(", mSpi.mResourceId=") |
| 648 | .append(mSpi.mResourceId) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 649 | .append(", mConfig=") |
| 650 | .append(mConfig) |
| 651 | .append("}"); |
| 652 | return strBuilder.toString(); |
| 653 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 654 | } |
| 655 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 656 | /** |
| 657 | * Tracks a single SA in the kernel, and manages cleanup paths. Once used in a Transform, the |
| 658 | * responsibility for cleaning up underlying resources will be passed to the TransformRecord |
| 659 | * object |
| 660 | */ |
| 661 | private final class SpiRecord extends OwnedResourceRecord { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 662 | private final String mSourceAddress; |
| 663 | private final String mDestinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 664 | private int mSpi; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 665 | |
| 666 | private boolean mOwnedByTransform = false; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 667 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 668 | SpiRecord(int resourceId, String sourceAddress, String destinationAddress, int spi) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 669 | super(resourceId); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 670 | mSourceAddress = sourceAddress; |
| 671 | mDestinationAddress = destinationAddress; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 672 | mSpi = spi; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 673 | } |
| 674 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 675 | /** always guarded by IpSecService#this */ |
| 676 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 677 | public void freeUnderlyingResources() { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 678 | try { |
Nathan Harold | 82485a2 | 2018-02-27 19:19:40 -0800 | [diff] [blame] | 679 | if (!mOwnedByTransform) { |
| 680 | mSrvConfig |
| 681 | .getNetdInstance() |
| 682 | .ipSecDeleteSecurityAssociation( |
| 683 | mResourceId, mSourceAddress, mDestinationAddress, mSpi, 0, 0); |
| 684 | } |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 685 | } catch (ServiceSpecificException | RemoteException e) { |
| 686 | Log.e(TAG, "Failed to delete SPI reservation with ID: " + mResourceId, e); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 687 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 688 | |
| 689 | mSpi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 690 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 691 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 694 | public int getSpi() { |
| 695 | return mSpi; |
| 696 | } |
| 697 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 698 | public String getDestinationAddress() { |
| 699 | return mDestinationAddress; |
| 700 | } |
| 701 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 702 | public void setOwnedByTransform() { |
| 703 | if (mOwnedByTransform) { |
| 704 | // Programming error |
Andreas Gampe | d6d8e45 | 2017-07-11 10:25:09 -0700 | [diff] [blame] | 705 | throw new IllegalStateException("Cannot own an SPI twice!"); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | mOwnedByTransform = true; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 709 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 710 | |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 711 | public boolean getOwnedByTransform() { |
| 712 | return mOwnedByTransform; |
| 713 | } |
| 714 | |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 715 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 716 | public void invalidate() throws RemoteException { |
| 717 | getUserRecord().removeSpiRecord(mResourceId); |
| 718 | } |
| 719 | |
| 720 | @Override |
| 721 | protected ResourceTracker getResourceTracker() { |
| 722 | return getUserRecord().mSpiQuotaTracker; |
| 723 | } |
| 724 | |
| 725 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 726 | public String toString() { |
| 727 | StringBuilder strBuilder = new StringBuilder(); |
| 728 | strBuilder |
| 729 | .append("{super=") |
| 730 | .append(super.toString()) |
| 731 | .append(", mSpi=") |
| 732 | .append(mSpi) |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 733 | .append(", mSourceAddress=") |
| 734 | .append(mSourceAddress) |
| 735 | .append(", mDestinationAddress=") |
| 736 | .append(mDestinationAddress) |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 737 | .append(", mOwnedByTransform=") |
| 738 | .append(mOwnedByTransform) |
| 739 | .append("}"); |
| 740 | return strBuilder.toString(); |
| 741 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 742 | } |
| 743 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 744 | // These values have been reserved in ConnectivityService |
| 745 | @VisibleForTesting static final int TUN_INTF_NETID_START = 0xFC00; |
| 746 | |
| 747 | @VisibleForTesting static final int TUN_INTF_NETID_RANGE = 0x0400; |
| 748 | |
| 749 | private final SparseBooleanArray mTunnelNetIds = new SparseBooleanArray(); |
| 750 | private int mNextTunnelNetIdIndex = 0; |
| 751 | |
| 752 | /** |
| 753 | * Reserves a netId within the range of netIds allocated for IPsec tunnel interfaces |
| 754 | * |
| 755 | * <p>This method should only be called from Binder threads. Do not call this from within the |
| 756 | * system server as it will crash the system on failure. |
| 757 | * |
| 758 | * @return an integer key within the netId range, if successful |
| 759 | * @throws IllegalStateException if unsuccessful (all netId are currently reserved) |
| 760 | */ |
| 761 | @VisibleForTesting |
| 762 | int reserveNetId() { |
| 763 | synchronized (mTunnelNetIds) { |
| 764 | for (int i = 0; i < TUN_INTF_NETID_RANGE; i++) { |
| 765 | int index = mNextTunnelNetIdIndex; |
| 766 | int netId = index + TUN_INTF_NETID_START; |
| 767 | if (++mNextTunnelNetIdIndex >= TUN_INTF_NETID_RANGE) mNextTunnelNetIdIndex = 0; |
| 768 | if (!mTunnelNetIds.get(netId)) { |
| 769 | mTunnelNetIds.put(netId, true); |
| 770 | return netId; |
| 771 | } |
| 772 | } |
| 773 | } |
| 774 | throw new IllegalStateException("No free netIds to allocate"); |
| 775 | } |
| 776 | |
| 777 | @VisibleForTesting |
| 778 | void releaseNetId(int netId) { |
| 779 | synchronized (mTunnelNetIds) { |
| 780 | mTunnelNetIds.delete(netId); |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | private final class TunnelInterfaceRecord extends OwnedResourceRecord { |
| 785 | private final String mInterfaceName; |
| 786 | private final Network mUnderlyingNetwork; |
| 787 | |
| 788 | // outer addresses |
| 789 | private final String mLocalAddress; |
| 790 | private final String mRemoteAddress; |
| 791 | |
| 792 | private final int mIkey; |
| 793 | private final int mOkey; |
| 794 | |
| 795 | TunnelInterfaceRecord( |
| 796 | int resourceId, |
| 797 | String interfaceName, |
| 798 | Network underlyingNetwork, |
| 799 | String localAddr, |
| 800 | String remoteAddr, |
| 801 | int ikey, |
| 802 | int okey) { |
| 803 | super(resourceId); |
| 804 | |
| 805 | mInterfaceName = interfaceName; |
| 806 | mUnderlyingNetwork = underlyingNetwork; |
| 807 | mLocalAddress = localAddr; |
| 808 | mRemoteAddress = remoteAddr; |
| 809 | mIkey = ikey; |
| 810 | mOkey = okey; |
| 811 | } |
| 812 | |
| 813 | /** always guarded by IpSecService#this */ |
| 814 | @Override |
| 815 | public void freeUnderlyingResources() { |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 816 | // Calls to netd |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 817 | // Teardown VTI |
| 818 | // Delete global policies |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 819 | try { |
| 820 | mSrvConfig.getNetdInstance().removeVirtualTunnelInterface(mInterfaceName); |
| 821 | |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 822 | for(String wildcardAddr : WILDCARD_ADDRESSES) { |
| 823 | for (int direction : DIRECTIONS) { |
| 824 | int mark = (direction == IpSecManager.DIRECTION_IN) ? mIkey : mOkey; |
| 825 | mSrvConfig |
| 826 | .getNetdInstance() |
| 827 | .ipSecDeleteSecurityPolicy( |
| 828 | 0, direction, wildcardAddr, wildcardAddr, mark, 0xffffffff); |
| 829 | } |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 830 | } |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 831 | } catch (ServiceSpecificException | RemoteException e) { |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 832 | Log.e( |
| 833 | TAG, |
| 834 | "Failed to delete VTI with interface name: " |
| 835 | + mInterfaceName |
| 836 | + " and id: " |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 837 | + mResourceId, e); |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 838 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 839 | |
| 840 | getResourceTracker().give(); |
| 841 | releaseNetId(mIkey); |
| 842 | releaseNetId(mOkey); |
| 843 | } |
| 844 | |
| 845 | public String getInterfaceName() { |
| 846 | return mInterfaceName; |
| 847 | } |
| 848 | |
| 849 | public Network getUnderlyingNetwork() { |
| 850 | return mUnderlyingNetwork; |
| 851 | } |
| 852 | |
| 853 | /** Returns the local, outer address for the tunnelInterface */ |
| 854 | public String getLocalAddress() { |
| 855 | return mLocalAddress; |
| 856 | } |
| 857 | |
| 858 | /** Returns the remote, outer address for the tunnelInterface */ |
| 859 | public String getRemoteAddress() { |
| 860 | return mRemoteAddress; |
| 861 | } |
| 862 | |
| 863 | public int getIkey() { |
| 864 | return mIkey; |
| 865 | } |
| 866 | |
| 867 | public int getOkey() { |
| 868 | return mOkey; |
| 869 | } |
| 870 | |
| 871 | @Override |
| 872 | protected ResourceTracker getResourceTracker() { |
| 873 | return getUserRecord().mTunnelQuotaTracker; |
| 874 | } |
| 875 | |
| 876 | @Override |
| 877 | public void invalidate() { |
| 878 | getUserRecord().removeTunnelInterfaceRecord(mResourceId); |
| 879 | } |
| 880 | |
| 881 | @Override |
| 882 | public String toString() { |
| 883 | return new StringBuilder() |
| 884 | .append("{super=") |
| 885 | .append(super.toString()) |
| 886 | .append(", mInterfaceName=") |
| 887 | .append(mInterfaceName) |
| 888 | .append(", mUnderlyingNetwork=") |
| 889 | .append(mUnderlyingNetwork) |
| 890 | .append(", mLocalAddress=") |
| 891 | .append(mLocalAddress) |
| 892 | .append(", mRemoteAddress=") |
| 893 | .append(mRemoteAddress) |
| 894 | .append(", mIkey=") |
| 895 | .append(mIkey) |
| 896 | .append(", mOkey=") |
| 897 | .append(mOkey) |
| 898 | .append("}") |
| 899 | .toString(); |
| 900 | } |
| 901 | } |
| 902 | |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 903 | /** |
| 904 | * Tracks a UDP encap socket, and manages cleanup paths |
| 905 | * |
| 906 | * <p>While this class does not manage non-kernel resources, race conditions around socket |
| 907 | * binding require that the service creates the encap socket, binds it and applies the socket |
| 908 | * policy before handing it to a user. |
| 909 | */ |
| 910 | private final class EncapSocketRecord extends OwnedResourceRecord { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 911 | private FileDescriptor mSocket; |
| 912 | private final int mPort; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 913 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 914 | EncapSocketRecord(int resourceId, FileDescriptor socket, int port) { |
| 915 | super(resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 916 | mSocket = socket; |
| 917 | mPort = port; |
| 918 | } |
| 919 | |
| 920 | /** always guarded by IpSecService#this */ |
| 921 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 922 | public void freeUnderlyingResources() { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 923 | Log.d(TAG, "Closing port " + mPort); |
| 924 | IoUtils.closeQuietly(mSocket); |
| 925 | mSocket = null; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 926 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 927 | getResourceTracker().give(); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 928 | } |
| 929 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 930 | public int getPort() { |
| 931 | return mPort; |
| 932 | } |
| 933 | |
| 934 | public FileDescriptor getSocket() { |
| 935 | return mSocket; |
| 936 | } |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 937 | |
| 938 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 939 | protected ResourceTracker getResourceTracker() { |
| 940 | return getUserRecord().mSocketQuotaTracker; |
| 941 | } |
| 942 | |
| 943 | @Override |
| 944 | public void invalidate() { |
| 945 | getUserRecord().removeEncapSocketRecord(mResourceId); |
| 946 | } |
| 947 | |
| 948 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 949 | public String toString() { |
| 950 | return new StringBuilder() |
| 951 | .append("{super=") |
| 952 | .append(super.toString()) |
| 953 | .append(", mSocket=") |
| 954 | .append(mSocket) |
| 955 | .append(", mPort=") |
| 956 | .append(mPort) |
| 957 | .append("}") |
| 958 | .toString(); |
| 959 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 960 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 961 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 962 | /** |
| 963 | * Constructs a new IpSecService instance |
| 964 | * |
| 965 | * @param context Binder context for this service |
| 966 | */ |
| 967 | private IpSecService(Context context) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 968 | this(context, IpSecServiceConfiguration.GETSRVINSTANCE); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | static IpSecService create(Context context) throws InterruptedException { |
| 972 | final IpSecService service = new IpSecService(context); |
| 973 | service.connectNativeNetdService(); |
| 974 | return service; |
| 975 | } |
| 976 | |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 977 | /** @hide */ |
| 978 | @VisibleForTesting |
| 979 | public IpSecService(Context context, IpSecServiceConfiguration config) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 980 | this( |
| 981 | context, |
| 982 | config, |
| 983 | (fd, uid) -> { |
| 984 | try { |
| 985 | TrafficStats.setThreadStatsUid(uid); |
| 986 | TrafficStats.tagFileDescriptor(fd); |
| 987 | } finally { |
| 988 | TrafficStats.clearThreadStatsUid(); |
| 989 | } |
| 990 | }); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | /** @hide */ |
| 994 | @VisibleForTesting |
| 995 | public IpSecService( |
| 996 | Context context, IpSecServiceConfiguration config, UidFdTagger uidFdTagger) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 997 | mContext = context; |
| 998 | mSrvConfig = config; |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 999 | mUidFdTagger = uidFdTagger; |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1000 | } |
| 1001 | |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1002 | public void systemReady() { |
| 1003 | if (isNetdAlive()) { |
| 1004 | Slog.d(TAG, "IpSecService is ready"); |
| 1005 | } else { |
| 1006 | Slog.wtf(TAG, "IpSecService not ready: failed to connect to NetD Native Service!"); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | private void connectNativeNetdService() { |
| 1011 | // Avoid blocking the system server to do this |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 1012 | new Thread() { |
| 1013 | @Override |
| 1014 | public void run() { |
| 1015 | synchronized (IpSecService.this) { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1016 | NetdService.get(NETD_FETCH_TIMEOUT_MS); |
Nathan Harold | b0e0508 | 2017-07-17 14:01:53 -0700 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | }.start(); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1020 | } |
| 1021 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1022 | synchronized boolean isNetdAlive() { |
| 1023 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1024 | final INetd netd = mSrvConfig.getNetdInstance(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1025 | if (netd == null) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1026 | return false; |
| 1027 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1028 | return netd.isAlive(); |
| 1029 | } catch (RemoteException re) { |
| 1030 | return false; |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1031 | } |
| 1032 | } |
| 1033 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1034 | /** |
| 1035 | * Checks that the provided InetAddress is valid for use in an IPsec SA. The address must not be |
| 1036 | * a wildcard address and must be in a numeric form such as 1.2.3.4 or 2001::1. |
| 1037 | */ |
| 1038 | private static void checkInetAddress(String inetAddress) { |
| 1039 | if (TextUtils.isEmpty(inetAddress)) { |
| 1040 | throw new IllegalArgumentException("Unspecified address"); |
| 1041 | } |
| 1042 | |
| 1043 | InetAddress checkAddr = NetworkUtils.numericToInetAddress(inetAddress); |
| 1044 | |
| 1045 | if (checkAddr.isAnyLocalAddress()) { |
| 1046 | throw new IllegalArgumentException("Inappropriate wildcard address: " + inetAddress); |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * Checks the user-provided direction field and throws an IllegalArgumentException if it is not |
| 1052 | * DIRECTION_IN or DIRECTION_OUT |
| 1053 | */ |
| 1054 | private static void checkDirection(int direction) { |
| 1055 | switch (direction) { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1056 | case IpSecManager.DIRECTION_OUT: |
| 1057 | case IpSecManager.DIRECTION_IN: |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1058 | return; |
| 1059 | } |
| 1060 | throw new IllegalArgumentException("Invalid Direction: " + direction); |
| 1061 | } |
| 1062 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1063 | /** Get a new SPI and maintain the reservation in the system server */ |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 1064 | @Override |
| 1065 | public synchronized IpSecSpiResponse allocateSecurityParameterIndex( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1066 | String destinationAddress, int requestedSpi, IBinder binder) throws RemoteException { |
| 1067 | checkInetAddress(destinationAddress); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1068 | /* requestedSpi can be anything in the int range, so no check is needed. */ |
Jonathan Basseri | 5fb9290 | 2017-11-16 10:58:01 -0800 | [diff] [blame] | 1069 | checkNotNull(binder, "Null Binder passed to allocateSecurityParameterIndex"); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1070 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1071 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1072 | final int resourceId = mNextResourceId++; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1073 | |
| 1074 | int spi = IpSecManager.INVALID_SECURITY_PARAMETER_INDEX; |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1075 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1076 | if (!userRecord.mSpiQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1077 | return new IpSecSpiResponse( |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1078 | IpSecManager.Status.RESOURCE_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1079 | } |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1080 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1081 | spi = |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1082 | mSrvConfig |
| 1083 | .getNetdInstance() |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1084 | .ipSecAllocateSpi(resourceId, "", destinationAddress, requestedSpi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1085 | Log.d(TAG, "Allocated SPI " + spi); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1086 | userRecord.mSpiRecords.put( |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1087 | resourceId, |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1088 | new RefcountedResource<SpiRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1089 | new SpiRecord(resourceId, "", destinationAddress, spi), binder)); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1090 | } catch (ServiceSpecificException e) { |
| 1091 | // TODO: Add appropriate checks when other ServiceSpecificException types are supported |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1092 | return new IpSecSpiResponse( |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1093 | IpSecManager.Status.SPI_UNAVAILABLE, INVALID_RESOURCE_ID, spi); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1094 | } catch (RemoteException e) { |
| 1095 | throw e.rethrowFromSystemServer(); |
| 1096 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1097 | return new IpSecSpiResponse(IpSecManager.Status.OK, resourceId, spi); |
| 1098 | } |
| 1099 | |
| 1100 | /* This method should only be called from Binder threads. Do not call this from |
| 1101 | * within the system server as it will crash the system on failure. |
| 1102 | */ |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1103 | private void releaseResource(RefcountedResourceArray resArray, int resourceId) |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1104 | throws RemoteException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1105 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1106 | resArray.getRefcountedResourceOrThrow(resourceId).userRelease(); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | /** Release a previously allocated SPI that has been registered with the system server */ |
| 1110 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1111 | public synchronized void releaseSecurityParameterIndex(int resourceId) throws RemoteException { |
| 1112 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1113 | releaseResource(userRecord.mSpiRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * This function finds and forcibly binds to a random system port, ensuring that the port cannot |
| 1118 | * be unbound. |
| 1119 | * |
| 1120 | * <p>A socket cannot be un-bound from a port if it was bound to that port by number. To select |
| 1121 | * a random open port and then bind by number, this function creates a temp socket, binds to a |
| 1122 | * random port (specifying 0), gets that port number, and then uses is to bind the user's UDP |
| 1123 | * Encapsulation Socket forcibly, so that it cannot be un-bound by the user with the returned |
| 1124 | * FileHandle. |
| 1125 | * |
| 1126 | * <p>The loop in this function handles the inherent race window between un-binding to a port |
| 1127 | * and re-binding, during which the system could *technically* hand that port out to someone |
| 1128 | * else. |
| 1129 | */ |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 1130 | private int bindToRandomPort(FileDescriptor sockFd) throws IOException { |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1131 | for (int i = MAX_PORT_BIND_ATTEMPTS; i > 0; i--) { |
| 1132 | try { |
| 1133 | FileDescriptor probeSocket = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 1134 | Os.bind(probeSocket, INADDR_ANY, 0); |
| 1135 | int port = ((InetSocketAddress) Os.getsockname(probeSocket)).getPort(); |
| 1136 | Os.close(probeSocket); |
| 1137 | Log.v(TAG, "Binding to port " + port); |
| 1138 | Os.bind(sockFd, INADDR_ANY, port); |
Benedict Wong | f186d67 | 2017-10-10 20:44:28 -0700 | [diff] [blame] | 1139 | return port; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1140 | } catch (ErrnoException e) { |
| 1141 | // Someone miraculously claimed the port just after we closed probeSocket. |
| 1142 | if (e.errno == OsConstants.EADDRINUSE) { |
| 1143 | continue; |
| 1144 | } |
| 1145 | throw e.rethrowAsIOException(); |
| 1146 | } |
| 1147 | } |
| 1148 | throw new IOException("Failed " + MAX_PORT_BIND_ATTEMPTS + " attempts to bind to a port"); |
| 1149 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1150 | |
| 1151 | /** |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1152 | * Functional interface to do traffic tagging of given sockets to UIDs. |
| 1153 | * |
| 1154 | * <p>Specifically used by openUdpEncapsulationSocket to ensure data usage on the UDP encap |
| 1155 | * sockets are billed to the UID that the UDP encap socket was created on behalf of. |
| 1156 | * |
| 1157 | * <p>Separate class so that the socket tagging logic can be mocked; TrafficStats uses static |
| 1158 | * methods that cannot be easily mocked/tested. |
| 1159 | */ |
| 1160 | @VisibleForTesting |
| 1161 | public interface UidFdTagger { |
| 1162 | /** |
| 1163 | * Sets socket tag to assign all traffic to the provided UID. |
| 1164 | * |
| 1165 | * <p>Since the socket is created on behalf of an unprivileged application, all traffic |
| 1166 | * should be accounted to the UID of the unprivileged application. |
| 1167 | */ |
| 1168 | public void tag(FileDescriptor fd, int uid) throws IOException; |
| 1169 | } |
| 1170 | |
| 1171 | /** |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1172 | * Open a socket via the system server and bind it to the specified port (random if port=0). |
| 1173 | * This will return a PFD to the user that represent a bound UDP socket. The system server will |
| 1174 | * cache the socket and a record of its owner so that it can and must be freed when no longer |
| 1175 | * needed. |
| 1176 | */ |
| 1177 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1178 | public synchronized IpSecUdpEncapResponse openUdpEncapsulationSocket(int port, IBinder binder) |
| 1179 | throws RemoteException { |
| 1180 | if (port != 0 && (port < FREE_PORT_MIN || port > PORT_MAX)) { |
| 1181 | throw new IllegalArgumentException( |
| 1182 | "Specified port number must be a valid non-reserved UDP port"); |
| 1183 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1184 | checkNotNull(binder, "Null Binder passed to openUdpEncapsulationSocket"); |
| 1185 | |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1186 | int callingUid = Binder.getCallingUid(); |
| 1187 | UserRecord userRecord = mUserResourceTracker.getUserRecord(callingUid); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1188 | final int resourceId = mNextResourceId++; |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1189 | FileDescriptor sockFd = null; |
| 1190 | try { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1191 | if (!userRecord.mSocketQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1192 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1193 | } |
| 1194 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1195 | sockFd = Os.socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); |
Benedict Wong | babe5d7 | 2017-12-03 19:42:36 -0800 | [diff] [blame] | 1196 | mUidFdTagger.tag(sockFd, callingUid); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1197 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1198 | // This code is common to both the unspecified and specified port cases |
| 1199 | Os.setsockoptInt( |
| 1200 | sockFd, |
| 1201 | OsConstants.IPPROTO_UDP, |
| 1202 | OsConstants.UDP_ENCAP, |
| 1203 | OsConstants.UDP_ENCAP_ESPINUDP); |
| 1204 | |
Benedict Wong | ba8d313 | 2017-12-06 21:56:35 -0800 | [diff] [blame] | 1205 | mSrvConfig.getNetdInstance().ipSecSetEncapSocketOwner(sockFd, callingUid); |
| 1206 | if (port != 0) { |
| 1207 | Log.v(TAG, "Binding to port " + port); |
| 1208 | Os.bind(sockFd, INADDR_ANY, port); |
| 1209 | } else { |
| 1210 | port = bindToRandomPort(sockFd); |
| 1211 | } |
| 1212 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1213 | userRecord.mEncapSocketRecords.put( |
| 1214 | resourceId, |
| 1215 | new RefcountedResource<EncapSocketRecord>( |
| 1216 | new EncapSocketRecord(resourceId, sockFd, port), binder)); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1217 | return new IpSecUdpEncapResponse(IpSecManager.Status.OK, resourceId, port, sockFd); |
| 1218 | } catch (IOException | ErrnoException e) { |
| 1219 | IoUtils.closeQuietly(sockFd); |
| 1220 | } |
| 1221 | // If we make it to here, then something has gone wrong and we couldn't open a socket. |
| 1222 | // The only reasonable condition that would cause that is resource unavailable. |
| 1223 | return new IpSecUdpEncapResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1224 | } |
| 1225 | |
| 1226 | /** close a socket that has been been allocated by and registered with the system server */ |
| 1227 | @Override |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1228 | public synchronized void closeUdpEncapsulationSocket(int resourceId) throws RemoteException { |
| 1229 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1230 | releaseResource(userRecord.mEncapSocketRecords, resourceId); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1231 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1232 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1233 | /** |
| 1234 | * Create a tunnel interface for use in IPSec tunnel mode. The system server will cache the |
| 1235 | * tunnel interface and a record of its owner so that it can and must be freed when no longer |
| 1236 | * needed. |
| 1237 | */ |
| 1238 | @Override |
| 1239 | public synchronized IpSecTunnelInterfaceResponse createTunnelInterface( |
| 1240 | String localAddr, String remoteAddr, Network underlyingNetwork, IBinder binder) { |
| 1241 | checkNotNull(binder, "Null Binder passed to createTunnelInterface"); |
| 1242 | checkNotNull(underlyingNetwork, "No underlying network was specified"); |
| 1243 | checkInetAddress(localAddr); |
| 1244 | checkInetAddress(remoteAddr); |
| 1245 | |
| 1246 | // TODO: Check that underlying network exists, and IP addresses not assigned to a different |
| 1247 | // network (b/72316676). |
| 1248 | |
| 1249 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1250 | if (!userRecord.mTunnelQuotaTracker.isAvailable()) { |
| 1251 | return new IpSecTunnelInterfaceResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1252 | } |
| 1253 | |
| 1254 | final int resourceId = mNextResourceId++; |
| 1255 | final int ikey = reserveNetId(); |
| 1256 | final int okey = reserveNetId(); |
| 1257 | String intfName = String.format("%s%d", TUNNEL_INTERFACE_PREFIX, resourceId); |
| 1258 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1259 | try { |
| 1260 | // Calls to netd: |
| 1261 | // Create VTI |
| 1262 | // Add inbound/outbound global policies |
| 1263 | // (use reqid = 0) |
| 1264 | mSrvConfig |
| 1265 | .getNetdInstance() |
| 1266 | .addVirtualTunnelInterface(intfName, localAddr, remoteAddr, ikey, okey); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1267 | |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1268 | for(String wildcardAddr : WILDCARD_ADDRESSES) { |
| 1269 | for (int direction : DIRECTIONS) { |
| 1270 | int mark = (direction == IpSecManager.DIRECTION_OUT) ? okey : ikey; |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1271 | |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1272 | mSrvConfig |
| 1273 | .getNetdInstance() |
| 1274 | .ipSecAddSecurityPolicy( |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1275 | 0, // Use 0 for reqId |
| 1276 | direction, |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1277 | wildcardAddr, |
| 1278 | wildcardAddr, |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1279 | 0, |
| 1280 | mark, |
| 1281 | 0xffffffff); |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1282 | } |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | userRecord.mTunnelInterfaceRecords.put( |
| 1286 | resourceId, |
| 1287 | new RefcountedResource<TunnelInterfaceRecord>( |
| 1288 | new TunnelInterfaceRecord( |
| 1289 | resourceId, |
| 1290 | intfName, |
| 1291 | underlyingNetwork, |
| 1292 | localAddr, |
| 1293 | remoteAddr, |
| 1294 | ikey, |
| 1295 | okey), |
| 1296 | binder)); |
| 1297 | return new IpSecTunnelInterfaceResponse(IpSecManager.Status.OK, resourceId, intfName); |
| 1298 | } catch (RemoteException e) { |
| 1299 | // Release keys if we got an error. |
| 1300 | releaseNetId(ikey); |
| 1301 | releaseNetId(okey); |
| 1302 | throw e.rethrowFromSystemServer(); |
| 1303 | } catch (ServiceSpecificException e) { |
| 1304 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 1305 | } |
| 1306 | |
| 1307 | // If we make it to here, then something has gone wrong and we couldn't create a VTI. |
| 1308 | // Release the keys that we reserved, and return an error status. |
| 1309 | releaseNetId(ikey); |
| 1310 | releaseNetId(okey); |
| 1311 | return new IpSecTunnelInterfaceResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | /** |
| 1315 | * Adds a new local address to the tunnel interface. This allows packets to be sent and received |
| 1316 | * from multiple local IP addresses over the same tunnel. |
| 1317 | */ |
| 1318 | @Override |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1319 | public synchronized void addAddressToTunnelInterface( |
| 1320 | int tunnelResourceId, LinkAddress localAddr) { |
| 1321 | enforceNetworkStackPermission(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1322 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1323 | |
| 1324 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1325 | // IllegalArgumentException |
| 1326 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1327 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1328 | |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1329 | try { |
| 1330 | // We can assume general validity of the IP address, since we get them as a |
| 1331 | // LinkAddress, which does some validation. |
| 1332 | mSrvConfig |
| 1333 | .getNetdInstance() |
| 1334 | .interfaceAddAddress( |
| 1335 | tunnelInterfaceInfo.mInterfaceName, |
| 1336 | localAddr.getAddress().getHostAddress(), |
| 1337 | localAddr.getPrefixLength()); |
| 1338 | } catch (RemoteException e) { |
| 1339 | throw e.rethrowFromSystemServer(); |
| 1340 | } catch (ServiceSpecificException e) { |
| 1341 | // If we get here, one of the arguments provided was invalid. Wrap the SSE, and throw. |
| 1342 | throw new IllegalArgumentException(e); |
| 1343 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | /** |
| 1347 | * Remove a new local address from the tunnel interface. After removal, the address will no |
| 1348 | * longer be available to send from, or receive on. |
| 1349 | */ |
| 1350 | @Override |
| 1351 | public synchronized void removeAddressFromTunnelInterface( |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1352 | int tunnelResourceId, LinkAddress localAddr) { |
| 1353 | enforceNetworkStackPermission(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1354 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1355 | |
| 1356 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1357 | // IllegalArgumentException |
| 1358 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1359 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1360 | |
Benedict Wong | da4b0c6 | 2018-03-01 18:53:07 -0800 | [diff] [blame] | 1361 | try { |
| 1362 | // We can assume general validity of the IP address, since we get them as a |
| 1363 | // LinkAddress, which does some validation. |
| 1364 | mSrvConfig |
| 1365 | .getNetdInstance() |
| 1366 | .interfaceDelAddress( |
| 1367 | tunnelInterfaceInfo.mInterfaceName, |
| 1368 | localAddr.getAddress().getHostAddress(), |
| 1369 | localAddr.getPrefixLength()); |
| 1370 | } catch (RemoteException e) { |
| 1371 | throw e.rethrowFromSystemServer(); |
| 1372 | } catch (ServiceSpecificException e) { |
| 1373 | // If we get here, one of the arguments provided was invalid. Wrap the SSE, and throw. |
| 1374 | throw new IllegalArgumentException(e); |
| 1375 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * Delete a TunnelInterface that has been been allocated by and registered with the system |
| 1380 | * server |
| 1381 | */ |
| 1382 | @Override |
| 1383 | public synchronized void deleteTunnelInterface(int resourceId) throws RemoteException { |
| 1384 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1385 | releaseResource(userRecord.mTunnelInterfaceRecords, resourceId); |
| 1386 | } |
| 1387 | |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1388 | @VisibleForTesting |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1389 | void validateAlgorithms(IpSecConfig config) throws IllegalArgumentException { |
| 1390 | IpSecAlgorithm auth = config.getAuthentication(); |
| 1391 | IpSecAlgorithm crypt = config.getEncryption(); |
| 1392 | IpSecAlgorithm aead = config.getAuthenticatedEncryption(); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1393 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1394 | // Validate the algorithm set |
| 1395 | Preconditions.checkArgument( |
| 1396 | aead != null || crypt != null || auth != null, |
| 1397 | "No Encryption or Authentication algorithms specified"); |
| 1398 | Preconditions.checkArgument( |
| 1399 | auth == null || auth.isAuthentication(), |
| 1400 | "Unsupported algorithm for Authentication"); |
| 1401 | Preconditions.checkArgument( |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1402 | crypt == null || crypt.isEncryption(), "Unsupported algorithm for Encryption"); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1403 | Preconditions.checkArgument( |
| 1404 | aead == null || aead.isAead(), |
| 1405 | "Unsupported algorithm for Authenticated Encryption"); |
| 1406 | Preconditions.checkArgument( |
| 1407 | aead == null || (auth == null && crypt == null), |
| 1408 | "Authenticated Encryption is mutually exclusive with other Authentication " |
| 1409 | + "or Encryption algorithms"); |
Benedict Wong | 4f25570 | 2017-11-06 20:49:10 -0800 | [diff] [blame] | 1410 | } |
| 1411 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1412 | /** |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1413 | * Checks an IpSecConfig parcel to ensure that the contents are sane and throws an |
| 1414 | * IllegalArgumentException if they are not. |
| 1415 | */ |
| 1416 | private void checkIpSecConfig(IpSecConfig config) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1417 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1418 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1419 | switch (config.getEncapType()) { |
| 1420 | case IpSecTransform.ENCAP_NONE: |
| 1421 | break; |
| 1422 | case IpSecTransform.ENCAP_ESPINUDP: |
| 1423 | case IpSecTransform.ENCAP_ESPINUDP_NON_IKE: |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1424 | // Retrieve encap socket record; will throw IllegalArgumentException if not found |
| 1425 | userRecord.mEncapSocketRecords.getResourceOrThrow( |
| 1426 | config.getEncapSocketResourceId()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1427 | |
| 1428 | int port = config.getEncapRemotePort(); |
| 1429 | if (port <= 0 || port > 0xFFFF) { |
| 1430 | throw new IllegalArgumentException("Invalid remote UDP port: " + port); |
| 1431 | } |
| 1432 | break; |
| 1433 | default: |
| 1434 | throw new IllegalArgumentException("Invalid Encap Type: " + config.getEncapType()); |
| 1435 | } |
| 1436 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1437 | validateAlgorithms(config); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1438 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1439 | // Retrieve SPI record; will throw IllegalArgumentException if not found |
| 1440 | SpiRecord s = userRecord.mSpiRecords.getResourceOrThrow(config.getSpiResourceId()); |
| 1441 | |
Benedict Wong | e6b4277 | 2017-12-13 18:26:40 -0800 | [diff] [blame] | 1442 | // Check to ensure that SPI has not already been used. |
| 1443 | if (s.getOwnedByTransform()) { |
| 1444 | throw new IllegalStateException("SPI already in use; cannot be used in new Transforms"); |
| 1445 | } |
| 1446 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1447 | // If no remote address is supplied, then use one from the SPI. |
| 1448 | if (TextUtils.isEmpty(config.getDestinationAddress())) { |
| 1449 | config.setDestinationAddress(s.getDestinationAddress()); |
| 1450 | } |
| 1451 | |
| 1452 | // All remote addresses must match |
| 1453 | if (!config.getDestinationAddress().equals(s.getDestinationAddress())) { |
| 1454 | throw new IllegalArgumentException("Mismatched remote addresseses."); |
| 1455 | } |
| 1456 | |
| 1457 | // This check is technically redundant due to the chain of custody between the SPI and |
| 1458 | // the IpSecConfig, but in the future if the dest is allowed to be set explicitly in |
| 1459 | // the transform, this will prevent us from messing up. |
| 1460 | checkInetAddress(config.getDestinationAddress()); |
| 1461 | |
| 1462 | // Require a valid source address for all transforms. |
| 1463 | checkInetAddress(config.getSourceAddress()); |
| 1464 | |
| 1465 | switch (config.getMode()) { |
| 1466 | case IpSecTransform.MODE_TRANSPORT: |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1467 | break; |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1468 | case IpSecTransform.MODE_TUNNEL: |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1469 | enforceNetworkStackPermission(); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1470 | break; |
| 1471 | default: |
| 1472 | throw new IllegalArgumentException( |
| 1473 | "Invalid IpSecTransform.mode: " + config.getMode()); |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1474 | } |
| 1475 | } |
| 1476 | |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1477 | private void enforceNetworkStackPermission() { |
| 1478 | mContext.enforceCallingOrSelfPermission(android.Manifest.permission.NETWORK_STACK, |
| 1479 | "IpSecService"); |
| 1480 | } |
| 1481 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1482 | private void createOrUpdateTransform( |
| 1483 | IpSecConfig c, int resourceId, SpiRecord spiRecord, EncapSocketRecord socketRecord) |
| 1484 | throws RemoteException { |
| 1485 | |
| 1486 | int encapType = c.getEncapType(), encapLocalPort = 0, encapRemotePort = 0; |
| 1487 | if (encapType != IpSecTransform.ENCAP_NONE) { |
| 1488 | encapLocalPort = socketRecord.getPort(); |
| 1489 | encapRemotePort = c.getEncapRemotePort(); |
| 1490 | } |
| 1491 | |
| 1492 | IpSecAlgorithm auth = c.getAuthentication(); |
| 1493 | IpSecAlgorithm crypt = c.getEncryption(); |
| 1494 | IpSecAlgorithm authCrypt = c.getAuthenticatedEncryption(); |
| 1495 | |
Benedict Wong | 9be845c | 2018-03-15 19:41:41 -0700 | [diff] [blame] | 1496 | String cryptName; |
| 1497 | if (crypt == null) { |
| 1498 | cryptName = (authCrypt == null) ? IpSecAlgorithm.CRYPT_NULL : ""; |
| 1499 | } else { |
| 1500 | cryptName = crypt.getName(); |
| 1501 | } |
| 1502 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1503 | mSrvConfig |
| 1504 | .getNetdInstance() |
| 1505 | .ipSecAddSecurityAssociation( |
| 1506 | resourceId, |
| 1507 | c.getMode(), |
| 1508 | c.getSourceAddress(), |
| 1509 | c.getDestinationAddress(), |
| 1510 | (c.getNetwork() != null) ? c.getNetwork().netId : 0, |
| 1511 | spiRecord.getSpi(), |
| 1512 | c.getMarkValue(), |
| 1513 | c.getMarkMask(), |
| 1514 | (auth != null) ? auth.getName() : "", |
| 1515 | (auth != null) ? auth.getKey() : new byte[] {}, |
| 1516 | (auth != null) ? auth.getTruncationLengthBits() : 0, |
Benedict Wong | 9be845c | 2018-03-15 19:41:41 -0700 | [diff] [blame] | 1517 | cryptName, |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1518 | (crypt != null) ? crypt.getKey() : new byte[] {}, |
| 1519 | (crypt != null) ? crypt.getTruncationLengthBits() : 0, |
| 1520 | (authCrypt != null) ? authCrypt.getName() : "", |
| 1521 | (authCrypt != null) ? authCrypt.getKey() : new byte[] {}, |
| 1522 | (authCrypt != null) ? authCrypt.getTruncationLengthBits() : 0, |
| 1523 | encapType, |
| 1524 | encapLocalPort, |
| 1525 | encapRemotePort); |
| 1526 | } |
| 1527 | |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1528 | /** |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1529 | * Create a IPsec transform, which represents a single security association in the kernel. The |
| 1530 | * transform will be cached by the system server and must be freed when no longer needed. It is |
| 1531 | * possible to free one, deleting the SA from underneath sockets that are using it, which will |
| 1532 | * result in all of those sockets becoming unable to send or receive data. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1533 | */ |
| 1534 | @Override |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 1535 | public synchronized IpSecTransformResponse createTransform(IpSecConfig c, IBinder binder) |
| 1536 | throws RemoteException { |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1537 | checkIpSecConfig(c); |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 1538 | checkNotNull(binder, "Null Binder passed to createTransform"); |
Nathan Harold | d8c7429 | 2017-12-13 19:16:33 -0800 | [diff] [blame] | 1539 | final int resourceId = mNextResourceId++; |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1540 | |
| 1541 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Benedict Wong | 4f9fb81 | 2017-12-13 17:16:53 -0800 | [diff] [blame] | 1542 | List<RefcountedResource> dependencies = new ArrayList<>(); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1543 | |
| 1544 | if (!userRecord.mTransformQuotaTracker.isAvailable()) { |
Nathan Harold | a1afbd8 | 2017-04-24 16:16:34 -0700 | [diff] [blame] | 1545 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
| 1546 | } |
Nathan Harold | a10003d | 2017-08-23 13:46:33 -0700 | [diff] [blame] | 1547 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1548 | EncapSocketRecord socketRecord = null; |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1549 | if (c.getEncapType() != IpSecTransform.ENCAP_NONE) { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1550 | RefcountedResource<EncapSocketRecord> refcountedSocketRecord = |
| 1551 | userRecord.mEncapSocketRecords.getRefcountedResourceOrThrow( |
| 1552 | c.getEncapSocketResourceId()); |
| 1553 | dependencies.add(refcountedSocketRecord); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1554 | socketRecord = refcountedSocketRecord.getResource(); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1555 | } |
| 1556 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1557 | RefcountedResource<SpiRecord> refcountedSpiRecord = |
| 1558 | userRecord.mSpiRecords.getRefcountedResourceOrThrow(c.getSpiResourceId()); |
| 1559 | dependencies.add(refcountedSpiRecord); |
| 1560 | SpiRecord spiRecord = refcountedSpiRecord.getResource(); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1561 | |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1562 | try { |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1563 | createOrUpdateTransform(c, resourceId, spiRecord, socketRecord); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1564 | } catch (ServiceSpecificException e) { |
| 1565 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 1566 | return new IpSecTransformResponse(IpSecManager.Status.RESOURCE_UNAVAILABLE); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1567 | } |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1568 | |
| 1569 | // SA was created successfully, time to construct a record and lock it away |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1570 | userRecord.mTransformRecords.put( |
| 1571 | resourceId, |
| 1572 | new RefcountedResource<TransformRecord>( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1573 | new TransformRecord(resourceId, c, spiRecord, socketRecord), |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1574 | binder, |
| 1575 | dependencies.toArray(new RefcountedResource[dependencies.size()]))); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1576 | return new IpSecTransformResponse(IpSecManager.Status.OK, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | /** |
| 1580 | * Delete a transport mode transform that was previously allocated by + registered with the |
| 1581 | * system server. If this is called on an inactive (or non-existent) transform, it will not |
| 1582 | * return an error. It's safe to de-allocate transforms that may have already been deleted for |
| 1583 | * other reasons. |
| 1584 | */ |
| 1585 | @Override |
Benedict Wong | f33f0313 | 2018-01-18 14:38:16 -0800 | [diff] [blame] | 1586 | public synchronized void deleteTransform(int resourceId) throws RemoteException { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1587 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1588 | releaseResource(userRecord.mTransformRecords, resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * Apply an active transport mode transform to a socket, which will apply the IPsec security |
| 1593 | * association as a correspondent policy to the provided socket |
| 1594 | */ |
| 1595 | @Override |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1596 | public synchronized void applyTransportModeTransform( |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1597 | ParcelFileDescriptor socket, int direction, int resourceId) throws RemoteException { |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1598 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1599 | checkDirection(direction); |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1600 | // Get transform record; if no transform is found, will throw IllegalArgumentException |
| 1601 | TransformRecord info = userRecord.mTransformRecords.getResourceOrThrow(resourceId); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1602 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1603 | // TODO: make this a function. |
| 1604 | if (info.pid != getCallingPid() || info.uid != getCallingUid()) { |
| 1605 | throw new SecurityException("Only the owner of an IpSec Transform may apply it!"); |
| 1606 | } |
| 1607 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1608 | // Get config and check that to-be-applied transform has the correct mode |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1609 | IpSecConfig c = info.getConfig(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1610 | Preconditions.checkArgument( |
| 1611 | c.getMode() == IpSecTransform.MODE_TRANSPORT, |
| 1612 | "Transform mode was not Transport mode; cannot be applied to a socket"); |
| 1613 | |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1614 | try { |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1615 | mSrvConfig |
| 1616 | .getNetdInstance() |
| 1617 | .ipSecApplyTransportModeTransform( |
| 1618 | socket.getFileDescriptor(), |
| 1619 | resourceId, |
| 1620 | direction, |
| 1621 | c.getSourceAddress(), |
| 1622 | c.getDestinationAddress(), |
| 1623 | info.getSpiRecord().getSpi()); |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1624 | } catch (ServiceSpecificException e) { |
manojboopathi | c4be79d | 2017-11-30 17:11:49 -0800 | [diff] [blame] | 1625 | if (e.errorCode == EINVAL) { |
| 1626 | throw new IllegalArgumentException(e.toString()); |
| 1627 | } else { |
| 1628 | throw e; |
| 1629 | } |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1630 | } |
| 1631 | } |
Nathan Harold | 8dc1fd0 | 2017-04-04 19:37:48 -0700 | [diff] [blame] | 1632 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1633 | /** |
Nathan Harold | a252331 | 2018-01-05 19:25:13 -0800 | [diff] [blame] | 1634 | * Remove transport mode transforms from a socket, applying the default (empty) policy. This |
| 1635 | * ensures that NO IPsec policy is applied to the socket (would be the equivalent of applying a |
| 1636 | * policy that performs no IPsec). Today the resourceId parameter is passed but not used: |
| 1637 | * reserved for future improved input validation. |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1638 | */ |
| 1639 | @Override |
Nathan Harold | f73d252 | 2018-01-17 01:00:20 -0800 | [diff] [blame] | 1640 | public synchronized void removeTransportModeTransforms(ParcelFileDescriptor socket) |
| 1641 | throws RemoteException { |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1642 | try { |
ludi | 1a06aa7 | 2017-05-12 09:15:00 -0700 | [diff] [blame] | 1643 | mSrvConfig |
| 1644 | .getNetdInstance() |
| 1645 | .ipSecRemoveTransportModeTransform(socket.getFileDescriptor()); |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1646 | } catch (ServiceSpecificException e) { |
| 1647 | // FIXME: get the error code and throw is at an IOException from Errno Exception |
| 1648 | } |
| 1649 | } |
| 1650 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1651 | /** |
| 1652 | * Apply an active tunnel mode transform to a TunnelInterface, which will apply the IPsec |
| 1653 | * security association as a correspondent policy to the provided interface |
| 1654 | */ |
| 1655 | @Override |
| 1656 | public synchronized void applyTunnelModeTransform( |
| 1657 | int tunnelResourceId, int direction, int transformResourceId) throws RemoteException { |
Nathan Harold | 5a920ca | 2018-02-02 18:34:25 -0800 | [diff] [blame] | 1658 | enforceNetworkStackPermission(); |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1659 | checkDirection(direction); |
| 1660 | |
| 1661 | UserRecord userRecord = mUserResourceTracker.getUserRecord(Binder.getCallingUid()); |
| 1662 | |
| 1663 | // Get transform record; if no transform is found, will throw IllegalArgumentException |
| 1664 | TransformRecord transformInfo = |
| 1665 | userRecord.mTransformRecords.getResourceOrThrow(transformResourceId); |
| 1666 | |
| 1667 | // Get tunnelInterface record; if no such interface is found, will throw |
| 1668 | // IllegalArgumentException |
| 1669 | TunnelInterfaceRecord tunnelInterfaceInfo = |
| 1670 | userRecord.mTunnelInterfaceRecords.getResourceOrThrow(tunnelResourceId); |
| 1671 | |
| 1672 | // Get config and check that to-be-applied transform has the correct mode |
| 1673 | IpSecConfig c = transformInfo.getConfig(); |
| 1674 | Preconditions.checkArgument( |
| 1675 | c.getMode() == IpSecTransform.MODE_TUNNEL, |
| 1676 | "Transform mode was not Tunnel mode; cannot be applied to a tunnel interface"); |
| 1677 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1678 | EncapSocketRecord socketRecord = null; |
| 1679 | if (c.getEncapType() != IpSecTransform.ENCAP_NONE) { |
| 1680 | socketRecord = |
| 1681 | userRecord.mEncapSocketRecords.getResourceOrThrow(c.getEncapSocketResourceId()); |
| 1682 | } |
| 1683 | SpiRecord spiRecord = userRecord.mSpiRecords.getResourceOrThrow(c.getSpiResourceId()); |
| 1684 | |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1685 | int mark = |
| 1686 | (direction == IpSecManager.DIRECTION_IN) |
| 1687 | ? tunnelInterfaceInfo.getIkey() |
| 1688 | : tunnelInterfaceInfo.getOkey(); |
| 1689 | |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1690 | try { |
| 1691 | c.setMarkValue(mark); |
| 1692 | c.setMarkMask(0xffffffff); |
| 1693 | |
| 1694 | if (direction == IpSecManager.DIRECTION_OUT) { |
| 1695 | // Set output mark via underlying network (output only) |
| 1696 | c.setNetwork(tunnelInterfaceInfo.getUnderlyingNetwork()); |
| 1697 | |
| 1698 | // If outbound, also add SPI to the policy. |
Benedict Wong | b8ef541 | 2018-01-24 15:31:39 -0800 | [diff] [blame] | 1699 | for(String wildcardAddr : WILDCARD_ADDRESSES) { |
| 1700 | mSrvConfig |
| 1701 | .getNetdInstance() |
| 1702 | .ipSecUpdateSecurityPolicy( |
| 1703 | 0, // Use 0 for reqId |
| 1704 | direction, |
| 1705 | wildcardAddr, |
| 1706 | wildcardAddr, |
| 1707 | transformInfo.getSpiRecord().getSpi(), |
| 1708 | mark, |
| 1709 | 0xffffffff); |
| 1710 | } |
Benedict Wong | 0fe58a9 | 2018-01-19 17:36:02 -0800 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | // Update SA with tunnel mark (ikey or okey based on direction) |
| 1714 | createOrUpdateTransform(c, transformResourceId, spiRecord, socketRecord); |
| 1715 | } catch (ServiceSpecificException e) { |
| 1716 | if (e.errorCode == EINVAL) { |
| 1717 | throw new IllegalArgumentException(e.toString()); |
| 1718 | } else { |
| 1719 | throw e; |
| 1720 | } |
| 1721 | } |
Benedict Wong | 8149f6e | 2018-01-18 18:31:45 -0800 | [diff] [blame] | 1722 | } |
| 1723 | |
Nathan Harold | 93962f3 | 2017-03-07 13:23:36 -0800 | [diff] [blame] | 1724 | @Override |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1725 | protected synchronized void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1726 | mContext.enforceCallingOrSelfPermission(DUMP, TAG); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1727 | |
| 1728 | pw.println("IpSecService dump:"); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1729 | pw.println("NetdNativeService Connection: " + (isNetdAlive() ? "alive" : "dead")); |
| 1730 | pw.println(); |
ludi | b0c95b1 | 2017-05-22 10:52:23 -0700 | [diff] [blame] | 1731 | |
Benedict Wong | 344bd62 | 2017-11-16 15:27:22 -0800 | [diff] [blame] | 1732 | pw.println("mUserResourceTracker:"); |
| 1733 | pw.println(mUserResourceTracker); |
Nathan Harold | 1afbef4 | 2017-03-01 18:55:06 -0800 | [diff] [blame] | 1734 | } |
| 1735 | } |