Skip to contentSkip to conversation

p2putil: Replace free with clear in function names

PATCH In review Andrew Zaborowski 2 participants 6 messages Nov 21, 2019 – Nov 22, 2019

lore
Share & tools
lore
#0 lore raw

PATCH 1/4 p2putil: Replace free with clear in function names

All these functions free up the resources used by the struct but don't free the struct itself (allowing it to be static) so rename the functions to avoid confusion.
---
The patch probably would be easier to read without -n / -u

---
 src/p2putil.c | 132 +++++++++++++++++++++++++-------------------------
 src/p2putil.h |  30 ++++++------
 2 files changed, 81 insertions(+), 81 deletions(-)
src/p2putil.c +66 -66
index ce53b2a2..02c02b4d 100644
@@ -321,7 +321,7 @@ static bool extract_p2p_device_info(const uint8_t *attr, size_t len,
 	return true;
 }
 
-static void p2p_free_client_info_descriptor(void *data)
+static void p2p_clear_client_info_descriptor(void *data)
 {
 	struct p2p_client_info_descriptor *desc = data;
 
@@ -397,7 +397,7 @@ static bool extract_p2p_group_info(const uint8_t *attr, size_t len,
 	return true;
 
 error:
-	l_queue_destroy(*out, p2p_free_client_info_descriptor);
+	l_queue_destroy(*out, p2p_clear_client_info_descriptor);
 	*out = NULL;
 
 	return false;
@@ -502,7 +502,7 @@ static bool extract_p2p_advertisement_id(const uint8_t *attr, size_t len,
 	return true;
 }
 
-static void p2p_free_advertised_service_descriptor(void *data)
+static void p2p_clear_advertised_service_descriptor(void *data)
 {
 	struct p2p_advertised_service_descriptor *desc = data;
 
@@ -548,7 +548,7 @@ static bool extract_p2p_advertised_service_info(const uint8_t *attr, size_t len,
 	return true;
 
 error:
-	l_queue_destroy(*out, p2p_free_advertised_service_descriptor);
+	l_queue_destroy(*out, p2p_clear_advertised_service_descriptor);
 	return false;
 }
 
@@ -758,7 +758,7 @@ int p2p_parse_beacon(const uint8_t *pdu, size_t len, struct p2p_beacon *out)
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_beacon(&d);
+		p2p_clear_beacon(&d);
 
 	return r;
 }
@@ -784,7 +784,7 @@ int p2p_parse_probe_req(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_probe_req(&d);
+		p2p_clear_probe_req(&d);
 
 	/*
 	 * The additional WSC IE attributes are already covered by
@@ -814,7 +814,7 @@ int p2p_parse_probe_resp(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_probe_resp(&d);
+		p2p_clear_probe_resp(&d);
 
 	return r;
 }
@@ -837,7 +837,7 @@ int p2p_parse_association_req(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_association_req(&d);
+		p2p_clear_association_req(&d);
 
 	return r;
 }
@@ -961,7 +961,7 @@ int p2p_parse_go_negotiation_req(const uint8_t *pdu, size_t len,
 	return 0;
 
 error:
-	p2p_free_go_negotiation_req(&d);
+	p2p_clear_go_negotiation_req(&d);
 	return r;
 }
 
@@ -1020,7 +1020,7 @@ int p2p_parse_go_negotiation_resp(const uint8_t *pdu, size_t len,
 	return 0;
 
 error:
-	p2p_free_go_negotiation_resp(&d);
+	p2p_clear_go_negotiation_resp(&d);
 	return r;
 }
 
@@ -1049,7 +1049,7 @@ int p2p_parse_go_negotiation_confirmation(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_go_negotiation_confirmation(&d);
+		p2p_clear_go_negotiation_confirmation(&d);
 
 	return r;
 }
@@ -1101,7 +1101,7 @@ done:
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_invitation_req(&d);
+		p2p_clear_invitation_req(&d);
 
 	return r;
 }
@@ -1131,7 +1131,7 @@ int p2p_parse_invitation_resp(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_invitation_resp(&d);
+		p2p_clear_invitation_resp(&d);
 
 	return r;
 }
@@ -1254,7 +1254,7 @@ int p2p_parse_provision_disc_req(const uint8_t *pdu, size_t len,
 	return 0;
 
 error:
-	p2p_free_provision_disc_req(&d);
+	p2p_clear_provision_disc_req(&d);
 	return r;
 }
 
@@ -1332,7 +1332,7 @@ int p2p_parse_provision_disc_resp(const uint8_t *pdu, size_t len,
 	return 0;
 
 error:
-	p2p_free_provision_disc_resp(&d);
+	p2p_clear_provision_disc_resp(&d);
 	return r;
 }
 
@@ -1353,7 +1353,7 @@ int p2p_parse_notice_of_absence(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_notice_of_absence(&d);
+		p2p_clear_notice_of_absence(&d);
 
 	return r;
 }
@@ -1379,7 +1379,7 @@ int p2p_parse_presence_req(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_presence_req(&d);
+		p2p_clear_presence_req(&d);
 
 	return r;
 }
@@ -1406,7 +1406,7 @@ int p2p_parse_presence_resp(const uint8_t *pdu, size_t len,
 	if (r >= 0)
 		memcpy(out, &d, sizeof(d));
 	else
-		p2p_free_presence_resp(&d);
+		p2p_clear_presence_resp(&d);
 
 	return r;
 }
@@ -1420,132 +1420,132 @@ int p2p_parse_go_disc_req(const uint8_t *pdu, size_t len)
 	return 0;
 }
 
-static void p2p_free_channel_list_attr(struct p2p_channel_list_attr *attr)
+static void p2p_clear_channel_list_attr(struct p2p_channel_list_attr *attr)
 {
 	l_queue_destroy(attr->channel_entries, l_free);
 	attr->channel_entries = NULL;
 }
 
-static void p2p_free_notice_of_absence_attr(
+static void p2p_clear_notice_of_absence_attr(
 					struct p2p_notice_of_absence_attr *attr)
 {
 	l_queue_destroy(attr->descriptors, l_free);
 	attr->descriptors = NULL;
 }
 
-static void p2p_free_device_info_attr(struct p2p_device_info_attr *attr)
+static void p2p_clear_device_info_attr(struct p2p_device_info_attr *attr)
 {
 	l_queue_destroy(attr->secondary_device_types, l_free);
 	attr->secondary_device_types = NULL;
 }
 
-static void p2p_free_group_info_attr(struct l_queue **group_clients)
+static void p2p_clear_group_info_attr(struct l_queue **group_clients)
 {
-	l_queue_destroy(*group_clients, p2p_free_client_info_descriptor);
+	l_queue_destroy(*group_clients, p2p_clear_client_info_descriptor);
 	*group_clients = NULL;
 }
 
-static void p2p_free_interface_attr(struct p2p_interface_attr *attr)
+static void p2p_clear_interface_attr(struct p2p_interface_attr *attr)
 {
 	l_queue_destroy(attr->interface_addrs, l_free);
 	attr->interface_addrs = NULL;
 }
 
-static void p2p_free_svc_hash_attr(struct l_queue **hashes)
+static void p2p_clear_svc_hash_attr(struct l_queue **hashes)
 {
 	l_queue_destroy(*hashes, l_free);
 	*hashes = NULL;
 }
 
-static void p2p_free_advertised_service_info_attr(struct l_queue **descriptors)
+static void p2p_clear_advertised_service_info_attr(struct l_queue **descriptors)
 {
-	l_queue_destroy(*descriptors, p2p_free_advertised_service_descriptor);
+	l_queue_destroy(*descriptors, p2p_clear_advertised_service_descriptor);
 	*descriptors = NULL;
 }
 
-void p2p_free_beacon(struct p2p_beacon *data)
+void p2p_clear_beacon(struct p2p_beacon *data)
 {
-	p2p_free_notice_of_absence_attr(&data->notice_of_absence);
+	p2p_clear_notice_of_absence_attr(&data->notice_of_absence);
 }
 
-void p2p_free_probe_req(struct p2p_probe_req *data)
+void p2p_clear_probe_req(struct p2p_probe_req *data)
 {
-	p2p_free_device_info_attr(&data->device_info);
-	p2p_free_svc_hash_attr(&data->service_hashes);
+	p2p_clear_device_info_attr(&data->device_info);
+	p2p_clear_svc_hash_attr(&data->service_hashes);
 }
 
-void p2p_free_probe_resp(struct p2p_probe_resp *data)
+void p2p_clear_probe_resp(struct p2p_probe_resp *data)
 {
-	p2p_free_notice_of_absence_attr(&data->notice_of_absence);
-	p2p_free_device_info_attr(&data->device_info);
-	p2p_free_group_info_attr(&data->group_clients);
-	p2p_free_advertised_service_info_attr(&data->advertised_svcs);
+	p2p_clear_notice_of_absence_attr(&data->notice_of_absence);
+	p2p_clear_device_info_attr(&data->device_info);
+	p2p_clear_group_info_attr(&data->group_clients);
+	p2p_clear_advertised_service_info_attr(&data->advertised_svcs);
 }
 
-void p2p_free_association_req(struct p2p_association_req *data)
+void p2p_clear_association_req(struct p2p_association_req *data)
 {
-	p2p_free_device_info_attr(&data->device_info);
-	p2p_free_interface_attr(&data->interface);
+	p2p_clear_device_info_attr(&data->device_info);
+	p2p_clear_interface_attr(&data->interface);
 }
 
-void p2p_free_association_resp(struct p2p_association_resp *data)
+void p2p_clear_association_resp(struct p2p_association_resp *data)
 {
 }
 
-void p2p_free_go_negotiation_req(struct p2p_go_negotiation_req *data)
+void p2p_clear_go_negotiation_req(struct p2p_go_negotiation_req *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
-	p2p_free_device_info_attr(&data->device_info);
+	p2p_clear_channel_list_attr(&data->channel_list);
+	p2p_clear_device_info_attr(&data->device_info);
 }
 
-void p2p_free_go_negotiation_resp(struct p2p_go_negotiation_resp *data)
+void p2p_clear_go_negotiation_resp(struct p2p_go_negotiation_resp *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
-	p2p_free_device_info_attr(&data->device_info);
+	p2p_clear_channel_list_attr(&data->channel_list);
+	p2p_clear_device_info_attr(&data->device_info);
 }
 
-void p2p_free_go_negotiation_confirmation(
+void p2p_clear_go_negotiation_confirmation(
 				struct p2p_go_negotiation_confirmation *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
+	p2p_clear_channel_list_attr(&data->channel_list);
 }
 
-void p2p_free_invitation_req(struct p2p_invitation_req *data)
+void p2p_clear_invitation_req(struct p2p_invitation_req *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
-	p2p_free_device_info_attr(&data->device_info);
+	p2p_clear_channel_list_attr(&data->channel_list);
+	p2p_clear_device_info_attr(&data->device_info);
 }
 
-void p2p_free_invitation_resp(struct p2p_invitation_resp *data)
+void p2p_clear_invitation_resp(struct p2p_invitation_resp *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
+	p2p_clear_channel_list_attr(&data->channel_list);
 }
 
-void p2p_free_provision_disc_req(struct p2p_provision_discovery_req *data)
+void p2p_clear_provision_disc_req(struct p2p_provision_discovery_req *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
-	p2p_free_device_info_attr(&data->device_info);
+	p2p_clear_channel_list_attr(&data->channel_list);
+	p2p_clear_device_info_attr(&data->device_info);
 }
 
-void p2p_free_provision_disc_resp(struct p2p_provision_discovery_resp *data)
+void p2p_clear_provision_disc_resp(struct p2p_provision_discovery_resp *data)
 {
-	p2p_free_channel_list_attr(&data->channel_list);
-	p2p_free_device_info_attr(&data->device_info);
+	p2p_clear_channel_list_attr(&data->channel_list);
+	p2p_clear_device_info_attr(&data->device_info);
 }
 
-void p2p_free_notice_of_absence(struct p2p_notice_of_absence *data)
+void p2p_clear_notice_of_absence(struct p2p_notice_of_absence *data)
 {
-	p2p_free_notice_of_absence_attr(&data->notice_of_absence);
+	p2p_clear_notice_of_absence_attr(&data->notice_of_absence);
 }
 
-void p2p_free_presence_req(struct p2p_presence_req *data)
+void p2p_clear_presence_req(struct p2p_presence_req *data)
 {
-	p2p_free_notice_of_absence_attr(&data->notice_of_absence);
+	p2p_clear_notice_of_absence_attr(&data->notice_of_absence);
 }
 
-void p2p_free_presence_resp(struct p2p_presence_resp *data)
+void p2p_clear_presence_resp(struct p2p_presence_resp *data)
 {
-	p2p_free_notice_of_absence_attr(&data->notice_of_absence);
+	p2p_clear_notice_of_absence_attr(&data->notice_of_absence);
 }
 
 struct p2p_attr_builder {
src/p2putil.h +15 -15
index 681d9e33..09c72c74 100644
@@ -462,22 +462,22 @@ int p2p_parse_presence_resp(const uint8_t *pdu, size_t len,
 				struct p2p_presence_resp *out);
 int p2p_parse_go_disc_req(const uint8_t *pdu, size_t len);
 
-void p2p_free_beacon(struct p2p_beacon *data);
-void p2p_free_probe_req(struct p2p_probe_req *data);
-void p2p_free_probe_resp(struct p2p_probe_resp *data);
-void p2p_free_association_req(struct p2p_association_req *data);
-void p2p_free_association_resp(struct p2p_association_resp *data);
-void p2p_free_go_negotiation_req(struct p2p_go_negotiation_req *data);
-void p2p_free_go_negotiation_resp(struct p2p_go_negotiation_resp *data);
-void p2p_free_go_negotiation_confirmation(
+void p2p_clear_beacon(struct p2p_beacon *data);
+void p2p_clear_probe_req(struct p2p_probe_req *data);
+void p2p_clear_probe_resp(struct p2p_probe_resp *data);
+void p2p_clear_association_req(struct p2p_association_req *data);
+void p2p_clear_association_resp(struct p2p_association_resp *data);
+void p2p_clear_go_negotiation_req(struct p2p_go_negotiation_req *data);
+void p2p_clear_go_negotiation_resp(struct p2p_go_negotiation_resp *data);
+void p2p_clear_go_negotiation_confirmation(
 				struct p2p_go_negotiation_confirmation *data);
-void p2p_free_invitation_req(struct p2p_invitation_req *data);
-void p2p_free_invitation_resp(struct p2p_invitation_resp *data);
-void p2p_free_provision_disc_req(struct p2p_provision_discovery_req *data);
-void p2p_free_provision_disc_resp(struct p2p_provision_discovery_resp *data);
-void p2p_free_notice_of_absence(struct p2p_notice_of_absence *data);
-void p2p_free_presence_req(struct p2p_presence_req *data);
-void p2p_free_presence_resp(struct p2p_presence_resp *data);
+void p2p_clear_invitation_req(struct p2p_invitation_req *data);
+void p2p_clear_invitation_resp(struct p2p_invitation_resp *data);
+void p2p_clear_provision_disc_req(struct p2p_provision_discovery_req *data);
+void p2p_clear_provision_disc_resp(struct p2p_provision_discovery_resp *data);
+void p2p_clear_notice_of_absence(struct p2p_notice_of_absence *data);
+void p2p_clear_presence_req(struct p2p_presence_req *data);
+void p2p_clear_presence_resp(struct p2p_presence_resp *data);
 
 uint8_t *p2p_build_beacon(const struct p2p_beacon *data, size_t *out_len);
 uint8_t *p2p_build_probe_req(const struct p2p_probe_req *data, size_t *out_len);
Signature · 1 line
2.20.1
#1 lore raw

Hi Andrew,

Quoted 10 lines · Andrew Zaborowski

All these functions free up the resources used by the struct but don't free the struct itself (allowing it to be static) so rename the functions to avoid confusion.
---
The patch probably would be easier to read without -n / -u

---
  src/p2putil.c | 132 +++++++++++++++++++++++++-------------------------
  src/p2putil.h |  30 ++++++------
  2 files changed, 81 insertions(+), 81 deletions(-)

Patch 1 & 2 applied, thanks.

Regards,
-Denis

#2 lore raw

PATCH 2/4 unit: Update p2p_free_* function names

In reply to Andrew Zaborowski
---
 unit/test-p2p.c | 30 +++++++++++++++---------------
 1 file changed, 15 insertions(+), 15 deletions(-)
unit/test-p2p.c +15 -15
index 6638b5d9..08495e78 100644
@@ -277,8 +277,8 @@ static void p2p_test_parse_beacon(const void *data)
 				attrs2.notice_of_absence.descriptors,
 				p2p_noa_desc_cmp));
 
-	p2p_free_beacon(&attrs1);
-	p2p_free_beacon(&attrs2);
+	p2p_clear_beacon(&attrs1);
+	p2p_clear_beacon(&attrs2);
 }
 
 static void p2p_test_build_beacon(const void *data)
@@ -301,7 +301,7 @@ static void p2p_test_build_beacon(const void *data)
 	}
 
 	ies = p2p_build_beacon(&attrs2, &ies_len);
-	p2p_free_beacon(&attrs2);
+	p2p_clear_beacon(&attrs2);
 
 	assert(p2p_payload_cmd(ies, ies_len, test->ies, test->ies_len));
 	l_free(ies);
@@ -407,7 +407,7 @@ static void p2p_test_parse_probe_req(const void *data)
 	assert(l_queue_length(attrs.service_hashes) ==
 		l_queue_length(test->data.service_hashes));
 
-	p2p_free_probe_req(&attrs);
+	p2p_clear_probe_req(&attrs);
 }
 
 static void p2p_test_build_probe_req(const void *data)
@@ -617,8 +617,8 @@ static void p2p_test_parse_probe_resp(const void *data)
 	assert(l_queue_length(attrs1.advertised_svcs) ==
 		l_queue_length(attrs2.advertised_svcs));
 
-	p2p_free_probe_resp(&attrs1);
-	p2p_free_probe_resp(&attrs2);
+	p2p_clear_probe_resp(&attrs1);
+	p2p_clear_probe_resp(&attrs2);
 }
 
 static void p2p_test_build_probe_resp(const void *data)
@@ -641,7 +641,7 @@ static void p2p_test_build_probe_resp(const void *data)
 	}
 
 	ies = p2p_build_probe_resp(&attrs2, &ies_len);
-	p2p_free_probe_resp(&attrs2);
+	p2p_clear_probe_resp(&attrs2);
 
 	assert(p2p_payload_cmd(ies, ies_len, test->ies, test->ies_len));
 	l_free(ies);
@@ -738,8 +738,8 @@ static void p2p_test_parse_association_req(const void *data)
 	assert(l_queue_length(attrs1.interface.interface_addrs) ==
 		l_queue_length(attrs2.interface.interface_addrs));
 
-	p2p_free_association_req(&attrs1);
-	p2p_free_association_req(&attrs2);
+	p2p_clear_association_req(&attrs1);
+	p2p_clear_association_req(&attrs2);
 }
 
 static void p2p_test_build_association_req(const void *data)
@@ -808,8 +808,8 @@ static void p2p_test_parse_association_resp(const void *data)
 	assert(attrs1.listen_availability.avail_interval_ms ==
 		attrs2.listen_availability.avail_interval_ms);
 
-	p2p_free_association_resp(&attrs1);
-	p2p_free_association_resp(&attrs2);
+	p2p_clear_association_resp(&attrs1);
+	p2p_clear_association_resp(&attrs2);
 }
 
 static void p2p_test_build_association_resp(const void *data)
@@ -976,8 +976,8 @@ static void p2p_test_parse_provision_disc_req(const void *data)
 
 	assert(attrs1.wsc_config_method == attrs2.wsc_config_method);
 
-	p2p_free_provision_disc_req(&attrs1);
-	p2p_free_provision_disc_req(&attrs2);
+	p2p_clear_provision_disc_req(&attrs1);
+	p2p_clear_provision_disc_req(&attrs2);
 }
 
 static void p2p_test_build_provision_disc_req(const void *data)
@@ -1114,8 +1114,8 @@ static void p2p_test_parse_provision_disc_resp(const void *data)
 
 	assert(attrs1.wsc_config_method == attrs2.wsc_config_method);
 
-	p2p_free_provision_disc_resp(&attrs1);
-	p2p_free_provision_disc_resp(&attrs2);
+	p2p_clear_provision_disc_resp(&attrs1);
+	p2p_clear_provision_disc_resp(&attrs2);
 }
 
 int main(int argc, char *argv[])
Signature · 1 line
2.20.1
#3 lore raw

PATCH 3/4 scan: Parse P2P IEs according to frame type

In reply to Andrew Zaborowski

Save the source frame type in struct scan_bss as it may affect how some of the data in the struct will be parsed. Also replace the P2P IE payload data in that struct with a union containing pre-parsed p2p attributes corresponding to the frame type.

This means users don't have to call the parsers in p2putil.c on that data, which wouldn't have worked anyway because those parsers assume input is the raw IE sequence rather than just the "payload".

---
 src/scan.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 src/scan.h |  17 +++++++-
 2 files changed, 128 insertions(+), 12 deletions(-)
src/scan.c +113 -10
index c5dbbb1c..ed4b1717 100644
@@ -46,6 +46,7 @@
 #include "src/nl80211cmd.h"
 #include "src/nl80211util.h"
 #include "src/util.h"
+#include "src/p2putil.h"
 #include "src/scan.h"
 
 #define SCAN_MAX_INTERVAL 320
@@ -1054,6 +1055,65 @@ static bool scan_parse_bss_information_elements(struct scan_bss *bss,
 		}
 	}
 
+	bss->wsc = ie_tlv_extract_wsc_payload(data, len, &bss->wsc_size);
+
+	switch (bss->source_frame) {
+	case SCAN_BSS_PROBE_RESP:
+		bss->p2p_probe_resp_info = l_new(struct p2p_probe_resp, 1);
+
+		if (p2p_parse_probe_resp(data, len, bss->p2p_probe_resp_info) ==
+				0)
+			break;
+
+		l_free(bss->p2p_probe_resp_info);
+		bss->p2p_probe_resp_info = NULL;
+		break;
+	case SCAN_BSS_PROBE_REQ:
+		bss->p2p_probe_req_info = l_new(struct p2p_probe_req, 1);
+
+		if (p2p_parse_probe_req(data, len, bss->p2p_probe_req_info) ==
+				0)
+			break;
+
+		l_free(bss->p2p_probe_req_info);
+		bss->p2p_probe_req_info = NULL;
+		break;
+	case SCAN_BSS_BEACON:
+	{
+		/*
+		 * Beacon and Probe Response P2P IE subelement formats are
+		 * mutually incompatible and can help us distinguish one frame
+		 * subtype from the other if the driver is not exposing enough
+		 * information.  As a result of trusting the frame contents on
+		 * this, no critical code should depend on the
+		 * bss->source_frame information being right.
+		 */
+		struct p2p_beacon info;
+		int r;
+
+		r = p2p_parse_beacon(data, len, &info);
+		if (r == 0) {
+			bss->p2p_beacon_info = l_memdup(&info, sizeof(info));
+			break;
+		}
+
+		if (r == -ENOENT)
+			break;
+
+		bss->p2p_probe_resp_info = l_new(struct p2p_probe_resp, 1);
+
+		if (p2p_parse_probe_resp(data, len, bss->p2p_probe_resp_info) ==
+				0) {
+			bss->source_frame = SCAN_BSS_PROBE_RESP;
+			break;
+		}
+
+		l_free(bss->p2p_probe_resp_info);
+		bss->p2p_probe_resp_info = NULL;
+		break;
+	}
+	}
+
 	return have_ssid;
 }
 
