Search in sources :

Example 46 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by ResurrectionRemix.

the class LinkPropertiesTest method testIsReachable.

@SmallTest
// Failing.
@Suppress
public void testIsReachable() {
    final LinkProperties v4lp = new LinkProperties();
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Add an on-link route, making the on-link DNS server reachable,
    // but there is still no IPv4 address.
    assertTrue(v4lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("75.208.0.0"), 16))));
    assertFalse(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding an IPv4 address (right now, any IPv4 address) means we use
    // the routes to compute likely reachability.
    assertTrue(v4lp.addLinkAddress(new LinkAddress(ADDRV4, 16)));
    assertTrue(v4lp.isReachable(DNS1));
    assertFalse(v4lp.isReachable(DNS2));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v4lp.addRoute(new RouteInfo(GATEWAY1)));
    assertTrue(v4lp.isReachable(DNS1));
    assertTrue(v4lp.isReachable(DNS2));
    final LinkProperties v6lp = new LinkProperties();
    final InetAddress kLinkLocalDns = NetworkUtils.numericToInetAddress("fe80::6:1");
    final InetAddress kLinkLocalDnsWithScope = NetworkUtils.numericToInetAddress("fe80::6:2%43");
    final InetAddress kOnLinkDns = NetworkUtils.numericToInetAddress("2001:db8:85a3::53");
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertFalse(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local route, making the link-local DNS servers reachable. Because
    // we assume the presence of an IPv6 link-local address, link-local DNS servers
    // are considered reachable, but only those with a non-zero scope identifier.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("fe80::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a link-local address--nothing changes.
    assertTrue(v6lp.addLinkAddress(LINKADDRV6LINKLOCAL));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertFalse(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global route on link, but no global address yet. DNS servers reachable
    // via a route that doesn't require a gateway: give them the benefit of the
    // doubt and hope the link-local source address suffices for communication.
    assertTrue(v6lp.addRoute(new RouteInfo(new IpPrefix(NetworkUtils.numericToInetAddress("2001:db8:85a3::"), 64))));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Add a global address; the on-link global address DNS server is (still)
    // presumed reachable.
    assertTrue(v6lp.addLinkAddress(new LinkAddress(ADDRV6, 64)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertFalse(v6lp.isReachable(DNS6));
    // Adding a default route makes the off-link DNS server reachable.
    assertTrue(v6lp.addRoute(new RouteInfo(GATEWAY62)));
    assertFalse(v6lp.isReachable(kLinkLocalDns));
    assertTrue(v6lp.isReachable(kLinkLocalDnsWithScope));
    assertTrue(v6lp.isReachable(kOnLinkDns));
    assertTrue(v6lp.isReachable(DNS6));
    // Check isReachable on stacked links. This requires that the source IP address be assigned
    // on the interface returned by the route lookup.
    LinkProperties stacked = new LinkProperties();
    // Can't add a stacked link without an interface name.
    stacked.setInterfaceName("v4-test0");
    v6lp.addStackedLink(stacked);
    InetAddress stackedAddress = Address("192.0.0.4");
    LinkAddress stackedLinkAddress = new LinkAddress(stackedAddress, 32);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addLinkAddress(stackedLinkAddress);
    assertFalse(v6lp.isReachable(stackedAddress));
    stacked.addRoute(new RouteInfo(stackedLinkAddress));
    assertTrue(stacked.isReachable(stackedAddress));
    assertTrue(v6lp.isReachable(stackedAddress));
    assertFalse(v6lp.isReachable(DNS1));
    stacked.addRoute(new RouteInfo((IpPrefix) null, stackedAddress));
    assertTrue(v6lp.isReachable(DNS1));
}
Also used : IpPrefix(android.net.IpPrefix) LinkAddress(android.net.LinkAddress) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress) SmallTest(android.test.suitebuilder.annotation.SmallTest) Suppress(android.test.suitebuilder.annotation.Suppress)

Example 47 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by ResurrectionRemix.

the class LinkAddressTest method testConstructors.

public void testConstructors() throws SocketException {
    LinkAddress address;
    // Valid addresses work as expected.
    address = new LinkAddress(V4_ADDRESS, 25);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(25, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    address = new LinkAddress(V6_ADDRESS, 127);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(127, address.getPrefixLength());
    assertEquals(0, address.getFlags());
    assertEquals(RT_SCOPE_UNIVERSE, address.getScope());
    // Nonsensical flags/scopes or combinations thereof are acceptable.
    address = new LinkAddress(V6 + "/64", IFA_F_DEPRECATED | IFA_F_PERMANENT, RT_SCOPE_LINK);
    assertEquals(V6_ADDRESS, address.getAddress());
    assertEquals(64, address.getPrefixLength());
    assertEquals(IFA_F_DEPRECATED | IFA_F_PERMANENT, address.getFlags());
    assertEquals(RT_SCOPE_LINK, address.getScope());
    address = new LinkAddress(V4 + "/23", 123, 456);
    assertEquals(V4_ADDRESS, address.getAddress());
    assertEquals(23, address.getPrefixLength());
    assertEquals(123, address.getFlags());
    assertEquals(456, address.getScope());
    // InterfaceAddress doesn't have a constructor. Fetch some from an interface.
    List<InterfaceAddress> addrs = NetworkInterface.getByName("lo").getInterfaceAddresses();
    // We expect to find 127.0.0.1/8 and ::1/128, in any order.
    LinkAddress ipv4Loopback, ipv6Loopback;
    assertEquals(2, addrs.size());
    if (addrs.get(0).getAddress() instanceof Inet4Address) {
        ipv4Loopback = new LinkAddress(addrs.get(0));
        ipv6Loopback = new LinkAddress(addrs.get(1));
    } else {
        ipv4Loopback = new LinkAddress(addrs.get(1));
        ipv6Loopback = new LinkAddress(addrs.get(0));
    }
    assertEquals(NetworkUtils.numericToInetAddress("127.0.0.1"), ipv4Loopback.getAddress());
    assertEquals(8, ipv4Loopback.getPrefixLength());
    assertEquals(NetworkUtils.numericToInetAddress("::1"), ipv6Loopback.getAddress());
    assertEquals(128, ipv6Loopback.getPrefixLength());
    // Null addresses are rejected.
    try {
        address = new LinkAddress(null, 24);
        fail("Null InetAddress should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((String) null, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("Null string should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress((InterfaceAddress) null);
        fail("Null string should cause NullPointerException");
    } catch (NullPointerException expected) {
    }
    // Invalid prefix lengths are rejected.
    try {
        address = new LinkAddress(V4_ADDRESS, -1);
        fail("Negative IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, -1);
        fail("Negative IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4_ADDRESS, 33);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V4 + "/33", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/33 IPv4 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6_ADDRESS, 129, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress(V6 + "/129", IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
        fail("/129 IPv6 prefix length should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    // Multicast addresses are rejected.
    try {
        address = new LinkAddress("224.0.0.2/32");
        fail("IPv4 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
    try {
        address = new LinkAddress("ff02::1/128");
        fail("IPv6 multicast address should cause IllegalArgumentException");
    } catch (IllegalArgumentException expected) {
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet4Address(java.net.Inet4Address) InterfaceAddress(java.net.InterfaceAddress)

Example 48 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by ResurrectionRemix.

the class LinkAddressTest method testIsGlobalPreferred.

public void testIsGlobalPreferred() {
    LinkAddress l;
    l = new LinkAddress(V4_ADDRESS, 32, 0, RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v4,global,noflags");
    l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v4-rfc1918,global,noflags");
    l = new LinkAddress("10.10.1.7/23", 0, RT_SCOPE_SITE);
    assertNotGlobalPreferred(l, "v4-rfc1918,site-local,noflags");
    l = new LinkAddress("127.0.0.7/8", 0, RT_SCOPE_HOST);
    assertNotGlobalPreferred(l, "v4-localhost,node-local,noflags");
    l = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v6,global,noflags");
    l = new LinkAddress(V6_ADDRESS, 64, IFA_F_PERMANENT, RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v6,global,permanent");
    // IPv6 ULAs are not acceptable "global preferred" addresses.
    l = new LinkAddress("fc12::1/64", 0, RT_SCOPE_UNIVERSE);
    assertNotGlobalPreferred(l, "v6,ula1,noflags");
    l = new LinkAddress("fd34::1/64", 0, RT_SCOPE_UNIVERSE);
    assertNotGlobalPreferred(l, "v6,ula2,noflags");
    l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v6,global,tempaddr");
    l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY | IFA_F_DADFAILED), RT_SCOPE_UNIVERSE);
    assertNotGlobalPreferred(l, "v6,global,tempaddr+dadfailed");
    l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY | IFA_F_DEPRECATED), RT_SCOPE_UNIVERSE);
    assertNotGlobalPreferred(l, "v6,global,tempaddr+deprecated");
    l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_SITE);
    assertNotGlobalPreferred(l, "v6,site-local,tempaddr");
    l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_LINK);
    assertNotGlobalPreferred(l, "v6,link-local,tempaddr");
    l = new LinkAddress(V6_ADDRESS, 64, IFA_F_TEMPORARY, RT_SCOPE_HOST);
    assertNotGlobalPreferred(l, "v6,node-local,tempaddr");
    l = new LinkAddress("::1/128", IFA_F_PERMANENT, RT_SCOPE_HOST);
    assertNotGlobalPreferred(l, "v6-localhost,node-local,permanent");
    l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY | IFA_F_TENTATIVE), RT_SCOPE_UNIVERSE);
    assertNotGlobalPreferred(l, "v6,global,tempaddr+tentative");
    l = new LinkAddress(V6_ADDRESS, 64, (IFA_F_TEMPORARY | IFA_F_TENTATIVE | IFA_F_OPTIMISTIC), RT_SCOPE_UNIVERSE);
    assertGlobalPreferred(l, "v6,global,tempaddr+optimistic");
}
Also used : LinkAddress(android.net.LinkAddress)

Example 49 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by ResurrectionRemix.

the class NetworkDiagnostics method prepareExplicitSourceIcmpMeasurements.

private void prepareExplicitSourceIcmpMeasurements(InetAddress target) {
    for (LinkAddress l : mLinkProperties.getLinkAddresses()) {
        InetAddress source = l.getAddress();
        if (source instanceof Inet6Address && l.isGlobalPreferred()) {
            Pair<InetAddress, InetAddress> srcTarget = new Pair<>(source, target);
            if (!mExplicitSourceIcmpChecks.containsKey(srcTarget)) {
                Measurement measurement = new Measurement();
                measurement.thread = new Thread(new IcmpCheck(source, target, measurement));
                mExplicitSourceIcmpChecks.put(srcTarget, measurement);
            }
        }
    }
}
Also used : LinkAddress(android.net.LinkAddress) Inet6Address(java.net.Inet6Address) InetAddress(java.net.InetAddress) Pair(android.util.Pair)

Example 50 with LinkAddress

use of android.net.LinkAddress in project android_frameworks_base by ResurrectionRemix.

the class Nat464Xlat method interfaceLinkStateChanged.

@Override
public void interfaceLinkStateChanged(String iface, boolean up) {
    // Called by the InterfaceObserver on its own thread, so can race with stop().
    if (isStarted() && up && mIface.equals(iface)) {
        Slog.i(TAG, "interface " + iface + " is up, mIsRunning " + mIsRunning + "->true");
        if (!mIsRunning) {
            LinkAddress clatAddress = getLinkAddress(iface);
            if (clatAddress == null) {
                return;
            }
            mIsRunning = true;
            maybeSetIpv6NdOffload(mBaseIface, false);
            LinkProperties lp = new LinkProperties(mNetwork.linkProperties);
            lp.addStackedLink(makeLinkProperties(clatAddress));
            Slog.i(TAG, "Adding stacked link " + mIface + " on top of " + mBaseIface);
            updateConnectivityService(lp);
        }
    }
}
Also used : LinkAddress(android.net.LinkAddress) LinkProperties(android.net.LinkProperties)

Aggregations

LinkAddress (android.net.LinkAddress)210 LinkProperties (android.net.LinkProperties)70 InetAddress (java.net.InetAddress)70 RouteInfo (android.net.RouteInfo)52 SmallTest (android.test.suitebuilder.annotation.SmallTest)29 RemoteException (android.os.RemoteException)27 Inet4Address (java.net.Inet4Address)24 InterfaceConfiguration (android.net.InterfaceConfiguration)22 Inet6Address (java.net.Inet6Address)21 IpPrefix (android.net.IpPrefix)20 StaticIpConfiguration (android.net.StaticIpConfiguration)20 IOException (java.io.IOException)15 ApfFilter (android.net.apf.ApfFilter)12 ByteBuffer (java.nio.ByteBuffer)12 ProxyInfo (android.net.ProxyInfo)10 VpnConfig (com.android.internal.net.VpnConfig)10 DhcpResults (android.net.DhcpResults)9 LargeTest (android.test.suitebuilder.annotation.LargeTest)9 Command (com.android.server.NativeDaemonConnector.Command)9 EOFException (java.io.EOFException)6