Search in sources :

Example 76 with LinkAddress

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

the class ApfTest method testApfFilterMulticast.

@LargeTest
public void testApfFilterMulticast() throws Exception {
    final byte[] unicastIpv4Addr = { (byte) 192, 0, 2, 63 };
    final byte[] broadcastIpv4Addr = { (byte) 192, 0, 2, (byte) 255 };
    final byte[] multicastIpv4Addr = { (byte) 224, 0, 0, 1 };
    final byte[] multicastIpv6Addr = { (byte) 0xff, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xfb };
    MockIpManagerCallback ipManagerCallback = new MockIpManagerCallback();
    LinkAddress link = new LinkAddress(InetAddress.getByAddress(unicastIpv4Addr), 24);
    LinkProperties lp = new LinkProperties();
    lp.addLinkAddress(link);
    ApfFilter apfFilter = new TestApfFilter(ipManagerCallback, ALLOW_MULTICAST, mLog);
    apfFilter.setLinkProperties(lp);
    byte[] program = ipManagerCallback.getApfProgram();
    // Construct IPv4 and IPv6 multicast packets.
    ByteBuffer mcastv4packet = ByteBuffer.wrap(new byte[100]);
    mcastv4packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(mcastv4packet, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
    ByteBuffer mcastv6packet = ByteBuffer.wrap(new byte[100]);
    mcastv6packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IPV6);
    mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte) IPPROTO_UDP);
    put(mcastv6packet, IPV6_DEST_ADDR_OFFSET, multicastIpv6Addr);
    // Construct IPv4 broadcast packet.
    ByteBuffer bcastv4packet1 = ByteBuffer.wrap(new byte[100]);
    bcastv4packet1.put(ETH_BROADCAST_MAC_ADDRESS);
    bcastv4packet1.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4packet1, IPV4_DEST_ADDR_OFFSET, multicastIpv4Addr);
    ByteBuffer bcastv4packet2 = ByteBuffer.wrap(new byte[100]);
    bcastv4packet2.put(ETH_BROADCAST_MAC_ADDRESS);
    bcastv4packet2.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4packet2, IPV4_DEST_ADDR_OFFSET, IPV4_BROADCAST_ADDRESS);
    // Construct IPv4 broadcast with L2 unicast address packet (b/30231088).
    ByteBuffer bcastv4unicastl2packet = ByteBuffer.wrap(new byte[100]);
    bcastv4unicastl2packet.put(TestApfFilter.MOCK_MAC_ADDR);
    bcastv4unicastl2packet.putShort(ETH_ETHERTYPE_OFFSET, (short) ETH_P_IP);
    put(bcastv4unicastl2packet, IPV4_DEST_ADDR_OFFSET, broadcastIpv4Addr);
    // Verify initially disabled multicast filter is off
    assertPass(program, mcastv4packet.array());
    assertPass(program, mcastv6packet.array());
    assertPass(program, bcastv4packet1.array());
    assertPass(program, bcastv4packet2.array());
    assertPass(program, bcastv4unicastl2packet.array());
    // Turn on multicast filter and verify it works
    ipManagerCallback.resetApfProgramWait();
    apfFilter.setMulticastFilter(true);
    program = ipManagerCallback.getApfProgram();
    assertDrop(program, mcastv4packet.array());
    assertDrop(program, mcastv6packet.array());
    assertDrop(program, bcastv4packet1.array());
    assertDrop(program, bcastv4packet2.array());
    assertDrop(program, bcastv4unicastl2packet.array());
    // Turn off multicast filter and verify it's off
    ipManagerCallback.resetApfProgramWait();
    apfFilter.setMulticastFilter(false);
    program = ipManagerCallback.getApfProgram();
    assertPass(program, mcastv4packet.array());
    assertPass(program, mcastv6packet.array());
    assertPass(program, bcastv4packet1.array());
    assertPass(program, bcastv4packet2.array());
    assertPass(program, bcastv4unicastl2packet.array());
    // Verify it can be initialized to on
    ipManagerCallback.resetApfProgramWait();
    apfFilter.shutdown();
    apfFilter = new TestApfFilter(ipManagerCallback, DROP_MULTICAST, mLog);
    apfFilter.setLinkProperties(lp);
    program = ipManagerCallback.getApfProgram();
    assertDrop(program, mcastv4packet.array());
    assertDrop(program, mcastv6packet.array());
    assertDrop(program, bcastv4packet1.array());
    assertDrop(program, bcastv4unicastl2packet.array());
    // Verify that ICMPv6 multicast is not dropped.
    mcastv6packet.put(IPV6_NEXT_HEADER_OFFSET, (byte) IPPROTO_ICMPV6);
    assertPass(program, mcastv6packet.array());
    apfFilter.shutdown();
}
Also used : LinkAddress(android.net.LinkAddress) ApfFilter(android.net.apf.ApfFilter) LinkProperties(android.net.LinkProperties) ByteBuffer(java.nio.ByteBuffer) LargeTest(android.test.suitebuilder.annotation.LargeTest)

