Skip to contentSkip to conversation

Mesh replay protection clean up

PATCH Applied Inga Stotland 2 participants 4 messages Aug 28, 2020 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

cover Mesh replay protection clean up

This patch set moves RPL initialization to the node init sequence instead of checking for every incoming message whether RPL has been initialized.

Also, use consistent size check for RPL directory/file paths.

Inga Stotland (2):
mesh: Initialize RPL when creating or loading a node
mesh: Move RPL check from model to net layer

 mesh/model.c |  18 ++---
 mesh/model.h |   6 +-
 mesh/net.c   | 198 +++++++++++++++++++++++++++------------------------
 mesh/net.h   |   5 +-
 mesh/node.c  |  11 +--
 mesh/rpl.c   |  29 +++++---
 mesh/rpl.h   |   3 +-
 7 files changed, 143 insertions(+), 127 deletions(-)
Signature · 1 line
2.26.2
#1 lore raw

PATCH 1/2 mesh: Initialize RPL when creating or loading a node

When either a new node is created or an existing node is loaded from storage, initialize RPL storage directory.

Additionally, when an existing node configguration is read from storage, load saved RPL entries into the corresponding RPL lists.

---
 mesh/net.c  | 14 +++++++-------
 mesh/net.h  |  1 +
 mesh/node.c | 11 +++++++----
 mesh/rpl.c  | 29 ++++++++++++++++++++---------
 mesh/rpl.h  |  3 ++-
 5 files changed, 37 insertions(+), 21 deletions(-)
mesh/net.c +7 -7
index 15d8f1c4b..83647d6ad 100644
@@ -633,6 +633,7 @@ struct mesh_net *mesh_net_new(struct mesh_node *node)
 	net->frnd_msgs = l_queue_new();
 	net->destinations = l_queue_new();
 	net->app_keys = l_queue_new();
+	net->replay_cache = l_queue_new();
 
 	if (!nets)
 		nets = l_queue_new();
@@ -2596,7 +2597,7 @@ static void update_iv_ivu_state(struct mesh_net *net, uint32_t iv_index,
 		mesh_config_write_iv_index(cfg, iv_index, ivu);
 
 		/* Cleanup Replay Protection List NVM */
-		rpl_init(net->node, iv_index);
+		rpl_update(net->node, iv_index);
 	}
 
 	node_property_changed(net->node, "IVIndex");
@@ -3480,12 +3481,6 @@ bool net_msg_check_replay_cache(struct mesh_net *net, uint16_t src,
 	if (!net || !net->node)
 		return true;
 
-	if (!net->replay_cache) {
-		net->replay_cache = l_queue_new();
-		rpl_init(net->node, net->iv_index);
-		rpl_get_list(net->node, net->replay_cache);
-	}
-
 	rpe = l_queue_find(net->replay_cache, match_replay_cache,
 						L_UINT_TO_PTR(src));
 
@@ -3688,3 +3683,8 @@ int mesh_net_set_heartbeat_pub(struct mesh_net *net, uint16_t dst,
 	/* TODO: Save to node config */
 	return MESH_STATUS_SUCCESS;
 }
+
+bool mesh_net_load_rpl(struct mesh_net *net)
+{
+	return rpl_get_list(net->node, net->replay_cache);
+}
mesh/net.h +1 -0
index 253185e42..725054ce6 100644
@@ -358,3 +358,4 @@ bool net_msg_check_replay_cache(struct mesh_net *net, uint16_t src,
 				uint16_t crpl, uint32_t seq, uint32_t iv_index);
 void net_msg_add_replay_cache(struct mesh_net *net, uint16_t src, uint32_t seq,
 							uint32_t iv_index);
+bool mesh_net_load_rpl(struct mesh_net *net);
mesh/node.c +7 -4
index c564bdb91..f7ac5ff67 100644
@@ -44,6 +44,7 @@
 #include "mesh/dbus.h"
 #include "mesh/agent.h"
 #include "mesh/manager.h"
+#include "mesh/rpl.h"
 #include "mesh/node.h"
 
 #define MESH_NODE_PATH_PREFIX "/node"
@@ -399,7 +400,8 @@ static bool init_storage_dir(struct mesh_node *node)
 
 	node->storage_dir = l_strdup(dir_name);
 
-	return true;
+	/* Initialize directory for storing RPL info */
+	return rpl_init(node->storage_dir);
 }
 
 static void update_net_settings(struct mesh_node *node)
