Add config_p2p_leases_subnet_prefix_length configuration

Add new rro configuration which can be used to make the p2p dhcp
prefix length larger to reserve the address range outside of leases
subnet prefix length for EAPOL-Key feature. This configuration only
valid if its value larger than dhcp server address prefix length and
config_tether_enable_legacy_wifi_p2p_dedicated_ip is true.
E.g.:leaseSubnetPrefixLength = 25, p2p static address = 192.168.49.1/24
dhcp range: 192.168.49.0 ~ 192.168.49.127 (192.168.49.1/25), reserved
192.168.49.128 ~ 192.168.49.255 for EAPOL-Key feature.

Bug: 170056953
Test: atest TetheringTests

Change-Id: I1319efd871796da7234383a29ab64a1623101ae7
This commit is contained in:
markchien
2022-02-25 23:14:58 +08:00
parent 3d4de57752
commit fb65dfeb8d
7 changed files with 95 additions and 1 deletions

View File

@@ -78,6 +78,12 @@
<!-- Use legacy wifi p2p dedicated address instead of randomize address. --> <!-- Use legacy wifi p2p dedicated address instead of randomize address. -->
<bool translatable="false" name="config_tether_enable_legacy_wifi_p2p_dedicated_ip">false</bool> <bool translatable="false" name="config_tether_enable_legacy_wifi_p2p_dedicated_ip">false</bool>
<!-- Use lease subnet prefix length to reserve the range outside of subnet prefix length.
This configuration only valid if its value larger than dhcp server address prefix length
and config_tether_enable_legacy_wifi_p2p_dedicated_ip is true.
-->
<integer translatable="false" name="config_p2p_leases_subnet_prefix_length">0</integer>
<!-- Dhcp range (min, max) to use for tethering purposes --> <!-- Dhcp range (min, max) to use for tethering purposes -->
<string-array translatable="false" name="config_tether_dhcp_range"> <string-array translatable="false" name="config_tether_dhcp_range">
</string-array> </string-array>

View File

@@ -32,6 +32,7 @@
<item type="bool" name="config_tether_enable_bpf_offload"/> <item type="bool" name="config_tether_enable_bpf_offload"/>
<item type="bool" name="config_tether_enable_legacy_dhcp_server"/> <item type="bool" name="config_tether_enable_legacy_dhcp_server"/>
<item type="bool" name="config_tether_enable_legacy_wifi_p2p_dedicated_ip"/> <item type="bool" name="config_tether_enable_legacy_wifi_p2p_dedicated_ip"/>
<item type="integer" name="config_p2p_leases_subnet_prefix_length"/>
<item type="integer" name="config_tether_offload_poll_interval"/> <item type="integer" name="config_tether_offload_poll_interval"/>
<item type="array" name="config_tether_upstream_types"/> <item type="array" name="config_tether_upstream_types"/>
<item type="bool" name="config_tether_upstream_automatic"/> <item type="bool" name="config_tether_upstream_automatic"/>

View File

