Skip to contentSkip to conversation

mesh: Fix some SNB details

PATCH In review Brian Gix 1 participant 4 messages Dec 20, 2019 – Dec 24, 2019 Bluetooth drivers

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

cover mesh: Fix some SNB details

This fixes two minor flaws with Secure Net Beacons:

1. When a local node's SNB changes, all local nodes should be made aware of the change a the same time we start sending out that beacon.

2. The second that a remote beacon is "seen" needs to be recorded so that the SecondsSinceLastHeard property can be correctly updated.

Brian Gix (2):
mesh: Deliver newly composed SNBs to local nodes
mesh: Recognize SNBs in SecondsSinceLastHeard property

 mesh/net-keys.c | 19 ++++++++++++++++---
 mesh/net-keys.h |  1 +
 mesh/net.c      | 28 ++++++++++++++++++++++++++++
 mesh/net.h      |  1 +
 4 files changed, 46 insertions(+), 3 deletions(-)
Signature · 1 line
2.21.0
author
#1 lore raw

PATCH 1/2 mesh: Deliver newly composed SNBs to local nodes

---
 mesh/net-keys.c |  3 +++
 mesh/net.c      | 13 +++++++++++++
 mesh/net.h      |  1 +
 3 files changed, 17 insertions(+)
mesh/net-keys.c +3 -0
index 65f0808dd..6f1758fe3 100644
@@ -480,6 +480,9 @@ bool net_key_beacon_refresh(uint32_t id, uint32_t iv_index, bool kr, bool ivu)
 	l_debug("Setting SNB: IVI: %8.8x, IVU: %d, KR: %d", iv_index, ivu, kr);
 	print_packet("Set SNB Beacon to", beacon, sizeof(beacon));
 
+	/* Propagate changes to all local nodes */
+	net_local_beacon(id, beacon);
+
 	/* Send one new SNB soon, after all nodes have seen it */
 	l_getrandom(&rand_ms, sizeof(rand_ms));
 	rand_ms %= 1000;
mesh/net.c +13 -0
index 0485086bc..5aeeab188 100644
@@ -2739,6 +2739,19 @@ static void beacon_recv(void *user_data, struct mesh_io_recv_info *info,
 		net_key_beacon_seen(beacon_data.key_id);
 }
 
+void net_local_beacon(uint32_t key_id, uint8_t *beacon)
+{
+	struct net_beacon_data beacon_data = {
+		.key_id = key_id,
+		.ivu = !!(beacon[2] & 0x02),
+		.kr = !!(beacon[2] & 0x01),
+		.ivi = l_get_be32(beacon + 11),
+	};
+
+	/* Deliver locally generated beacons to all nodes */
+	l_queue_foreach(nets, process_beacon, &beacon_data);
+}
+
 bool mesh_net_set_beacon_mode(struct mesh_net *net, bool enable)
 {
 	if (!net)
mesh/net.h +1 -0
index 07b87830d..023b61e71 100644
@@ -280,6 +280,7 @@ uint16_t mesh_net_get_address(struct mesh_net *net);
 bool mesh_net_register_unicast(struct mesh_net *net,
 					uint16_t unicast, uint8_t num_ele);
 uint8_t mesh_net_get_num_ele(struct mesh_net *net);
+void net_local_beacon(uint32_t key_id, uint8_t *beacon);
 bool mesh_net_set_beacon_mode(struct mesh_net *net, bool enable);
 bool mesh_net_set_proxy_mode(struct mesh_net *net, bool enable);
 bool mesh_net_set_relay_mode(struct mesh_net *net, bool enable, uint8_t cnt,
Signature · 1 line
2.21.0
author
#2 lore raw

PATCH 2/2 mesh: Recognize SNBs in SecondsSinceLastHeard

In reply to Brian Gix

Timestamp is taken on each OTA beacon reception, and considered in the SecondsSinceLastHeard property.

---
 mesh/net-keys.c | 16 +++++++++++++---
 mesh/net-keys.h |  1 +
 mesh/net.c      | 15 +++++++++++++++
 3 files changed, 29 insertions(+), 3 deletions(-)
mesh/net-keys.c +13 -3
index 6f1758fe3..683a924c4 100644
@@ -410,7 +410,6 @@ static void snb_timeout(struct l_timeout *timeout, void *user_data)
 	}
 
 	interval = key->snb.observe_period / 2;
-	key->snb.ts = get_timestamp_secs();
 	key->snb.half_period = !key->snb.half_period;
 
 	if (key->beacon_enables)
@@ -425,8 +424,20 @@ void net_key_beacon_seen(uint32_t id)
 {
 	struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
 
-	if (key)
+	if (key) {
 		key->snb.observed++;
+		key->snb.ts = get_timestamp_secs();
+	}
+}
+
+uint32_t net_key_beacon_last_seen(uint32_t id)
+{
+	struct net_key *key = l_queue_find(keys, match_id, L_UINT_TO_PTR(id));
+
+	if (key)
+		return key->snb.ts;
+
+	return 0;
 }
 
 void net_key_beacon_enable(uint32_t id)
@@ -451,7 +462,6 @@ void net_key_beacon_enable(uint32_t id)
 	rand_ms++;
 
 	/* Enable Periodic Beaconing on this key */
-	key->snb.ts = get_timestamp_secs();
 	key->snb.observe_period = BEACON_INTERVAL_MIN * 2;
 	key->snb.expected = 2;
 	key->snb.observed = 0;
mesh/net-keys.h +1 -0
index 3c2c4d0da..9385e2c51 100644
@@ -39,3 +39,4 @@ void net_key_beacon_seen(uint32_t id);
 void net_key_beacon_enable(uint32_t id);
 bool net_key_beacon_refresh(uint32_t id, uint32_t iv_index, bool kr, bool ivu);
 void net_key_beacon_disable(uint32_t id);
+uint32_t net_key_beacon_last_seen(uint32_t id);
mesh/net.c +15 -0
index 5aeeab188..23565c11d 100644
@@ -3662,7 +3662,22 @@ void mesh_net_set_prov(struct mesh_net *net, struct mesh_prov *prov)
 	net->prov = prov;
 }
 
+static void refresh_instant(void *a, void *b)
+{
+	struct mesh_subnet *subnet = a;
+	struct mesh_net *net = b;
+	uint32_t instant = net_key_beacon_last_seen(subnet->net_key_tx);
+
+	if (net->instant < instant)
+		net->instant = instant;
+}
+
 uint32_t mesh_net_get_instant(struct mesh_net *net)
 {
+	if (!net)
+		return 0;
+
+	l_queue_foreach(net->subnets, refresh_instant, net);
+
 	return net->instant;
 }
Signature · 1 line
2.21.0
author
#3 lore raw
In reply to Brian Gix

Patchset applied

Quoted 18 lines · Brian Gix

This fixes two minor flaws with Secure Net Beacons:

1. When a local node's SNB changes, all local nodes should be made aware of the change a the same time we start sending out that beacon.

2. The second that a remote beacon is "seen" needs to be recorded so that the SecondsSinceLastHeard property can be correctly updated.

Brian Gix (2):
mesh: Deliver newly composed SNBs to local nodes
mesh: Recognize SNBs in SecondsSinceLastHeard property

 mesh/net-keys.c | 19 ++++++++++++++++---
 mesh/net-keys.h |  1 +
 mesh/net.c      | 28 ++++++++++++++++++++++++++++
 mesh/net.h      |  1 +
 4 files changed, 46 insertions(+), 3 deletions(-)