Search in sources :

Example 16 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class LinkAddressTest method testEqualsAndSameAddressAs.

public void testEqualsAndSameAddressAs() {
    LinkAddress l1, l2, l3;
    l1 = new LinkAddress("2001:db8::1/64");
    l2 = new LinkAddress("2001:db8::1/64");
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress("2001:db8::1/65");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l2 = new LinkAddress("2001:db8::2/64");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l1 = new LinkAddress("192.0.2.1/24");
    l2 = new LinkAddress("192.0.2.1/24");
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress("192.0.2.1/23");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    l2 = new LinkAddress("192.0.2.2/24");
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    // Check equals() and isSameAddressAs() on identical addresses with different flags.
    l1 = new LinkAddress(V6_ADDRESS, 64);
    l2 = new LinkAddress(V6_ADDRESS, 64, 0, RT_SCOPE_UNIVERSE);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress(V6_ADDRESS, 64, IFA_F_DEPRECATED, RT_SCOPE_UNIVERSE);
    assertLinkAddressesNotEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    // Check equals() and isSameAddressAs() on identical addresses with different scope.
    l1 = new LinkAddress(V4_ADDRESS, 24);
    l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_UNIVERSE);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    l2 = new LinkAddress(V4_ADDRESS, 24, 0, RT_SCOPE_HOST);
    assertLinkAddressesNotEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
    // Addresses with the same start or end bytes aren't equal between families.
    l1 = new LinkAddress("32.1.13.184/24");
    l2 = new LinkAddress("2001:db8::1/24");
    l3 = new LinkAddress("::2001:db8/24");
    byte[] ipv4Bytes = l1.getAddress().getAddress();
    byte[] l2FirstIPv6Bytes = Arrays.copyOf(l2.getAddress().getAddress(), 4);
    byte[] l3LastIPv6Bytes = Arrays.copyOfRange(l3.getAddress().getAddress(), 12, 16);
    assertTrue(Arrays.equals(ipv4Bytes, l2FirstIPv6Bytes));
    assertTrue(Arrays.equals(ipv4Bytes, l3LastIPv6Bytes));
    assertLinkAddressesNotEqual(l1, l2);
    assertIsNotSameAddressAs(l1, l2);
    assertLinkAddressesNotEqual(l1, l3);
    assertIsNotSameAddressAs(l1, l3);
    // Because we use InetAddress, an IPv4 address is equal to its IPv4-mapped address.
    // TODO: Investigate fixing this.
    String addressString = V4 + "/24";
    l1 = new LinkAddress(addressString);
    l2 = new LinkAddress("::ffff:" + addressString);
    assertLinkAddressesEqual(l1, l2);
    assertIsSameAddressAs(l1, l2);
}
Also used : LinkAddress(android.net.LinkAddress)

Example 17 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class IPv6TetheringCoordinator method getIPv6OnlyLinkProperties.

private static LinkProperties getIPv6OnlyLinkProperties(LinkProperties lp) {
    final LinkProperties v6only = new LinkProperties();
    if (lp == null) {
        return v6only;
    }
    // NOTE: At this time we don't copy over any information about any
    // stacked links. No current stacked link configuration has IPv6.
    v6only.setInterfaceName(lp.getInterfaceName());
    v6only.setMtu(lp.getMtu());
    for (LinkAddress linkAddr : lp.getLinkAddresses()) {
        if (linkAddr.isGlobalPreferred() && linkAddr.getPrefixLength() == 64) {
            v6only.addLinkAddress(linkAddr);
        }
    }
    for (RouteInfo routeInfo : lp.getRoutes()) {
        final IpPrefix destination = routeInfo.getDestination();
        if ((destination.getAddress() instanceof Inet6Address) && (destination.getPrefixLength() <= 64)) {
            v6only.addRoute(routeInfo);
        }
    }
    for (InetAddress dnsServer : lp.getDnsServers()) {
        if (isIPv6GlobalAddress(dnsServer)) {
            // For now we include ULAs.
            v6only.addDnsServer(dnsServer);
        }
    }
    v6only.setDomains(lp.getDomains());
    return v6only;
}
Also used : LinkAddress(android.net.LinkAddress) IpPrefix(android.net.IpPrefix) Inet6Address(java.net.Inet6Address) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

