Skip to contentSkip to conversation

mesh: Fix Friendship for multiple local nodes

PATCH In review Brian Gix 1 participant 4 messages Nov 27, 2019 – Nov 28, 2019 Bluetooth drivers

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

v2 cover mesh: Fix Friendship for multiple local nodes

Primary purpose of this patch-set is to get Friendship working again, and clean-up the logic so that it correctly works accross multiple Networks and Nodes. Currently friendship must be enabled by manually editting the appropriate node.json file, but suppport will eventually be available with a soon-to-come mesh.conf file.

v2:
Added an additional Debug Output clean-up patch that makes logging of incoming messages less noisy when multiple local nodes can decrypt the same incoming messages.

Brian Gix (2):
mesh: clean-up Recieved Message debug output
mesh: Friendship clean-up and rewrite

 mesh/crypto.c |   4 +-
 mesh/friend.c | 610 ++++++++------------------------------------------
 mesh/friend.h |   7 +-
 mesh/net.c    | 328 ++++++++++++---------------
 mesh/net.h    |  31 ++-
 mesh/node.c   |   2 +-
 6 files changed, 271 insertions(+), 711 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH v2 1/2 mesh: clean-up Recieved Message debug output

For each received packet, this change limits outputing the encrypted version once, and the decrypted version once per applicable local node.

---
 mesh/net.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)