@@ -1062,9 +1122,14 @@ static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr)
 	uint16_t type, len;
 	const void *data;
 	struct scan_bss *bss;
+	const uint8_t *ies = NULL;
+	size_t ies_len;
+	const uint8_t *beacon_ies = NULL;
+	size_t beacon_ies_len;
 
 	bss = l_new(struct scan_bss, 1);
 	bss->utilization = 127;
+	bss->source_frame = SCAN_BSS_BEACON;
 
 	while (l_genl_attr_next(attr, &type, &len, &data)) {
 		switch (type) {
@@ -1093,15 +1158,8 @@ static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr)
 			bss->signal_strength = *((int32_t *) data);
 			break;
 		case NL80211_BSS_INFORMATION_ELEMENTS:
-			if (!scan_parse_bss_information_elements(bss,
-								data, len))
-				goto fail;
-
-			bss->wsc = ie_tlv_extract_wsc_payload(data, len,
-								&bss->wsc_size);
-			bss->p2p = ie_tlv_extract_p2p_payload(data, len,
-								&bss->p2p_size);
-
+			ies = data;
+			ies_len = len;
 			break;
 		case NL80211_BSS_PARENT_TSF:
 			if (len != sizeof(uint64_t))
@@ -1109,9 +1167,30 @@ static struct scan_bss *scan_parse_attr_bss(struct l_genl_attr *attr)
 
 			bss->parent_tsf = l_get_u64(data);
 			break;
+		case NL80211_BSS_PRESP_DATA:
+			bss->source_frame = SCAN_BSS_PROBE_RESP;
+			break;
+		case NL80211_BSS_BEACON_IES:
+			beacon_ies = data;
+			beacon_ies_len = len;
+			break;
 		}
 	}
 
