mesh: Save key refresh phase state to node config file
authorInga Stotland <inga.stotland@intel.com>
Thu, 7 Feb 2019 03:55:36 +0000 (19:55 -0800)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 14:14:25 +0000 (19:44 +0530)
This adds implementation for saving the key refresh phase to
a node configuration file in JSON format. When the key refresh
procedure is finished, the old network keys are remove from the
configuration file.

Change-Id: I33341489ae538cf9734c0a68025865d53f49548f
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/mesh-db.c
mesh/mesh-db.h
mesh/net.c
mesh/storage.c
mesh/storage.h

index 49e1e03..894b02f 100644 (file)
@@ -1490,3 +1490,59 @@ bool mesh_db_add_node(json_object *jnode, struct mesh_db_node *node) {
 
        return true;
 }
+
+static void finish_key_refresh(json_object *jobj, uint16_t net_idx)
+{
+       json_object *jarray;
+       int i, len;
+
+       /* Clean up all the bound appkeys */
+       json_object_object_get_ex(jobj, "appKeys", &jarray);
+       if (!jarray)
+               return;
+
+       len = json_object_array_length(jarray);
+
+       for (i = 0; i < len; ++i) {
+               json_object *jentry;
+               uint16_t idx;
+
+               jentry = json_object_array_get_idx(jarray, i);
+
+               if (!get_key_index(jentry, "boundNetKey", &idx))
+                       continue;
+
+               if (idx != net_idx)
+                       continue;
+
+               json_object_object_del(jentry, "oldKey");
+
+               if (!get_key_index(jentry, "index", &idx))
+                       continue;
+       }
+
+}
+
+bool mesh_db_net_key_set_phase(json_object *jobj, uint16_t idx, uint8_t phase)
+{
+       json_object *jarray, *jentry = NULL;
+
+       json_object_object_get_ex(jobj, "netKeys", &jarray);
+
+       if (jarray)
+               jentry = get_key_object(jarray, idx);
+
+       if (!jentry)
+               return false;
+
+       json_object_object_del(jentry, "keyRefresh");
+       json_object_object_add(jentry, "keyRefresh",
+                                       json_object_new_int(phase));
+
+       if (phase == KEY_REFRESH_PHASE_NONE) {
+               json_object_object_del(jentry, "oldKey");
+               finish_key_refresh(jobj, idx);
+       }
+
+       return true;
+}
index 40e60f7..db7ea60 100644 (file)
@@ -135,7 +135,7 @@ bool mesh_db_app_key_del(json_object *jobj, uint16_t net_idx, uint16_t idx);
 bool mesh_db_net_key_add(json_object *jobj, uint16_t net_idx,
                                        const uint8_t key[16], int phase);
 bool mesh_db_net_key_del(json_object *jobj, uint16_t net_idx);
-bool mesh_db_write_kr_phase(json_object *jobj, uint16_t net_idx, int phase);
+bool mesh_db_net_key_set_phase(json_object *jobj, uint16_t idx, uint8_t phase);
 bool mesh_db_write_address(json_object *jobj, uint16_t address);
 bool mesh_db_write_iv_index(json_object *jobj, uint32_t idx, bool update);
 void mesh_db_remove_property(json_object *jobj, const char *desc);
index f28fb61..9a4bed2 100644 (file)
@@ -2655,6 +2655,8 @@ static int key_refresh_phase_two(struct mesh_net *net, uint16_t idx)
        else
                l_queue_foreach(net->friends, frnd_kr_phase2, net);
 
+       storage_set_key_refresh_phase(net, idx, KEY_REFRESH_PHASE_TWO);
+
        return MESH_STATUS_SUCCESS;
 }
 
@@ -2688,6 +2690,8 @@ static int key_refresh_finish(struct mesh_net *net, uint16_t idx)
        else
                l_queue_foreach(net->friends, frnd_kr_phase3, net);
 
+       storage_set_key_refresh_phase(net, idx, KEY_REFRESH_PHASE_NONE);
+
        return MESH_STATUS_SUCCESS;
 }
 
index e866faf..6dd8b5f 100644 (file)
@@ -320,6 +320,15 @@ bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
        return mesh_db_write_iv_index(jnode, iv_index, update);
 }
 
+bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
+                                                               uint8_t phase)
+{
+       struct mesh_node *node = mesh_net_node_get(net);
+       json_object *jnode = node_jconfig_get(node);
+
+       return mesh_db_net_key_set_phase(jnode, net_idx, phase);
+}
+
 bool storage_write_sequence_number(struct mesh_net *net, uint32_t seq)
 {
        struct mesh_node *node = mesh_net_node_get(net);
index 91299f0..7dad276 100644 (file)
@@ -47,3 +47,5 @@ bool storage_set_iv_index(struct mesh_net *net, uint32_t iv_index,
                                                                bool update);
 bool storage_set_device_key(struct mesh_node *node, uint8_t dev_key[16]);
 bool storage_set_unicast(struct mesh_node *node, uint16_t unicast);
+bool storage_set_key_refresh_phase(struct mesh_net *net, uint16_t net_idx,
+                                                               uint8_t phase);