mesh/net.c +8 -15
index 33b498135..0283a782c 100644
@@ -255,6 +255,7 @@ struct net_queue_data {
 	uint32_t key_id;
 	uint32_t iv_index;
 	uint16_t len;
+	bool seen;
 };
 
 struct net_beacon_data {
@@ -2321,22 +2322,15 @@ static enum _relay_advice packet_received(void *user_data,
 	uint8_t packet[31];
 	bool net_ctl, net_segmented, net_szmic, net_relay;
 	struct mesh_friend *net_frnd = NULL;
-	bool drop = false;
-
-	/* Tester--Drop 90% of packets */
-	/* l_getrandom(&iv_flag, 1); */
-	/* if (iv_flag%10<9) drop = true; */
-
 
 	memcpy(packet + 2, data, size);
 
-	if (!drop)
-		print_packet("RX: Network [clr] :", packet + 2, size);
-
 	net_idx = key_id_to_net_idx(net, key_id);
 	if (net_idx == NET_IDX_INVALID)
 		return RELAY_NONE;
 
+	print_packet("RX: Network [clr] :", packet + 2, size);
+
 	if (!mesh_crypto_packet_parse(packet + 2, size,
 					&net_ctl, &net_ttl,
 					&net_seq,
@@ -2377,11 +2371,6 @@ static enum _relay_advice packet_received(void *user_data,
 	if (is_us(net, net_src, true))
 		return RELAY_NONE;
 
-	if (drop) {
-		l_info("Dropping SEQ 0x%06x", net_seq);
-		return RELAY_NONE;
-	}
-
 	l_debug("check %08x", cache_cookie);
 
 	/* As a Relay, suppress repeats of last N packets that pass through */
@@ -2512,7 +2501,10 @@ static void net_rx(void *net_ptr, void *user_data)
 	if (!key_id)
 		return;
 
-	print_packet("RX: Network [enc] :", data->data, data->len);
+	if (!data->seen) {
+		data->seen = true;
+		print_packet("RX: Network [enc] :", data->data, data->len);
+	}
 
 	if (data->info) {
 		net->instant = data->info->instant;
@@ -2542,6 +2534,7 @@ static void net_msg_recv(void *user_data, struct mesh_io_recv_info *info,
 		.data = data + 1,
 		.len = len - 1,
 		.relay_advice = RELAY_NONE,
+		.seen = false,
 	};
 
 	if (len < 9)
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH v2 2/2 mesh: Friendship clean-up and rewrite

In reply to Brian Gix

Friendship support re-written such that it can now support multiple nodes (on multiple mesh networks) as friends to remote Low Power Nodes (LPNs). Validated to properly respond to Friend Requests when enabled, and a hard coded Friend Queue size of 32 (FRND_CACHE_MAX).

---
 mesh/crypto.c |   4 +-
 mesh/friend.c | 610 ++++++++------------------------------------------
 mesh/friend.h |   7 +-
 mesh/net.c    | 305 ++++++++++++-------------
 mesh/net.h    |  31 ++-
 mesh/node.c   |   2 +-
 6 files changed, 263 insertions(+), 696 deletions(-)
mesh/crypto.c +2 -2
index 15ee6739d..8ea906ac9 100644
@@ -699,7 +699,7 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 	uint16_t this_dst;
 	bool is_segmented;
 
-	if (packet_len < 14)
+	if (packet_len < 10)
 		return false;
 
 	this_dst = l_get_be16(packet + 7);
@@ -730,7 +730,7 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 		uint8_t this_opcode = packet[9] & OPCODE_MASK;
 
 		if (cookie)
-			*cookie = l_get_be32(packet + 9);
+			*cookie = l_get_be32(packet + 2) ^ packet[6];
 
 		if (opcode)
 			*opcode = this_opcode;
mesh/friend.c +92 -518

841 lines not on this page · raw message

mesh/friend.h +4 -3
index 7ab5dea2e..2351dee31 100644
@@ -28,9 +28,10 @@
 
 void friend_poll(struct mesh_net *net, uint16_t src, bool seq,
 						struct mesh_friend *frnd);
-void friend_request(struct mesh_net *net, uint16_t src, uint8_t minReq,
-			uint8_t delay, uint32_t timeout, uint16_t prev,
-			uint8_t num_elements, uint16_t cntr, int8_t rssi);
+void friend_request(struct mesh_net *net, uint16_t net_idx, uint16_t src,
+			uint8_t minReq, uint8_t delay, uint32_t timeout,
+			uint16_t prev, uint8_t num_elements, uint16_t cntr,
+			int8_t rssi);
 void friend_clear_confirm(struct mesh_net *net, uint16_t src, uint16_t lpn,
 							uint16_t lpnCounter);
 void friend_clear(struct mesh_net *net, uint16_t src, uint16_t lpn,
mesh/net.c +140 -165

657 lines not on this page · raw message

mesh/net.h +24 -7
index 150240ff8..fe4e0b653 100644
@@ -162,25 +162,40 @@ struct mesh_key_set {
 	uint8_t privacy_key[16];
 };
 
+struct friend_neg {
+	int8_t rssi;
+	bool clearing;
+};
+
+struct friend_act {
+	uint16_t *grp_list;
+	uint32_t last_hdr;
+	int16_t grp_cnt;
+	bool seq;
+	bool last;
+};
+
 struct mesh_friend {
 	struct mesh_net *net;
-	struct l_queue *pkt_cache;
 	struct l_timeout *timeout;
+	struct l_queue *pkt_cache;
 	void *pkt;
-	uint16_t *grp_list;
 	uint32_t poll_timeout;
-	uint32_t last_hdr;
 	uint32_t net_key_cur;
 	uint32_t net_key_upd;
-	uint16_t dst; /* Primary Element unicast addr */
+	uint16_t old_friend;
+	uint16_t net_idx;
+	uint16_t lp_addr;/* dst; * Primary Element unicast addr */
 	uint16_t fn_cnt;
 	uint16_t lp_cnt;
-	int16_t grp_cnt;
+	uint8_t	receive_delay;
 	uint8_t ele_cnt;
 	uint8_t frd;
 	uint8_t frw;
-	bool seq;
-	bool last;
+	union {
+		struct friend_neg negotiate;
+		struct friend_act active;
+	} u;
 };
 
 struct mesh_frnd_pkt {
@@ -360,3 +375,5 @@ void mesh_net_transmit_params_get(struct mesh_net *net, uint8_t *count,
 struct mesh_prov *mesh_net_get_prov(struct mesh_net *net);
 void mesh_net_set_prov(struct mesh_net *net, struct mesh_prov *prov);
 uint32_t mesh_net_get_instant(struct mesh_net *net);
+struct l_queue *mesh_net_get_friends(struct mesh_net *net);
+struct l_queue *mesh_net_get_negotiations(struct mesh_net *net);
mesh/node.c +1 -1
index b52b9e437..7b4ee0505 100644
@@ -531,7 +531,7 @@ static bool init_from_storage(struct mesh_config_node *db_node,
 	if (mode == MESH_MODE_ENABLED || mode == MESH_MODE_DISABLED)
 		mesh_net_set_proxy_mode(node->net, mode == MESH_MODE_ENABLED);
 
-	mode = node->lpn;
+	mode = node->friend;
 	if (mode == MESH_MODE_ENABLED || mode == MESH_MODE_DISABLED)
 		mesh_net_set_friend_mode(node->net, mode == MESH_MODE_ENABLED);
 
Signature · 1 line
2.21.0
author
#3 lore raw
In reply to Brian Gix

Patchset applied

Quoted 28 lines · Brian

Primary purpose of this patch-set is to get Friendship working again, and clean-up the logic so that it correctly works accross multiple Networks and Nodes. Currently friendship must be enabled by manually editting the appropriate node.json file, but suppport will eventually be available with a soon-to-come mesh.conf file.

v2:
Added an additional Debug Output clean-up patch that makes logging of incoming messages less noisy when multiple local nodes can decrypt the same incoming messages.

Brian Gix (2):
mesh: clean-up Recieved Message debug output
mesh: Friendship clean-up and rewrite

mesh/crypto.c |   4 +-
mesh/friend.c | 610 ++++++++------------------------------------------
mesh/friend.h |   7 +-
mesh/net.c    | 328 ++++++++++++---------------
mesh/net.h    |  31 ++-
mesh/node.c   |   2 +-
6 files changed, 271 insertions(+), 711 deletions(-)
Signature · 1 line
2.21.0