1 /* 2 * Copyright (C) 2020 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 package com.android.internal.net.ipsec.ike.utils; 17 18 import android.util.CloseGuard; 19 import android.util.Pair; 20 21 import java.net.InetAddress; 22 import java.util.HashSet; 23 import java.util.Set; 24 25 /** 26 * This class represents a reserved IKE SPI. 27 * 28 * <p>This class is created to avoid assigning same SPI to the same address. 29 * 30 * <p>Objects of this type are used to track reserved IKE SPI to avoid SPI collision. They can be 31 * obtained by calling {@link #allocateSecurityParameterIndex()} and must be released by calling 32 * {@link #close()} when they are no longer needed. 33 * 34 * <p>This class MUST only be called from IKE worker thread. Methods that allocate and close IKE 35 * SPI resource are not thread safe. 36 * 37 * <p>This class follows the pattern of {@link IpSecManager.SecurityParameterIndex}. 38 */ 39 public final class IkeSecurityParameterIndex implements AutoCloseable { 40 // Package private Set to remember assigned IKE SPIs to avoid SPI collision. MUST be 41 // accessed only by IkeSecurityParameterIndex and IkeSpiGenerator 42 static final Set<Pair<InetAddress, Long>> sAssignedIkeSpis = new HashSet<>(); 43 44 private final InetAddress mSourceAddress; 45 private final long mSpi; 46 private final CloseGuard mCloseGuard = new CloseGuard(); 47 48 // Package private constructor that MUST only be called from IkeSpiGenerator IkeSecurityParameterIndex(InetAddress sourceAddress, long spi)49 IkeSecurityParameterIndex(InetAddress sourceAddress, long spi) { 50 mSourceAddress = sourceAddress; 51 mSpi = spi; 52 mCloseGuard.open("close"); 53 } 54 55 /** 56 * Get the underlying SPI held by this object. 57 * 58 * @return the underlying IKE SPI. 59 */ getSpi()60 public long getSpi() { 61 return mSpi; 62 } 63 64 /** Release an SPI that was previously reserved. */ 65 @Override close()66 public void close() { 67 sAssignedIkeSpis.remove(new Pair<InetAddress, Long>(mSourceAddress, mSpi)); 68 mCloseGuard.close(); 69 } 70 71 /** Check that the IkeSecurityParameterIndex was closed properly. */ 72 @Override finalize()73 protected void finalize() throws Throwable { 74 if (mCloseGuard != null) { 75 mCloseGuard.warnIfOpen(); 76 } 77 close(); 78 } 79 } 80