Merge "Allow wifi p2p to use legacy dedicated address" am: d2af4d9e2a am: b537c25685 am: 04720e79b0 am: e0e75de37e am: 264d16941a

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1382398

Change-Id: I3d24b5c68ca741ee3f63bf60623ff7882ea9cc50
This commit is contained in:
Mark Chien
2020-08-05 07:35:53 +00:00
committed by Automerger Merge Worker
7 changed files with 108 additions and 4 deletions

View File

@@ -15,6 +15,8 @@
*/
package com.android.networkstack.tethering;
import static android.net.TetheringManager.TETHERING_WIFI_P2P;
import static java.util.Arrays.asList;
import android.content.Context;
@@ -58,6 +60,7 @@ public class PrivateAddressCoordinator {
private static final int BYTE_MASK = 0xff;
// reserved for bluetooth tethering.
private static final int BLUETOOTH_RESERVED = 44;
private static final int WIFI_P2P_RESERVED = 49;
private static final byte DEFAULT_ID = (byte) 42;
// Upstream monitor would be stopped when tethering is down. When tethering restart, downstream
@@ -71,15 +74,18 @@ public class PrivateAddressCoordinator {
// 10.0.0.0/8, 172.16.0.0/12, 192.168.0.0/16
// Tethering use 192.168.0.0/16 that has 256 contiguous class C network numbers.
private static final String DEFAULT_TETHERING_PREFIX = "192.168.0.0/16";
private static final String LEGACY_WIFI_P2P_IFACE_ADDRESS = "192.168.49.1/24";
private final IpPrefix mTetheringPrefix;
private final ConnectivityManager mConnectivityMgr;
private final TetheringConfiguration mConfig;
public PrivateAddressCoordinator(Context context) {
public PrivateAddressCoordinator(Context context, TetheringConfiguration config) {
mDownstreams = new ArraySet<>();
mUpstreamPrefixMap = new ArrayMap<>();
mTetheringPrefix = new IpPrefix(DEFAULT_TETHERING_PREFIX);
mConnectivityMgr = (ConnectivityManager) context.getSystemService(
Context.CONNECTIVITY_SERVICE);
mConfig = config;
}
/**
@@ -141,12 +147,21 @@ public class PrivateAddressCoordinator {
mUpstreamPrefixMap.removeAll(toBeRemoved);
}
private boolean isReservedSubnet(final int subnet) {
return subnet == BLUETOOTH_RESERVED || subnet == WIFI_P2P_RESERVED;
}
/**
* Pick a random available address and mark its prefix as in use for the provided IpServer,
* returns null if there is no available address.
*/
@Nullable
public LinkAddress requestDownstreamAddress(final IpServer ipServer) {
if (mConfig.shouldEnableWifiP2pDedicatedIp()
&& ipServer.interfaceType() == TETHERING_WIFI_P2P) {
return new LinkAddress(LEGACY_WIFI_P2P_IFACE_ADDRESS);
}
// Address would be 192.168.[subAddress]/24.
final byte[] bytes = mTetheringPrefix.getRawAddress();
final int subAddress = getRandomSubAddr();
@@ -154,7 +169,7 @@ public class PrivateAddressCoordinator {
bytes[3] = getSanitizedAddressSuffix(subAddress, (byte) 0, (byte) 1, (byte) 0xff);
for (int i = 0; i < MAX_UBYTE; i++) {
final int newSubNet = (subNet + i) & BYTE_MASK;
if (newSubNet == BLUETOOTH_RESERVED) continue;
if (isReservedSubnet(newSubNet)) continue;
bytes[2] = (byte) newSubNet;
final InetAddress addr;

View File

@@ -320,10 +320,13 @@ public class Tethering {
mExecutor = new TetheringThreadExecutor(mHandler);
mActiveDataSubIdListener = new ActiveDataSubIdListener(mExecutor);
mNetdCallback = new NetdCallback();
mPrivateAddressCoordinator = new PrivateAddressCoordinator(mContext);
// Load tethering configuration.
updateConfiguration();
// It is OK for the configuration to be passed to the PrivateAddressCoordinator at
// construction time because the only part of the configuration it uses is
// shouldEnableWifiP2pDedicatedIp(), and currently do not support changing that.
mPrivateAddressCoordinator = new PrivateAddressCoordinator(mContext, mConfig);
// Must be initialized after tethering configuration is loaded because BpfCoordinator
// constructor needs to use the configuration.

View File

@@ -84,6 +84,9 @@ public class TetheringConfiguration {
public static final String TETHER_ENABLE_LEGACY_DHCP_SERVER =
"tether_enable_legacy_dhcp_server";
public static final String USE_LEGACY_WIFI_P2P_DEDICATED_IP =
"use_legacy_wifi_p2p_dedicated_ip";
/**
* Default value that used to periodic polls tether offload stats from tethering offload HAL
* to make the data warnings work.
@@ -113,6 +116,7 @@ public class TetheringConfiguration {
private final int mOffloadPollInterval;
// TODO: Add to TetheringConfigurationParcel if required.
private final boolean mEnableBpfOffload;
private final boolean mEnableWifiP2pDedicatedIp;
public TetheringConfiguration(Context ctx, SharedLog log, int id) {
final SharedLog configLog = log.forSubComponent("config");
@@ -156,6 +160,10 @@ public class TetheringConfiguration {
R.integer.config_tether_offload_poll_interval,
DEFAULT_TETHER_OFFLOAD_POLL_INTERVAL_MS);
mEnableWifiP2pDedicatedIp = getResourceBoolean(res,
R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip,
false /* defaultValue */);
configLog.log(toString());
}
@@ -199,6 +207,11 @@ public class TetheringConfiguration {
return !TextUtils.isEmpty(provisioningAppNoUi);
}
/** Check whether dedicated wifi p2p address is enabled. */
public boolean shouldEnableWifiP2pDedicatedIp() {
return mEnableWifiP2pDedicatedIp;
}
/** Does the dumping.*/
public void dump(PrintWriter pw) {
pw.print("activeDataSubId: ");
@@ -233,6 +246,9 @@ public class TetheringConfiguration {
pw.print("enableLegacyDhcpServer: ");
pw.println(enableLegacyDhcpServer);
pw.print("enableWifiP2pDedicatedIp: ");
pw.println(mEnableWifiP2pDedicatedIp);
}
/** Returns the string representation of this object.*/