Linux-Bluetooth Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH BlueZ v2] mesh: Correctly generate NetKey list
@ 2020-01-09 21:54 Inga Stotland
  2020-01-14 14:43 ` Gix, Brian
  0 siblings, 1 reply; 2+ messages in thread
From: Inga Stotland @ 2020-01-09 21:54 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: brian.gix, michal.lowas-rzechonek, Inga Stotland

When responding with NetKey List Status, packed NetKey indices into
3 octets per pair. If number of NetKeys is odd, append the last key
index as a 2-octet value.
---
 mesh/net.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)

diff --git a/mesh/net.c b/mesh/net.c
index 0a4d2e72c..dfaf7ccef 100644
--- a/mesh/net.c
+++ b/mesh/net.c
@@ -1065,26 +1065,46 @@ bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
 bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *size)
 {
 	const struct l_queue_entry *entry;
-	uint16_t n, buf_size;
+	uint16_t num_keys, req_size, buf_size;
+	struct mesh_subnet *subnet;
 
 	if (!net || !buf || !size)
 		return false;
 
 	buf_size = *size;
-	if (buf_size < l_queue_length(net->subnets) * 2)
+
+	num_keys = l_queue_length(net->subnets);
+	req_size = (num_keys / 2) * 3 + (num_keys % 2) * 2;
+
+	if (buf_size < req_size)
 		return false;
 
-	n = 0;
-	entry = l_queue_get_entries(net->subnets);
+	*size = req_size;
+
+	/* Pack NetKey indices in 3 octets */
+	for (entry = l_queue_get_entries(net->subnets); num_keys > 1;) {
+		uint32_t idx_pair;
 
-	for (; entry; entry = entry->next) {
-		struct mesh_subnet *subnet = entry->data;
+		subnet = entry->data;
+		idx_pair = subnet->idx;
+		idx_pair <<= 12;
+
+		subnet = entry->next->data;
+		idx_pair += subnet->idx;
+
+		l_put_le32(idx_pair, buf);
+		buf += 3;
+
+		num_keys -= 2;
+		entry = entry->next->next;
+	}
 
+	/* If odd number of NetKeys, fill in the end of the buffer */
+	if (num_keys % 2) {
+		subnet = entry->data;
 		l_put_le16(subnet->idx, buf);
-		n += 2;
 	}
 
-	*size = n;
 	return true;
 }
 
-- 
2.21.1


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH BlueZ v2] mesh: Correctly generate NetKey list
  2020-01-09 21:54 [PATCH BlueZ v2] mesh: Correctly generate NetKey list Inga Stotland
@ 2020-01-14 14:43 ` Gix, Brian
  0 siblings, 0 replies; 2+ messages in thread
From: Gix, Brian @ 2020-01-14 14:43 UTC (permalink / raw)
  To: linux-bluetooth, Stotland, Inga; +Cc: michal.lowas-rzechonek

Applied
On Thu, 2020-01-09 at 13:54 -0800, Inga Stotland wrote:
> When responding with NetKey List Status, packed NetKey indices into
> 3 octets per pair. If number of NetKeys is odd, append the last key
> index as a 2-octet value.
> ---
>  mesh/net.c | 36 ++++++++++++++++++++++++++++--------
>  1 file changed, 28 insertions(+), 8 deletions(-)
> 
> diff --git a/mesh/net.c b/mesh/net.c
> index 0a4d2e72c..dfaf7ccef 100644
> --- a/mesh/net.c
> +++ b/mesh/net.c
> @@ -1065,26 +1065,46 @@ bool mesh_net_get_key(struct mesh_net *net, bool new_key, uint16_t idx,
>  bool mesh_net_key_list_get(struct mesh_net *net, uint8_t *buf, uint16_t *size)
>  {
>  	const struct l_queue_entry *entry;
> -	uint16_t n, buf_size;
> +	uint16_t num_keys, req_size, buf_size;
> +	struct mesh_subnet *subnet;
>  
>  	if (!net || !buf || !size)
>  		return false;
>  
>  	buf_size = *size;
> -	if (buf_size < l_queue_length(net->subnets) * 2)
> +
> +	num_keys = l_queue_length(net->subnets);
> +	req_size = (num_keys / 2) * 3 + (num_keys % 2) * 2;
> +
> +	if (buf_size < req_size)
>  		return false;
>  
> -	n = 0;
> -	entry = l_queue_get_entries(net->subnets);
> +	*size = req_size;
> +
> +	/* Pack NetKey indices in 3 octets */
> +	for (entry = l_queue_get_entries(net->subnets); num_keys > 1;) {
> +		uint32_t idx_pair;
>  
> -	for (; entry; entry = entry->next) {
> -		struct mesh_subnet *subnet = entry->data;
> +		subnet = entry->data;
> +		idx_pair = subnet->idx;
> +		idx_pair <<= 12;
> +
> +		subnet = entry->next->data;
> +		idx_pair += subnet->idx;
> +
> +		l_put_le32(idx_pair, buf);
> +		buf += 3;
> +
> +		num_keys -= 2;
> +		entry = entry->next->next;
> +	}
>  
> +	/* If odd number of NetKeys, fill in the end of the buffer */
> +	if (num_keys % 2) {
> +		subnet = entry->data;
>  		l_put_le16(subnet->idx, buf);
> -		n += 2;
>  	}
>  
> -	*size = n;
>  	return true;
>  }
>  

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-09 21:54 [PATCH BlueZ v2] mesh: Correctly generate NetKey list Inga Stotland
2020-01-14 14:43 ` Gix, Brian

Linux-Bluetooth Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-bluetooth/0 linux-bluetooth/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-bluetooth linux-bluetooth/ https://lore.kernel.org/linux-bluetooth \
		linux-bluetooth@vger.kernel.org
	public-inbox-index linux-bluetooth

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-bluetooth


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git