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