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