Example 18 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class Nat464Xlat method makeLinkProperties.

private LinkProperties makeLinkProperties(LinkAddress clatAddress) {
    LinkProperties stacked = new LinkProperties();
    stacked.setInterfaceName(mIface);
    // Although the clat interface is a point-to-point tunnel, we don't
    // point the route directly at the interface because some apps don't
    // understand routes without gateways (see, e.g., http://b/9597256
    // http://b/9597516). Instead, set the next hop of the route to the
    // clat IPv4 address itself (for those apps, it doesn't matter what
    // the IP of the gateway is, only that there is one).
    RouteInfo ipv4Default = new RouteInfo(new LinkAddress(Inet4Address.ANY, 0), clatAddress.getAddress(), mIface);
    stacked.addRoute(ipv4Default);
    stacked.addLinkAddress(clatAddress);
    return stacked;
}
Also used : LinkAddress(android.net.LinkAddress) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties)

Example 19 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class Vpn method establish.

/**
     * Establish a VPN network and return the file descriptor of the VPN
     * interface. This methods returns {@code null} if the application is
     * revoked or not prepared.
     *
     * @param config The parameters to configure the network.
     * @return The file descriptor of the VPN interface.
     */
public synchronized ParcelFileDescriptor establish(VpnConfig config) {
    // Check if the caller is already prepared.
    UserManager mgr = UserManager.get(mContext);
    if (Binder.getCallingUid() != mOwnerUID) {
        return null;
    }
    // Check to ensure consent hasn't been revoked since we were prepared.
    if (!isVpnUserPreConsented(mPackage)) {
        return null;
    }
    // Check if the service is properly declared.
    Intent intent = new Intent(VpnConfig.SERVICE_INTERFACE);
    intent.setClassName(mPackage, config.user);
    long token = Binder.clearCallingIdentity();
    try {
        // Restricted users are not allowed to create VPNs, they are tied to Owner
        UserInfo user = mgr.getUserInfo(mUserHandle);
        if (user.isRestricted()) {
            throw new SecurityException("Restricted users cannot establish VPNs");
        }
        ResolveInfo info = AppGlobals.getPackageManager().resolveService(intent, null, 0, mUserHandle);
        if (info == null) {
            throw new SecurityException("Cannot find " + config.user);
        }
        if (!BIND_VPN_SERVICE.equals(info.serviceInfo.permission)) {
            throw new SecurityException(config.user + " does not require " + BIND_VPN_SERVICE);
        }
    } catch (RemoteException e) {
        throw new SecurityException("Cannot find " + config.user);
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    // Save the old config in case we need to go back.
    VpnConfig oldConfig = mConfig;
    String oldInterface = mInterface;
    Connection oldConnection = mConnection;
    NetworkAgent oldNetworkAgent = mNetworkAgent;
    mNetworkAgent = null;
    Set<UidRange> oldUsers = mVpnUsers;
    // Configure the interface. Abort if any of these steps fails.
    ParcelFileDescriptor tun = ParcelFileDescriptor.adoptFd(jniCreate(config.mtu));
    try {
        updateState(DetailedState.CONNECTING, "establish");
        String interfaze = jniGetName(tun.getFd());
        // TEMP use the old jni calls until there is support for netd address setting
        StringBuilder builder = new StringBuilder();
        for (LinkAddress address : config.addresses) {
            builder.append(" " + address);
        }
        if (jniSetAddresses(interfaze, builder.toString()) < 1) {
            throw new IllegalArgumentException("At least one address must be specified");
        }
        Connection connection = new Connection();
        if (!mContext.bindServiceAsUser(intent, connection, Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE, new UserHandle(mUserHandle))) {
            throw new IllegalStateException("Cannot bind " + config.user);
        }
        mConnection = connection;
        mInterface = interfaze;
        // Fill more values.
        config.user = mPackage;
        config.interfaze = mInterface;
        config.startTime = SystemClock.elapsedRealtime();
        mConfig = config;
        // Set up forwarding and DNS rules.
        agentConnect();
        if (oldConnection != null) {
            mContext.unbindService(oldConnection);
        }
        // Remove the old tun's user forwarding rules
        // The new tun's user rules have already been added so they will take over
        // as rules are deleted. This prevents data leakage as the rules are moved over.
        agentDisconnect(oldNetworkAgent);
        if (oldInterface != null && !oldInterface.equals(interfaze)) {
            jniReset(oldInterface);
        }
        try {
            IoUtils.setBlocking(tun.getFileDescriptor(), config.blocking);
        } catch (IOException e) {
            throw new IllegalStateException("Cannot set tunnel's fd as blocking=" + config.blocking, e);
        }
    } catch (RuntimeException e) {
        IoUtils.closeQuietly(tun);
        agentDisconnect();
        // restore old state
        mConfig = oldConfig;
        mConnection = oldConnection;
        mVpnUsers = oldUsers;
        mNetworkAgent = oldNetworkAgent;
        mInterface = oldInterface;
        throw e;
    }
    Log.i(TAG, "Established by " + config.user + " on " + mInterface);
    return tun;
}
Also used : LinkAddress(android.net.LinkAddress) NetworkAgent(android.net.NetworkAgent) VpnConfig(com.android.internal.net.VpnConfig) UidRange(android.net.UidRange) ServiceConnection(android.content.ServiceConnection) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) UserInfo(android.content.pm.UserInfo) IOException(java.io.IOException) ResolveInfo(android.content.pm.ResolveInfo) UserManager(android.os.UserManager) UserHandle(android.os.UserHandle) ParcelFileDescriptor(android.os.ParcelFileDescriptor) RemoteException(android.os.RemoteException)

