Skip to contentSkip to conversation

dpp: parse configuration request values

PATCH In review James Prestwood 2 participants 3 messages Jan 6, 2022 – Jan 7, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH 1/2 dpp: parse configuration request values

With better JSON support the configuration request object can now be fully parsed. As stated in the previous comment there really isn't much use from the configurator side apart from verifying mandatory values are included.

This patch also modifies the configuration result to handle sending non 'OK' status codes in case of JSON parsing errors.

---
 src/dpp.c | 76 ++++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 58 insertions(+), 18 deletions(-)
src/dpp.c +58 -18
index 5f25f1c2..dedbd3b2 100644
@@ -579,14 +579,13 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 	send_config_result(dpp, dpp->auth_addr);
 }
 
-static void dpp_send_config_response(struct dpp_sm *dpp)
+static void dpp_send_config_response(struct dpp_sm *dpp, uint8_t status)
 {
-	_auto_(l_free) char *json;
+	_auto_(l_free) char *json = NULL;
 	struct iovec iov[3];
 	uint8_t hdr[41];
 	uint8_t attrs[512];
 	size_t json_len;
-	uint8_t zero = 0;
 	uint8_t *ptr = hdr + 24;
 
 	l_put_le16(0x00d0, hdr);
@@ -611,9 +610,6 @@ static void dpp_send_config_response(struct dpp_sm *dpp)
 	*ptr++ = 0x1a;
 	*ptr++ = 1;
 
-	json = dpp_configuration_to_json(dpp->config);
-	json_len = strlen(json);
-
 	iov[0].iov_base = hdr;
 	iov[0].iov_len = ptr - hdr;
 
@@ -621,12 +617,31 @@ static void dpp_send_config_response(struct dpp_sm *dpp)
 
 	ptr += 2; /* length */
 
-	ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &zero, 1);
+	ptr += dpp_append_attr(ptr, DPP_ATTR_STATUS, &status, 1);
 
-	ptr += dpp_append_wrapped_data(attrs + 2, ptr - attrs - 2, NULL, 0, ptr,
-			sizeof(attrs), dpp->ke, dpp->key_len, 2,
-			DPP_ATTR_ENROLLEE_NONCE, dpp->nonce_len, dpp->e_nonce,
-			DPP_ATTR_CONFIGURATION_OBJECT, json_len, json);
+	/*
+	 * There are several failure status codes that can be used (defined in
+	 * 6.4.3.1), each with their own set of attributes that should be
+	 * included. For now IWD's basic DPP implementation will assume
+	 * STATUS_CONFIGURE_FAILURE which only includes the E-Nonce.
+	 */
+	if (status == DPP_STATUS_OK) {
+		json = dpp_configuration_to_json(dpp->config);
+		json_len = strlen(json);
+
+		ptr += dpp_append_wrapped_data(attrs + 2, ptr - attrs - 2,
+						NULL, 0, ptr, sizeof(attrs),
+						dpp->ke, dpp->key_len, 2,
+						DPP_ATTR_ENROLLEE_NONCE,
+						dpp->nonce_len, dpp->e_nonce,
+						DPP_ATTR_CONFIGURATION_OBJECT,
+						json_len, json);
+	} else
+		ptr += dpp_append_wrapped_data(attrs + 2, ptr - attrs - 2,
+						NULL, 0, ptr, sizeof(attrs),
+						dpp->ke, dpp->key_len, 2,
+						DPP_ATTR_ENROLLEE_NONCE,
+						dpp->nonce_len, dpp->e_nonce);
 
 	l_put_le16(ptr - attrs - 2, attrs);
 
