PATCH: P2P randomized MAC address support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

To enhance privacy, we try to hide the real MAC address of P2P interface.
These two patches would provide P2P randomized MAC address support.

Best regards,
Jimmy
From 11b7a2a009d23330375a594202ec831ca5ed1206 Mon Sep 17 00:00:00 2001
From: Jimmy Chen <jimmycmchen@xxxxxxxxxx>
Date: Tue, 13 Nov 2018 15:19:57 +0800
Subject: [PATCH 1/2] p2p: support random device address

To enhance privacy, generate a ramdom device address for p2p interface.
If there is no saved persistent group, it generate a new random MAC address on bringing up p2p0.
If there is saved persistent group, it will use last MAC address to avoid breaking
group reinvoke behavior.

There are two configurations are introduced:
* p2p_device_random_mac_addr
  enable device random MAC address feature, default disable.
* p2p_device_persistent_mac_addr
  store last used random MAC address.

Change-Id: I3a519209752e0e79c82c7fbd9c7a18669f778e84
Signed-off-by: Jimmy Chen <jimmycmchen@xxxxxxxxxx>
---
 wpa_supplicant/config.c         |  3 ++
 wpa_supplicant/config.h         | 20 ++++++++++++
 wpa_supplicant/config_file.c    |  6 ++++
 wpa_supplicant/p2p_supplicant.c | 57 +++++++++++++++++++++++++++++++++
 wpa_supplicant/p2p_supplicant.h |  1 +
 5 files changed, 87 insertions(+)

diff --git a/wpa_supplicant/config.c b/wpa_supplicant/config.c
index bfca69ff7..caecfd15b 100644
--- a/wpa_supplicant/config.c
+++ b/wpa_supplicant/config.c
@@ -2722,6 +2722,7 @@ void wpa_config_free(struct wpa_config *config)
 #ifdef CONFIG_MBO
 	os_free(config->non_pref_chan);
 #endif /* CONFIG_MBO */
+	os_free(config->p2p_device_persistent_mac_addr);
 
 	os_free(config);
 }
@@ -4844,6 +4845,8 @@ static const struct global_parse_data global_fields[] = {
 	{ INT_RANGE(gas_rand_mac_addr, 0, 2), 0 },
 	{ INT_RANGE(dpp_config_processing, 0, 2), 0 },
 	{ INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
+	{ INT(p2p_device_random_mac_addr), 0 },
+	{ STR(p2p_device_persistent_mac_addr), 0 },
 };
 
 #undef FUNC
diff --git a/wpa_supplicant/config.h b/wpa_supplicant/config.h
index cd7571f59..6e009e836 100644
--- a/wpa_supplicant/config.h
+++ b/wpa_supplicant/config.h
@@ -1478,6 +1478,26 @@ struct wpa_config {
 	 * 1 = enabled (true)
 	 */
 	int coloc_intf_reporting;
+
+	/**
+	 * p2p_device_random_mac_addr - P2P Device MAC address policy default
+	 *
+	 * 0 = use permanent MAC address
+	 * 1 = use random MAC address on creating the interface if there is no persistent groups.
+	 *
+	 * By default, permanent MAC address is used.
+	 */
+	int p2p_device_random_mac_addr;
+
+	/**
+	 * p2p_device_persistent_mac_addr - Record last used MAC address
+	 *
+	 * If there are saved persistent groups, P2P cannot generate another random MAC address,
+	 * and need to restore to last used MAC address.
+	 * format: aa:bb:cc:dd:ee:ff
+	 */
+	char *p2p_device_persistent_mac_addr;
+
 };
 
 
diff --git a/wpa_supplicant/config_file.c b/wpa_supplicant/config_file.c
index 73fcf2c5a..ea45ed398 100644
--- a/wpa_supplicant/config_file.c
+++ b/wpa_supplicant/config_file.c
@@ -1527,6 +1527,12 @@ static void wpa_config_write_global(FILE *f, struct wpa_config *config)
 	if (config->coloc_intf_reporting)
 		fprintf(f, "coloc_intf_reporting=%d\n",
 			config->coloc_intf_reporting);
+	if (config->p2p_device_random_mac_addr)
+		fprintf(f, "p2p_device_random_mac_addr=%d\n",
+			config->p2p_device_random_mac_addr);
+	if (config->p2p_device_persistent_mac_addr)
+		fprintf(f, "p2p_device_persistent_mac_addr=%s\n",
+			config->p2p_device_persistent_mac_addr);
 }
 
 #endif /* CONFIG_NO_CONFIG_WRITE */
diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c
index b549103e8..8d0d1ad5a 100644
--- a/wpa_supplicant/p2p_supplicant.c
+++ b/wpa_supplicant/p2p_supplicant.c
@@ -4326,6 +4326,57 @@ static int wpas_p2p_get_pref_freq_list(void *ctx, int go,
 					  WPA_IF_P2P_CLIENT, len, freq_list);
 }
 
+int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s)
+{
+	u8 addr[ETH_ALEN] = {0};
+
+	if (wpa_s->conf->p2p_device_random_mac_addr == 0)
+		return 0;
+
+	if (wpa_s->conf->ssid == NULL) {
+		if (random_mac_addr(addr) < 0) {
+			wpa_msg(wpa_s, MSG_INFO,
+				"Failed to generate random MAC address");
+			return -EINVAL;
+		}
+
+		// store generated MAC address.
+		if (wpa_s->conf->p2p_device_persistent_mac_addr)
+			os_free(wpa_s->conf->p2p_device_persistent_mac_addr);
+		size_t mac_addr_str_len = sizeof("00:00:00:00:00:00");
+		wpa_s->conf->p2p_device_persistent_mac_addr =
+			os_zalloc(mac_addr_str_len + 1);
+		os_snprintf(wpa_s->conf->p2p_device_persistent_mac_addr,
+			mac_addr_str_len, MACSTR, MAC2STR(addr));
+	} else {
+		// If there are existing saved groups, restore last MAC address.
+		// if there is no last used MAC address, the last one is factory MAC.
+		if (!wpa_s->conf->p2p_device_persistent_mac_addr)
+			return 0;
+
+		if (hwaddr_aton(wpa_s->conf->p2p_device_persistent_mac_addr, addr) < 0)
+			return -EINVAL;
+		wpa_msg(wpa_s, MSG_DEBUG, "Restore last used MAC address.");
+	}
+
+	if (wpa_drv_set_mac_addr(wpa_s, addr) < 0) {
+		wpa_msg(wpa_s, MSG_INFO,
+			"Failed to set random MAC address");
+		return -EINVAL;
+	}
+
+	if (wpa_supplicant_update_mac_addr(wpa_s) < 0) {
+		wpa_msg(wpa_s, MSG_INFO,
+			"Could not update MAC address information");
+		return -EINVAL;
+	}
+
+	wpa_msg(wpa_s, MSG_DEBUG, "Using random MAC address " MACSTR,
+		MAC2STR(addr));
+
+	return 0;
+}
+
 
 /**
  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
@@ -4347,6 +4398,12 @@ int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
 	if (global->p2p)
 		return 0;
 
+	if (wpas_p2p_mac_setup(wpa_s) < 0) {
+		wpa_msg(wpa_s, MSG_ERROR,
+			"Failed to initialize P2P random MAC address.");
+		return -1;
+	}
+
 	os_memset(&p2p, 0, sizeof(p2p));
 	p2p.cb_ctx = wpa_s;
 	p2p.debug_print = wpas_p2p_debug_print;
diff --git a/wpa_supplicant/p2p_supplicant.h b/wpa_supplicant/p2p_supplicant.h
index 63910d1c2..0a08a88ca 100644
--- a/wpa_supplicant/p2p_supplicant.h
+++ b/wpa_supplicant/p2p_supplicant.h
@@ -211,6 +211,7 @@ int wpas_p2p_lo_start(struct wpa_supplicant *wpa_s, unsigned int freq,
 		      unsigned int period, unsigned int interval,
 		      unsigned int count);
 int wpas_p2p_lo_stop(struct wpa_supplicant *wpa_s);
+int wpas_p2p_mac_setup(struct wpa_supplicant *wpa_s);
 
 #else /* CONFIG_P2P */
 
-- 
2.20.1.97.g81188d93c3-goog

From 98f217240eb38735c0b6eeef611daa1cd7acc732 Mon Sep 17 00:00:00 2001
From: Jimmy Chen <jimmycmchen@xxxxxxxxxx>
Date: Thu, 29 Nov 2018 16:46:43 +0800
Subject: [PATCH 2/2] p2p: support random interface address

To enhance privacy, generate a ramdom interface for each group.

There are two configurations are introduced:
* p2p_interface_random_mac_addr
  enable interface random MAC address feature, default disable.