@@ -470,6 +472,10 @@ static bool init_from_storage(struct mesh_config_node *db_node,
 
 	mesh_net_set_iv_index(node->net, db_node->iv_index, db_node->iv_update);
 
+	/* Initialize directory for storing keyring and RPL info */
+	if (!init_storage_dir(node) || !mesh_net_load_rpl(node->net))
+		goto fail;
+
 	if (db_node->net_transmit)
 		mesh_net_transmit_params_set(node->net,
 					db_node->net_transmit->count,
@@ -497,9 +503,6 @@ static bool init_from_storage(struct mesh_config_node *db_node,
 
 	node->cfg = cfg;
 
-	/* Initialize directory for storing keyring info */
-	init_storage_dir(node);
-
 	return true;
 fail:
 	node_remove(node);
mesh/rpl.c +20 -9
index 5a667468e..792c52ad6 100644
@@ -51,7 +51,7 @@ bool rpl_put_entry(struct mesh_node *node, uint16_t src, uint32_t iv_index,
 	DIR *dir;
 	int fd;
 
-	if (!node || !IS_UNICAST(src))
+	if (!IS_UNICAST(src))
 		return false;
 
 	node_path = node_get_storage_dir(node);
@@ -100,7 +100,7 @@ void rpl_del_entry(struct mesh_node *node, uint16_t src)
 	struct dirent *entry;
 	DIR *dir;
 
-	if (!node || !IS_UNICAST(src))
+	if (!IS_UNICAST(src))
 		return;
 
 	node_path = node_get_storage_dir(node);
@@ -205,12 +205,12 @@ bool rpl_get_list(struct mesh_node *node, struct l_queue *rpl_list)
 	size_t len;
 	DIR *dir;
 
-	if (!node || !rpl_list)
+	if (!rpl_list)
 		return false;
 
 	node_path = node_get_storage_dir(node);
 
-	len = strlen(node_path) + strlen(rpl_dir) + 14;
+	len = strlen(node_path) + strlen(rpl_dir) + 15;
 
 	if (len > PATH_MAX)
 		return false;
@@ -241,7 +241,7 @@ bool rpl_get_list(struct mesh_node *node, struct l_queue *rpl_list)
 	return true;
 }
 
-void rpl_init(struct mesh_node *node, uint32_t cur)
+void rpl_update(struct mesh_node *node, uint32_t cur)
 {
 	uint32_t old = cur - 1;
 	const char *node_path;
@@ -249,12 +249,11 @@ void rpl_init(struct mesh_node *node, uint32_t cur)
 	char path[PATH_MAX];
 	DIR *dir;
 
-	if (!node)
-		return;
-
 	node_path = node_get_storage_dir(node);
+	if (!node_path)
+		return;
 
-	if (strlen(node_path) + strlen(rpl_dir) + 10 >= PATH_MAX)
+	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
 		return;
 
 	/* Make sure path exists */
@@ -287,3 +286,15 @@ void rpl_init(struct mesh_node *node, uint32_t cur)
 
 	closedir(dir);
 }
+
+bool rpl_init(const char *node_path)
+{
+	char path[PATH_MAX];
+
+	if (strlen(node_path) + strlen(rpl_dir) + 15 >= PATH_MAX)
+		return false;
+
+	snprintf(path, PATH_MAX, "%s%s", node_path, rpl_dir);
+	mkdir(path, 0755);
+	return true;
+}
mesh/rpl.h +2 -1
index 17d2e3f05..2bbbdc9b9 100644
@@ -27,4 +27,5 @@ bool rpl_put_entry(struct mesh_node *node, uint16_t src, uint32_t iv_index,
 								uint32_t seq);
 void rpl_del_entry(struct mesh_node *node, uint16_t src);
 bool rpl_get_list(struct mesh_node *node, struct l_queue *rpl_list);
-void rpl_init(struct mesh_node *node, uint32_t iv_index);
+void rpl_update(struct mesh_node *node, uint32_t iv_index);
+bool rpl_init(const char *node_path);
Signature · 1 line
2.26.2
#2 lore raw

PATCH 2/2 mesh: Move RPL check from model to net layer

In reply to Inga Stotland

Check whether an incoming message has an RPL entry prior to handing it over to model layer for processing. If present in RPL or the RPL queue is full, ignore the incoming message.

If the incoming message is processed successfully, add the message as a new RPL entry.

---
 mesh/model.c |  18 ++---
 mesh/model.h |   6 +-
 mesh/net.c   | 184 +++++++++++++++++++++++++++------------------------
 mesh/net.h   |   4 --
 4 files changed, 106 insertions(+), 106 deletions(-)
mesh/model.c +4 -14
index 7bbc9cca7..ed2a75215 100644
@@ -862,9 +862,9 @@ static void send_msg_rcvd(struct mesh_node *node, uint8_t ele_idx,
 }
 
 bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
-			uint32_t seq, uint32_t iv_index,
-			uint16_t net_idx, uint16_t src, uint16_t dst,
-			uint8_t key_aid, const uint8_t *data, uint16_t size)
+			uint32_t iv_index, uint16_t net_idx, uint16_t src,
+			uint16_t dst, uint8_t key_aid, const uint8_t *data,
+								uint16_t size)
 {
 	uint8_t *clear_text;
 	struct mod_forward forward = {
@@ -877,7 +877,7 @@ bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
 	struct mesh_net *net = node_get_net(node);
 	uint8_t num_ele;
 	int decrypt_idx, i, ele_idx;
-	uint16_t addr, crpl;
+	uint16_t addr;
 	struct mesh_virtual *decrypt_virt = NULL;
 	bool result = false;
 	bool is_subscription;
@@ -892,12 +892,6 @@ bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
 		/* Unicast and not addressed to us */
 		return false;
 
-	/* Don't process if already in RPL */
-	crpl = node_get_crpl(node);
-
-	if (net_msg_check_replay_cache(net, src, crpl, seq, iv_index))
-		return false;
-
 	clear_text = l_malloc(size);
 	forward.data = clear_text;
 
@@ -988,10 +982,6 @@ bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
 			break;
 	}
 
-	/* If message has been handled by us, add to RPL */
-	if (result)
-		net_msg_add_replay_cache(net, src, seq, iv_index);
-
 done:
 	l_free(clear_text);
 
mesh/model.h +3 -3
index fe0648d04..2eec761a0 100644
@@ -117,9 +117,9 @@ bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst,
 int mesh_model_publish(struct mesh_node *node, uint32_t id, uint16_t src,
 			bool segmented, uint16_t len, const void *data);
 bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
-			uint32_t seq, uint32_t iv_index, uint16_t net_idx,
-			uint16_t src, uint16_t dst, uint8_t key_aid,
-			const uint8_t *data, uint16_t size);
+			uint32_t iv_index, uint16_t net_idx, uint16_t src,
+			uint16_t dst, uint8_t key_aid, const uint8_t *data,
+								uint16_t size);
 void mesh_model_app_key_delete(struct mesh_node *node, uint16_t ele_idx,
 				struct l_queue *models, uint16_t app_idx);
 uint16_t mesh_model_opcode_set(uint32_t opcode, uint8_t *buf);
