Skip to contentSkip to conversation

dpp: don't allocate transient ssid strings

PATCH In review James Prestwood 2 participants 7 messages Jan 8, 2022 – Jan 10, 2022

lore
Share & tools
lore

James Prestwood

author
#0 lore raw

PATCH v2 1/6 dpp: don't allocate transient ssid strings

These can be kept on the stack and avoid the need for allocated memory and unneeded auto-free functionality.

---
 src/dpp.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
src/dpp.c +2 -3
index 20c27a57..61c6b36d 100644
@@ -400,7 +400,7 @@ static void send_config_result(struct dpp_sm *dpp, const uint8_t *to)
 static void dpp_write_config(struct dpp_configuration *config,
 				struct network *network)
 {
-	_auto_(l_free) char *ssid = l_malloc(config->ssid_len + 1);
+	char ssid[33];
 	_auto_(l_settings_free) struct l_settings *settings = l_settings_new();
 	_auto_(l_free) char *path;
 	_auto_(l_free) uint8_t *psk = NULL;
@@ -494,7 +494,7 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 	struct station *station = station_find(netdev_get_ifindex(dpp->netdev));
 	struct network *network;
 	struct scan_bss *bss = NULL;
-	_auto_(l_free) char *ssid = NULL;
+	char ssid[33];
 
 	if (dpp->state != DPP_STATE_CONFIGURING)
 		return;
@@ -618,7 +618,6 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 		return;
 	}
 
-	ssid = l_malloc(config->ssid_len + 1);
 	memcpy(ssid, config->ssid, config->ssid_len);
 	ssid[config->ssid_len] = '\0';
 
Signature · 1 line
2.31.1
author
#1 lore raw

PATCH v2 2/6 dpp: don't allow StartEnrollee while connected

While connected the driver ends up choosing quite small ROC durations leading to excessive calls to ROC. This also will negatively effect any wireless performance for the current network and possibly lead to missed DPP frames.

---
 src/dpp.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
