mesh: Add NVM storage of Replay Protection List
Subsystems (2)
v5 cover mesh: Add NVM storage of Replay Protection List
Version 5: Extra clean-up fixes added for malformed RPL trees, and iv_index dirs created "as needed" instead of at RPL initialization. (patch 5/5)
Version 4: Style guide fixes from Inga (patch 5/5).
Version 3: Fix problem found by Rafał relating to the handling of sequence numbers of messages being handled internally (loop-backs), which were being rejected by the Replay Protection List (please see patch 3/5).
Version 2: Fix path construction code when first reading RPL from NVM
An oversight led to losing our Replay Protection List with every re-boot. This patch-set makes a number of Replay Protect List modifications that culminate in adding rpl.c/h, which stores the latest iv_index/sequence values for each node that handles an incoming packet.
The first 4 patches, does some maintenance required to handle RPL according the the Mesh Specification.
Brian Gix (5):
mesh: Relocate tree deletion to util.c/h
mesh: Move Replay Protection to mesh/net.c
mesh: Clean-up unneeded Sequence Number increments
mesh: Apply Replay Protection to all incoming packets
mesh: Add NVM storage of Replay Protection
Makefile.mesh | 1 + mesh/appkey.c | 102 -------------- mesh/appkey.h | 3 - mesh/mesh-config-json.c | 20 +-- mesh/model.c | 16 +-- mesh/net.c | 128 +++++++++++++++--- mesh/net.h | 3 + mesh/rpl.c | 289 ++++++++++++++++++++++++++++++++++++++++ mesh/rpl.h | 30 +++++ mesh/util.c | 25 ++++ mesh/util.h | 1 + 11 files changed, 468 insertions(+), 150 deletions(-) create mode 100644 mesh/rpl.c create mode 100644 mesh/rpl.h
Signature · 1 line
2.21.1
PATCH v5 1/5 mesh: Relocate tree deletion to util.c/h
--- mesh/mesh-config-json.c | 20 +------------------- mesh/util.c | 25 +++++++++++++++++++++++++ mesh/util.h | 1 + 3 files changed, 27 insertions(+), 19 deletions(-)
mesh/mesh-config-json.c +1 -19
@@ -2253,24 +2253,6 @@ bool mesh_config_load_nodes(const char *cfgdir_name, mesh_config_node_func_t cb,return true; } -static int del_fobject (const char *fpath,const struct stat *sb,int typeflag, -struct FTW *ftwbuf) -{ -switch (typeflag) { -case FTW_DP : -rmdir (fpath); -l_debug ("RMDIR %s" , fpath); -break ; - -case FTW_SL : -default : -remove (fpath); -l_debug ("RM %s" , fpath); -break ; - } -return 0 ; -} -void mesh_config_destroy (struct mesh_config *cfg) {char *node_dir, *node_name;
@@ -2291,7 +2273,7 @@ void mesh_config_destroy(struct mesh_config *cfg)if (strcmp (node_name, uuid))return ; -nftw (node_dir, del_fobject,5 ,FTW_DEPTH |FTW_PHYS ); +del_path (node_dir);/* Release node config object */ mesh_config_release (cfg);
mesh/util.c +25 -0
@@ -24,6 +24,7 @@#define _GNU_SOURCE#include <dirent.h> #include <ftw.h> +#include <unistd.h> #include <stdio.h> #include <limits.h> #include <time.h>
@@ -129,3 +130,27 @@ int create_dir(const char *dir_name)return 0 ; } + +static int del_fobject (const char *fpath,const struct stat *sb,int typeflag, +struct FTW *ftwbuf) +{ +switch (typeflag) { +case FTW_DP : +rmdir (fpath); +l_debug ("RMDIR %s" , fpath); +break ; + +case FTW_SL : +default : +remove (fpath); +l_debug ("RM %s" , fpath); +break ; + } +return 0 ; +} + + +void del_path (const char *path) +{ +nftw (path, del_fobject,5 ,FTW_DEPTH |FTW_PHYS ); +}
mesh/util.h +1 -0
@@ -23,3 +23,4 @@ bool str2hex(const char *str, uint16_t in_len, uint8_t *out,size_t hex2str (uint8_t *in,size_t in_len,char *out,size_t out_len);void print_packet (const char *label,const void *data,uint16_t size);int create_dir (const char *dir_name); +void del_path (const char *path);
Signature · 1 line
2.21.1
PATCH v5 2/5 mesh: Move Replay Protection to mesh/net.c
In reply to Brian GixThe specification calls for a flatter Replay Protection List that applies to all processed messages, regardless of which credentials were used to secure them. So storage and checking is now centralized in mesh/net.c
--- mesh/appkey.c | 102 ------------------------------------------------- mesh/appkey.h | 3 -- mesh/model.c | 2 +- mesh/net.c | 104 +++++++++++++++++++++++++++++++++++++++++++++----- mesh/net.h | 3 ++ 5 files changed, 99 insertions(+), 115 deletions(-)
mesh/appkey.c +0 -102
@@ -35,7 +35,6 @@#include "mesh/appkey.h" struct mesh_app_key { -struct l_queue *replay_cache;uint16_t net_idx;uint16_t app_idx;uint8_t key[16 ];
@@ -44,12 +43,6 @@ struct mesh_app_key {uint8_t new_key_aid; }; -struct mesh_msg { -uint32_t iv_index; -uint32_t seq; -uint16_t src; -}; -static bool match_key_index (const void *a,const void *b) {const struct mesh_app_key *key = a;
@@ -66,103 +59,11 @@ static bool match_bound_key(const void *a, const void *b)return key->net_idx == idx; } -static bool match_replay_cache (const void *a,const void *b) -{ -const struct mesh_msg *msg = a; -uint16_t src =L_PTR_TO_UINT (b); - -return src == msg->src; -} - -static bool clean_old_iv_index (void *a,void *b) -{ -struct mesh_msg *msg = a; -uint32_t iv_index =L_PTR_TO_UINT (b); - -if (iv_index <2 ) -return false; - -if (msg->iv_index < iv_index -1 ) { -l_free (msg); -return true; - } - -return false; -} - -bool appkey_msg_in_replay_cache (struct mesh_net *net,uint16_t idx, -uint16_t src,uint16_t crpl,uint32_t seq, -uint32_t iv_index) -{ -struct mesh_app_key *key; -struct mesh_msg *msg; -struct l_queue *app_keys; - - app_keys =mesh_net_get_app_keys (net); -if (!app_keys) -return false; - -l_debug ("Test Replay src: %4.4x seq: %6.6x iv: %8.8x" , - src, seq, iv_index); - - key =l_queue_find (app_keys, match_key_index,L_UINT_TO_PTR (idx)); - -if (!key) -return false; - - msg =l_queue_find (key->replay_cache, match_replay_cache, -L_UINT_TO_PTR (src)); - -if (msg) { -if (iv_index > msg->iv_index) { - msg->seq = seq; - msg->iv_index = iv_index; -return false; - } - -if (seq < msg->seq) { -l_debug ("Ignoring packet with lower sequence number" ); -return true; - } - -if (seq == msg->seq) { -l_debug ("Message already processed (duplicate)" ); -return true; - } - - msg->seq = seq; - -return false; - } - -l_debug ("New Entry for %4.4x" , src); -if (key->replay_cache ==NULL ) - key->replay_cache =l_queue_new (); - -/* Replay Cache is fixed sized */ -if (l_queue_length (key->replay_cache) >= crpl) { -int ret =l_queue_foreach_remove (key->replay_cache, - clean_old_iv_index,L_UINT_TO_PTR (iv_index)); - -if (!ret) -return true; - } - - msg = l_new(struct mesh_msg, 1); - msg->src = src; - msg->seq = seq; - msg->iv_index = iv_index; -l_queue_push_head (key->replay_cache, msg); - -return false; -} -static struct mesh_app_key *app_key_new (void ) { struct mesh_app_key *key = l_new(struct mesh_app_key, 1); key->new_key_aid =0xFF ; - key->replay_cache =l_queue_new ();return key; }
@@ -192,7 +93,6 @@ void appkey_key_free(void *data)if (!key)return ; -l_queue_destroy (key->replay_cache, l_free);l_free (key); }
@@ -403,8 +303,6 @@ int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx, key->app_idx = app_idx;l_queue_push_tail (app_keys, key); -l_queue_clear (key->replay_cache, l_free); -return MESH_STATUS_SUCCESS ; }
mesh/appkey.h +0 -3
@@ -25,9 +25,6 @@ struct mesh_app_key;bool appkey_key_init (struct mesh_net *net,uint16_t net_idx,uint16_t app_idx,uint8_t *key_value,uint8_t *new_key_value);void appkey_key_free (void *data); -bool appkey_msg_in_replay_cache (struct mesh_net *net,uint16_t idx, -uint16_t src,uint16_t crpl,uint32_t seq, -uint32_t iv_index);const uint8_t *appkey_get_key (struct mesh_net *net,uint16_t app_idx,uint8_t *key_id); int appkey_get_key_idx(struct mesh_app_key *app_key,
mesh/model.c +1 -1
@@ -1000,7 +1000,7 @@ bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,if (key_aid !=APP_AID_DEV ) {uint16_t crpl =node_get_crpl (node); -if (appkey_msg_in_replay_cache (net, (uint16_t ) decrypt_idx, src, +if (net_msg_in_replay_cache (net, (uint16_t ) decrypt_idx, src, crpl, seq, iv_index)) { result = true; goto done;
mesh/net.c +95 -9
137 lines not on this page · raw message
mesh/net.h +3 -0
@@ -379,3 +379,6 @@ 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_in_replay_cache (struct mesh_net *net,uint16_t idx, +uint16_t src,uint16_t crpl,uint32_t seq, +uint32_t iv_index);
Signature · 1 line
2.21.1
PATCH v5 3/5 mesh: Clean-up unneeded Sequence Number increments
In reply to Brian GixScrub of Sequence Number handling of OB messages to account for in-node delivery of segmented messages, so that each discrete message has a unique sequence number for the RPL.
--- mesh/net.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
mesh/net.c +9 -8
@@ -3043,14 +3043,19 @@ static bool send_seg(struct mesh_net *net, struct mesh_sar *msg, uint8_t segO)uint8_t segN =SEG_MAX (msg->len);uint16_t seg_off =SEG_OFF (segO);uint32_t key_id =0 ; -uint32_t seq_num =mesh_net_next_seq_num (net); +uint32_t seq_num;if (segN) { +/* Send each segment on unique seq_num */ + seq_num =mesh_net_next_seq_num (net); +if (msg->len - seg_off >SEG_OFF (1 )) seg_len =SEG_OFF (1 );else seg_len = msg->len - seg_off; }else { +/* Send on same seq_num used for Access Layer */ + seq_num = msg->seqAuth; seg_len = msg->len; }
@@ -3185,7 +3190,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,/* First enqueue to any Friends and internal models */ result = msg_rxed(net, false, iv_index, ttl, - seq + seg_max, + seq, net_idx, src, dst, key_aid,
@@ -3196,12 +3201,8 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,* or delivered to one of our Unicast addresses we are done */ if ((result &&IS_UNICAST (dst)) || src == dst || - (dst >= net->src_addr && dst <= net->last_addr)) { -/* Adjust our seq_num for "virtual" delivery */ - net->seq_num += seg_max; -mesh_net_next_seq_num (net); + (dst >= net->src_addr && dst <= net->last_addr))return true; - }/* If Segmented, Cancel any OB segmented message to same DST */ if (seg_max) {
@@ -3226,7 +3227,7 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, } payload->iv_index =mesh_net_get_iv_index (net); - payload->seqAuth = net->seq_num; + payload->seqAuth = seq; result = true;if (!IS_UNICAST (dst) && seg_max) {
Signature · 1 line
2.21.1
PATCH v5 4/5 mesh: Apply Replay Protection to all incoming packets
In reply to Brian GixReplay Protection was only being applied against Application Keys, but messages with Device Keys are just as vulnerable, and need to be checked as well.
--- mesh/model.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
mesh/model.c +7 -9
@@ -608,7 +608,7 @@ static bool msg_send(struct mesh_node *node, bool credential, uint16_t src, iv_index =mesh_net_get_iv_index (net); - seq_num =mesh_net_get_seq_num (net); + seq_num =mesh_net_next_seq_num (net);if (!mesh_crypto_payload_encrypt (label, msg, out, msg_len, src, dst, key_aid, seq_num, iv_index, szmic, key)) {l_error ("Failed to Encrypt Payload" );
@@ -949,7 +949,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; +uint16_t addr, crpl;struct mesh_virtual *decrypt_virt =NULL ;bool result = false;bool is_subscription;
@@ -997,14 +997,12 @@ bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,/* print_packet("Clr Rx (pre-cache-check)", clear_text, size - 4); */ -if (key_aid !=APP_AID_DEV ) { -uint16_t crpl =node_get_crpl (node); + crpl =node_get_crpl (node); -if (net_msg_in_replay_cache (net, (uint16_t ) decrypt_idx, src, - crpl, seq, iv_index)) { - result = true; - goto done; - } +if (net_msg_in_replay_cache (net, (uint16_t ) decrypt_idx, src, + crpl, seq, iv_index)) { + result = true; + goto done; }print_packet ("Clr Rx" , clear_text, size - (szmict ?8 :4 ));
Signature · 1 line
2.21.1
PATCH v5 5/5 mesh: Add NVM storage of Replay Protection
In reply to Brian GixMesh specification requires that Replay Protection be preserved across node restarts. This adds that storage in <node_uuid>/rpl/<iv_index>/<src>
Realtime access remains in an l_queue structure, and stored as messages are processed.
--- Makefile.mesh | 1 + mesh/net.c | 21 ++-- mesh/rpl.c | 289 ++++++++++++++++++++++++++++++++++++++++++++++++++ mesh/rpl.h | 30 ++++++ 4 files changed, 334 insertions(+), 7 deletions(-) create mode 100644 mesh/rpl.c create mode 100644 mesh/rpl.h
Makefile.mesh +1 -0
@@ -32,6 +32,7 @@ mesh_sources = mesh/mesh.h mesh/mesh.c \ mesh/manager.h mesh/manager.c \ mesh/pb-adv.h mesh/pb-adv.c \ mesh/keyring.h mesh/keyring.c \ + mesh/rpl.h mesh/rpl.c \ mesh/mesh-defs.h pkglibexec_PROGRAMS += mesh/bluetooth-meshd
mesh/net.c +14 -7
@@ -36,6 +36,7 @@#include "mesh/mesh-config.h" #include "mesh/model.h" #include "mesh/appkey.h" +#include "mesh/rpl.h" #define abs_diff (a, b) ((a) > (b) ? (a) - (b) : (b) - (a))
@@ -256,12 +257,6 @@ struct net_beacon_data {bool processed; }; -struct mesh_rpl { -uint32_t iv_index; -uint32_t seq; -uint16_t src; -}; -#define FAST_CACHE_SIZE 8static struct l_queue *fast_cache;static struct l_queue *nets;
@@ -2714,6 +2709,9 @@ static void update_iv_ivu_state(struct mesh_net *net, uint32_t iv_index,struct mesh_config *cfg =node_config_get (net->node);mesh_config_write_iv_index (cfg, iv_index, ivu); + +/* Cleanup Replay Protection List NVM */ +rpl_init (net->node, iv_index); } net->iv_index = iv_index;
@@ -3771,8 +3769,11 @@ bool net_msg_in_replay_cache(struct mesh_net *net, uint16_t idx,if (!net || !net->node)return true; -if (!net->replay_cache) +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); + }l_debug ("Test Replay src: %4.4x seq: %6.6x iv: %8.8x" , src, seq, iv_index);
@@ -3784,6 +3785,7 @@ bool net_msg_in_replay_cache(struct mesh_net *net, uint16_t idx,if (iv_index > rpe->iv_index) { rpe->seq = seq; rpe->iv_index = iv_index; +rpl_put_entry (net->node, src, iv_index, seq);return false; }
@@ -3799,6 +3801,8 @@ bool net_msg_in_replay_cache(struct mesh_net *net, uint16_t idx, rpe->seq = seq; +rpl_put_entry (net->node, src, iv_index, seq); +return false; }
@@ -3813,6 +3817,9 @@ bool net_msg_in_replay_cache(struct mesh_net *net, uint16_t idx,return true; } +if (!rpl_put_entry (net->node, src, iv_index, seq)) +return true; + rpe = l_new(struct mesh_rpl, 1); rpe->src = src; rpe->seq = seq;
mesh/rpl.c new file +289 -0
289 lines not on this page · raw message
mesh/rpl.h new file +30 -0
@@ -0,0 +1,30 @@ +/* +* +* BlueZ - Bluetooth protocol stack for Linux +* +* Copyright (C) 2020 Intel Corporation. All rights reserved. +* +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU Lesser General Public +* License as published by the Free Software Foundation; either +* version 2.1 of the License, or (at your option) any later version. +* +* This library is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +* Lesser General Public License for more details. +* +*/ + +struct mesh_rpl { +uint32_t iv_index; +uint32_t seq; +uint16_t src; +}; + +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);
Signature · 1 line
2.21.1
Patchset Applied
Quoted 45 lines · Brian Gix
Version 5: Extra clean-up fixes added for malformed RPL trees, and
iv_indexdirs created "as needed" instead of at RPL initialization. (patch 5/5)Version 4: Style guide fixes from Inga (patch 5/5).
Version 3: Fix problem found by Rafał relating to the handling of sequence numbers of messages being handled internally (loop-backs), which were being rejected by the Replay Protection List (please see patch 3/5).
Version 2: Fix path construction code when first reading RPL from NVM
An oversight led to losing our Replay Protection List with every re-boot. This patch-set makes a number of Replay Protect List modifications that culminate in adding rpl.c/h, which stores the latest iv_index/sequence values for each node that handles an incoming packet.
The first 4 patches, does some maintenance required to handle RPL according the the Mesh Specification.
Brian Gix (5):
mesh: Relocate tree deletion to util.c/h
mesh: Move Replay Protection tomesh/net.c
mesh: Clean-up unneeded Sequence Number increments
mesh: Apply Replay Protection to all incoming packets
mesh: Add NVM storage of Replay ProtectionMakefile.mesh | 1 + mesh/appkey.c | 102 -------------- mesh/appkey.h | 3 - mesh/mesh-config-json.c | 20 +-- mesh/model.c | 16 +-- mesh/net.c | 128 +++++++++++++++--- mesh/net.h | 3 + mesh/rpl.c | 289 ++++++++++++++++++++++++++++++++++++++++ mesh/rpl.h | 30 +++++ mesh/util.c | 25 ++++ mesh/util.h | 1 + 11 files changed, 468 insertions(+), 150 deletions(-) create mode 100644 mesh/rpl.c create mode 100644 mesh/rpl.h