Example 77 with LinkAddress

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

the class DhcpPacketTest method checkIpAddress.

private void checkIpAddress(String expected, byte type, Inet4Address clientIp, Inet4Address yourIp, byte[] netmaskBytes) throws Exception {
    ByteBuffer packet = new TestDhcpPacket(type, clientIp, yourIp).setNetmaskBytes(netmaskBytes).build();
    DhcpPacket offerPacket = DhcpPacket.decodeFullPacket(packet, ENCAP_BOOTP);
    DhcpResults results = offerPacket.toDhcpResults();
    if (expected != null) {
        LinkAddress expectedAddress = new LinkAddress(expected);
        assertEquals(expectedAddress, results.ipAddress);
    } else {
        assertNull(results);
    }
}
Also used : LinkAddress(android.net.LinkAddress) DhcpResults(android.net.DhcpResults) ByteBuffer(java.nio.ByteBuffer) DhcpPacket(android.net.dhcp.DhcpPacket)

Example 78 with LinkAddress

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

the class WifiConfigurationHelper method getWifiConfiguration.

/**
     * Parse a {@link JSONObject} and return the wifi configuration.
     *
     * @throws IllegalArgumentException if any mandatory fields are missing.
     */
private static WifiConfiguration getWifiConfiguration(JSONObject jsonConfig) throws JSONException {
    String ssid = jsonConfig.getString("ssid");
    String password = null;
    WifiConfiguration config;
    int securityType = getSecurityType(jsonConfig.getString("security"));
    switch(securityType) {
        case NONE:
            config = createOpenConfig(ssid);
            break;
        case WEP:
            password = jsonConfig.getString("password");
            config = createWepConfig(ssid, password);
            break;
        case PSK:
            password = jsonConfig.getString("password");
            config = createPskConfig(ssid, password);
            break;
        case EAP:
            password = jsonConfig.getString("password");
            int eapMethod = getEapMethod(jsonConfig.getString("eap"));
            Integer phase2 = null;
            if (jsonConfig.has("phase2")) {
                phase2 = getPhase2(jsonConfig.getString("phase2"));
            }
            String identity = null;
            if (jsonConfig.has("identity")) {
                identity = jsonConfig.getString("identity");
            }
            String anonymousIdentity = null;
            if (jsonConfig.has("anonymous_identity")) {
                anonymousIdentity = jsonConfig.getString("anonymous_identity");
            }
            String caCert = null;
            if (jsonConfig.has("ca_cert")) {
                caCert = (jsonConfig.getString("ca_cert"));
            }
            String clientCert = null;
            if (jsonConfig.has("client_cert")) {
                clientCert = jsonConfig.getString("client_cert");
            }
            config = createEapConfig(ssid, password, eapMethod, phase2, identity, anonymousIdentity, caCert, clientCert);
            break;
        default:
            // Should never reach here as getSecurityType will already throw an exception
            throw new IllegalArgumentException();
    }
    if (jsonConfig.has("ip")) {
        StaticIpConfiguration staticIpConfig = new StaticIpConfiguration();
        InetAddress ipAddress = getInetAddress(jsonConfig.getString("ip"));
        int prefixLength = getPrefixLength(jsonConfig.getInt("prefix_length"));
        staticIpConfig.ipAddress = new LinkAddress(ipAddress, prefixLength);
        staticIpConfig.gateway = getInetAddress(jsonConfig.getString("gateway"));
        staticIpConfig.dnsServers.add(getInetAddress(jsonConfig.getString("dns1")));
        staticIpConfig.dnsServers.add(getInetAddress(jsonConfig.getString("dns2")));
        config.setIpAssignment(IpAssignment.STATIC);
        config.setStaticIpConfiguration(staticIpConfig);
    } else {
        config.setIpAssignment(IpAssignment.DHCP);
    }
    config.setProxySettings(ProxySettings.NONE);
    return config;
}
Also used : LinkAddress(android.net.LinkAddress) WifiConfiguration(android.net.wifi.WifiConfiguration) StaticIpConfiguration(android.net.StaticIpConfiguration) InetAddress(java.net.InetAddress)

