| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.net.vcn; |
| 17 |
|
| 18 |
import static android.net.ipsec.ike.IkeSessionParams.IKE_OPTION_MOBIKE; |
| 19 |
import static android.net.vcn.VcnUnderlyingNetworkTemplate.MATCH_REQUIRED; |
| 20 |
|
| 21 |
import static com.android.internal.annotations.VisibleForTesting.Visibility; |
| 22 |
|
| 23 |
import android.annotation.IntDef; |
| 24 |
import android.annotation.IntRange; |
| 25 |
import android.annotation.NonNull; |
| 26 |
import android.annotation.Nullable; |
| 27 |
import android.annotation.SuppressLint; |
| 28 |
import android.net.Network; |
| 29 |
import android.net.NetworkCapabilities; |
| 30 |
import android.net.ipsec.ike.IkeTunnelConnectionParams; |
| 31 |
import android.net.vcn.persistablebundleutils.TunnelConnectionParamsUtils; |
| 32 |
import android.os.PersistableBundle; |
| 33 |
import android.util.ArraySet; |
| 34 |
|
| 35 |
import com.android.internal.annotations.VisibleForTesting; |
| 36 |
import com.android.internal.util.ArrayUtils; |
| 37 |
import com.android.internal.util.Preconditions; |
| 38 |
import com.android.server.vcn.util.PersistableBundleUtils; |
| 39 |
|
| 40 |
import java.lang.annotation.Retention; |
| 41 |
import java.lang.annotation.RetentionPolicy; |
| 42 |
import java.util.ArrayList; |
| 43 |
import java.util.Arrays; |
| 44 |
import java.util.Collections; |
| 45 |
import java.util.List; |
| 46 |
import java.util.Objects; |
| 47 |
import java.util.Set; |
| 48 |
import java.util.SortedSet; |
| 49 |
import java.util.TreeSet; |
| 50 |
import java.util.concurrent.TimeUnit; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public final class VcnGatewayConnectionConfig { |
| 84 |
|
| 85 |
public static final int MIN_UDP_PORT_4500_NAT_TIMEOUT_UNSET = -1; |
| 86 |
|
| 87 |
|
| 88 |
public static final int MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS = 120; |
| 89 |
|
| 90 |
|
| 91 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 92 |
static final int MIN_MTU_V6 = 1280; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final Set<Integer> ALLOWED_CAPABILITIES; |
| 100 |
|
| 101 |
static { |
| 102 |
Set<Integer> allowedCaps = new ArraySet<>(); |
| 103 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_MMS); |
| 104 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_SUPL); |
| 105 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_DUN); |
| 106 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_FOTA); |
| 107 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_IMS); |
| 108 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_CBS); |
| 109 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_IA); |
| 110 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_RCS); |
| 111 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_XCAP); |
| 112 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_EIMS); |
| 113 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_INTERNET); |
| 114 |
allowedCaps.add(NetworkCapabilities.NET_CAPABILITY_MCX); |
| 115 |
|
| 116 |
ALLOWED_CAPABILITIES = Collections.unmodifiableSet(allowedCaps); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
@Retention(RetentionPolicy.SOURCE) |
| 121 |
@IntDef( |
| 122 |
prefix = {"NET_CAPABILITY_"}, |
| 123 |
value = { |
| 124 |
NetworkCapabilities.NET_CAPABILITY_MMS, |
| 125 |
NetworkCapabilities.NET_CAPABILITY_SUPL, |
| 126 |
NetworkCapabilities.NET_CAPABILITY_DUN, |
| 127 |
NetworkCapabilities.NET_CAPABILITY_FOTA, |
| 128 |
NetworkCapabilities.NET_CAPABILITY_IMS, |
| 129 |
NetworkCapabilities.NET_CAPABILITY_CBS, |
| 130 |
NetworkCapabilities.NET_CAPABILITY_IA, |
| 131 |
NetworkCapabilities.NET_CAPABILITY_RCS, |
| 132 |
NetworkCapabilities.NET_CAPABILITY_XCAP, |
| 133 |
NetworkCapabilities.NET_CAPABILITY_EIMS, |
| 134 |
NetworkCapabilities.NET_CAPABILITY_INTERNET, |
| 135 |
NetworkCapabilities.NET_CAPABILITY_MCX, |
| 136 |
}) |
| 137 |
public @interface VcnSupportedCapability {} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static final int VCN_GATEWAY_OPTION_ENABLE_DATA_STALL_RECOVERY_WITH_MOBILITY = 0; |
| 147 |
|
| 148 |
|
| 149 |
@Retention(RetentionPolicy.SOURCE) |
| 150 |
@IntDef( |
| 151 |
prefix = {"VCN_GATEWAY_OPTION_"}, |
| 152 |
value = { |
| 153 |
VCN_GATEWAY_OPTION_ENABLE_DATA_STALL_RECOVERY_WITH_MOBILITY, |
| 154 |
}) |
| 155 |
public @interface VcnGatewayOption {} |
| 156 |
|
| 157 |
private static final Set<Integer> ALLOWED_GATEWAY_OPTIONS = new ArraySet<>(); |
| 158 |
|
| 159 |
static { |
| 160 |
ALLOWED_GATEWAY_OPTIONS.add(VCN_GATEWAY_OPTION_ENABLE_DATA_STALL_RECOVERY_WITH_MOBILITY); |
| 161 |
} |
| 162 |
|
| 163 |
private static final int DEFAULT_MAX_MTU = 1500; |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
private static final int MAX_RETRY_INTERVAL_COUNT = 10; |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private static final long MINIMUM_REPEATING_RETRY_INTERVAL_MS = TimeUnit.MINUTES.toMillis(15); |
| 181 |
|
| 182 |
private static final long[] DEFAULT_RETRY_INTERVALS_MS = |
| 183 |
new long[] { |
| 184 |
TimeUnit.SECONDS.toMillis(1), |
| 185 |
TimeUnit.SECONDS.toMillis(2), |
| 186 |
TimeUnit.SECONDS.toMillis(5), |
| 187 |
TimeUnit.SECONDS.toMillis(30), |
| 188 |
TimeUnit.MINUTES.toMillis(1), |
| 189 |
TimeUnit.MINUTES.toMillis(5), |
| 190 |
TimeUnit.MINUTES.toMillis(15) |
| 191 |
}; |
| 192 |
|
| 193 |
|
| 194 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 195 |
public static final List<VcnUnderlyingNetworkTemplate> DEFAULT_UNDERLYING_NETWORK_TEMPLATES = |
| 196 |
new ArrayList<>(); |
| 197 |
|
| 198 |
static { |
| 199 |
DEFAULT_UNDERLYING_NETWORK_TEMPLATES.add( |
| 200 |
new VcnCellUnderlyingNetworkTemplate.Builder() |
| 201 |
.setOpportunistic(MATCH_REQUIRED) |
| 202 |
.build()); |
| 203 |
|
| 204 |
DEFAULT_UNDERLYING_NETWORK_TEMPLATES.add( |
| 205 |
new VcnWifiUnderlyingNetworkTemplate.Builder() |
| 206 |
.build()); |
| 207 |
|
| 208 |
DEFAULT_UNDERLYING_NETWORK_TEMPLATES.add( |
| 209 |
new VcnCellUnderlyingNetworkTemplate.Builder() |
| 210 |
.build()); |
| 211 |
} |
| 212 |
|
| 213 |
private static final String GATEWAY_CONNECTION_NAME_KEY = "mGatewayConnectionName"; |
| 214 |
@NonNull private final String mGatewayConnectionName; |
| 215 |
|
| 216 |
private static final String TUNNEL_CONNECTION_PARAMS_KEY = "mTunnelConnectionParams"; |
| 217 |
@NonNull private IkeTunnelConnectionParams mTunnelConnectionParams; |
| 218 |
|
| 219 |
private static final String EXPOSED_CAPABILITIES_KEY = "mExposedCapabilities"; |
| 220 |
@NonNull private final SortedSet<Integer> mExposedCapabilities; |
| 221 |
|
| 222 |
|
| 223 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 224 |
public static final String UNDERLYING_NETWORK_TEMPLATES_KEY = "mUnderlyingNetworkTemplates"; |
| 225 |
|
| 226 |
@NonNull private final List<VcnUnderlyingNetworkTemplate> mUnderlyingNetworkTemplates; |
| 227 |
|
| 228 |
private static final String MAX_MTU_KEY = "mMaxMtu"; |
| 229 |
private final int mMaxMtu; |
| 230 |
|
| 231 |
private static final String RETRY_INTERVAL_MS_KEY = "mRetryIntervalsMs"; |
| 232 |
@NonNull private final long[] mRetryIntervalsMs; |
| 233 |
|
| 234 |
private static final String MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS_KEY = |
| 235 |
"mMinUdpPort4500NatTimeoutSeconds"; |
| 236 |
private final int mMinUdpPort4500NatTimeoutSeconds; |
| 237 |
|
| 238 |
private static final String GATEWAY_OPTIONS_KEY = "mGatewayOptions"; |
| 239 |
@NonNull private final Set<Integer> mGatewayOptions; |
| 240 |
|
| 241 |
|
| 242 |
private VcnGatewayConnectionConfig( |
| 243 |
@NonNull String gatewayConnectionName, |
| 244 |
@NonNull IkeTunnelConnectionParams tunnelConnectionParams, |
| 245 |
@NonNull Set<Integer> exposedCapabilities, |
| 246 |
@NonNull List<VcnUnderlyingNetworkTemplate> underlyingNetworkTemplates, |
| 247 |
@NonNull long[] retryIntervalsMs, |
| 248 |
@IntRange(from = MIN_MTU_V6) int maxMtu, |
| 249 |
@NonNull int minUdpPort4500NatTimeoutSeconds, |
| 250 |
@NonNull Set<Integer> gatewayOptions) { |
| 251 |
mGatewayConnectionName = gatewayConnectionName; |
| 252 |
mTunnelConnectionParams = tunnelConnectionParams; |
| 253 |
mExposedCapabilities = new TreeSet(exposedCapabilities); |
| 254 |
mRetryIntervalsMs = retryIntervalsMs; |
| 255 |
mMaxMtu = maxMtu; |
| 256 |
mMinUdpPort4500NatTimeoutSeconds = minUdpPort4500NatTimeoutSeconds; |
| 257 |
mGatewayOptions = Collections.unmodifiableSet(new ArraySet(gatewayOptions)); |
| 258 |
|
| 259 |
mUnderlyingNetworkTemplates = new ArrayList<>(underlyingNetworkTemplates); |
| 260 |
if (mUnderlyingNetworkTemplates.isEmpty()) { |
| 261 |
mUnderlyingNetworkTemplates.addAll(DEFAULT_UNDERLYING_NETWORK_TEMPLATES); |
| 262 |
} |
| 263 |
|
| 264 |
validate(); |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
@VisibleForTesting(visibility = Visibility.PRIVATE) |
| 271 |
public VcnGatewayConnectionConfig(@NonNull PersistableBundle in) { |
| 272 |
final PersistableBundle tunnelConnectionParamsBundle = |
| 273 |
in.getPersistableBundle(TUNNEL_CONNECTION_PARAMS_KEY); |
| 274 |
Objects.requireNonNull( |
| 275 |
tunnelConnectionParamsBundle, "tunnelConnectionParamsBundle was null"); |
| 276 |
|
| 277 |
final PersistableBundle exposedCapsBundle = |
| 278 |
in.getPersistableBundle(EXPOSED_CAPABILITIES_KEY); |
| 279 |
mGatewayConnectionName = in.getString(GATEWAY_CONNECTION_NAME_KEY); |
| 280 |
mTunnelConnectionParams = |
| 281 |
TunnelConnectionParamsUtils.fromPersistableBundle(tunnelConnectionParamsBundle); |
| 282 |
mExposedCapabilities = new TreeSet<>(PersistableBundleUtils.toList( |
| 283 |
exposedCapsBundle, PersistableBundleUtils.INTEGER_DESERIALIZER)); |
| 284 |
|
| 285 |
final PersistableBundle networkTemplatesBundle = |
| 286 |
in.getPersistableBundle(UNDERLYING_NETWORK_TEMPLATES_KEY); |
| 287 |
|
| 288 |
if (networkTemplatesBundle == null) { |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
mUnderlyingNetworkTemplates = new ArrayList<>(DEFAULT_UNDERLYING_NETWORK_TEMPLATES); |
| 293 |
} else { |
| 294 |
mUnderlyingNetworkTemplates = |
| 295 |
PersistableBundleUtils.toList( |
| 296 |
networkTemplatesBundle, |
| 297 |
VcnUnderlyingNetworkTemplate::fromPersistableBundle); |
| 298 |
} |
| 299 |
|
| 300 |
final PersistableBundle gatewayOptionsBundle = in.getPersistableBundle(GATEWAY_OPTIONS_KEY); |
| 301 |
|
| 302 |
if (gatewayOptionsBundle == null) { |
| 303 |
|
| 304 |
|
| 305 |
mGatewayOptions = Collections.emptySet(); |
| 306 |
} else { |
| 307 |
mGatewayOptions = |
| 308 |
new ArraySet<>( |
| 309 |
PersistableBundleUtils.toList( |
| 310 |
gatewayOptionsBundle, |
| 311 |
PersistableBundleUtils.INTEGER_DESERIALIZER)); |
| 312 |
} |
| 313 |
|
| 314 |
mRetryIntervalsMs = in.getLongArray(RETRY_INTERVAL_MS_KEY); |
| 315 |
mMaxMtu = in.getInt(MAX_MTU_KEY); |
| 316 |
mMinUdpPort4500NatTimeoutSeconds = |
| 317 |
in.getInt( |
| 318 |
MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS_KEY, |
| 319 |
MIN_UDP_PORT_4500_NAT_TIMEOUT_UNSET); |
| 320 |
|
| 321 |
validate(); |
| 322 |
} |
| 323 |
|
| 324 |
private void validate() { |
| 325 |
Objects.requireNonNull(mGatewayConnectionName, "gatewayConnectionName was null"); |
| 326 |
Objects.requireNonNull(mTunnelConnectionParams, "tunnel connection parameter was null"); |
| 327 |
|
| 328 |
Preconditions.checkArgument( |
| 329 |
mExposedCapabilities != null && !mExposedCapabilities.isEmpty(), |
| 330 |
"exposedCapsBundle was null or empty"); |
| 331 |
for (Integer cap : getAllExposedCapabilities()) { |
| 332 |
checkValidCapability(cap); |
| 333 |
} |
| 334 |
|
| 335 |
validateNetworkTemplateList(mUnderlyingNetworkTemplates); |
| 336 |
Objects.requireNonNull(mRetryIntervalsMs, "retryIntervalsMs was null"); |
| 337 |
validateRetryInterval(mRetryIntervalsMs); |
| 338 |
|
| 339 |
Preconditions.checkArgument( |
| 340 |
mMaxMtu >= MIN_MTU_V6, "maxMtu must be at least IPv6 min MTU (1280)"); |
| 341 |
|
| 342 |
Preconditions.checkArgument( |
| 343 |
mMinUdpPort4500NatTimeoutSeconds == MIN_UDP_PORT_4500_NAT_TIMEOUT_UNSET |
| 344 |
|| mMinUdpPort4500NatTimeoutSeconds |
| 345 |
>= MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS, |
| 346 |
"minUdpPort4500NatTimeoutSeconds must be at least 120s"); |
| 347 |
|
| 348 |
for (int option : mGatewayOptions) { |
| 349 |
validateGatewayOption(option); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
private static void checkValidCapability(int capability) { |
| 354 |
Preconditions.checkArgument( |
| 355 |
ALLOWED_CAPABILITIES.contains(capability), |
| 356 |
"NetworkCapability " + capability + "out of range"); |
| 357 |
} |
| 358 |
|
| 359 |
private static void validateRetryInterval(@Nullable long[] retryIntervalsMs) { |
| 360 |
Preconditions.checkArgument( |
| 361 |
retryIntervalsMs != null |
| 362 |
&& retryIntervalsMs.length > 0 |
| 363 |
&& retryIntervalsMs.length <= MAX_RETRY_INTERVAL_COUNT, |
| 364 |
"retryIntervalsMs was null, empty or exceed max interval count"); |
| 365 |
|
| 366 |
final long repeatingInterval = retryIntervalsMs[retryIntervalsMs.length - 1]; |
| 367 |
if (repeatingInterval < MINIMUM_REPEATING_RETRY_INTERVAL_MS) { |
| 368 |
throw new IllegalArgumentException( |
| 369 |
"Repeating retry interval was too short, must be a minimum of 15 minutes: " |
| 370 |
+ repeatingInterval); |
| 371 |
} |
| 372 |
} |
| 373 |
|
| 374 |
private static void validateNetworkTemplateList( |
| 375 |
List<VcnUnderlyingNetworkTemplate> networkPriorityRules) { |
| 376 |
Objects.requireNonNull(networkPriorityRules, "networkPriorityRules is null"); |
| 377 |
|
| 378 |
Set<VcnUnderlyingNetworkTemplate> existingRules = new ArraySet<>(); |
| 379 |
for (VcnUnderlyingNetworkTemplate rule : networkPriorityRules) { |
| 380 |
Objects.requireNonNull(rule, "Found null value VcnUnderlyingNetworkTemplate"); |
| 381 |
if (!existingRules.add(rule)) { |
| 382 |
throw new IllegalArgumentException("Found duplicate VcnUnderlyingNetworkTemplate"); |
| 383 |
} |
| 384 |
} |
| 385 |
} |
| 386 |
|
| 387 |
private static void validateGatewayOption(int option) { |
| 388 |
if (!ALLOWED_GATEWAY_OPTIONS.contains(option)) { |
| 389 |
throw new IllegalArgumentException("Invalid vcn gateway option: " + option); |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
@NonNull |
| 403 |
public String getGatewayConnectionName() { |
| 404 |
return mGatewayConnectionName; |
| 405 |
} |
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
@NonNull |
| 413 |
public IkeTunnelConnectionParams getTunnelConnectionParams() { |
| 414 |
return mTunnelConnectionParams; |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
@NonNull |
| 427 |
public int[] getExposedCapabilities() { |
| 428 |
|
| 429 |
return ArrayUtils.convertToIntArray(new ArrayList<>(mExposedCapabilities)); |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
@Deprecated |
| 441 |
@NonNull |
| 442 |
public Set<Integer> getAllExposedCapabilities() { |
| 443 |
return Collections.unmodifiableSet(mExposedCapabilities); |
| 444 |
} |
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
|
| 451 |
@NonNull |
| 452 |
public List<VcnUnderlyingNetworkTemplate> getVcnUnderlyingNetworkPriorities() { |
| 453 |
return new ArrayList<>(mUnderlyingNetworkTemplates); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
@NonNull |
| 462 |
public long[] getRetryIntervalsMillis() { |
| 463 |
return Arrays.copyOf(mRetryIntervalsMs, mRetryIntervalsMs.length); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
@IntRange(from = MIN_MTU_V6) |
| 472 |
public int getMaxMtu() { |
| 473 |
return mMaxMtu; |
| 474 |
} |
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
public int getMinUdpPort4500NatTimeoutSeconds() { |
| 482 |
return mMinUdpPort4500NatTimeoutSeconds; |
| 483 |
} |
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
public boolean hasGatewayOption(@VcnGatewayOption int option) { |
| 494 |
validateGatewayOption(option); |
| 495 |
return mGatewayOptions.contains(option); |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
@NonNull |
| 504 |
@VisibleForTesting(visibility = Visibility.PROTECTED) |
| 505 |
public PersistableBundle toPersistableBundle() { |
| 506 |
final PersistableBundle result = new PersistableBundle(); |
| 507 |
|
| 508 |
final PersistableBundle tunnelConnectionParamsBundle = |
| 509 |
TunnelConnectionParamsUtils.toPersistableBundle(mTunnelConnectionParams); |
| 510 |
final PersistableBundle exposedCapsBundle = |
| 511 |
PersistableBundleUtils.fromList( |
| 512 |
new ArrayList<>(mExposedCapabilities), |
| 513 |
PersistableBundleUtils.INTEGER_SERIALIZER); |
| 514 |
final PersistableBundle networkTemplatesBundle = |
| 515 |
PersistableBundleUtils.fromList( |
| 516 |
mUnderlyingNetworkTemplates, |
| 517 |
VcnUnderlyingNetworkTemplate::toPersistableBundle); |
| 518 |
final PersistableBundle gatewayOptionsBundle = |
| 519 |
PersistableBundleUtils.fromList( |
| 520 |
new ArrayList<>(mGatewayOptions), |
| 521 |
PersistableBundleUtils.INTEGER_SERIALIZER); |
| 522 |
|
| 523 |
result.putString(GATEWAY_CONNECTION_NAME_KEY, mGatewayConnectionName); |
| 524 |
result.putPersistableBundle(TUNNEL_CONNECTION_PARAMS_KEY, tunnelConnectionParamsBundle); |
| 525 |
result.putPersistableBundle(EXPOSED_CAPABILITIES_KEY, exposedCapsBundle); |
| 526 |
result.putPersistableBundle(UNDERLYING_NETWORK_TEMPLATES_KEY, networkTemplatesBundle); |
| 527 |
result.putPersistableBundle(GATEWAY_OPTIONS_KEY, gatewayOptionsBundle); |
| 528 |
result.putLongArray(RETRY_INTERVAL_MS_KEY, mRetryIntervalsMs); |
| 529 |
result.putInt(MAX_MTU_KEY, mMaxMtu); |
| 530 |
result.putInt(MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS_KEY, mMinUdpPort4500NatTimeoutSeconds); |
| 531 |
|
| 532 |
return result; |
| 533 |
} |
| 534 |
|
| 535 |
@Override |
| 536 |
public int hashCode() { |
| 537 |
return Objects.hash( |
| 538 |
mGatewayConnectionName, |
| 539 |
mTunnelConnectionParams, |
| 540 |
mExposedCapabilities, |
| 541 |
mUnderlyingNetworkTemplates, |
| 542 |
Arrays.hashCode(mRetryIntervalsMs), |
| 543 |
mMaxMtu, |
| 544 |
mMinUdpPort4500NatTimeoutSeconds, |
| 545 |
mGatewayOptions); |
| 546 |
} |
| 547 |
|
| 548 |
@Override |
| 549 |
public boolean equals(@Nullable Object other) { |
| 550 |
if (!(other instanceof VcnGatewayConnectionConfig)) { |
| 551 |
return false; |
| 552 |
} |
| 553 |
|
| 554 |
final VcnGatewayConnectionConfig rhs = (VcnGatewayConnectionConfig) other; |
| 555 |
return mGatewayConnectionName.equals(rhs.mGatewayConnectionName) |
| 556 |
&& mTunnelConnectionParams.equals(rhs.mTunnelConnectionParams) |
| 557 |
&& mExposedCapabilities.equals(rhs.mExposedCapabilities) |
| 558 |
&& mUnderlyingNetworkTemplates.equals(rhs.mUnderlyingNetworkTemplates) |
| 559 |
&& Arrays.equals(mRetryIntervalsMs, rhs.mRetryIntervalsMs) |
| 560 |
&& mMaxMtu == rhs.mMaxMtu |
| 561 |
&& mMinUdpPort4500NatTimeoutSeconds == rhs.mMinUdpPort4500NatTimeoutSeconds |
| 562 |
&& mGatewayOptions.equals(rhs.mGatewayOptions); |
| 563 |
} |
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
public static final class Builder { |
| 569 |
@NonNull private final String mGatewayConnectionName; |
| 570 |
@NonNull private final IkeTunnelConnectionParams mTunnelConnectionParams; |
| 571 |
@NonNull private final Set<Integer> mExposedCapabilities = new ArraySet(); |
| 572 |
|
| 573 |
@NonNull |
| 574 |
private final List<VcnUnderlyingNetworkTemplate> mUnderlyingNetworkTemplates = |
| 575 |
new ArrayList<>(DEFAULT_UNDERLYING_NETWORK_TEMPLATES); |
| 576 |
|
| 577 |
@NonNull private long[] mRetryIntervalsMs = DEFAULT_RETRY_INTERVALS_MS; |
| 578 |
private int mMaxMtu = DEFAULT_MAX_MTU; |
| 579 |
private int mMinUdpPort4500NatTimeoutSeconds = MIN_UDP_PORT_4500_NAT_TIMEOUT_UNSET; |
| 580 |
|
| 581 |
@NonNull private final Set<Integer> mGatewayOptions = new ArraySet<>(); |
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
public Builder( |
| 602 |
@NonNull String gatewayConnectionName, |
| 603 |
@NonNull IkeTunnelConnectionParams tunnelConnectionParams) { |
| 604 |
Objects.requireNonNull(gatewayConnectionName, "gatewayConnectionName was null"); |
| 605 |
Objects.requireNonNull(tunnelConnectionParams, "tunnelConnectionParams was null"); |
| 606 |
if (!tunnelConnectionParams.getIkeSessionParams().hasIkeOption(IKE_OPTION_MOBIKE)) { |
| 607 |
throw new IllegalArgumentException( |
| 608 |
"MOBIKE must be configured for the provided IkeSessionParams"); |
| 609 |
} |
| 610 |
|
| 611 |
mGatewayConnectionName = gatewayConnectionName; |
| 612 |
mTunnelConnectionParams = tunnelConnectionParams; |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
@NonNull |
| 625 |
public Builder addExposedCapability(@VcnSupportedCapability int exposedCapability) { |
| 626 |
checkValidCapability(exposedCapability); |
| 627 |
|
| 628 |
mExposedCapabilities.add(exposedCapability); |
| 629 |
return this; |
| 630 |
} |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
@NonNull |
| 642 |
@SuppressLint("BuilderSetStyle") |
| 643 |
public Builder removeExposedCapability(@VcnSupportedCapability int exposedCapability) { |
| 644 |
checkValidCapability(exposedCapability); |
| 645 |
|
| 646 |
mExposedCapabilities.remove(exposedCapability); |
| 647 |
return this; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
@NonNull |
| 672 |
public Builder setVcnUnderlyingNetworkPriorities( |
| 673 |
@NonNull List<VcnUnderlyingNetworkTemplate> underlyingNetworkTemplates) { |
| 674 |
validateNetworkTemplateList(underlyingNetworkTemplates); |
| 675 |
|
| 676 |
mUnderlyingNetworkTemplates.clear(); |
| 677 |
|
| 678 |
if (underlyingNetworkTemplates.isEmpty()) { |
| 679 |
mUnderlyingNetworkTemplates.addAll(DEFAULT_UNDERLYING_NETWORK_TEMPLATES); |
| 680 |
} else { |
| 681 |
mUnderlyingNetworkTemplates.addAll(underlyingNetworkTemplates); |
| 682 |
} |
| 683 |
|
| 684 |
return this; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
@NonNull |
| 711 |
public Builder setRetryIntervalsMillis(@NonNull long[] retryIntervalsMs) { |
| 712 |
validateRetryInterval(retryIntervalsMs); |
| 713 |
|
| 714 |
mRetryIntervalsMs = retryIntervalsMs; |
| 715 |
return this; |
| 716 |
} |
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
@NonNull |
| 732 |
public Builder setMaxMtu(@IntRange(from = MIN_MTU_V6) int maxMtu) { |
| 733 |
Preconditions.checkArgument( |
| 734 |
maxMtu >= MIN_MTU_V6, "maxMtu must be at least IPv6 min MTU (1280)"); |
| 735 |
|
| 736 |
mMaxMtu = maxMtu; |
| 737 |
return this; |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
@NonNull |
| 752 |
public Builder setMinUdpPort4500NatTimeoutSeconds( |
| 753 |
@IntRange(from = MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS) |
| 754 |
int minUdpPort4500NatTimeoutSeconds) { |
| 755 |
Preconditions.checkArgument( |
| 756 |
minUdpPort4500NatTimeoutSeconds >= MIN_UDP_PORT_4500_NAT_TIMEOUT_SECONDS, |
| 757 |
"Timeout must be at least 120s"); |
| 758 |
|
| 759 |
mMinUdpPort4500NatTimeoutSeconds = minUdpPort4500NatTimeoutSeconds; |
| 760 |
return this; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
@NonNull |
| 771 |
public Builder addGatewayOption(@VcnGatewayOption int option) { |
| 772 |
validateGatewayOption(option); |
| 773 |
mGatewayOptions.add(option); |
| 774 |
return this; |
| 775 |
} |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
@NonNull |
| 785 |
public Builder removeGatewayOption(@VcnGatewayOption int option) { |
| 786 |
validateGatewayOption(option); |
| 787 |
mGatewayOptions.remove(option); |
| 788 |
return this; |
| 789 |
} |
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
@NonNull |
| 797 |
public VcnGatewayConnectionConfig build() { |
| 798 |
return new VcnGatewayConnectionConfig( |
| 799 |
mGatewayConnectionName, |
| 800 |
mTunnelConnectionParams, |
| 801 |
mExposedCapabilities, |
| 802 |
mUnderlyingNetworkTemplates, |
| 803 |
mRetryIntervalsMs, |
| 804 |
mMaxMtu, |
| 805 |
mMinUdpPort4500NatTimeoutSeconds, |
| 806 |
mGatewayOptions); |
| 807 |
} |
| 808 |
} |
| 809 |
} |