Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 8c93a769 authored by Lorenzo Colitti's avatar Lorenzo Colitti Committed by Gerrit Code Review
Browse files

Merge changes Idfb5e149,Ia52b24c5

* changes:
  Stop using NetworkState internally in ConnectivityService.
  Isolate an ad-hoc legacy API codepath.
parents 0fb5c3f3 1881ae03
Loading
Loading
Loading
Loading
+64 −61
Original line number Original line Diff line number Diff line
@@ -1403,7 +1403,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
        return null;
        return null;
    }
    }


    private NetworkState getUnfilteredActiveNetworkState(int uid) {
    private NetworkAgentInfo getNetworkAgentInfoForUid(int uid) {
        NetworkAgentInfo nai = getDefaultNetworkForUid(uid);
        NetworkAgentInfo nai = getDefaultNetworkForUid(uid);


        final Network[] networks = getVpnUnderlyingNetworks(uid);
        final Network[] networks = getVpnUnderlyingNetworks(uid);
@@ -1419,12 +1419,7 @@ public class ConnectivityService extends IConnectivityManager.Stub
                nai = null;
                nai = null;
            }
            }
        }
        }

        return nai;
        if (nai != null) {
            return nai.getNetworkState();
        } else {
            return NetworkState.EMPTY;
        }
    }
    }


    /**
    /**
@@ -1477,24 +1472,28 @@ public class ConnectivityService extends IConnectivityManager.Stub
                "%s %d(%d) on netId %d", action, nri.mUid, requestId, net.getNetId()));
                "%s %d(%d) on netId %d", action, nri.mUid, requestId, net.getNetId()));
    }
    }


    private void filterNetworkInfo(@NonNull NetworkInfo networkInfo,
            @NonNull NetworkCapabilities nc, int uid, boolean ignoreBlocked) {
        if (isNetworkWithCapabilitiesBlocked(nc, uid, ignoreBlocked)) {
            networkInfo.setDetailedState(DetailedState.BLOCKED, null, null);
        }
        networkInfo.setDetailedState(
                getLegacyLockdownState(networkInfo.getDetailedState()),
                "" /* reason */, null /* extraInfo */);
    }

    /**
    /**
     * Apply any relevant filters to {@link NetworkState} for the given UID. For
     * Apply any relevant filters to the specified {@link NetworkInfo} for the given UID. For
     * example, this may mark the network as {@link DetailedState#BLOCKED} based
     * example, this may mark the network as {@link DetailedState#BLOCKED} based
     * on {@link #isNetworkWithCapabilitiesBlocked}.
     * on {@link #isNetworkWithCapabilitiesBlocked}.
     */
     */
    private void filterNetworkStateForUid(NetworkState state, int uid, boolean ignoreBlocked) {
    @NonNull
        if (state == null || state.networkInfo == null || state.linkProperties == null) return;
    private NetworkInfo filterNetworkInfo(@NonNull NetworkInfo networkInfo, int type,
        filterNetworkInfo(state.networkInfo, state.networkCapabilities, uid, ignoreBlocked);
            @NonNull NetworkCapabilities nc, int uid, boolean ignoreBlocked) {
        NetworkInfo filtered = new NetworkInfo(networkInfo);
        filtered.setType(type);
        final DetailedState state = isNetworkWithCapabilitiesBlocked(nc, uid, ignoreBlocked)
                ? DetailedState.BLOCKED
                : filtered.getDetailedState();
        filtered.setDetailedState(getLegacyLockdownState(state),
                "" /* reason */, null /* extraInfo */);
        return filtered;
    }

    private NetworkInfo getFilteredNetworkInfo(NetworkAgentInfo nai, int uid,
            boolean ignoreBlocked) {
        return filterNetworkInfo(nai.networkInfo, nai.networkInfo.getType(),
                nai.networkCapabilities, uid, ignoreBlocked);
    }
    }


    /**
    /**
@@ -1508,10 +1507,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
    public NetworkInfo getActiveNetworkInfo() {
    public NetworkInfo getActiveNetworkInfo() {
        enforceAccessPermission();
        enforceAccessPermission();
        final int uid = mDeps.getCallingUid();
        final int uid = mDeps.getCallingUid();
        final NetworkState state = getUnfilteredActiveNetworkState(uid);
        final NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid);
        filterNetworkStateForUid(state, uid, false);
        if (nai == null) return null;
        maybeLogBlockedNetworkInfo(state.networkInfo, uid);
        final NetworkInfo networkInfo = getFilteredNetworkInfo(nai, uid, false);
        return state.networkInfo;
        maybeLogBlockedNetworkInfo(networkInfo, uid);
        return networkInfo;
    }
    }


    @Override
    @Override
@@ -1546,30 +1546,37 @@ public class ConnectivityService extends IConnectivityManager.Stub
    @Override
    @Override
    public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
    public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
        PermissionUtils.enforceNetworkStackPermission(mContext);
        PermissionUtils.enforceNetworkStackPermission(mContext);
        final NetworkState state = getUnfilteredActiveNetworkState(uid);
        final NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid);
        filterNetworkStateForUid(state, uid, ignoreBlocked);
        if (nai == null) return null;
        return state.networkInfo;
        return getFilteredNetworkInfo(nai, uid, ignoreBlocked);
    }
    }


    private NetworkInfo getFilteredNetworkInfo(int networkType, int uid) {
    /** Returns a NetworkInfo object for a network that doesn't exist. */
    private NetworkInfo makeFakeNetworkInfo(int networkType, int uid) {
        final NetworkInfo info = new NetworkInfo(networkType, 0 /* subtype */,
                getNetworkTypeName(networkType), "" /* subtypeName */);
        info.setIsAvailable(true);
        // For compatibility with legacy code, return BLOCKED instead of DISCONNECTED when
        // background data is restricted.
        final NetworkCapabilities nc = new NetworkCapabilities();  // Metered.
        final DetailedState state = isNetworkWithCapabilitiesBlocked(nc, uid, false)
                ? DetailedState.BLOCKED
                : DetailedState.DISCONNECTED;
        info.setDetailedState(getLegacyLockdownState(state),
                "" /* reason */, null /* extraInfo */);
        return info;
    }

    private NetworkInfo getFilteredNetworkInfoForType(int networkType, int uid) {
        if (!mLegacyTypeTracker.isTypeSupported(networkType)) {
        if (!mLegacyTypeTracker.isTypeSupported(networkType)) {
            return null;
            return null;
        }
        }
        final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
        final NetworkAgentInfo nai = mLegacyTypeTracker.getNetworkForType(networkType);
        final NetworkInfo info;
        if (nai == null) {
        final NetworkCapabilities nc;
            return makeFakeNetworkInfo(networkType, uid);
        if (nai != null) {
            info = new NetworkInfo(nai.networkInfo);
            info.setType(networkType);
            nc = nai.networkCapabilities;
        } else {
            info = new NetworkInfo(networkType, 0, getNetworkTypeName(networkType), "");
            info.setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, null, null);
            info.setIsAvailable(true);
            nc = new NetworkCapabilities();
        }
        }
        filterNetworkInfo(info, nc, uid, false);
        return filterNetworkInfo(nai.networkInfo, networkType, nai.networkCapabilities, uid,
        return info;
                false);
    }
    }


    @Override
    @Override