Change-Id: I519629eb8520a15e6f2d158cf3b9a4058f66e124
Signed-off-by: Jimmy Chen <jimmycmchen@xxxxxxxxxx>
---
 wpa_supplicant/config.c         |  1 +
 wpa_supplicant/config.h         | 10 ++++++++++
 wpa_supplicant/config_file.c    |  3 +++
 wpa_supplicant/p2p_supplicant.c | 26 ++++++++++++++++++++++++++
 4 files changed, 40 insertions(+)

diff --git a/wpa_supplicant/config.c b/wpa_supplicant/config.c
index caecfd15b..f5c606703 100644
--- a/wpa_supplicant/config.c
+++ b/wpa_supplicant/config.c
@@ -4847,6 +4847,7 @@ static const struct global_parse_data global_fields[] = {
 	{ INT_RANGE(coloc_intf_reporting, 0, 1), 0 },
 	{ INT(p2p_device_random_mac_addr), 0 },
 	{ STR(p2p_device_persistent_mac_addr), 0 },
+	{ INT(p2p_interface_random_mac_addr), 0 },
 };
 
 #undef FUNC
diff --git a/wpa_supplicant/config.h b/wpa_supplicant/config.h
index 6e009e836..f735671a6 100644
--- a/wpa_supplicant/config.h
+++ b/wpa_supplicant/config.h
@@ -1498,6 +1498,16 @@ struct wpa_config {
 	 */
 	char *p2p_device_persistent_mac_addr;
 
+	/**
+	 * p2p_interface_random_mac_addr - P2P Interface MAC address policy default
+	 *
+	 * 0 = use permanent MAC address
+	 * 1 = use random MAC address on creating the interface.
+	 *
+	 * By default, permanent MAC address is used.
+	 */
+	int p2p_interface_random_mac_addr;
+
 };
 
 
diff --git a/wpa_supplicant/config_file.c b/wpa_supplicant/config_file.c
index ea45ed398..1e1c53085 100644
--- a/wpa_supplicant/config_file.c
+++ b/wpa_supplicant/config_file.c
@@ -1533,6 +1533,9 @@ static void wpa_config_write_global(FILE *f, struct wpa_config *config)
 	if (config->p2p_device_persistent_mac_addr)
 		fprintf(f, "p2p_device_persistent_mac_addr=%s\n",
 			config->p2p_device_persistent_mac_addr);
+	if (config->p2p_interface_random_mac_addr)
+		fprintf(f, "p2p_interface_random_mac_addr=%d\n",
+			config->p2p_interface_random_mac_addr);
 }
 
 #endif /* CONFIG_NO_CONFIG_WRITE */
diff --git a/wpa_supplicant/p2p_supplicant.c b/wpa_supplicant/p2p_supplicant.c
index 8d0d1ad5a..836673463 100644
--- a/wpa_supplicant/p2p_supplicant.c
+++ b/wpa_supplicant/p2p_supplicant.c
@@ -2075,6 +2075,13 @@ static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
 		return -1;
 	}
 
+	if (wpa_s->conf->p2p_interface_random_mac_addr) {
+		random_mac_addr(wpa_s->pending_interface_addr);
+		wpa_printf(MSG_DEBUG, "P2P: Generate random MAC address " MACSTR " for the group",
+			MAC2STR(wpa_s->pending_interface_addr));
+	}
+
+
 	if (force_ifname[0]) {
 		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
 			   force_ifname);
@@ -2153,6 +2160,25 @@ wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
 
 	wpas_p2p_clone_config(group_wpa_s, wpa_s);
 
+	if (wpa_s->conf->p2p_interface_random_mac_addr) {
+		if (wpa_drv_set_mac_addr(group_wpa_s, wpa_s->pending_interface_addr) < 0) {
+			wpa_msg(group_wpa_s, MSG_INFO,
+				"Failed to set random MAC address");
+			wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0);
+			return NULL;
+		}
+
+		if (wpa_supplicant_update_mac_addr(group_wpa_s) < 0) {
+			wpa_msg(group_wpa_s, MSG_INFO,
+				"Could not update MAC address information");
+			wpa_supplicant_remove_iface(wpa_s->global, group_wpa_s, 0);
+			return NULL;
+		}
+
+		wpa_printf(MSG_DEBUG, "P2P: Using random MAC address " MACSTR " for the group",
+			MAC2STR(wpa_s->pending_interface_addr));
+	}
+
 	return group_wpa_s;
 }
 
-- 
2.20.1.97.g81188d93c3-goog

_______________________________________________
Hostap mailing list
Hostap@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/hostap

[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux