mesh: Separate functions for net key add and update
authorInga Stotland <inga.stotland@intel.com>
Thu, 7 Feb 2019 03:55:33 +0000 (19:55 -0800)
committerAnupam Roy <anupam.r@samsung.com>
Tue, 17 Dec 2019 14:14:12 +0000 (19:44 +0530)
This splits mesh_net_key_add() into two separate functions:
mesh_net_key_add() and mesh_net_key_update().
mesh_net_key_update() essentially replaces mesh_net_kr_phase_one()
since switching to Key Refresh phase one can only be triggered
by successful network key update.

Change-Id: I9be2f9784ebb094906107bd8923694199e0900f3
Signed-off-by: Anupam Roy <anupam.r@samsung.com>
mesh/cfgmod-server.c
mesh/net.c
mesh/net.h
mesh/node.c
mesh/storage.c

index 062bdaa..899bdde 100644 (file)
@@ -981,8 +981,12 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
                if (size != 18)
                        return true;
 
-               b_res = mesh_net_add_key(net, opcode == OP_NETKEY_UPDATE,
-                                               l_get_le16(pkt), pkt + 2);
+               net_idx = l_get_le16(pkt);
+
+               if (opcode == OP_NETKEY_ADD)
+                       b_res = mesh_net_add_key(net, net_idx, pkt + 2);
+               else
+                       b_res = mesh_net_update_key(net, net_idx, pkt + 2);
 
                l_debug("NetKey Add/Update %s",
                        (b_res == MESH_STATUS_SUCCESS) ? "success" : "fail");
index 44e0abb..f939a45 100644 (file)
@@ -969,27 +969,13 @@ int mesh_net_del_key(struct mesh_net *net, uint16_t idx)
        return MESH_STATUS_SUCCESS;
 }
 
-int mesh_net_add_key(struct mesh_net *net, bool update, uint16_t idx,
-                                                       const void *value)
+int mesh_net_add_key(struct mesh_net *net, uint16_t idx, const uint8_t *value)
 {
-       int status;
        struct mesh_subnet *subnet;
 
        subnet = l_queue_find(net->subnets, match_key_index,
                                                        L_UINT_TO_PTR(idx));
 
-       if (update) {
-               if (subnet && subnet->kr_phase == KEY_REFRESH_PHASE_NONE) {
-                       l_info("Start key refresh");
-                       status = mesh_net_kr_phase_one(net, idx, value);
-                       if (status == MESH_STATUS_SUCCESS &&
-                               !storage_net_key_add(net, idx,
-                                               value, KEY_REFRESH_PHASE_ONE))
-                               return MESH_STATUS_STORAGE_FAIL;
-               } else
-                       return MESH_STATUS_CANNOT_UPDATE;
-       }
-
        if (subnet) {
                if (net_key_confirm(subnet->net_key_cur, value))
                        return MESH_STATUS_SUCCESS;
@@ -3569,7 +3555,7 @@ uint8_t mesh_net_key_refresh_phase_get(struct mesh_net *net, uint16_t idx,
        return MESH_STATUS_SUCCESS;
 }
 
-int mesh_net_kr_phase_one(struct mesh_net *net, uint16_t idx,
+int mesh_net_update_key(struct mesh_net *net, uint16_t idx,
                                                        const uint8_t *value)
 {
        struct mesh_subnet *subnet;
@@ -3579,9 +3565,15 @@ int mesh_net_kr_phase_one(struct mesh_net *net, uint16_t idx,
 
        subnet = l_queue_find(net->subnets, match_key_index,
                                                        L_UINT_TO_PTR(idx));
+
        if (!subnet)
                return MESH_STATUS_CANNOT_UPDATE;
 
+       /* Check if the key has been already successfully updated */
+       if (subnet->kr_phase == KEY_REFRESH_PHASE_ONE &&
+                               net_key_confirm(subnet->net_key_upd, value))
+               return MESH_STATUS_SUCCESS;
+
        if (subnet->net_key_upd) {
                net_key_unref(subnet->net_key_upd);
                l_info("Warning: overwriting new keys");
@@ -3605,6 +3597,9 @@ int mesh_net_kr_phase_one(struct mesh_net *net, uint16_t idx,
 
        l_info("key refresh phase 1: Key ID %d", subnet->net_key_upd);
 
+       if (!storage_net_key_add(net, idx, value, KEY_REFRESH_PHASE_ONE))
+               return MESH_STATUS_STORAGE_FAIL;
+
        subnet->kr_phase = KEY_REFRESH_PHASE_ONE;
 
        return MESH_STATUS_SUCCESS;
index 0ef01b6..b27a4e6 100644 (file)
@@ -280,8 +280,10 @@ bool mesh_net_set_relay_mode(struct mesh_net *net, bool enable, uint8_t cnt,
                                                        uint8_t interval);
 bool mesh_net_set_friend_mode(struct mesh_net *net, bool enable);
 int mesh_net_del_key(struct mesh_net *net, uint16_t net_idx);
-int mesh_net_add_key(struct mesh_net *net, bool update,
-                                       uint16_t net_idx, const void *key);
+int mesh_net_add_key(struct mesh_net *net, uint16_t net_idx,
+                                                       const uint8_t *key);
+int mesh_net_update_key(struct mesh_net *net, uint16_t net_idx,
+                                                       const uint8_t *key);
 uint32_t mesh_net_get_iv_index(struct mesh_net *net);
 void mesh_net_get_snb_state(struct mesh_net *net,
                                        uint8_t *flags, uint32_t *iv_index);
@@ -335,8 +337,6 @@ uint8_t mesh_net_key_refresh_phase_set(struct mesh_net *net, uint16_t net_idx,
                                                        uint8_t transition);
 uint8_t mesh_net_key_refresh_phase_get(struct mesh_net *net, uint16_t net_idx,
                                                        uint8_t *phase);
-int mesh_net_kr_phase_one(struct mesh_net *net, uint16_t net_idx,
-                                                       const uint8_t *key);
 int mesh_net_key_refresh_phase_two(struct mesh_net *net, uint16_t net_idx);
 int mesh_net_key_refresh_finish(struct mesh_net *net, uint16_t net_idx);
 void mesh_net_send_seg(struct mesh_net *net, uint32_t key_id,
index e921b72..1845f9a 100644 (file)
@@ -1726,8 +1726,8 @@ bool node_add_pending_local(struct mesh_node *node, void *prov_node_info,
        if (!mesh_db_write_device_key(node->jconfig, info->device_key))
                return false;
 
-       if (mesh_net_add_key(node->net, kr, info->net_index,
-                       info->net_key) != MESH_STATUS_SUCCESS)
+       if (mesh_net_add_key(node->net, info->net_index, info->net_key) !=
+                                                       MESH_STATUS_SUCCESS)
                return false;
 
        if (!storage_net_key_add(node->net, info->net_index, info->net_key,
index 3486862..2429e2b 100644 (file)
@@ -119,7 +119,7 @@ static bool read_net_keys_cb(uint16_t idx, uint8_t *key, uint8_t *new_key,
        if (!net)
                return false;
 
-       if (mesh_net_add_key(net, false, idx, key) != MESH_STATUS_SUCCESS)
+       if (mesh_net_add_key(net, idx, key) != MESH_STATUS_SUCCESS)
                return false;
        /* TODO: handle restoring key refresh phase and new keys */