Example 20 with LinkAddress

use of android.net.LinkAddress in project platform_frameworks_base by android.

the class Vpn method makeLinkProperties.

private LinkProperties makeLinkProperties() {
    boolean allowIPv4 = mConfig.allowIPv4;
    boolean allowIPv6 = mConfig.allowIPv6;
    LinkProperties lp = new LinkProperties();
    lp.setInterfaceName(mInterface);
    if (mConfig.addresses != null) {
        for (LinkAddress address : mConfig.addresses) {
            lp.addLinkAddress(address);
            allowIPv4 |= address.getAddress() instanceof Inet4Address;
            allowIPv6 |= address.getAddress() instanceof Inet6Address;
        }
    }
    if (mConfig.routes != null) {
        for (RouteInfo route : mConfig.routes) {
            lp.addRoute(route);
            InetAddress address = route.getDestination().getAddress();
            allowIPv4 |= address instanceof Inet4Address;
            allowIPv6 |= address instanceof Inet6Address;
        }
    }
    if (mConfig.dnsServers != null) {
        for (String dnsServer : mConfig.dnsServers) {
            InetAddress address = InetAddress.parseNumericAddress(dnsServer);
            lp.addDnsServer(address);
            allowIPv4 |= address instanceof Inet4Address;
            allowIPv6 |= address instanceof Inet6Address;
        }
    }
    if (!allowIPv4) {
        lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.ANY, 0), RTN_UNREACHABLE));
    }
    if (!allowIPv6) {
        lp.addRoute(new RouteInfo(new IpPrefix(Inet6Address.ANY, 0), RTN_UNREACHABLE));
    }
    // Concatenate search domains into a string.
    StringBuilder buffer = new StringBuilder();
    if (mConfig.searchDomains != null) {
        for (String domain : mConfig.searchDomains) {
            buffer.append(domain).append(' ');
        }
    }
    lp.setDomains(buffer.toString().trim());
    return lp;
}
Also used : LinkAddress(android.net.LinkAddress) IpPrefix(android.net.IpPrefix) Inet4Address(java.net.Inet4Address) Inet6Address(java.net.Inet6Address) RouteInfo(android.net.RouteInfo) LinkProperties(android.net.LinkProperties) InetAddress(java.net.InetAddress)

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