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(-) diff --git a/mesh/net.c b/mesh/net.c index 15d8f1c4b..83647d6ad 100644 --- a/mesh/net.c +++ b/mesh/net.c @@ -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); +} diff --git a/mesh/net.h b/mesh/net.h index 253185e42..725054ce6 100644 --- a/mesh/net.h +++ b/mesh/net.h @@ -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); diff --git a/mesh/node.c b/mesh/node.c index c564bdb91..f7ac5ff67 100644 --- a/mesh/node.c +++ b/mesh/node.c @@ -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); diff --git a/mesh/rpl.c b/mesh/rpl.c index 5a667468e..792c52ad6 100644 --- a/mesh/rpl.c +++ b/mesh/rpl.c @@ -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; +} diff --git a/mesh/rpl.h b/mesh/rpl.h index 17d2e3f05..2bbbdc9b9 100644 --- a/mesh/rpl.h +++ b/mesh/rpl.h @@ -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); -- 2.26.2