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