@@ -185,6 +185,16 @@ public class DhcpServingParamsParcelExt extends DhcpServingParamsParcel {
return this; return this;
} }
/** Set leases subnet prefix length. If the value is smaller than server address prefix length,
* this configuration will be ignored.
*
* <p>If not set, the default value is zero.
*/
public DhcpServingParamsParcelExt setLeasesSubnetPrefixLength(int prefixLength) {
this.leasesSubnetPrefixLength = prefixLength;
return this;
}
private static int[] toIntArray(@NonNull Collection<Inet4Address> addrs) { private static int[] toIntArray(@NonNull Collection<Inet4Address> addrs) {
int[] res = new int[addrs.size()]; int[] res = new int[addrs.size()];
int i = 0; int i = 0;

View File

@@ -241,6 +241,7 @@ public class IpServer extends StateMachine {
private final LinkProperties mLinkProperties; private final LinkProperties mLinkProperties;
private final boolean mUsingLegacyDhcp; private final boolean mUsingLegacyDhcp;
private final boolean mUsingBpfOffload; private final boolean mUsingBpfOffload;
private final int mP2pLeasesSubnetPrefixLength;
private final Dependencies mDeps; private final Dependencies mDeps;
@@ -299,6 +300,7 @@ public class IpServer extends StateMachine {
mLinkProperties = new LinkProperties(); mLinkProperties = new LinkProperties();
mUsingLegacyDhcp = config.useLegacyDhcpServer(); mUsingLegacyDhcp = config.useLegacyDhcpServer();
mUsingBpfOffload = config.isBpfOffloadEnabled(); mUsingBpfOffload = config.isBpfOffloadEnabled();
mP2pLeasesSubnetPrefixLength = config.getP2pLeasesSubnetPrefixLength();
mPrivateAddressCoordinator = addressCoordinator; mPrivateAddressCoordinator = addressCoordinator;
mDeps = deps; mDeps = deps;
resetLinkProperties(); resetLinkProperties();
@@ -527,6 +529,9 @@ public class IpServer extends StateMachine {
@Nullable Inet4Address clientAddr) { @Nullable Inet4Address clientAddr) {
final boolean changePrefixOnDecline = final boolean changePrefixOnDecline =
(mInterfaceType == TetheringManager.TETHERING_NCM && clientAddr == null); (mInterfaceType == TetheringManager.TETHERING_NCM && clientAddr == null);
final int subnetPrefixLength = mInterfaceType == TetheringManager.TETHERING_WIFI_P2P
? mP2pLeasesSubnetPrefixLength : 0 /* default value */;
return new DhcpServingParamsParcelExt() return new DhcpServingParamsParcelExt()
.setDefaultRouters(defaultRouter) .setDefaultRouters(defaultRouter)
.setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS) .setDhcpLeaseTimeSecs(DHCP_LEASE_TIME_SECS)
@@ -534,7 +539,8 @@ public class IpServer extends StateMachine {
.setServerAddr(serverAddr) .setServerAddr(serverAddr)
.setMetered(true) .setMetered(true)
.setSingleClientAddr(clientAddr) .setSingleClientAddr(clientAddr)
.setChangePrefixOnDecline(changePrefixOnDecline); .setChangePrefixOnDecline(changePrefixOnDecline)
.setLeasesSubnetPrefixLength(subnetPrefixLength);
// TODO: also advertise link MTU // TODO: also advertise link MTU
} }

View File

@@ -149,6 +149,7 @@ public class TetheringConfiguration {
// TODO: Add to TetheringConfigurationParcel if required. // TODO: Add to TetheringConfigurationParcel if required.
private final boolean mEnableBpfOffload; private final boolean mEnableBpfOffload;
private final boolean mEnableWifiP2pDedicatedIp; private final boolean mEnableWifiP2pDedicatedIp;
private final int mP2pLeasesSubnetPrefixLength;
private final int mUsbTetheringFunction; private final int mUsbTetheringFunction;
protected final ContentResolver mContentResolver; protected final ContentResolver mContentResolver;
@@ -214,9 +215,27 @@ public class TetheringConfiguration {
R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip, R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip,
false /* defaultValue */); false /* defaultValue */);
mP2pLeasesSubnetPrefixLength = getP2pLeasesSubnetPrefixLengthFromRes(res, configLog);
configLog.log(toString()); configLog.log(toString());
} }
private int getP2pLeasesSubnetPrefixLengthFromRes(final Resources res, final SharedLog log) {
if (!mEnableWifiP2pDedicatedIp) return 0;
int prefixLength = getResourceInteger(res,
R.integer.config_p2p_leases_subnet_prefix_length, 0 /* default value */);
// DhcpLeaseRepository ignores the first and last addresses of the range so the max prefix
// length is 30.
if (prefixLength < 0 || prefixLength > 30) {
log.e("Invalid p2p leases subnet prefix length configuration: " + prefixLength);
return 0;
}
return prefixLength;
}
/** Check whether using legacy dhcp server. */ /** Check whether using legacy dhcp server. */
public boolean useLegacyDhcpServer() { public boolean useLegacyDhcpServer() {
return mEnableLegacyDhcpServer; return mEnableLegacyDhcpServer;
@@ -272,6 +291,15 @@ public class TetheringConfiguration {
return mEnableWifiP2pDedicatedIp; return mEnableWifiP2pDedicatedIp;
} }
/**
* Get subnet prefix length of dhcp leases for wifi p2p.
* This feature only support when wifi p2p use dedicated address. If
* #shouldEnableWifiP2pDedicatedIp is false, this method would always return 0.
*/
public int getP2pLeasesSubnetPrefixLength() {
return mP2pLeasesSubnetPrefixLength;
}
/** Does the dumping.*/ /** Does the dumping.*/
public void dump(PrintWriter pw) { public void dump(PrintWriter pw) {
pw.print("activeDataSubId: "); pw.print("activeDataSubId: ");
@@ -310,6 +338,9 @@ public class TetheringConfiguration {
pw.print("enableWifiP2pDedicatedIp: "); pw.print("enableWifiP2pDedicatedIp: ");
pw.println(mEnableWifiP2pDedicatedIp); pw.println(mEnableWifiP2pDedicatedIp);
pw.print("p2pLeasesSubnetPrefixLength: ");
pw.println(mP2pLeasesSubnetPrefixLength);
pw.print("mUsbTetheringFunction: "); pw.print("mUsbTetheringFunction: ");
pw.println(isUsingNcm() ? "NCM" : "RNDIS"); pw.println(isUsingNcm() ? "NCM" : "RNDIS");
} }

View File

@@ -156,6 +156,8 @@ public class IpServerTest {
private static final int BLUETOOTH_DHCP_PREFIX_LENGTH = 24; private static final int BLUETOOTH_DHCP_PREFIX_LENGTH = 24;
private static final int DHCP_LEASE_TIME_SECS = 3600; private static final int DHCP_LEASE_TIME_SECS = 3600;
private static final boolean DEFAULT_USING_BPF_OFFLOAD = true; private static final boolean DEFAULT_USING_BPF_OFFLOAD = true;
private static final int DEFAULT_SUBNET_PREFIX_LENGTH = 0;
private static final int P2P_SUBNET_PREFIX_LENGTH = 25;
private static final InterfaceParams TEST_IFACE_PARAMS = new InterfaceParams( private static final InterfaceParams TEST_IFACE_PARAMS = new InterfaceParams(
IFACE_NAME, 42 /* index */, MacAddress.ALL_ZEROS_ADDRESS, 1500 /* defaultMtu */); IFACE_NAME, 42 /* index */, MacAddress.ALL_ZEROS_ADDRESS, 1500 /* defaultMtu */);
@@ -230,6 +232,7 @@ public class IpServerTest {
when(mTetherConfig.isBpfOffloadEnabled()).thenReturn(usingBpfOffload); when(mTetherConfig.isBpfOffloadEnabled()).thenReturn(usingBpfOffload);
when(mTetherConfig.useLegacyDhcpServer()).thenReturn(usingLegacyDhcp); when(mTetherConfig.useLegacyDhcpServer()).thenReturn(usingLegacyDhcp);
when(mTetherConfig.getP2pLeasesSubnetPrefixLength()).thenReturn(P2P_SUBNET_PREFIX_LENGTH);
mIpServer = new IpServer( mIpServer = new IpServer(
IFACE_NAME, mLooper.getLooper(), interfaceType, mSharedLog, mNetd, mBpfCoordinator, IFACE_NAME, mLooper.getLooper(), interfaceType, mSharedLog, mNetd, mBpfCoordinator,
mCallback, mTetherConfig, mAddressCoordinator, mDependencies); mCallback, mTetherConfig, mAddressCoordinator, mDependencies);
@@ -1312,6 +1315,12 @@ public class IpServerTest {
if (mIpServer.interfaceType() == TETHERING_NCM) { if (mIpServer.interfaceType() == TETHERING_NCM) {
assertTrue(params.changePrefixOnDecline); assertTrue(params.changePrefixOnDecline);
} }
if (mIpServer.interfaceType() == TETHERING_WIFI_P2P) {
assertEquals(P2P_SUBNET_PREFIX_LENGTH, params.leasesSubnetPrefixLength);
} else {
assertEquals(DEFAULT_SUBNET_PREFIX_LENGTH, params.leasesSubnetPrefixLength);
}
} }
private void assertDhcpStarted(IpPrefix expectedPrefix) throws Exception { private void assertDhcpStarted(IpPrefix expectedPrefix) throws Exception {

View File

@@ -616,4 +616,35 @@ public class TetheringConfigurationTest {
assertArrayEquals(ncmRegexs, cfg.tetherableNcmRegexs); assertArrayEquals(ncmRegexs, cfg.tetherableNcmRegexs);
} }
@Test
public void testP2pLeasesSubnetPrefixLength() throws Exception {
when(mResources.getBoolean(R.bool.config_tether_enable_legacy_wifi_p2p_dedicated_ip))
.thenReturn(true);
final int defaultSubnetPrefixLength = 0;
final TetheringConfiguration defaultCfg =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
assertEquals(defaultSubnetPrefixLength, defaultCfg.getP2pLeasesSubnetPrefixLength());
final int prefixLengthTooSmall = -1;
when(mResources.getInteger(R.integer.config_p2p_leases_subnet_prefix_length)).thenReturn(
prefixLengthTooSmall);
final TetheringConfiguration tooSmallCfg =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
assertEquals(defaultSubnetPrefixLength, tooSmallCfg.getP2pLeasesSubnetPrefixLength());
final int prefixLengthTooLarge = 31;
when(mResources.getInteger(R.integer.config_p2p_leases_subnet_prefix_length)).thenReturn(
prefixLengthTooLarge);
final TetheringConfiguration tooLargeCfg =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
assertEquals(defaultSubnetPrefixLength, tooLargeCfg.getP2pLeasesSubnetPrefixLength());
final int p2pLeasesSubnetPrefixLength = 27;
when(mResources.getInteger(R.integer.config_p2p_leases_subnet_prefix_length)).thenReturn(
p2pLeasesSubnetPrefixLength);
final TetheringConfiguration p2pCfg =
new TetheringConfiguration(mMockContext, mLog, INVALID_SUBSCRIPTION_ID);
assertEquals(p2pLeasesSubnetPrefixLength, p2pCfg.getP2pLeasesSubnetPrefixLength());
}
} }