wlan: hdd: Add host scan mac-spoofing support

Extend the functionality of spoofing of mac addr
in host initiated scans. Both FW initiated
scans and host initiated scans will be spoofed.

- Check for MAC_SPOOFED_SCAN feature capability flag
- provided spoof mac addr is stored in pHddCtx
- Spoofing request will be deferred till scan is completed
- new spoofed mac addr is updated with FW after scan completion
- 00:00:00 mac addr OUI provided to disable spoofing

Change-Id: I85fad66779f2e4eed0303b938386bd4b90caeda8
CRs-Fixed: 737889
diff --git a/CORE/HDD/src/wlan_hdd_cfg80211.c b/CORE/HDD/src/wlan_hdd_cfg80211.c
index 76403fa..90de832 100644
--- a/CORE/HDD/src/wlan_hdd_cfg80211.c
+++ b/CORE/HDD/src/wlan_hdd_cfg80211.c
@@ -4011,7 +4011,7 @@
     if (FALSE == pHddCtx->cfg_ini->enableMacSpoofing) {
         hddLog(VOS_TRACE_LEVEL_ERROR, FL("MAC_SPOOFED_SCAN disabled in ini"));
         return -ENOTSUPP;
-     }
+    }
     if (TRUE != sme_IsFeatureSupportedByFW(MAC_SPOOFED_SCAN)){
         hddLog(VOS_TRACE_LEVEL_ERROR, FL("MAC_SPOOFED_SCAN not supported by FW"));
         return -ENOTSUPP;
@@ -4033,21 +4033,23 @@
                 tb[QCA_WLAN_VENDOR_ATTR_SET_SCANNING_MAC_OUI]),
             VOS_MAC_ADDR_LAST_3_BYTES);
 