@@ -1579,27 +1586,23 @@ public class ConnectivityService extends IConnectivityManager.Stub
        if (getVpnUnderlyingNetworks(uid) != null) {
        if (getVpnUnderlyingNetworks(uid) != null) {
            // A VPN is active, so we may need to return one of its underlying networks. This
            // A VPN is active, so we may need to return one of its underlying networks. This
            // information is not available in LegacyTypeTracker, so we have to get it from
            // information is not available in LegacyTypeTracker, so we have to get it from
            // getUnfilteredActiveNetworkState.
            // getNetworkAgentInfoForUid.
            final NetworkState state = getUnfilteredActiveNetworkState(uid);
            final NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid);
            if (state.networkInfo != null && state.networkInfo.getType() == networkType) {
            if (nai == null) return null;
                filterNetworkStateForUid(state, uid, false);
            final NetworkInfo networkInfo = getFilteredNetworkInfo(nai, uid, false);
                return state.networkInfo;
            if (networkInfo.getType() == networkType) {
                return networkInfo;
            }
            }
        }
        }
        return getFilteredNetworkInfo(networkType, uid);
        return getFilteredNetworkInfoForType(networkType, uid);
    }
    }


    @Override
    @Override
    public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
    public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
        enforceAccessPermission();
        enforceAccessPermission();
        final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
        final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network);
        if (nai != null) {
        if (nai == null) return null;
            final NetworkState state = nai.getNetworkState();
        return getFilteredNetworkInfo(nai, uid, ignoreBlocked);
            filterNetworkStateForUid(state, uid, ignoreBlocked);
            return state.networkInfo;
        } else {
            return null;
        }
    }
    }


    @Override
    @Override
@@ -1627,11 +1630,11 @@ public class ConnectivityService extends IConnectivityManager.Stub
            return null;
            return null;
        }
        }
        final int uid = mDeps.getCallingUid();
        final int uid = mDeps.getCallingUid();
        if (!isNetworkWithCapabilitiesBlocked(nai.networkCapabilities, uid, false)) {
        if (isNetworkWithCapabilitiesBlocked(nai.networkCapabilities, uid, false)) {
            return nai.network;
        }
            return null;
            return null;
        }
        }
        return nai.network;
    }


    @Override
    @Override
    public Network[] getAllNetworks() {
    public Network[] getAllNetworks() {
@@ -1719,9 +1722,9 @@ public class ConnectivityService extends IConnectivityManager.Stub
    public LinkProperties getActiveLinkProperties() {
    public LinkProperties getActiveLinkProperties() {
        enforceAccessPermission();
        enforceAccessPermission();
        final int uid = mDeps.getCallingUid();
        final int uid = mDeps.getCallingUid();
        NetworkState state = getUnfilteredActiveNetworkState(uid);
        NetworkAgentInfo nai = getNetworkAgentInfoForUid(uid);
        if (state.linkProperties == null) return null;
        if (nai == null) return null;
        return linkPropertiesRestrictedForCallerPermissions(state.linkProperties,
        return linkPropertiesRestrictedForCallerPermissions(nai.linkProperties,
                Binder.getCallingPid(), uid);
                Binder.getCallingPid(), uid);
    }
    }