@@ -655,6 +670,9 @@ static void dpp_handle_config_request_frame(const struct mmpdu_header *frame,
 	_auto_(l_free) uint8_t *unwrapped = NULL;
 	uint8_t hdr_check[] = { IE_TYPE_ADVERTISEMENT_PROTOCOL, 0x08, 0x7f,
 				IE_TYPE_VENDOR_SPECIFIC, 5 };
+	struct json_iter jsiter;
+	_auto_(l_free) char *tech = NULL;
+	_auto_(l_free) char *role = NULL;
 
 	if (dpp->state != DPP_STATE_AUTHENTICATING) {
 		l_debug("Configuration request in wrong state");
@@ -747,25 +765,47 @@ static void dpp_handle_config_request_frame(const struct mmpdu_header *frame,
 		return;
 	}
 
-	/*
-	 * TODO: Full JSON type support (arrays/numbers) is not yet implemented.
-	 * Just verify the JSON is valid for now. There really isn't much need
-	 * to parse this anyhow since IWD only supports configuring stations.
-	 * If this request is for anything else it will fail regardless.
-	 */
 	c = json_contents_new(json, json_len);
 	if (!c) {
 		json_contents_free(c);
 		return;
 	}
 
+	json_iter_init(&jsiter, c);
+
+	/*
+	 * Check mandatory values (Table 7). There isn't much that can be done
+	 * with these, but the spec requires they be included.
+	 */
+	if (!json_iter_parse(&jsiter,
+			JSON_MANDATORY("name", JSON_STRING, NULL),
+			JSON_MANDATORY("wi-fi_tech", JSON_STRING, &tech),
+			JSON_MANDATORY("netRole", JSON_STRING, &role),
+			JSON_UNDEFINED))
+		goto configure_failure;
+
+	if (strcmp(tech, "infra"))
+		goto configure_failure;
+
+	if (strcmp(role, "sta"))
+		goto configure_failure;
+
 	json_contents_free(c);
 
 	memcpy(dpp->e_nonce, e_nonce, dpp->nonce_len);
 
 	dpp->state = DPP_STATE_CONFIGURING;
 
-	dpp_send_config_response(dpp);
+	dpp_send_config_response(dpp, DPP_STATUS_OK);
+
+	return;
+
+configure_failure:
+	dpp_send_config_response(dpp, DPP_STATUS_CONFIGURE_FAILURE);
+	/*
+	 * The other peer is still authenticated, and can potentially send
+	 * additional requests so keep this session alive.
+	 */
 }
 
 static void dpp_handle_config_result_frame(struct dpp_sm *dpp,
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH 2/2 dpp: speed up connection after being configured

Currently the enrollee relied on autoconnect to handle connecting to the newly configured network. This usually resulted in poor performance since periodic scans are done at large intervals apart.

Instead first check if the newly configured network is already in IWD's network queue. If so it can be connected to immediately. If not, a full scan must be done and results given to station.

---
 src/dpp.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 78 insertions(+), 12 deletions(-)
src/dpp.c +78 -12
index dedbd3b2..54947193 100644
@@ -114,6 +114,7 @@ struct dpp_sm {
 	struct l_timeout *timeout;
 
 	struct dpp_configuration *config;
+	uint32_t connect_scan_id;
 };
 
 static void dpp_free_auth_data(struct dpp_sm *dpp)
@@ -161,6 +162,11 @@ static void dpp_reset(struct dpp_sm *dpp)
 		dpp->config = NULL;
 	}
 
+	if (dpp->connect_scan_id) {
+		scan_cancel(dpp->wdev_id, dpp->connect_scan_id);
+		dpp->connect_scan_id = 0;
+	}
+
 	dpp->state = DPP_STATE_NOTHING;
 
 	explicit_bzero(dpp->r_nonce, dpp->nonce_len);
@@ -389,11 +395,14 @@ static void send_config_result(struct dpp_sm *dpp, const uint8_t *to)
 	dpp_send_frame(dpp->wdev_id, iov, 2, dpp->current_freq);
 }
 
-static void dpp_write_config(struct dpp_configuration *config)
+static void dpp_write_config(struct dpp_configuration *config,
+				struct network *network)
 {
 	_auto_(l_free) char *ssid = l_malloc(config->ssid_len + 1);
 	_auto_(l_settings_free) struct l_settings *settings = l_settings_new();
 	_auto_(l_free) char *path;
+	_auto_(l_free) uint8_t *psk = NULL;
+	size_t psk_len;
 
 	memcpy(ssid, config->ssid, config->ssid_len);
 	ssid[config->ssid_len] = '\0';
@@ -405,18 +414,57 @@ static void dpp_write_config(struct dpp_configuration *config)
 		l_settings_remove_group(settings, "Security");
 	}
 