-    vos_trace_hex_dump( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,nla_data(
+    pHddCtx->spoofMacAddr.isEnabled = TRUE;
+
+    vos_trace_hex_dump( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO, nla_data(
             tb[QCA_WLAN_VENDOR_ATTR_SET_SCANNING_MAC_OUI]),
             VOS_MAC_ADDR_FIRST_3_BYTES);
-
-    if (VOS_STATUS_SUCCESS != vos_randomize_n_bytes(
-        (void *)(&pHddCtx->spoofMacAddr.randomMacAddr.bytes[3]),
-        VOS_MAC_ADDR_LAST_3_BYTES)) {
-        hddLog(LOGE, FL("Failed to generate random Mac Addr"));
+    if ((pHddCtx->spoofMacAddr.randomMacAddr.bytes[0] == 0) &&
+        (pHddCtx->spoofMacAddr.randomMacAddr.bytes[1] == 0) &&
+        (pHddCtx->spoofMacAddr.randomMacAddr.bytes[2] == 0))
+    {
+            hddLog(LOG1, FL("ZERO MAC OUI Recieved. Disabling Spoofing"));
+            vos_mem_zero(pHddCtx->spoofMacAddr.randomMacAddr.bytes,
+                                                        VOS_MAC_ADDRESS_LEN);
+            pHddCtx->spoofMacAddr.isEnabled = FALSE;
     }
-    vos_trace_hex_dump( VOS_MODULE_ID_HDD, VOS_TRACE_LEVEL_INFO,
-        pHddCtx->spoofMacAddr.randomMacAddr.bytes,
-            VOS_MAC_ADDR_SIZE);
 
-    if (eHAL_STATUS_SUCCESS != sme_SpoofMacAddrReq(pHddCtx->hHal,
-                &pHddCtx->spoofMacAddr.randomMacAddr)) {
+    if (VOS_STATUS_SUCCESS != hdd_processSpoofMacAddrRequest(pHddCtx))
+    {
         hddLog(LOGE, FL("Failed to send Spoof Mac Addr to FW"));
     }
 
@@ -9335,6 +9337,12 @@
     cfg80211_scan_done(req, aborted);
     complete(&pScanInfo->abortscan_event_var);
 
+    if (pHddCtx->spoofMacAddr.isEnabled || pHddCtx->spoofMacAddr.isReqDeferred) {
+        /* Generate new random mac addr for next scan */
+        hddLog(VOS_TRACE_LEVEL_INFO, "scan completed - generate new spoof mac addr");
+        hdd_processSpoofMacAddrRequest(pHddCtx);
+    }
+
 allow_suspend:
     /* release the wake lock at the end of the scan*/
     hdd_allow_suspend();
@@ -9819,6 +9827,17 @@
            scanRequest.minChnTime, scanRequest.maxChnTime,
            scanRequest.p2pSearch, scanRequest.skipDfsChnlInP2pSearch);
 
+    if (pHddCtx->spoofMacAddr.isEnabled)
+    {
+        hddLog(VOS_TRACE_LEVEL_INFO,
+                        "%s: MAC Spoofing enabled for current scan", __func__);
+        /* Updating SelfSta Mac Addr in TL which will be used to get staidx
+         * to fill TxBds for probe request during current scan
+         */
+        WLANTL_updateSpoofMacAddr(pHddCtx->pvosContext,
+            &pHddCtx->spoofMacAddr.randomMacAddr, &pAdapter->macAddressCurrent);
+    }
+
     status = sme_ScanRequest( WLAN_HDD_GET_HAL_CTX(pAdapter),
                               pAdapter->sessionId, &scanRequest, &scanId,
                               &hdd_cfg80211_scan_done_callback, dev );
diff --git a/CORE/HDD/src/wlan_hdd_main.c b/CORE/HDD/src/wlan_hdd_main.c
index 6a8127c..5541c8a 100644
--- a/CORE/HDD/src/wlan_hdd_main.c
+++ b/CORE/HDD/src/wlan_hdd_main.c
@@ -8642,6 +8642,7 @@
     */
    mutex_init(&pHddCtx->tdls_lock);
 #endif
+   mutex_init(&pHddCtx->spoofMacAddr.macSpoofingLock);
    /* By default Strict Regulatory For FCC should be false */
 
    pHddCtx->nEnableStrictRegulatoryForFCC = FALSE;
diff --git a/CORE/HDD/src/wlan_hdd_scan.c b/CORE/HDD/src/wlan_hdd_scan.c
index 4366fef..ad57958 100644
--- a/CORE/HDD/src/wlan_hdd_scan.c
+++ b/CORE/HDD/src/wlan_hdd_scan.c
@@ -534,6 +534,64 @@
 
 /**---------------------------------------------------------------------------
 
+  \brief hdd_processSpoofMacAddrRequest() -
+
+   The function is called from scan completion callback and from
+   cfg80211 vendor command
+
+  \param  - pHddCtx - Pointer to the HDD Context.
+
+  \return - 0 for success, non zero for failure
+
+  --------------------------------------------------------------------------*/
+
+VOS_STATUS hdd_processSpoofMacAddrRequest(hdd_context_t *pHddCtx)
+{
+
+    ENTER();
+
+    mutex_lock(&pHddCtx->spoofMacAddr.macSpoofingLock);
+
+    if (pHddCtx->spoofMacAddr.isEnabled) {
+        if (VOS_STATUS_SUCCESS != vos_randomize_n_bytes(
+                (void *)(&pHddCtx->spoofMacAddr.randomMacAddr.bytes[3]),
+                VOS_MAC_ADDR_LAST_3_BYTES)) {
+                hddLog(LOGE, FL("Failed to generate random Mac Addr"));
+                pHddCtx->spoofMacAddr.isEnabled = FALSE;
+                mutex_unlock(&pHddCtx->spoofMacAddr.macSpoofingLock);
+                return VOS_STATUS_E_FAILURE;
+        }
+    }
+
+    hddLog(LOG1, FL("New Mac Addr Generated "MAC_ADDRESS_STR),
+                 MAC_ADDR_ARRAY(pHddCtx->spoofMacAddr.randomMacAddr.bytes));
+
+    if (pHddCtx->scan_info.mScanPending != TRUE)
+    {
+        pHddCtx->spoofMacAddr.isReqDeferred = FALSE;
+        hddLog(LOG1, FL("Processing Spoof request now"));
+        /* Inform SME about spoof mac addr request*/
+        if ( eHAL_STATUS_SUCCESS != sme_SpoofMacAddrReq(pHddCtx->hHal,
+                &pHddCtx->spoofMacAddr.randomMacAddr))
+        {
+            hddLog(LOGE, FL("Sending Spoof request failed - Disable spoofing"));
+            pHddCtx->spoofMacAddr.isEnabled = FALSE;
+        }
+    } else
+    {
+        hddLog(LOG1, FL("Scan in Progress. Spoofing Deferred"));
+        pHddCtx->spoofMacAddr.isReqDeferred = TRUE;
+    }
+
+    mutex_unlock(&pHddCtx->spoofMacAddr.macSpoofingLock);
+
+    EXIT();
+
+    return VOS_STATUS_SUCCESS;
+}
+
+/**---------------------------------------------------------------------------
+
   \brief hdd_ScanRequestCallback() -
 
    The sme module calls this callback function once it finish the scan request