Example 79 with LinkAddress

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

the class StaticIpConfigurationTest method testHashCodeAndEquals.

@SmallTest
public void testHashCodeAndEquals() {
    HashSet<Integer> hashCodes = new HashSet();
    hashCodes.add(0);
    StaticIpConfiguration s = new StaticIpConfiguration();
    // Check that this hash code is nonzero and different from all the ones seen so far.
    assertTrue(hashCodes.add(s.hashCode()));
    s.ipAddress = ADDR;
    assertTrue(hashCodes.add(s.hashCode()));
    s.gateway = GATEWAY;
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS1);
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS2);
    assertTrue(hashCodes.add(s.hashCode()));
    s.dnsServers.add(DNS3);
    assertTrue(hashCodes.add(s.hashCode()));
    s.domains = "example.com";
    assertTrue(hashCodes.add(s.hashCode()));
    assertFalse(s.equals(null));
    assertEquals(s, s);
    StaticIpConfiguration s2 = new StaticIpConfiguration(s);
    assertEquals(s, s2);
    s.ipAddress = new LinkAddress(DNS1, 32);
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.domains = "foo";
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.gateway = DNS2;
    assertNotEquals(s, s2);
    s2 = new StaticIpConfiguration(s);
    s.dnsServers.add(DNS3);
    assertNotEquals(s, s2);
}
Also used : LinkAddress(android.net.LinkAddress) StaticIpConfiguration(android.net.StaticIpConfiguration) HashSet(java.util.HashSet) SmallTest(android.test.suitebuilder.annotation.SmallTest)

Example 80 with LinkAddress

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

the class LockdownVpnTracker method clearSourceRulesLocked.

private void clearSourceRulesLocked() {
    try {
        if (mAcceptedIface != null) {
            mNetService.setFirewallInterfaceRule(mAcceptedIface, false);
            mAcceptedIface = null;
        }
        if (mAcceptedSourceAddr != null) {
            for (LinkAddress addr : mAcceptedSourceAddr) {
                setFirewallEgressSourceRule(addr, false);
            }
            mNetService.setFirewallUidRule(FIREWALL_CHAIN_NONE, ROOT_UID, FIREWALL_RULE_DEFAULT);
            mNetService.setFirewallUidRule(FIREWALL_CHAIN_NONE, Os.getuid(), FIREWALL_RULE_DEFAULT);
            mAcceptedSourceAddr = null;
        }
    } catch (RemoteException e) {
        throw new RuntimeException("Problem setting firewall rules", e);
    }
}
Also used : LinkAddress(android.net.LinkAddress) RemoteException(android.os.RemoteException)

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