Search in sources :

Example 31 with UidRange

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

the class Vpn method agentConnect.

private void agentConnect() {
    LinkProperties lp = makeLinkProperties();
    if (lp.hasIPv4DefaultRoute() || lp.hasIPv6DefaultRoute()) {
        mNetworkCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    } else {
        mNetworkCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
    }
    mNetworkInfo.setDetailedState(DetailedState.CONNECTING, null, null);
    NetworkMisc networkMisc = new NetworkMisc();
    networkMisc.allowBypass = mConfig.allowBypass && !mLockdown;
    long token = Binder.clearCallingIdentity();
    try {
        mNetworkAgent = new NetworkAgent(mLooper, mContext, NETWORKTYPE, mNetworkInfo, mNetworkCapabilities, lp, 0, networkMisc) {

            @Override
            public void unwanted() {
            // We are user controlled, not driven by NetworkRequest.
            }
        };
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    mVpnUsers = createUserAndRestrictedProfilesRanges(mUserHandle, mConfig.allowedApplications, mConfig.disallowedApplications);
    mNetworkAgent.addUidRanges(mVpnUsers.toArray(new UidRange[mVpnUsers.size()]));
    mNetworkInfo.setIsAvailable(true);
    updateState(DetailedState.CONNECTED, "agentConnect");
}
Also used : NetworkMisc(android.net.NetworkMisc) NetworkAgent(android.net.NetworkAgent) UidRange(android.net.UidRange) LinkProperties(android.net.LinkProperties)

Example 32 with UidRange

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

the class Vpn method createUserAndRestrictedProfilesRanges.

/**
     * Creates a {@link Set} of non-intersecting {@link UidRange} objects including all UIDs
     * associated with one user, and any restricted profiles attached to that user.
     *
     * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided,
     * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs
     * in each user and profile will be included.
     *
     * @param userHandle The userId to create UID ranges for along with any of its restricted
     *                   profiles.
     * @param allowedApplications (optional) whitelist of applications to include.
     * @param disallowedApplications (optional) blacklist of applications to exclude.
     */
@VisibleForTesting
Set<UidRange> createUserAndRestrictedProfilesRanges(@UserIdInt int userHandle, @Nullable List<String> allowedApplications, @Nullable List<String> disallowedApplications) {
    final Set<UidRange> ranges = new ArraySet<>();
    // Assign the top-level user to the set of ranges
    addUserToRanges(ranges, userHandle, allowedApplications, disallowedApplications);
    // If the user can have restricted profiles, assign all its restricted profiles too
    if (canHaveRestrictedProfile(userHandle)) {
        final long token = Binder.clearCallingIdentity();
        List<UserInfo> users;
        try {
            users = UserManager.get(mContext).getUsers(true);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        for (UserInfo user : users) {
            if (user.isRestricted() && (user.restrictedProfileParentId == userHandle)) {
                addUserToRanges(ranges, user.id, allowedApplications, disallowedApplications);
            }
        }
    }
    return ranges;
}
Also used : ArraySet(android.util.ArraySet) UidRange(android.net.UidRange) UserInfo(android.content.pm.UserInfo) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 33 with UidRange

use of android.net.UidRange in project android_frameworks_base by crdroidandroid.

the class NetworkManagementService method closeSocketsForFirewallChainLocked.

private void closeSocketsForFirewallChainLocked(int chain, String chainName) {
    // UID ranges to close sockets on.
    UidRange[] ranges;
    // UID ranges whose sockets we won't touch.
    int[] exemptUids;
    final SparseIntArray rules = getUidFirewallRules(chain);
    int numUids = 0;
    if (getFirewallType(chain) == FIREWALL_TYPE_WHITELIST) {
        // Close all sockets on all non-system UIDs...
        ranges = new UidRange[] { // specify their ranges here.
        new UidRange(Process.FIRST_APPLICATION_UID, Integer.MAX_VALUE) };
        // ... except for the UIDs that have allow rules.
        exemptUids = new int[rules.size()];
        for (int i = 0; i < exemptUids.length; i++) {
            if (rules.valueAt(i) == NetworkPolicyManager.FIREWALL_RULE_ALLOW) {
                exemptUids[numUids] = rules.keyAt(i);
                numUids++;
            }
        }
        // fix setFirewallEnabled to grab mQuotaLock and clear rules.
        if (numUids != exemptUids.length) {
            exemptUids = Arrays.copyOf(exemptUids, numUids);
        }
    } else {
        // Close sockets for every UID that has a deny rule...
        ranges = new UidRange[rules.size()];
        for (int i = 0; i < ranges.length; i++) {
            if (rules.valueAt(i) == NetworkPolicyManager.FIREWALL_RULE_DENY) {
                int uid = rules.keyAt(i);
                ranges[numUids] = new UidRange(uid, uid);
                numUids++;
            }
        }
        // As above; usually numUids == ranges.length, but not always.
        if (numUids != ranges.length) {
            ranges = Arrays.copyOf(ranges, numUids);
        }
        // ... with no exceptions.
        exemptUids = new int[0];
    }
    try {
        mNetdService.socketDestroy(ranges, exemptUids);
    } catch (RemoteException | ServiceSpecificException e) {
        Slog.e(TAG, "Error closing sockets after enabling chain " + chainName + ": " + e);
    }
}
Also used : ServiceSpecificException(android.os.ServiceSpecificException) UidRange(android.net.UidRange) SparseIntArray(android.util.SparseIntArray) RemoteException(android.os.RemoteException)

Example 34 with UidRange

use of android.net.UidRange in project android_frameworks_base by crdroidandroid.

the class Vpn method uidRangesForUser.

// Returns the subset of the full list of active UID ranges the VPN applies to (mVpnUsers) that
// apply to userHandle.
private List<UidRange> uidRangesForUser(int userHandle) {
    final UidRange userRange = UidRange.createForUser(userHandle);
    final List<UidRange> ranges = new ArrayList<UidRange>();
    for (UidRange range : mVpnUsers) {
        if (userRange.containsRange(range)) {
            ranges.add(range);
        }
    }
    return ranges;
}
Also used : UidRange(android.net.UidRange) ArrayList(java.util.ArrayList)

Example 35 with UidRange

use of android.net.UidRange in project android_frameworks_base by crdroidandroid.

the class Vpn method createUserAndRestrictedProfilesRanges.

/**
     * Creates a {@link Set} of non-intersecting {@link UidRange} objects including all UIDs
     * associated with one user, and any restricted profiles attached to that user.
     *
     * <p>If one of {@param allowedApplications} or {@param disallowedApplications} is provided,
     * the UID ranges will match the app whitelist or blacklist specified there. Otherwise, all UIDs
     * in each user and profile will be included.
     *
     * @param userHandle The userId to create UID ranges for along with any of its restricted
     *                   profiles.
     * @param allowedApplications (optional) whitelist of applications to include.
     * @param disallowedApplications (optional) blacklist of applications to exclude.
     */
@VisibleForTesting
Set<UidRange> createUserAndRestrictedProfilesRanges(@UserIdInt int userHandle, @Nullable List<String> allowedApplications, @Nullable List<String> disallowedApplications) {
    final Set<UidRange> ranges = new ArraySet<>();
    // Assign the top-level user to the set of ranges
    addUserToRanges(ranges, userHandle, allowedApplications, disallowedApplications);
    // If the user can have restricted profiles, assign all its restricted profiles too
    if (canHaveRestrictedProfile(userHandle)) {
        final long token = Binder.clearCallingIdentity();
        List<UserInfo> users;
        try {
            users = UserManager.get(mContext).getUsers(true);
        } finally {
            Binder.restoreCallingIdentity(token);
        }
        for (UserInfo user : users) {
            if (user.isRestricted() && (user.restrictedProfileParentId == userHandle)) {
                addUserToRanges(ranges, user.id, allowedApplications, disallowedApplications);
            }
        }
    }
    return ranges;
}
Also used : ArraySet(android.util.ArraySet) UidRange(android.net.UidRange) UserInfo(android.content.pm.UserInfo) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

UidRange (android.net.UidRange)42 UserInfo (android.content.pm.UserInfo)19 RemoteException (android.os.RemoteException)15 SmallTest (android.test.suitebuilder.annotation.SmallTest)12 NetworkAgent (android.net.NetworkAgent)10 IOException (java.io.IOException)10 PendingIntent (android.app.PendingIntent)5 Intent (android.content.Intent)5 ServiceConnection (android.content.ServiceConnection)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 ResolveInfo (android.content.pm.ResolveInfo)5 LinkAddress (android.net.LinkAddress)5 LinkProperties (android.net.LinkProperties)5 NetworkMisc (android.net.NetworkMisc)5 ParcelFileDescriptor (android.os.ParcelFileDescriptor)5 ServiceSpecificException (android.os.ServiceSpecificException)5 UserHandle (android.os.UserHandle)5 UserManager (android.os.UserManager)5 ArraySet (android.util.ArraySet)5 SparseIntArray (android.util.SparseIntArray)5