qcacld-3.0: Acquire mutex before accessing tdls context

qcacld-2.0 to qcacld-3.0 propagation

peer_list is a parameter for tdls_ctx, so every access to
peer_list should be protected with mutex lock.
This change refactors the code for wlan_hdd_tdls_get_peer(),
wlan_hdd_tdls_set_cap(), wlan_hdd_tdls_recv_discovery_resp(),
wlan_hdd_tdls_set_peer_caps(), wlan_hdd_tdls_get_link_estab
lish_params(), wlan_hdd_tdls_set_responder() and
wlan_hdd_tdls_set_signature.

Change-Id: I4589eea7f5f97d0e9887e008921a89af05bce329
CRs-Fixed: 1108972
diff --git a/core/hdd/src/wlan_hdd_tdls.c b/core/hdd/src/wlan_hdd_tdls.c
index feb6fd9..c7592a7 100644
--- a/core/hdd/src/wlan_hdd_tdls.c
+++ b/core/hdd/src/wlan_hdd_tdls.c
@@ -985,22 +985,26 @@
 	if (0 != (wlan_hdd_validate_context(pHddCtx)))
 		return NULL;
 
+	if (need_mutex_lock)
+		mutex_lock(&pHddCtx->tdls_lock);
+
 	/* if already there, just update */
-	peer = wlan_hdd_tdls_find_peer(pAdapter, mac, need_mutex_lock);
+	peer = wlan_hdd_tdls_find_peer(pAdapter, mac, false);
 	if (peer != NULL) {
+		if (need_mutex_lock)
+			mutex_unlock(&pHddCtx->tdls_lock);
 		return peer;
 	}
 
 	/* not found, allocate and add the list */
 	peer = qdf_mem_malloc(sizeof(hddTdlsPeer_t));
 	if (NULL == peer) {
+		if (need_mutex_lock)
+			mutex_unlock(&pHddCtx->tdls_lock);
 		hdd_err("peer malloc failed!");
 		return NULL;
 	}
 