src/dpp.c +11 -0
index 61c6b36d..de897266 100644
@@ -1638,10 +1638,21 @@ static struct l_dbus_message *dpp_dbus_start_enrollee(struct l_dbus *dbus,
 	struct dpp_sm *dpp = user_data;
 	uint32_t freq = band_channel_to_freq(6, BAND_FREQ_2_4_GHZ);
 	struct l_dbus_message *reply;
+	struct station *station = station_find(netdev_get_ifindex(dpp->netdev));
 
 	if (dpp->state != DPP_STATE_NOTHING)
 		return dbus_error_busy(message);
 
+	/*
+	 * Station isn't actually required for DPP itself, although this will
+	 * prevent connecting to the network once configured.
+	 */
+	if (station && station_get_connected_network(station)) {
+		l_warn("cannot be enrollee while connected, please disconnect");
+		return dbus_error_busy(message);
+	} else
+		l_debug("No station device, continuing anyways...");
+
 	dpp->uri = dpp_generate_uri(dpp->pub_asn1, dpp->pub_asn1_len, 2,
 					netdev_get_address(dpp->netdev), &freq,
 					1, NULL, NULL);
Signature · 1 line
2.31.1
author
#2 lore raw

PATCH v2 3/6 dpp: move r_auth into dpp_sm

In reply to James Prestwood

In order to support channel switching during authentication r_auth needs to be held onto in dpp_sm for after the ROC call starts.

---
 src/dpp.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
src/dpp.c +6 -5
index de897266..86178b53 100644
@@ -102,6 +102,7 @@ struct dpp_sm {
 
 	uint64_t ke[L_ECC_MAX_DIGITS];
 	uint64_t k2[L_ECC_MAX_DIGITS];
+	uint64_t r_auth[L_ECC_MAX_DIGITS];
 
 	struct l_ecc_scalar *proto_private;
 	struct l_ecc_point *proto_public;
@@ -174,6 +175,7 @@ static void dpp_reset(struct dpp_sm *dpp)
 	explicit_bzero(dpp->e_nonce, dpp->nonce_len);
 	explicit_bzero(dpp->ke, dpp->key_len);
 	explicit_bzero(dpp->k2, dpp->key_len);
+	explicit_bzero(dpp->r_auth, dpp->key_len);
 
 	dpp_free_auth_data(dpp);
 }
@@ -967,7 +969,7 @@ static void dpp_handle_config_result_frame(struct dpp_sm *dpp,
  * ad1 = attrs
  * ad1_len = ptr - attrs
  */
-static void send_authenticate_response(struct dpp_sm *dpp, void *r_auth)
+static void send_authenticate_response(struct dpp_sm *dpp)
 {
 	uint8_t hdr[32];
 	uint8_t attrs[256];
@@ -998,7 +1000,7 @@ static void send_authenticate_response(struct dpp_sm *dpp, void *r_auth)
 	/* Wrap up secondary data (R-Auth) */
 	wrapped2_len = dpp_append_attr(wrapped2_plaintext,
 					DPP_ATTR_RESPONDER_AUTH_TAG,
-					r_auth, dpp->key_len);
+					dpp->r_auth, dpp->key_len);
 	/*
 	 * "Invocations of AES-SIV in the DPP Authentication protocol that
 	 * produce ciphertext that is part of an additional AES-SIV invocation
@@ -1222,7 +1224,6 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *m = NULL;
 	_auto_(l_ecc_scalar_free) struct l_ecc_scalar *n = NULL;
 	uint64_t k1[L_ECC_MAX_DIGITS];
-	uint64_t r_auth[L_ECC_MAX_DIGITS];
 	const void *ad0 = body + 2;
 	const void *ad1 = body + 8;
 
@@ -1363,7 +1364,7 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 
 	if (!dpp_derive_r_auth(dpp->i_nonce, dpp->r_nonce, dpp->nonce_len,
 				dpp->i_proto_public, dpp->proto_public,
-				dpp->boot_public, r_auth))
+				dpp->boot_public, dpp->r_auth))
 		goto auth_request_failed;
 
 	memcpy(dpp->auth_addr, from, 6);
@@ -1371,7 +1372,7 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 	dpp->state = DPP_STATE_AUTHENTICATING;
 	dpp_reset_protocol_timer(dpp);
 
-	send_authenticate_response(dpp, r_auth);
+	send_authenticate_response(dpp);
 
 	return;
 
Signature · 1 line
2.31.1
author
#3 lore raw

PATCH v2 4/6 dpp: refactor calls to offchannel_start into common function

In reply to James Prestwood

This will aid in channel switching during authentication by allowing an arbitrary channel to be passed in rather than dpp->current_freq.

---
 src/dpp.c | 203 ++++++++++++++++++++++++++++--------------------------
 1 file changed, 107 insertions(+), 96 deletions(-)
src/dpp.c +107 -96
index 86178b53..5bf004d8 100644
@@ -1205,6 +1205,112 @@ static bool dpp_check_roles(struct dpp_sm *dpp, uint8_t peer_capa)
 	return true;
 }
 
+static void dpp_presence_announce(struct dpp_sm *dpp)
+{
+	struct netdev *netdev = dpp->netdev;
+	uint8_t hdr[32];
+	uint8_t attrs[32 + 4];
+	uint8_t hash[32];
+	uint8_t *ptr = attrs;
+	const uint8_t *addr = netdev_get_address(netdev);
+	struct iovec iov[2];
+
+	iov[0].iov_len = dpp_build_header(addr, broadcast,
+					DPP_FRAME_PRESENCE_ANNOUNCEMENT, hdr);
+	iov[0].iov_base = hdr;
+
+	dpp_hash(L_CHECKSUM_SHA256, hash, 2, "chirp", strlen("chirp"),
+			dpp->pub_asn1, dpp->pub_asn1_len);
+
+	ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, hash, 32);
+
+	iov[1].iov_base = attrs;
+	iov[1].iov_len = ptr - attrs;
+
+	l_debug("Sending presense annoucement on frequency %u and waiting %u",
+		dpp->current_freq, dpp->dwell);
+
+	dpp_send_frame(netdev_get_wdev_id(netdev), iov, 2, dpp->current_freq);
+}
+
+static void dpp_roc_started(void *user_data)
+{
+	struct dpp_sm *dpp = user_data;
+
+	/*
+	 * If not in presence procedure or in a configurator role, just stay
+	 * on channel.
+	 */
+	if (dpp->state != DPP_STATE_PRESENCE ||
+			dpp->role == DPP_CAPABILITY_CONFIGURATOR)
+		return;
+
+	dpp_presence_announce(dpp);
+}
+
+static void dpp_start_offchannel(struct dpp_sm *dpp, uint32_t freq);
+
+static void dpp_presence_timeout(int error, void *user_data)
+{
+	struct dpp_sm *dpp = user_data;
+
+	dpp->offchannel_id = 0;
+
+	/*
+	 * If cancelled this is likely due to netdev going down or from Stop().
+	 * Otherwise there was some other problem which is probably not
+	 * recoverable.
+	 */
+	if (error == -ECANCELED)
+		return;
+	else if (error == -EIO)
+		goto next_roc;
+	else if (error < 0)
+		goto protocol_failed;
+
+	switch (dpp->state) {
+	case DPP_STATE_PRESENCE:
+		break;
+	case DPP_STATE_NOTHING:
+		/* Protocol already terminated */
+		return;
+	case DPP_STATE_AUTHENTICATING:
+	case DPP_STATE_CONFIGURING:
+		goto next_roc;
+	}
+
+	dpp->freqs_idx++;
+
+	if (dpp->freqs_idx >= dpp->freqs_len) {
+		l_debug("Max retries on presence announcements");
+		dpp->freqs_idx = 0;
+	}
+
+	dpp->current_freq = dpp->freqs[dpp->freqs_idx];
+
+	l_debug("Presence timeout, moving to next frequency %u, duration %u",
+			dpp->current_freq, dpp->dwell);
+
+next_roc:
+	dpp_start_offchannel(dpp, dpp->current_freq);
+
+	return;
+
+protocol_failed:
+	dpp_reset(dpp);
+	return;
+}
+
+static void dpp_start_offchannel(struct dpp_sm *dpp, uint32_t freq)
+{
+	if (dpp->offchannel_id)
+		offchannel_cancel(dpp->wdev_id, dpp->offchannel_id);
+
+	dpp->offchannel_id = offchannel_start(netdev_get_wdev_id(dpp->netdev),
+				freq, dpp->dwell, dpp_roc_started,
+				dpp, dpp_presence_timeout);
+}
+
 static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 					const uint8_t *body, size_t body_len)
 {
@@ -1415,49 +1521,6 @@ static void dpp_handle_frame(const struct mmpdu_header *frame,
 	}
 }
 
