mesh: Initialize RPL when creating or loading a node
authorInga Stotland <inga.stotland@intel.com>
Fri, 28 Aug 2020 20:04:26 +0000 (13:04 -0700)
committerAbhay Agarwal <ay.agarwal@samsung.com>
Mon, 28 Dec 2020 06:20:04 +0000 (11:50 +0530)
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.

Change-Id: Ia9b2cb55e1170dcb149df59fccb93b917d3e060b
Signed-off-by: anuj.bhumiya <anuj.bhumiya@samsung.com>
mesh/net.c
mesh/net.h
mesh/node.c
mesh/rpl.c
mesh/rpl.h

index cecf0c8..6646c2a 100644 (file)
@@ -631,6 +631,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();
@@ -2594,7 +2595,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");
@@ -3478,12 +3479,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));
 
@@ -3686,3 +3681,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);
+}
index 253185e..725054c 100644 (file)
@@ -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);
index 1d1790c..d104bd0 100644 (file)
@@ -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"
@@ -415,7 +416,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)
@@ -486,6 +488,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,
@@ -513,9 +519,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);
index ad43a29..b58a692 100644 (file)
@@ -54,7 +54,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);
@@ -103,7 +103,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);
@@ -208,12 +208,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;
@@ -244,7 +244,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;
@@ -252,12 +252,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 */
@@ -290,3 +289,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;
+}
index 17d2e3f..2bbbdc9 100644 (file)
@@ -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);