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