-static void dpp_presence_announce(struct dpp_sm *dpp)
-{
-	struct netdev *netdev = dpp->netdev;
-	uint8_t hdr[32];
-	uint8_t attrs[32 + 4];
-	uint8_t hash[32];
-	uint8_t *ptr = attrs;
-	const uint8_t *addr = netdev_get_address(netdev);
-	struct iovec iov[2];
-
-	iov[0].iov_len = dpp_build_header(addr, broadcast,
-					DPP_FRAME_PRESENCE_ANNOUNCEMENT, hdr);
-	iov[0].iov_base = hdr;
-
-	dpp_hash(L_CHECKSUM_SHA256, hash, 2, "chirp", strlen("chirp"),
-			dpp->pub_asn1, dpp->pub_asn1_len);
-
-	ptr += dpp_append_attr(ptr, DPP_ATTR_RESPONDER_BOOT_KEY_HASH, hash, 32);
-
-	iov[1].iov_base = attrs;
-	iov[1].iov_len = ptr - attrs;
-
-	l_debug("Sending presense annoucement on frequency %u and waiting %u",
-		dpp->current_freq, dpp->dwell);
-
-	dpp_send_frame(netdev_get_wdev_id(netdev), iov, 2, dpp->current_freq);
-}
-
-static void dpp_roc_started(void *user_data)
-{
-	struct dpp_sm *dpp = user_data;
-
-	/*
-	 * If not in presence procedure or in a configurator role, just stay
-	 * on channel.
-	 */
-	if (dpp->state != DPP_STATE_PRESENCE ||
-			dpp->role == DPP_CAPABILITY_CONFIGURATOR)
-		return;
-
-	dpp_presence_announce(dpp);
-}
-
 static void dpp_create(struct netdev *netdev)
 {
 	struct l_dbus *dbus = dbus_get_bus();
@@ -1518,56 +1581,6 @@ static void dpp_netdev_watch(struct netdev *netdev,
 	}
 }
 