-	if (need_mutex_lock)
-		mutex_lock(&pHddCtx->tdls_lock);
-
 	pHddTdlsCtx = WLAN_HDD_GET_TDLS_CTX_PTR(pAdapter);
 
 	if (NULL == pHddTdlsCtx) {
@@ -1041,16 +1045,28 @@
 			  tTDLSCapType cap)
 {
 	hddTdlsPeer_t *curr_peer;
+	hdd_context_t *hdd_ctx;
+	int status = 0;
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
+	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
+		status = -EINVAL;
+		goto ret_status;
+	}
+
+	mutex_lock(&hdd_ctx->tdls_lock);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (curr_peer == NULL) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	curr_peer->tdls_support = cap;
-
-	return 0;
+rel_lock:
+	mutex_unlock(&hdd_ctx->tdls_lock);
+ret_status:
+	return status;
 }
 
 /**
@@ -1188,34 +1204,40 @@
 				      const uint8_t *mac)
 {
 	hddTdlsPeer_t *curr_peer;
-	tdlsCtx_t *pHddTdlsCtx = WLAN_HDD_GET_TDLS_CTX_PTR(pAdapter);
+	tdlsCtx_t *pHddTdlsCtx;
 	hdd_context_t *pHddCtx;
-
+	int status = 0;
 	ENTER();
 
-	if (NULL == pHddTdlsCtx) {
-		hdd_err("pHddTdlsCtx is NULL");
-		return -EINVAL;
+
+	pHddCtx = WLAN_HDD_GET_CTX(pAdapter);
+
+	if (0 != (wlan_hdd_validate_context(pHddCtx))) {
+		status = -EINVAL;
+		goto ret_status;
 	}
 
-	pHddCtx = WLAN_HDD_GET_CTX(pHddTdlsCtx->pAdapter);
+	mutex_lock(&pHddCtx->tdls_lock);
 
-	if (0 != (wlan_hdd_validate_context(pHddCtx)))
-		return -EINVAL;
+	pHddTdlsCtx = WLAN_HDD_GET_TDLS_CTX_PTR(pAdapter);
+	if (NULL == pHddTdlsCtx) {
+		hdd_err("pHddTdlsCtx is NULL");
+		status = -EINVAL;
+		goto rel_lock;
+	}
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (NULL == curr_peer) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	if (pHddTdlsCtx->discovery_sent_cnt)
 		pHddTdlsCtx->discovery_sent_cnt--;
 
-	mutex_lock(&pHddCtx->tdls_lock);
 	wlan_hdd_tdls_check_power_save_prohibited(pAdapter);
 
-	mutex_unlock(&pHddCtx->tdls_lock);
 	if (0 == pHddTdlsCtx->discovery_sent_cnt) {
 		qdf_mc_timer_stop(&pHddTdlsCtx->peerDiscoveryTimeoutTimer);
 	}
@@ -1235,7 +1257,7 @@
 			wlan_hdd_tdls_set_peer_link_status(curr_peer,
 							   eTDLS_LINK_DISCOVERED,
 							   eTDLS_LINK_SUCCESS,
-							   true);
+							   false);
 			hdd_notice("Rssi Threshold met: " MAC_ADDRESS_STR
 				   " rssi = %d threshold= %d",
 				   MAC_ADDR_ARRAY(curr_peer->peerMac),
@@ -1256,7 +1278,7 @@
 			wlan_hdd_tdls_set_peer_link_status(curr_peer,
 							   eTDLS_LINK_IDLE,
 							   eTDLS_LINK_UNSPECIFIED,
-							   true);
+							   false);
 
 			/* if RSSI threshold is not met then allow further discovery
 			 * attempts by decrementing count for the last attempt
@@ -1267,8 +1289,11 @@
 	}
 
 	curr_peer->tdls_support = eTDLS_CAP_SUPPORTED;
+rel_lock:
+	mutex_unlock(&pHddCtx->tdls_lock);
+ret_status:
 	EXIT();
-	return 0;
+	return status;
 }
 
 /**
@@ -1289,11 +1314,20 @@
 				bool is_qos_wmm_sta)
 {
 	hddTdlsPeer_t *curr_peer;
+	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
+	int status = 0;
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
+		status = -EINVAL;
+		goto ret_status;
+	}
+
+	mutex_lock(&hdd_ctx->tdls_lock);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (curr_peer == NULL) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	curr_peer->uapsdQueues = StaParams->uapsd_queues;
@@ -1314,7 +1348,10 @@
 	curr_peer->supported_oper_classes_len =
 		StaParams->supported_oper_classes_len;
 	curr_peer->qos = is_qos_wmm_sta;
-	return 0;
+rel_lock:
+	mutex_unlock(&hdd_ctx->tdls_lock);
+ret_status:
+	return status;
 }
 
 /**
@@ -1332,11 +1369,20 @@
 					    tdlsLinkEstablishParams)
 {
 	hddTdlsPeer_t *curr_peer;
+	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
+	int status = 0;
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
+		status = -EINVAL;
+		goto ret_status;
+	}
+
+	mutex_lock(&hdd_ctx->tdls_lock);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (curr_peer == NULL) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	tdlsLinkEstablishParams->isResponder = curr_peer->is_responder;
@@ -1360,7 +1406,10 @@
 	tdlsLinkEstablishParams->supportedOperClassesLen =
 		curr_peer->supported_oper_classes_len;
 	tdlsLinkEstablishParams->qos = curr_peer->qos;
-	return 0;
+rel_lock:
+	mutex_unlock(&hdd_ctx->tdls_lock);
+ret_status:
+	return status;
 }
 
 /**
@@ -1408,16 +1457,27 @@
 				uint8_t responder)
 {
 	hddTdlsPeer_t *curr_peer;
+	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
+	int status = 0;
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
+		status = -EINVAL;
+		goto ret_status;
+	}
+	mutex_lock(&hdd_ctx->tdls_lock);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (curr_peer == NULL) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	curr_peer->is_responder = responder;
 
-	return 0;
+rel_lock:
+	mutex_unlock(&hdd_ctx->tdls_lock);
+ret_status:
+	return status;
 }
 
 /**
@@ -1432,16 +1492,27 @@
 				uint8_t uSignature)
 {
 	hddTdlsPeer_t *curr_peer;
+	hdd_context_t *hdd_ctx = WLAN_HDD_GET_CTX(pAdapter);
+	int status = 0;
 
-	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, true);
+	if (0 != (wlan_hdd_validate_context(hdd_ctx))) {
+		status = -EINVAL;
+		goto ret_status;
+	}
+
+	mutex_lock(&hdd_ctx->tdls_lock);
+	curr_peer = wlan_hdd_tdls_get_peer(pAdapter, mac, false);
 	if (curr_peer == NULL) {
 		hdd_err("curr_peer is NULL");
-		return -EINVAL;
+		status = -EINVAL;
+		goto rel_lock;
 	}
 
 	curr_peer->signature = uSignature;
-
-	return 0;
+rel_lock:
+	mutex_unlock(&hdd_ctx->tdls_lock);
+ret_status:
+	return status;
 }
 
 /**