Merge changes I943d4efb,I02378125,I2ee4ae52
* changes: Reset the generated v6 clat address when stop Add helper method to translate v4 address pair to clat v6 Add methods to provide clat source address
This commit is contained in:
@@ -44,7 +44,9 @@ import com.android.net.module.util.NetworkStackConstants;
|
||||
import com.android.server.ConnectivityService;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
@@ -99,7 +101,8 @@ public class Nat464Xlat {
|
||||
private IpPrefix mNat64PrefixFromRa;
|
||||
private String mBaseIface;
|
||||
private String mIface;
|
||||
private Inet6Address mIPv6Address;
|
||||
@VisibleForTesting
|
||||
Inet6Address mIPv6Address;
|
||||
private State mState = State.IDLE;
|
||||
private final ClatCoordinator mClatCoordinator; // non-null iff T+
|
||||
|
||||
@@ -239,6 +242,7 @@ public class Nat464Xlat {
|
||||
mNat64PrefixInUse = null;
|
||||
mIface = null;
|
||||
mBaseIface = null;
|
||||
mIPv6Address = null;
|
||||
|
||||
if (!mPrefixDiscoveryRunning) {
|
||||
setPrefix64(null);
|
||||
@@ -540,6 +544,52 @@ public class Nat464Xlat {
|
||||
mNetwork.handler().post(() -> handleInterfaceRemoved(iface));
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the input v4 address to v6 clat address.
|
||||
*/
|
||||
@Nullable
|
||||
public Inet6Address translateV4toV6(@NonNull Inet4Address addr) {
|
||||
// Variables in Nat464Xlat should only be accessed from handler thread.
|
||||
ensureRunningOnHandlerThread();
|
||||
if (!isStarted()) return null;
|
||||
|
||||
return convertv4ToClatv6(mNat64PrefixInUse, addr);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static Inet6Address convertv4ToClatv6(
|
||||
@NonNull IpPrefix prefix, @NonNull Inet4Address addr) {
|
||||
final byte[] v6Addr = new byte[16];
|
||||
// Generate a v6 address from Nat64 prefix. Prefix should be 12 bytes long.
|
||||
System.arraycopy(prefix.getAddress().getAddress(), 0, v6Addr, 0, 12);
|
||||
System.arraycopy(addr.getAddress(), 0, v6Addr, 12, 4);
|
||||
|
||||
try {
|
||||
return (Inet6Address) Inet6Address.getByAddress(v6Addr);
|
||||
} catch (UnknownHostException e) {
|
||||
Log.e(TAG, "getByAddress should never throw for a numeric address");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the generated v6 address of clat.
|
||||
*/
|
||||
@Nullable
|
||||
public Inet6Address getClatv6SrcAddress() {
|
||||
// Variables in Nat464Xlat should only be accessed from handler thread.
|
||||
ensureRunningOnHandlerThread();
|
||||
|
||||
return mIPv6Address;
|
||||
}
|
||||
|
||||
private void ensureRunningOnHandlerThread() {
|
||||
if (mNetwork.handler().getLooper().getThread() != Thread.currentThread()) {
|
||||
throw new IllegalStateException(
|
||||
"Not running on handler thread: " + Thread.currentThread().getName());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the NAT64 xlat information.
|
||||
*
|
||||
|
||||
@@ -68,6 +68,8 @@ import com.android.modules.utils.build.SdkLevel;
|
||||
import com.android.server.ConnectivityService;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.Inet6Address;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -1032,6 +1034,22 @@ public class NetworkAgentInfo implements NetworkRanker.Scoreable {
|
||||
return network;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the generated v6 address of clat.
|
||||
*/
|
||||
@Nullable
|
||||
public Inet6Address getClatv6SrcAddress() {
|
||||
return clatd.getClatv6SrcAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* Translate the input v4 address to v6 clat address.
|
||||
*/
|
||||
@Nullable
|
||||
public Inet6Address translateV4toClatV6(@NonNull Inet4Address addr) {
|
||||
return clatd.translateV4toV6(addr);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the NetworkMonitorManager in this NetworkAgentInfo.
|
||||
*
|
||||
|
||||
@@ -20,10 +20,12 @@ import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.anyInt;
|
||||
import static org.mockito.Mockito.anyString;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.eq;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.never;
|
||||
@@ -72,6 +74,7 @@ public class Nat464XlatTest {
|
||||
static final String STACKED_IFACE = "v4-test0";
|
||||
static final LinkAddress V6ADDR = new LinkAddress("2001:db8:1::f00/64");
|
||||
static final LinkAddress ADDR = new LinkAddress("192.0.2.5/29");
|
||||
static final String CLAT_V6 = "64:ff9b::1";
|
||||
static final String NAT64_PREFIX = "64:ff9b::/96";
|
||||
static final String OTHER_NAT64_PREFIX = "2001:db8:0:64::/96";
|
||||
static final int NETID = 42;
|
||||
@@ -132,6 +135,8 @@ public class Nat464XlatTest {
|
||||
when(mNetd.interfaceGetCfg(eq(STACKED_IFACE))).thenReturn(mConfig);
|
||||
mConfig.ipv4Addr = ADDR.getAddress().getHostAddress();
|
||||
mConfig.prefixLength = ADDR.getPrefixLength();
|
||||
doReturn(CLAT_V6).when(mClatCoordinator).clatStart(
|
||||
BASE_IFACE, NETID, new IpPrefix(NAT64_PREFIX));
|
||||
}
|
||||
|
||||
private void assertRequiresClat(boolean expected, NetworkAgentInfo nai) {
|
||||
@@ -286,7 +291,8 @@ public class Nat464XlatTest {
|
||||
assertFalse(c.getValue().getAllInterfaceNames().contains(STACKED_IFACE));
|
||||
verify(mDnsResolver).stopPrefix64Discovery(eq(NETID));
|
||||
assertIdle(nat);
|
||||
|
||||
// Verify the generated v6 is reset when clat is stopped.
|
||||
assertNull(nat.mIPv6Address);
|
||||
// Stacked interface removed notification arrives and is ignored.
|
||||
nat.interfaceRemoved(STACKED_IFACE);
|
||||
mLooper.dispatchNext();
|
||||
|
||||
Reference in New Issue
Block a user