Search in sources :

Example 36 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 37 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 38 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 39 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)

Example 40 with LinkAddress

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

the class LockdownVpnTracker method handleStateChangedLocked.

/**
     * Watch for state changes to both active egress network, kicking off a VPN
     * connection when ready, or setting firewall rules once VPN is connected.
     */
private void handleStateChangedLocked() {
    final NetworkInfo egressInfo = mConnService.getActiveNetworkInfoUnfiltered();
    final LinkProperties egressProp = mConnService.getActiveLinkProperties();
    final NetworkInfo vpnInfo = mVpn.getNetworkInfo();
    final VpnConfig vpnConfig = mVpn.getLegacyVpnConfig();
    // Restart VPN when egress network disconnected or changed
    final boolean egressDisconnected = egressInfo == null || State.DISCONNECTED.equals(egressInfo.getState());
    final boolean egressChanged = egressProp == null || !TextUtils.equals(mAcceptedEgressIface, egressProp.getInterfaceName());
    final String egressTypeName = (egressInfo == null) ? null : ConnectivityManager.getNetworkTypeName(egressInfo.getType());
    final String egressIface = (egressProp == null) ? null : egressProp.getInterfaceName();
    Slog.d(TAG, "handleStateChanged: egress=" + egressTypeName + " " + mAcceptedEgressIface + "->" + egressIface);
    if (egressDisconnected || egressChanged) {
        clearSourceRulesLocked();
        mAcceptedEgressIface = null;
        mVpn.stopLegacyVpnPrivileged();
    }
    if (egressDisconnected) {
        hideNotification();
        return;
    }
    final int egressType = egressInfo.getType();
    if (vpnInfo.getDetailedState() == DetailedState.FAILED) {
        EventLogTags.writeLockdownVpnError(egressType);
    }
    if (mErrorCount > MAX_ERROR_COUNT) {
        showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);
    } else if (egressInfo.isConnected() && !vpnInfo.isConnectedOrConnecting()) {
        if (mProfile.isValidLockdownProfile()) {
            Slog.d(TAG, "Active network connected; starting VPN");
            EventLogTags.writeLockdownVpnConnecting(egressType);
            showNotification(R.string.vpn_lockdown_connecting, R.drawable.vpn_disconnected);
            mAcceptedEgressIface = egressProp.getInterfaceName();
            try {
                // Use the privileged method because Lockdown VPN is initiated by the system, so
                // no additional permission checks are necessary.
                mVpn.startLegacyVpnPrivileged(mProfile, KeyStore.getInstance(), egressProp);
            } catch (IllegalStateException e) {
                mAcceptedEgressIface = null;
                Slog.e(TAG, "Failed to start VPN", e);
                showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);
            }
        } else {
            Slog.e(TAG, "Invalid VPN profile; requires IP-based server and DNS");
            showNotification(R.string.vpn_lockdown_error, R.drawable.vpn_disconnected);
        }
    } else if (vpnInfo.isConnected() && vpnConfig != null) {
        final String iface = vpnConfig.interfaze;
        final List<LinkAddress> sourceAddrs = vpnConfig.addresses;
        if (TextUtils.equals(iface, mAcceptedIface) && sourceAddrs.equals(mAcceptedSourceAddr)) {
            return;
        }
        Slog.d(TAG, "VPN connected using iface=" + iface + ", sourceAddr=" + sourceAddrs.toString());
        EventLogTags.writeLockdownVpnConnected(egressType);
        showNotification(R.string.vpn_lockdown_connected, R.drawable.vpn_connected);
        try {
            clearSourceRulesLocked();
            mNetService.setFirewallInterfaceRule(iface, true);
            for (LinkAddress addr : sourceAddrs) {
                setFirewallEgressSourceRule(addr, true);
            }
            mNetService.setFirewallUidRule(FIREWALL_CHAIN_NONE, ROOT_UID, FIREWALL_RULE_ALLOW);
            mNetService.setFirewallUidRule(FIREWALL_CHAIN_NONE, Os.getuid(), FIREWALL_RULE_ALLOW);
            mErrorCount = 0;
            mAcceptedIface = iface;
            mAcceptedSourceAddr = sourceAddrs;
        } catch (RemoteException e) {
            throw new RuntimeException("Problem setting firewall rules", e);
        }
        final NetworkInfo clone = new NetworkInfo(egressInfo);
        augmentNetworkInfo(clone);
        mConnService.sendConnectedBroadcast(clone);
    }
}
Also used : LinkAddress(android.net.LinkAddress) VpnConfig(com.android.internal.net.VpnConfig) NetworkInfo(android.net.NetworkInfo) RemoteException(android.os.RemoteException) 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