mesh/net.c +99 -85

208 lines not on this page · raw message

mesh/net.h +0 -4
index 725054ce6..7bec6d0fc 100644
@@ -354,8 +354,4 @@ 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);
-bool net_msg_check_replay_cache(struct mesh_net *net, uint16_t src,
-				uint16_t crpl, uint32_t seq, uint32_t iv_index);
-void net_msg_add_replay_cache(struct mesh_net *net, uint16_t src, uint32_t seq,
-							uint32_t iv_index);
 bool mesh_net_load_rpl(struct mesh_net *net);
Signature · 1 line
2.26.2
Applied
#3 lore raw
In reply to Inga Stotland

Applied

Quoted 20 lines · Inga Stotland

This patch set moves RPL initialization to the node init sequence instead of checking for every incoming message whether RPL has been initialized.

Also, use consistent size check for RPL directory/file paths.

Inga Stotland (2):
mesh: Initialize RPL when creating or loading a node
mesh: Move RPL check from model to net layer

 mesh/model.c |  18 ++---
 mesh/model.h |   6 +-
 mesh/net.c   | 198 +++++++++++++++++++++++++++------------------------
 mesh/net.h   |   5 +-
 mesh/node.c  |  11 +--
 mesh/rpl.c   |  29 +++++---
 mesh/rpl.h   |   3 +-
 7 files changed, 143 insertions(+), 127 deletions(-)