+	/*
+	 * Try our best at deciding whether the IEs come from a Probe
+	 * Response based on the hints explained in nl80211.h
+	 * (enum nl80211_bss).
+	 */
+	if (bss->source_frame == SCAN_BSS_BEACON && ies && (
+				!beacon_ies ||
+				ies_len != beacon_ies_len ||
+				memcmp(ies, beacon_ies, ies_len)))
+		bss->source_frame = SCAN_BSS_PROBE_RESP;
+
+	if (ies && !scan_parse_bss_information_elements(bss, ies, ies_len))
+		goto fail;
+
 	return bss;
 
 fail:
@@ -1271,9 +1350,33 @@ void scan_bss_free(struct scan_bss *bss)
 	l_free(bss->rsne);
 	l_free(bss->wpa);
 	l_free(bss->wsc);
-	l_free(bss->p2p);
 	l_free(bss->osen);
 	l_free(bss->rc_ie);
+
+	switch (bss->source_frame) {
+	case SCAN_BSS_PROBE_RESP:
+		if (!bss->p2p_probe_resp_info)
+			break;
+
+		p2p_clear_probe_resp(bss->p2p_probe_resp_info);
+		l_free(bss->p2p_probe_resp_info);
+		break;
+	case SCAN_BSS_PROBE_REQ:
+		if (!bss->p2p_probe_req_info)
+			break;
+
+		p2p_clear_probe_req(bss->p2p_probe_req_info);
+		l_free(bss->p2p_probe_req_info);
+		break;
+	case SCAN_BSS_BEACON:
+		if (!bss->p2p_beacon_info)
+			break;
+
+		p2p_clear_beacon(bss->p2p_beacon_info);
+		l_free(bss->p2p_beacon_info);
+		break;
+	}
+
 	l_free(bss);
 }
 
