diff options
author | lesl <lesl@google.com> | 2020-03-24 21:31:27 +0800 |
---|---|---|
committer | lesl <lesl@google.com> | 2020-03-25 23:50:06 +0800 |
commit | a9d77c4c1ff8f730d0d188934721b061feb310aa (patch) | |
tree | 96ab0bdd478c492557a5c48336420a4e527a1d92 | |
parent | 70f3e3846b6544788a56dcb62eee807e4e6dd77b (diff) |
wifi: Support setBssid in tethered mode hotspot
Also fix: Remove BSSID field when convert to SoftApConfiguration since BSSID
field doesn't use for softap in WifiConfiguration.
PS: Merged to master first to avoid conflict.
Bug: 152180102
Test: atest FrameworksWifiTest
Test: atest android.net.wifi.cts.WifiManagerTest#testStartLocalOnlyHotspotSingleRequestByApps
Test: atest android.net.wifi.cts.WifiManagerTest#testStartLocalOnlyHotspotSuccess
Test: atest atest android.net.wifi.cts.WifiManagerTest#testStartLocalOnlyHotspotWithConfig
Test: atest android.net.wifi.cts.WifiManagerTest#testSetGetSoftApConfigurationAndSoftApCapabilityCallback
Change-Id: I7955b84ef025bf8cb97b55eaac6a8fecda551750
Merged-In: I7955b84ef025bf8cb97b55eaac6a8fecda551750
4 files changed, 51 insertions, 28 deletions
diff --git a/service/java/com/android/server/wifi/WifiApConfigStore.java b/service/java/com/android/server/wifi/WifiApConfigStore.java index b8d1d3b41..9b2cc670c 100644 --- a/service/java/com/android/server/wifi/WifiApConfigStore.java +++ b/service/java/com/android/server/wifi/WifiApConfigStore.java @@ -194,12 +194,6 @@ public class WifiApConfigStore { private SoftApConfiguration sanitizePersistentApConfig(SoftApConfiguration config) { SoftApConfiguration.Builder convertedConfigBuilder = null; - // Persistent config may not set BSSID. - if (config.getBssid() != null) { - convertedConfigBuilder = new SoftApConfiguration.Builder(config); - convertedConfigBuilder.setBssid(null); - } - // some countries are unable to support 5GHz only operation, always allow for 2GHz when // config doesn't force channel if (config.getChannel() == 0 && (config.getBand() & SoftApConfiguration.BAND_2GHZ) == 0) { @@ -357,16 +351,24 @@ public class WifiApConfigStore { * requires a password, was one provided?). * * @param apConfig {@link SoftApConfiguration} to use for softap mode + * @param isPrivileged indicate the caller can pass some fields check or not * @return boolean true if the provided config meets the minimum set of details, false * otherwise. */ - static boolean validateApWifiConfiguration(@NonNull SoftApConfiguration apConfig) { + static boolean validateApWifiConfiguration(@NonNull SoftApConfiguration apConfig, + boolean isPrivileged) { // first check the SSID if (!validateApConfigSsid(apConfig.getSsid())) { // failed SSID verificiation checks return false; } + // BSSID can be set if caller own permission:android.Manifest.permission.NETWORK_SETTINGS. + if (apConfig.getBssid() != null && !isPrivileged) { + Log.e(TAG, "Config BSSID needs NETWORK_SETTINGS permission"); + return false; + } + String preSharedKey = apConfig.getPassphrase(); boolean hasPreSharedKey = !TextUtils.isEmpty(preSharedKey); int authType; diff --git a/service/java/com/android/server/wifi/WifiServiceImpl.java b/service/java/com/android/server/wifi/WifiServiceImpl.java index c6ce36c43..2e9372874 100644 --- a/service/java/com/android/server/wifi/WifiServiceImpl.java +++ b/service/java/com/android/server/wifi/WifiServiceImpl.java @@ -953,14 +953,16 @@ public class WifiServiceImpl extends BaseWifiService { * proper permissions beyond the NetworkStack permission. */ private boolean startSoftApInternal(SoftApModeConfiguration apConfig) { + int uid = Binder.getCallingUid(); + boolean privileged = isSettingsOrSuw(Binder.getCallingPid(), uid); mLog.trace("startSoftApInternal uid=% mode=%") - .c(Binder.getCallingUid()).c(apConfig.getTargetMode()).flush(); + .c(uid).c(apConfig.getTargetMode()).flush(); // null wifiConfig is a meaningful input for CMD_SET_AP; it means to use the persistent // AP config. SoftApConfiguration softApConfig = apConfig.getSoftApConfiguration(); if (softApConfig != null - && (!WifiApConfigStore.validateApWifiConfiguration(softApConfig) + && (!WifiApConfigStore.validateApWifiConfiguration(softApConfig, privileged) || !validateSoftApBand(softApConfig.getBand()))) { Log.e(TAG, "Invalid SoftApConfiguration"); return false; @@ -1876,7 +1878,7 @@ public class WifiServiceImpl extends BaseWifiService { SoftApConfiguration softApConfig = ApConfigUtil.fromWifiConfiguration(wifiConfig); if (softApConfig == null) return false; if (WifiApConfigStore.validateApWifiConfiguration( - softApConfig)) { + softApConfig, false)) { mWifiThreadRunner.post(() -> mWifiApConfigStore.setApConfiguration(softApConfig)); return true; } else { @@ -1896,9 +1898,10 @@ public class WifiServiceImpl extends BaseWifiService { @NonNull SoftApConfiguration softApConfig, @NonNull String packageName) { enforceNetworkSettingsPermission(); int uid = Binder.getCallingUid(); + boolean privileged = mWifiPermissionsUtil.checkNetworkSettingsPermission(uid); mLog.info("setSoftApConfiguration uid=%").c(uid).flush(); if (softApConfig == null) return false; - if (WifiApConfigStore.validateApWifiConfiguration(softApConfig)) { + if (WifiApConfigStore.validateApWifiConfiguration(softApConfig, privileged)) { mActiveModeWarden.updateSoftApConfiguration(softApConfig); mWifiThreadRunner.post(() -> mWifiApConfigStore.setApConfiguration(softApConfig)); return true; diff --git a/service/java/com/android/server/wifi/util/ApConfigUtil.java b/service/java/com/android/server/wifi/util/ApConfigUtil.java index d385e3edc..cc286fad7 100644 --- a/service/java/com/android/server/wifi/util/ApConfigUtil.java +++ b/service/java/com/android/server/wifi/util/ApConfigUtil.java @@ -20,7 +20,6 @@ import android.annotation.NonNull; import android.annotation.Nullable; import android.content.Context; import android.content.res.Resources; -import android.net.MacAddress; import android.net.wifi.SoftApCapability; import android.net.wifi.SoftApConfiguration; import android.net.wifi.SoftApConfiguration.BandType; @@ -423,9 +422,6 @@ public class ApConfigUtil { SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); try { configBuilder.setSsid(wifiConfig.SSID); - if (wifiConfig.BSSID != null) { - configBuilder.setBssid(MacAddress.fromString(wifiConfig.BSSID)); - } if (wifiConfig.getAuthType() == WifiConfiguration.KeyMgmt.WPA2_PSK) { configBuilder.setPassphrase(wifiConfig.preSharedKey, SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java index 4aacb1d78..ad8acbcea 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiApConfigStoreTest.java @@ -435,7 +435,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { public void getDefaultApConfigurationIsValid() throws Exception { WifiApConfigStore store = createWifiApConfigStore(); SoftApConfiguration config = store.getApConfiguration(); - assertTrue(WifiApConfigStore.validateApWifiConfiguration(config)); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(config, true)); } /** @@ -450,7 +450,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { SoftApConfiguration.BAND_2GHZ); // verify that the config passes the validateApWifiConfiguration check - assertTrue(WifiApConfigStore.validateApWifiConfiguration(config)); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(config, true)); } /** @@ -464,7 +464,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { SoftApConfiguration.BAND_5GHZ); // verify that the config passes the validateApWifiConfiguration check - assertTrue(WifiApConfigStore.validateApWifiConfiguration(config)); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(config, true)); } @Test @@ -549,30 +549,30 @@ public class WifiApConfigStoreTest extends WifiBaseTest { public void testSsidVerificationInValidateApWifiConfigurationCheck() { Builder configBuilder = new SoftApConfiguration.Builder(); configBuilder.setSsid(null); - assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // check a string if it's larger than 32 bytes with UTF-8 encode // Case 1 : one byte per character (use english words and Arabic numerals) configBuilder.setSsid(generateRandomString(WifiApConfigStore.SSID_MAX_LEN + 1)); - assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // Case 2 : two bytes per character configBuilder.setSsid(TEST_STRING_UTF8_WITH_34_BYTES); - assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // Case 3 : three bytes per character configBuilder.setSsid(TEST_STRING_UTF8_WITH_33_BYTES); - assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertFalse(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // now check a valid SSID within 32 bytes // Case 1 : one byte per character with random length int validLength = WifiApConfigStore.SSID_MAX_LEN - WifiApConfigStore.SSID_MIN_LEN; configBuilder.setSsid(generateRandomString( mRandom.nextInt(validLength) + WifiApConfigStore.SSID_MIN_LEN)); - assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // Case 2 : two bytes per character configBuilder.setSsid(TEST_STRING_UTF8_WITH_32_BYTES); - assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); // Case 3 : three bytes per character configBuilder.setSsid(TEST_STRING_UTF8_WITH_30_BYTES); - assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); } /** @@ -587,7 +587,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { assertTrue(WifiApConfigStore.validateApWifiConfiguration( new SoftApConfiguration.Builder() .setSsid(TEST_DEFAULT_HOTSPOT_SSID) - .build())); + .build(), true)); } /** @@ -608,7 +608,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { configBuilder.setPassphrase( generateRandomString(mRandom.nextInt(maxLen - minLen) + minLen), SoftApConfiguration.SECURITY_TYPE_WPA2_PSK); - assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build())); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(configBuilder.build(), true)); } /** @@ -634,7 +634,7 @@ public class WifiApConfigStoreTest extends WifiBaseTest { SoftApConfiguration.BAND_5GHZ, true); // verify that the config passes the validateApWifiConfiguration check - assertTrue(WifiApConfigStore.validateApWifiConfiguration(config)); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(config, true)); } @@ -653,4 +653,26 @@ public class WifiApConfigStoreTest extends WifiBaseTest { assertEquals(resetedConfig.getMaxNumberOfClients(), 0); assertFalse(resetedConfig.isClientControlByUserEnabled()); } + + /** + * Verify Bssid field deny to set if caller without setting permission. + */ + @Test + public void testBssidDenyIfCallerWithoutPrivileged() throws Exception { + WifiApConfigStore store = createWifiApConfigStore(); + SoftApConfiguration config = new SoftApConfiguration.Builder(store.getApConfiguration()) + .setBssid(MacAddress.fromString("aa:bb:cc:dd:ee:ff")).build(); + assertFalse(WifiApConfigStore.validateApWifiConfiguration(config, false)); + } + + /** + * Verify Bssid field only allow to set if caller own setting permission. + */ + @Test + public void testBssidAllowIfCallerOwnPrivileged() throws Exception { + WifiApConfigStore store = createWifiApConfigStore(); + SoftApConfiguration config = new SoftApConfiguration.Builder(store.getApConfiguration()) + .setBssid(MacAddress.fromString("aa:bb:cc:dd:ee:ff")).build(); + assertTrue(WifiApConfigStore.validateApWifiConfiguration(config, true)); + } } |