P2P fixes for BRCM

1. Fix for stopping any on-going P2P-FIND, while doing a P2P-GROUP-ADD.
2. Fix for Supplicant crash. The crash was due to socket being closed
   during P2P-GROUP-REMOVE.
3. Append P2P DEV ADDR during AP-STA-CONNECTED Event for P2P Devices.
   For legacy STA, the event format would remain the same.
BUG: b/5262575

Change-Id: I8faf2159d35538fd07e42dba82d367b581cf2164
Signed-off-by: Dmitry Shmidt <dimitrysh@google.com>
diff --git a/src/ap/ieee802_1x.c b/src/ap/ieee802_1x.c
index 8737455..49eba69 100644
--- a/src/ap/ieee802_1x.c
+++ b/src/ap/ieee802_1x.c
@@ -36,6 +36,9 @@
 #include "ap_config.h"
 #include "ap_drv_ops.h"
 #include "ieee802_1x.h"
+#ifdef ANDROID_BRCM_P2P_PATCH
+#include "p2p/p2p_i.h"
+#endif
 
 
 static void ieee802_1x_finished(struct hostapd_data *hapd,
@@ -84,21 +87,37 @@
 				   struct sta_info *sta, int authorized)
 {
 	int res;
+#ifdef ANDROID_BRCM_P2P_PATCH
+	u8 *dev_addr = NULL;
+#endif
 
 	if (sta->flags & WLAN_STA_PREAUTH)
 		return;
 
 	if (authorized) {
 		if (!ap_sta_is_authorized(sta)) {
-			wpa_msg(hapd->msg_ctx, MSG_INFO,
-				AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
+#if defined(ANDROID_BRCM_P2P_PATCH) && defined(CONFIG_P2P)
+			if((dev_addr = p2p_group_get_dev_addr(hapd->p2p_group, sta->addr)))
+				wpa_msg(hapd->msg_ctx, MSG_INFO,
+					AP_STA_CONNECTED MACSTR " dev_addr="MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
+			else
+#endif /*ANDROID_BRCM_P2P_PATCH*/
+				wpa_msg(hapd->msg_ctx, MSG_INFO,
+					AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
+
 #ifdef ANDROID_BRCM_P2P_PATCH
 			/* Sending the event to parent is required as SSL listens on parent ctrl iface */
-			if(hapd->msg_ctx_parent)
-				wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
-					AP_STA_CONNECTED MACSTR, MAC2STR(sta->addr));
+			if(hapd->msg_ctx_parent) {
+				if(dev_addr)
+					wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
+						AP_STA_CONNECTED MACSTR " dev_addr="MACSTR, MAC2STR(sta->addr), MAC2STR(dev_addr));
+				else
+					wpa_msg(hapd->msg_ctx_parent, MSG_INFO,
+						AP_STA_CONNECTED MACSTR , MAC2STR(sta->addr));
+			}
 #endif /* ANDROID_BRCM_P2P_PATCH */
 		}
+
 		ap_sta_set_authorized(hapd, sta, 1);
 		res = hostapd_set_authorized(hapd, sta, 1);
 		hostapd_logger(hapd, sta->addr, HOSTAPD_MODULE_IEEE8021X,
diff --git a/src/p2p/p2p.h b/src/p2p/p2p.h
index 81ebe9b..72c90b1 100644
--- a/src/p2p/p2p.h
+++ b/src/p2p/p2p.h
@@ -1244,6 +1244,16 @@
  */
 int p2p_group_notif_assoc(struct p2p_group *group, const u8 *addr,
 			  const u8 *ie, size_t len);
+#ifdef ANDROID_BRCM_P2P_PATCH
+/**
+ * p2p_group_get_dev_addr - Retreive the device address of an assocated P2P
+ * client. 
+ * @group: P2P group context from p2p_group_init()
+ * @addr: Interface address of the P2P client
+ * Returns: P2P dev_addr on success, NULL on failure
+ */
+u8 *p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr);
+#endif /*ANDROID_BRCM_P2P_PATCH */
 
 /**
  * p2p_group_assoc_resp_ie - Build P2P IE for (re)association response
diff --git a/src/p2p/p2p_group.c b/src/p2p/p2p_group.c
index 169985f..9fb9f00 100644
--- a/src/p2p/p2p_group.c
+++ b/src/p2p/p2p_group.c
@@ -547,6 +547,19 @@
 	return NULL;
 }
 
+#ifdef ANDROID_BRCM_P2P_PATCH
+u8 * p2p_group_get_dev_addr(struct p2p_group *group, const u8 *addr)
+{
+	struct p2p_group_member *m;
+
+	m = p2p_group_get_client_iface(group, addr);
+
+	if (m)
+		return m->dev_addr;
+	else
+		return NULL;
+}
+#endif /* ANDROID_BRCM_P2P_PATCH */
 
 static struct wpabuf * p2p_build_go_disc_req(void)
 {
diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c
index d8b2e16..12dae34 100644
--- a/wpa_supplicant/p2p_supplicant.c
+++ b/wpa_supplicant/p2p_supplicant.c
@@ -3294,6 +3294,12 @@
 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
 		return -1;
 
+#ifdef ANDROID_BRCM_P2P_PATCH
+	/* Make sure we are not running find during connection establishment */
+	wpa_printf(MSG_DEBUG, "P2P: Stopping P2P FIND, if any");
+	wpas_p2p_stop_find(wpa_s);
+#endif
+
 	if (freq == 2) {
 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
 			   "band");