src/scan.h +15 -2
index 8fc2aa56..aa282084 100644
@@ -40,6 +40,15 @@ typedef void (*scan_freq_set_func_t)(uint32_t freq, void *userdata);
 
 struct scan_freq_set;
 struct ie_rsn_info;
+struct p2p_probe_resp;
+struct p2p_probe_req;
+struct p2p_beacon;
+
+enum scan_bss_frame_type {
+	SCAN_BSS_PROBE_RESP,
+	SCAN_BSS_PROBE_REQ,
+	SCAN_BSS_BEACON,
+};
 
 struct scan_bss {
 	uint8_t addr[6];
@@ -51,8 +60,12 @@ struct scan_bss {
 	uint8_t *osen;
 	uint8_t *wsc;		/* Concatenated WSC IEs */
 	ssize_t wsc_size;	/* Size of Concatenated WSC IEs */
-	uint8_t *p2p;		/* Concatenated P2P IEs */
-	ssize_t p2p_size;	/* Size of Concatenated P2P IEs */
+	enum scan_bss_frame_type source_frame;
+	union {
+		struct p2p_probe_resp *p2p_probe_resp_info;
+		struct p2p_probe_req *p2p_probe_req_info;
+		struct p2p_beacon *p2p_beacon_info;
+	};
 	uint8_t mde[3];
 	uint8_t ssid[32];
 	uint8_t ssid_len;
Signature · 1 line
2.20.1
#4 lore raw

Re: [PATCH 3/4] scan: Parse P2P IEs according to frame type

Hi Andrew,

Quoted 5 lines · Andrew Zaborowski

Save the source frame type in struct scan_bss as it may affect how some of the data in the struct will be parsed. Also replace the P2P IE payload data in that struct with a union containing pre-parsed p2p attributes corresponding to the frame type.

Quoted 8 lines

This means users don't have to call the parsers in p2putil.c on that data, which wouldn't have worked anyway because those parsers assume input is the raw IE sequence rather than just the "payload".

---
  src/scan.c | 123 ++++++++++++++++++++++++++++++++++++++++++++++++-----
  src/scan.h |  17 +++++++-
  2 files changed, 128 insertions(+), 12 deletions(-)

Patch 3 & 4 applied, thanks.

Regards,
-Denis

#5 lore raw

PATCH 4/4 scan: Add scan_bss_new_from_probe_req

In reply to Andrew Zaborowski
---
 src/scan.c | 27 +++++++++++++++++++++++++++
 src/scan.h |  6 ++++++
 2 files changed, 33 insertions(+)
src/scan.c +27 -0
index ed4b1717..9817fe24 100644
@@ -47,6 +47,7 @@
 #include "src/nl80211util.h"
 #include "src/util.h"
 #include "src/p2putil.h"
+#include "src/mpdu.h"
 #include "src/scan.h"
 
 #define SCAN_MAX_INTERVAL 320
@@ -1344,6 +1345,32 @@ static void scan_bss_compute_rank(struct scan_bss *bss)
 		bss->rank = irank;
 }
 
+struct scan_bss *scan_bss_new_from_probe_req(const struct mmpdu_header *mpdu,
+						const uint8_t *body,
+						size_t body_len,
+						uint32_t frequency, int rssi)
+
+{
+	struct scan_bss *bss;
+
+	bss = l_new(struct scan_bss, 1);
+	memcpy(bss->addr, mpdu->address_2, 6);
+	bss->utilization = 127;
+	bss->source_frame = SCAN_BSS_PROBE_REQ;
+	bss->frequency = frequency;
+	bss->signal_strength = rssi;
+
+	if (!scan_parse_bss_information_elements(bss, body, body_len))
+		goto fail;
+
+	scan_bss_compute_rank(bss);
+	return bss;
+
+fail:
+	scan_bss_free(bss);
+	return NULL;
+}
+
 void scan_bss_free(struct scan_bss *bss)
 {
 	l_free(bss->ext_supp_rates_ie);
src/scan.h +6 -0
index aa282084..b8089a16 100644
@@ -43,6 +43,7 @@ struct ie_rsn_info;
 struct p2p_probe_resp;
 struct p2p_probe_req;
 struct p2p_beacon;
+struct mmpdu_header;
 
 enum scan_bss_frame_type {
 	SCAN_BSS_PROBE_RESP,
@@ -142,6 +143,11 @@ int scan_bss_rank_compare(const void *a, const void *b, void *user);
 
 int scan_bss_get_rsn_info(const struct scan_bss *bss, struct ie_rsn_info *info);
 
+struct scan_bss *scan_bss_new_from_probe_req(const struct mmpdu_header *mpdu,
+						const uint8_t *body,
+						size_t body_len,
+						uint32_t frequency, int rssi);
+
 uint8_t scan_freq_to_channel(uint32_t freq, enum scan_band *out_band);
 uint32_t scan_channel_to_freq(uint8_t channel, enum scan_band band);
 enum scan_band scan_oper_class_to_band(const uint8_t *country,
Signature · 1 line
2.20.1