-static void dpp_presence_timeout(int error, void *user_data)
-{
-	struct dpp_sm *dpp = user_data;
-
-	/*
-	 * If cancelled this is likely due to netdev going down or from Stop().
-	 * Otherwise there was some other problem which is probably not
-	 * recoverable.
-	 */
-	if (error == -ECANCELED)
-		return;
-	else if (error == -EIO)
-		goto next_roc;
-	else if (error < 0)
-		goto protocol_failed;
-
-	switch (dpp->state) {
-	case DPP_STATE_PRESENCE:
-		break;
-	case DPP_STATE_NOTHING:
-		/* Protocol already terminated */
-		return;
-	case DPP_STATE_AUTHENTICATING:
-	case DPP_STATE_CONFIGURING:
-		goto next_roc;
-	}
-
-	dpp->freqs_idx++;
-
-	if (dpp->freqs_idx >= dpp->freqs_len) {
-		l_debug("Max retries on presence announcements");
-		dpp->freqs_idx = 0;
-	}
-
-	dpp->current_freq = dpp->freqs[dpp->freqs_idx];
-
-	l_debug("Presence timeout, moving to next frequency %u, duration %u",
-			dpp->current_freq, dpp->dwell);
-
-next_roc:
-	dpp->offchannel_id = offchannel_start(netdev_get_wdev_id(dpp->netdev),
-			dpp->current_freq, dpp->dwell, dpp_roc_started,
-			dpp, dpp_presence_timeout);
-	return;
-
-protocol_failed:
-	dpp_reset(dpp);
-	return;
-}
-
 /*
  * EasyConnect 2.0 - 6.2.2
  */
@@ -1627,9 +1640,7 @@ static void dpp_start_presence(struct dpp_sm *dpp, uint32_t *limit_freqs,
 	dpp->freqs_idx = 0;
 	dpp->current_freq = dpp->freqs[0];
 
-	dpp->offchannel_id = offchannel_start(netdev_get_wdev_id(dpp->netdev),
-			dpp->current_freq, dpp->dwell, dpp_roc_started,
-			dpp, dpp_presence_timeout);
+	dpp_start_offchannel(dpp, dpp->current_freq);
 }
 
 static struct l_dbus_message *dpp_dbus_start_enrollee(struct l_dbus *dbus,
Signature · 1 line
2.31.1
author
#4 lore raw

PATCH v2 5/6 dpp: handle CHANNEL attribute in auth request

In reply to James Prestwood

When this attribute is included, the initiator is requesting all future frames be sent on this channel. There is no reason for a configurator to act on this attribute (at least for now) so the request frame will be dropped in this case. Enrollees will act on it by switching to the new channel and sending the authentication response.

---
 src/dpp.c | 77 +++++++++++++++++++++++++++++++++++++++++--------------
 1 file changed, 58 insertions(+), 19 deletions(-)
src/dpp.c +58 -19
index 5bf004d8..35807a8c 100644
@@ -91,6 +91,7 @@ struct dpp_sm {
 	size_t freqs_idx;
 	uint32_t dwell;
 	uint32_t current_freq;
+	uint32_t new_freq;
 	struct scan_freq_set *presence_list;
 
 	uint32_t offchannel_id;
@@ -169,6 +170,7 @@ static void dpp_reset(struct dpp_sm *dpp)
 	}
 
 	dpp->state = DPP_STATE_NOTHING;
+	dpp->new_freq = 0;
 
 	explicit_bzero(dpp->r_nonce, dpp->nonce_len);
 	explicit_bzero(dpp->i_nonce, dpp->nonce_len);
@@ -1238,14 +1240,29 @@ static void dpp_roc_started(void *user_data)
 	struct dpp_sm *dpp = user_data;
 
 	/*
-	 * If not in presence procedure or in a configurator role, just stay
-	 * on channel.
+	 * - If a configurator, nothing to do but wait for a request
+	 * - If in the presence state continue sending announcements.
+	 * - If authenticating, and this is a result of a channel switch send
+	 *   the authenticate response now.
 	 */
-	if (dpp->state != DPP_STATE_PRESENCE ||
-			dpp->role == DPP_CAPABILITY_CONFIGURATOR)
+	if (dpp->role == DPP_CAPABILITY_CONFIGURATOR)
 		return;
 
-	dpp_presence_announce(dpp);
+	switch (dpp->state) {
+	case DPP_STATE_PRESENCE:
+		dpp_presence_announce(dpp);
+		break;
+	case DPP_STATE_AUTHENTICATING:
+		if (dpp->new_freq) {
+			dpp->current_freq = dpp->new_freq;
+			dpp->new_freq = 0;
+			send_authenticate_response(dpp);
+		}
+
+		break;
+	default:
+		break;
+	}
 }
 
 static void dpp_start_offchannel(struct dpp_sm *dpp, uint32_t freq);
@@ -1332,6 +1349,7 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 	uint64_t k1[L_ECC_MAX_DIGITS];
 	const void *ad0 = body + 2;
 	const void *ad1 = body + 8;
+	uint32_t freq;
 
 	if (dpp->state != DPP_STATE_PRESENCE)
 		return;
@@ -1371,20 +1389,39 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 			}
 
 			break;
-		/*
-		 * TODO: Go on this channel for remainder of auth protocol.
-		 *
-		 * "the Responder determines whether it can use the requested
-		 * channel for the following exchanges. If so, it sends the DPP
-		 * Authentication Response frame on that channel. If not, it
-		 * discards the DPP Authentication Request frame without
-		 * replying to it."
-		 *
-		 * For the time being this feature is not being implemented and
-		 * the frame will be dropped.
-		 */
+
 		case DPP_ATTR_CHANNEL:
-			return;
+			if (len != 2)
+				return;
+
+			freq = oci_to_frequency(l_get_u8(data),
+						l_get_u8(data + 1));
+
+			if (freq == dpp->current_freq)
+				break;
+
+			/*
+			 * Configurators are already connected to a network, so
+			 * to preserve wireless performance the enrollee will
+			 * be required to be on this channel, not a channel it
+			 * requests.
+			 */
+			if (dpp->role == DPP_CAPABILITY_CONFIGURATOR)
+				return;
+
+			/*
+			 * Otherwise, as an enrollee, we can jump to whatever
+			 * channel the configurator requests
+			 */
+			dpp->new_freq = freq;
+
+			l_debug("Configurator requested a new frequency %u",
+					dpp->new_freq);
+
+			offchannel_cancel(dpp->wdev_id, dpp->offchannel_id);
+			dpp_start_offchannel(dpp, dpp->new_freq);
+
+			break;
 		default:
 			break;
 		}
@@ -1478,7 +1515,9 @@ static void authenticate_request(struct dpp_sm *dpp, const uint8_t *from,
 	dpp->state = DPP_STATE_AUTHENTICATING;
 	dpp_reset_protocol_timer(dpp);
 
-	send_authenticate_response(dpp);
+	/* Don't send if the frequency is changing */
+	if (!dpp->new_freq)
+		send_authenticate_response(dpp);
 
 	return;
 
Signature · 1 line
2.31.1
author
#5 lore raw

PATCH v2 6/6 dpp: allow config response handling without station

In reply to James Prestwood

If the device is not in station mode DPP can still write out the credentials and finish without attempting to connect or scan.

---
 src/dpp.c | 20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)
src/dpp.c +13 -7
index 35807a8c..84e89f6c 100644
@@ -496,7 +496,7 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 	_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 network *network = NULL;
 	struct scan_bss *bss = NULL;
 	char ssid[33];
 
@@ -622,18 +622,24 @@ static void dpp_handle_config_response_frame(const struct mmpdu_header *frame,
 		return;
 	}
 
-	memcpy(ssid, config->ssid, config->ssid_len);
-	ssid[config->ssid_len] = '\0';
+	/*
+	 * We should have a station device, but if not DPP can write the
+	 * credentials out and be done
+	 */
+	if (station) {
+		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);
+		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 {
+	else if (station) {
 		dpp->connect_scan_id = scan_active(dpp->wdev_id, NULL, 0,
 						dpp_scan_triggered,
 						dpp_scan_results, dpp,
Signature · 1 line
2.31.1
#6 lore raw
In reply to James Prestwood

Hi James,

Quoted 6 lines · James Prestwood

These can be kept on the stack and avoid the need for allocated memory and unneeded auto-free functionality.

---
  src/dpp.c | 5 ++---
  1 file changed, 2 insertions(+), 3 deletions(-)

All applied, thanks.

Regards,
-Denis