-	if (config->passphrase)
+	if (config->passphrase) {
 		l_settings_set_string(settings, "Security", "Passphrase",
 				config->passphrase);
-	else if (config->psk)
+		if (network)
+			network_set_passphrase(network, config->passphrase);
+
+	} else if (config->psk) {
 		l_settings_set_string(settings, "Security", "PreSharedKey",
 				config->psk);
 
+		psk = l_util_from_hexstring(config->psk, &psk_len);
+
+		if (network)
+			network_set_psk(network, psk);
+	}
+
 	l_debug("Storing credential for '%s(%s)'", ssid,
 						security_to_str(SECURITY_PSK));
 	storage_network_sync(SECURITY_PSK, ssid, settings);
 }
 
+static void dpp_scan_triggered(int err, void *user_data)
+{
+	/* Not much can be done in this case */
+	if (err < 0)
+		l_error("Failed to trigger DPP scan");
+}
+
+static bool dpp_scan_results(int err, struct l_queue *bss_list,
+				const struct scan_freq_set *freqs,
+				void *userdata)
+{
+	struct dpp_sm *dpp = userdata;
+	struct station *station = station_find(netdev_get_ifindex(dpp->netdev));
+
+	if (err < 0)
+		return false;
+
+	station_set_scan_results(station, bss_list, freqs, true);
+
+	return true;
+}
+
+static void dpp_scan_destroy(void *userdata)
+{
+	struct dpp_sm *dpp = userdata;
+
+	dpp->connect_scan_id = 0;
+	dpp_reset(dpp);
+}
+
 static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 				const void *body, size_t body_len,
 				int rssi, void *user_data)
@@ -441,6 +489,10 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 	size_t wrapped_len = 0;
 	_auto_(l_free) uint8_t *unwrapped = NULL;
 	struct dpp_configuration *config;
+	struct station *station = station_find(netdev_get_ifindex(dpp->netdev));
+	struct network *network;
+	struct scan_bss *bss = NULL;
+	_auto_(l_free) char *ssid = NULL;
 
 	if (dpp->state != DPP_STATE_CONFIGURING)
 		return;
@@ -564,19 +616,33 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 		return;
 	}
 
-	dpp_write_config(config);
-	/*
-	 * TODO: Depending on the info included in the configuration object a
-	 * limited scan could be issued to get autoconnect to trigger faster.
-	 * In addition this network may already be in past scan results and
-	 * could be joined immediately.
-	 *
-	 * For now just wait for autoconnect.
-	 */
+	ssid = l_malloc(config->ssid_len + 1);
+	memcpy(ssid, config->ssid, config->ssid_len);
+	ssid[config->ssid_len] = '\0';
+
+	network = station_network_find(station, ssid, SECURITY_PSK);
+	if (network)
+		bss = network_bss_select(network, true);
+
+	dpp_write_config(config, network);
+
+	if (network && bss)
+		__station_connect_network(station, network, bss);
+	else {
+		dpp->connect_scan_id = scan_active(dpp->wdev_id, NULL, 0,
+						dpp_scan_triggered,
+						dpp_scan_results, dpp,
+						dpp_scan_destroy);
+		if (!dpp->connect_scan_id)
+			goto scan_failed;
+	}
 
 	dpp_configuration_free(config);
 
 	send_config_result(dpp, dpp->auth_addr);
+
+scan_failed:
+	dpp_reset(dpp);
 }
 
 static void dpp_send_config_response(struct dpp_sm *dpp, uint8_t status)
Signature · 1 line
2.31.1
#2 lore raw
In reply to James Prestwood

Hi James,

Quoted 11 lines · James Prestwood

With better JSON support the configuration request object can now be fully parsed. As stated in the previous comment there really isn't much use from the configurator side apart from verifying mandatory values are included.

This patch also modifies the configuration result to handle sending non 'OK' status codes in case of JSON parsing errors.

---
  src/dpp.c | 76 ++++++++++++++++++++++++++++++++++++++++++-------------
  1 file changed, 58 insertions(+), 18 deletions(-)

Both applied, thanks.

Regards,
-Denis