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