Skip to contentSkip to conversation

mesh: Propagate/Preserve Net Index for Access Layer

PATCH In review Brian Gix 2 participants 6 messages Jul 16, 2019 – Jul 18, 2019 Bluetooth drivers

lore
Share & tools
lore

Brian Gix

author
#0 lore raw

cover mesh: Propagate/Preserve Net Index for Access Layer

Two major flaws are corrected by this patch-set:

1. The Spec defined Access Layer AID was being confused with the internally used key_id which is used to identify Network keys for multiple networks, sub-networks, and friendships. This confused ID was then being used instead of the correct Network Key ID for outgoing messages.

2. The specification requires *responses* to all standard Server messages to be sent with the same credentials as the received command that triggered them. This requires more complete propagation of the Net Index value on received messages, so that the same Network key is used in the reverse direction.

This additionally now uses the requested Net Index when sending Device Key secured messages through the D-Bus interface.

Brian Gix (3):
mesh: Add bound Net Index lookup for App Keys
mesh: Normalize Access Key AID naming convention
mesh: Propagate Net Index up Rx message chain

 mesh/appkey.c        |  59 +++++++++++++-------
 mesh/appkey.h        |   1 +
 mesh/cfgmod-server.c | 154 ++++++++++++++++++++++++++++-----------------------
 mesh/crypto.c        |  37 ++++++-------
 mesh/crypto.h        |   8 +--
 mesh/model.c         | 129 +++++++++++++++++++++---------------------
 mesh/model.h         |  10 ++--
 mesh/net.c           |  92 +++++++++++++++++-------------
 mesh/net.h           |  10 ++--
 mesh/node.c          |  10 ++--
 10 files changed, 276 insertions(+), 234 deletions(-)
Signature · 1 line
2.14.5
author
#1 lore raw

PATCH 1/3 mesh: Add bound Net Index lookup for App Keys

This patch also normalizes the naming convention in favor of key_aid when referring to the calculated AID of the used Access Layer key.

---
 mesh/appkey.c | 59 ++++++++++++++++++++++++++++++++++++++---------------------
 mesh/appkey.h |  1 +
 2 files changed, 39 insertions(+), 21 deletions(-)
mesh/appkey.c +38 -21
index 412a2c48c..bdc6ac29e 100644
@@ -39,9 +39,9 @@ struct mesh_app_key {
 	uint16_t net_idx;
 	uint16_t app_idx;
 	uint8_t key[16];
-	uint8_t key_id;
+	uint8_t key_aid;
 	uint8_t new_key[16];
-	uint8_t new_key_id;
+	uint8_t new_key_aid;
 };
 
 struct mesh_msg {
@@ -62,7 +62,7 @@ struct mod_decrypt {
 	uint16_t idx;
 	uint16_t size;
 	uint16_t virt_size;
-	uint8_t key_id;
+	uint8_t key_aid;
 	bool szmict;
 	bool decrypted;
 };
@@ -108,16 +108,16 @@ static void packet_decrypt(void *a, void *b)
 	if (dec->decrypted)
 		return;
 
-	if (key->key_id != dec->key_id &&
-			key->new_key_id != dec->key_id)
+	if (key->key_aid != dec->key_aid &&
+			key->new_key_aid != dec->key_aid)
 		return;
 
 	dec->key = key;
 
-	if (key->key_id == dec->key_id) {
+	if (key->key_aid == dec->key_aid) {
 		dec->decrypted = mesh_crypto_payload_decrypt(dec->virt,
 				dec->virt_size, dec->data, dec->size,
-				dec->szmict, dec->src, dec->dst, dec->key_id,
+				dec->szmict, dec->src, dec->dst, dec->key_aid,
 				dec->seq, dec->iv_idx, dec->out, key->key);
 		if (dec->decrypted)
 			print_packet("Used App Key", dec->key->key, 16);
@@ -125,10 +125,10 @@ static void packet_decrypt(void *a, void *b)
 			print_packet("Failed with App Key", dec->key->key, 16);
 	}
 
-	if (!dec->decrypted && key->new_key_id == dec->key_id) {
+	if (!dec->decrypted && key->new_key_aid == dec->key_aid) {
 		dec->decrypted = mesh_crypto_payload_decrypt(dec->virt,
 				dec->virt_size, dec->data, dec->size,
-				dec->szmict, dec->src, dec->dst, dec->key_id,
+				dec->szmict, dec->src, dec->dst, dec->key_aid,
 				dec->seq, dec->iv_idx, dec->out, key->new_key);
 		if (dec->decrypted)
 			print_packet("Used App Key", dec->key->new_key, 16);
@@ -144,7 +144,7 @@ static void packet_decrypt(void *a, void *b)
 int appkey_packet_decrypt(struct mesh_net *net, bool szmict, uint32_t seq,
 				uint32_t iv_index, uint16_t src,
 				uint16_t dst, uint8_t *virt, uint16_t virt_size,
-				uint8_t key_id, const uint8_t *data,
+				uint8_t key_aid, const uint8_t *data,
 				uint16_t data_size, uint8_t *out)
 {
 	struct l_queue *app_keys;
@@ -156,7 +156,7 @@ int appkey_packet_decrypt(struct mesh_net *net, bool szmict, uint32_t seq,
 		.data = data,
 		.out = out,
 		.size = data_size,
-		.key_id = key_id,
+		.key_aid = key_aid,
 		.iv_idx = iv_index,
 		.virt = virt,
 		.virt_size = virt_size,
@@ -244,7 +244,7 @@ static struct mesh_app_key *app_key_new(void)
 {
 	struct mesh_app_key *key = l_new(struct mesh_app_key, 1);
 
-	key->new_key_id = 0xFF;
+	key->new_key_aid = 0xFF;
 	key->replay_cache = l_queue_new();
 	return key;
 }
@@ -252,16 +252,16 @@ static struct mesh_app_key *app_key_new(void)
 static bool set_key(struct mesh_app_key *key, uint16_t app_idx,
 			const uint8_t *key_value, bool is_new)
 {
-	uint8_t key_id;
+	uint8_t key_aid;
 
-	if (!mesh_crypto_k4(key_value, &key_id))
+	if (!mesh_crypto_k4(key_value, &key_aid))
 		return false;
 
-	key_id = KEY_ID_AKF | (key_id << KEY_AID_SHIFT);
+	key_aid = KEY_ID_AKF | (key_aid << KEY_AID_SHIFT);
 	if (!is_new)
-		key->key_id = key_id;
+		key->key_aid = key_aid;
 	else
-		key->new_key_id = key_id;
+		key->new_key_aid = key_aid;
 
 	memcpy(is_new ? key->new_key : key->key, key_value, 16);
 
@@ -314,7 +314,7 @@ bool appkey_key_init(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
 }
 
 const uint8_t *appkey_get_key(struct mesh_net *net, uint16_t app_idx,
-							uint8_t *key_id)
+							uint8_t *key_aid)
 {
 	struct mesh_app_key *app_key;
 	uint8_t phase;
@@ -334,14 +334,14 @@ const uint8_t *appkey_get_key(struct mesh_net *net, uint16_t app_idx,
 		return NULL;
 
 	if (phase != KEY_REFRESH_PHASE_TWO) {
-		*key_id = app_key->key_id;
+		*key_aid = app_key->key_aid;
 		return app_key->key;
 	}
 
-	if (app_key->new_key_id == NET_NID_INVALID)
+	if (app_key->new_key_aid == NET_NID_INVALID)
 		return NULL;
 
-	*key_id = app_key->new_key_id;
+	*key_aid = app_key->new_key_aid;
 	return app_key->new_key;
 }
 
@@ -362,6 +362,23 @@ bool appkey_have_key(struct mesh_net *net, uint16_t app_idx)
 		return true;
 }
 
+uint16_t appkey_net_idx(struct mesh_net *net, uint16_t app_idx)
+{
+	struct mesh_app_key *key;
+	struct l_queue *app_keys;
+
+	app_keys = mesh_net_get_app_keys(net);
+	if (!app_keys)
+		return NET_IDX_INVALID;
+
+	key = l_queue_find(app_keys, match_key_index, L_UINT_TO_PTR(app_idx));
+
+	if (!key)
+		return NET_IDX_INVALID;
+	else
+		return key->net_idx;
+}
+
 int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
 							const uint8_t *new_key)
 {
mesh/appkey.h +1 -0
index 1c22e801c..4c135580a 100644
@@ -34,6 +34,7 @@ bool appkey_msg_in_replay_cache(struct mesh_net *net, uint16_t idx,
 const uint8_t *appkey_get_key(struct mesh_net *net, uint16_t app_idx,
 							uint8_t *key_id);
 bool appkey_have_key(struct mesh_net *net, uint16_t app_idx);
+uint16_t appkey_net_idx(struct mesh_net *net, uint16_t app_idx);
 int appkey_key_add(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
 							const uint8_t *new_key);
 int appkey_key_update(struct mesh_net *net, uint16_t net_idx, uint16_t app_idx,
Signature · 1 line
2.14.5
author
#2 lore raw

PATCH 2/3 mesh: Normalize Access Key AID naming convention

In reply to Brian Gix
---
 mesh/crypto.c | 37 ++++++++++++++++++-------------------
 mesh/crypto.h |  8 ++++----
 mesh/net.h    |  4 +---
 3 files changed, 23 insertions(+), 26 deletions(-)
mesh/crypto.c +18 -19
index a4679d88e..b1b1fa9a8 100644
@@ -429,8 +429,9 @@ bool mesh_crypto_device_nonce(uint32_t seq, uint16_t src,
 	return true;
 }
 
-bool mesh_crypto_application_encrypt(uint8_t key_id, uint32_t seq, uint16_t src,
-					uint16_t dst, uint32_t iv_index,
+bool mesh_crypto_application_encrypt(uint8_t key_aid, uint32_t seq,
+					uint16_t src, uint16_t dst,
+					uint32_t iv_index,
 					const uint8_t app_key[16],
 					const uint8_t *aad, uint8_t aad_len,
 					const uint8_t *msg, uint8_t msg_len,
@@ -440,11 +441,11 @@ bool mesh_crypto_application_encrypt(uint8_t key_id, uint32_t seq, uint16_t src,
 	uint8_t nonce[13];
 	bool aszmic = (mic_size == 8) ? true : false;
 
-	if (!key_id && !mesh_crypto_device_nonce(seq, src, dst,
+	if (!key_aid && !mesh_crypto_device_nonce(seq, src, dst,
 						iv_index, aszmic, nonce))
 		return false;
 
-	if (key_id && !mesh_crypto_application_nonce(seq, src, dst,
+	if (key_aid && !mesh_crypto_application_nonce(seq, src, dst,
 						iv_index, aszmic, nonce))
 		return false;
 
@@ -453,8 +454,8 @@ bool mesh_crypto_application_encrypt(uint8_t key_id, uint32_t seq, uint16_t src,
 						out, app_mic, mic_size);
 }
 
-bool mesh_crypto_application_decrypt(uint8_t key_id, uint32_t seq, uint16_t src,
-				uint16_t dst, uint32_t iv_index,
+bool mesh_crypto_application_decrypt(uint8_t key_aid, uint32_t seq,
+				uint16_t src, uint16_t dst, uint32_t iv_index,
 				const uint8_t app_key[16],
 				const uint8_t *aad, uint8_t aad_len,
 				const uint8_t *enc_msg, uint8_t enc_msg_len,
@@ -463,11 +464,11 @@ bool mesh_crypto_application_decrypt(uint8_t key_id, uint32_t seq, uint16_t src,
 	uint8_t nonce[13];
 	bool aszmic = (mic_size == 8) ? true : false;
 
-	if (!key_id && !mesh_crypto_device_nonce(seq, src, dst,
+	if (!key_aid && !mesh_crypto_device_nonce(seq, src, dst,
 						iv_index, aszmic, nonce))
 		return false;
 
-	if (key_id && !mesh_crypto_application_nonce(seq, src, dst,
+	if (key_aid && !mesh_crypto_application_nonce(seq, src, dst,
 						iv_index, aszmic, nonce))
 		return false;
 
@@ -638,7 +639,7 @@ bool mesh_crypto_packet_build(bool ctl, uint8_t ttl,
 				uint32_t seq,
 				uint16_t src, uint16_t dst,
 				uint8_t opcode,
-				bool segmented, uint8_t key_id,
+				bool segmented, uint8_t key_aid,
 				bool szmic, bool relay, uint16_t seqZero,
 				uint8_t segO, uint8_t segN,
 				const uint8_t *payload, uint8_t payload_len,
@@ -656,7 +657,7 @@ bool mesh_crypto_packet_build(bool ctl, uint8_t ttl,
 
 	if (!ctl) {
 		hdr = segmented << SEG_HDR_SHIFT;
-		hdr |= (key_id & KEY_ID_MASK) << KEY_HDR_SHIFT;
+		hdr |= (key_aid & KEY_ID_MASK) << KEY_HDR_SHIFT;
 		if (segmented) {
 			hdr |= szmic << SZMIC_HDR_SHIFT;
 			hdr |= (seqZero & SEQ_ZERO_MASK) << SEQ_ZERO_HDR_SHIFT;
@@ -700,7 +701,7 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 				bool *ctl, uint8_t *ttl, uint32_t *seq,
 				uint16_t *src, uint16_t *dst,
 				uint32_t *cookie, uint8_t *opcode,
-				bool *segmented, uint8_t *key_id,
+				bool *segmented, uint8_t *key_aid,
 				bool *szmic, bool *relay, uint16_t *seqZero,
 				uint8_t *segO, uint8_t *segN,
 				const uint8_t **payload, uint8_t *payload_len)
@@ -769,8 +770,8 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 		if (cookie)
 			*cookie = l_get_be32(packet + packet_len - 8);
 
-		if (key_id)
-			*key_id = (hdr >> KEY_HDR_SHIFT) & KEY_ID_MASK;
+		if (key_aid)
+			*key_aid = (hdr >> KEY_HDR_SHIFT) & KEY_ID_MASK;
 
 		if (is_segmented) {
 			if (szmic)
@@ -805,7 +806,7 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 
 bool mesh_crypto_payload_encrypt(uint8_t *aad, const uint8_t *payload,
 				uint8_t *out, uint16_t payload_len,
-				uint16_t src, uint16_t dst, uint8_t key_id,
+				uint16_t src, uint16_t dst, uint8_t key_aid,
 				uint32_t seq_num, uint32_t iv_index,
 				bool aszmic,
 				const uint8_t application_key[16])
@@ -815,8 +816,7 @@ bool mesh_crypto_payload_encrypt(uint8_t *aad, const uint8_t *payload,
 	if (payload_len < 1)
 		return false;
 
-	/* Key_ID == 0 means the Device Key is being used */
-	if (!key_id)
+	if (key_aid == APP_AID_DEV)
 		application_nonce[0] = 0x02;
 
 	/* Seq Num */
@@ -848,7 +848,7 @@ bool mesh_crypto_payload_decrypt(uint8_t *aad, uint16_t aad_len,
 				const uint8_t *payload, uint16_t payload_len,
 				bool szmict,
 				uint16_t src, uint16_t dst,
-				uint8_t key_id, uint32_t seq_num,
+				uint8_t key_aid, uint32_t seq_num,
 				uint32_t iv_index, uint8_t *out,
 				const uint8_t app_key[16])
 {
@@ -859,8 +859,7 @@ bool mesh_crypto_payload_decrypt(uint8_t *aad, uint16_t aad_len,
 	if (payload_len < 5 || !out)
 		return false;
 
-	/* Key_ID == 0 means the Device Key is being used */
-	if (!key_id)
+	if (key_aid == APP_AID_DEV)
 		app_nonce[0] = 0x02;
 
 	/* Seq Num */
mesh/crypto.h +4 -4
index 3e6815a35..1a73bcaa3 100644
@@ -119,7 +119,7 @@ bool mesh_crypto_packet_build(bool ctl, uint8_t ttl,
 				uint32_t seq,
 				uint16_t src, uint16_t dst,
 				uint8_t opcode,
-				bool segmented, uint8_t key_id,
+				bool segmented, uint8_t key_aid,
 				bool szmic, bool relay, uint16_t seqZero,
 				uint8_t segO, uint8_t segN,
 				const uint8_t *payload, uint8_t payload_len,
@@ -128,20 +128,20 @@ bool mesh_crypto_packet_parse(const uint8_t *packet, uint8_t packet_len,
 				bool *ctl, uint8_t *ttl, uint32_t *seq,
 				uint16_t *src, uint16_t *dst,
 				uint32_t *cookie, uint8_t *opcode,
-				bool *segmented, uint8_t *key_id,
+				bool *segmented, uint8_t *key_aid,
 				bool *szmic, bool *relay, uint16_t *seqZero,
 				uint8_t *segO, uint8_t *segN,
 				const uint8_t **payload, uint8_t *payload_len);
 bool mesh_crypto_payload_encrypt(uint8_t *aad, const uint8_t *payload,
 				uint8_t *out, uint16_t payload_len,
-				uint16_t src, uint16_t dst, uint8_t key_id,
+				uint16_t src, uint16_t dst, uint8_t key_aid,
 				uint32_t seq_num, uint32_t iv_index,
 				bool aszmic,
 				const uint8_t application_key[16]);
 bool mesh_crypto_payload_decrypt(uint8_t *aad, uint16_t aad_len,
 				const uint8_t *payload, uint16_t payload_len,
 				bool szmict,
-				uint16_t src, uint16_t dst, uint8_t key_id,
+				uint16_t src, uint16_t dst, uint8_t key_aid,
 				uint32_t seq_num, uint32_t iv_index,
 				uint8_t *out,
 				const uint8_t application_key[16]);
mesh/net.h +1 -3
index 8848e6df0..7b0733578 100644
@@ -28,9 +28,7 @@ struct mesh_node;
 
 #define UNUSED_KEY_IDX	0xffff
 
-#define APP_ID_DEV	0
-#define APP_ID_ANY	((unsigned int) -1)
-#define NET_ID_ANY	(APP_ID_ANY - 1)
+#define APP_AID_DEV	0x00
 
 #define CTL		0x80
 #define TTL_MASK	0x7f
Signature · 1 line
2.14.5
author
#3 lore raw

PATCH 3/3 mesh: Propagate Net Index up Rx message chain

In reply to Brian Gix

When a model receives a message, it is required by the spec to respond using the same credentials. When an App Key is used, this is trivial because App keys are bound to Net keys, so only the App Index is required. Messages received on a Device key however, need the Net Index preserved from original message for re-use during the response.

---
 mesh/cfgmod-server.c | 154 ++++++++++++++++++++++++++++-----------------------
 mesh/model.c         | 129 +++++++++++++++++++++---------------------
 mesh/model.h         |  10 ++--
 mesh/net.c           |  92 +++++++++++++++++-------------
 mesh/net.h           |   6 +-
 mesh/node.c          |  10 ++--
 6 files changed, 214 insertions(+), 187 deletions(-)
mesh/cfgmod-server.c +85 -69
index a19ddc72e..420cac6ed 100644
@@ -38,7 +38,8 @@
 
 #define CFG_MAX_MSG_LEN 380
 
-static void send_pub_status(struct mesh_node *node, uint16_t src, uint16_t dst,
+static void send_pub_status(struct mesh_node *node, uint16_t net_idx,
+			uint16_t src, uint16_t dst,
 			uint8_t status, uint16_t ele_addr, uint16_t pub_addr,
 			uint32_t mod_id, uint16_t idx, bool cred_flag,
 			uint8_t ttl, uint8_t period, uint8_t retransmit)
@@ -69,11 +70,12 @@ static void send_pub_status(struct mesh_node *node, uint16_t src, uint16_t dst,
 		n += 2;
 	}
 
-	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, DEFAULT_TTL,
+	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx, DEFAULT_TTL,
 								msg, n);
 }
 
-static bool config_pub_get(struct mesh_node *node, uint16_t src, uint16_t dst,
+static bool config_pub_get(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size)
 {
 	uint32_t mod_id;
@@ -100,16 +102,17 @@ static bool config_pub_get(struct mesh_node *node, uint16_t src, uint16_t dst,
 		status = MESH_STATUS_INVALID_ADDRESS;
 
 	if (pub && status == MESH_STATUS_SUCCESS)
-		send_pub_status(node, src, dst, status, ele_addr, pub->addr,
-				mod_id, pub->idx, pub->credential, pub->ttl,
-						pub->period, pub->retransmit);
+		send_pub_status(node, net_idx, src, dst, status, ele_addr,
+				pub->addr, mod_id, pub->idx, pub->credential,
+				pub->ttl, pub->period, pub->retransmit);
 	else
-		send_pub_status(node, src, dst, status, ele_addr, 0, mod_id,
-								0, 0, 0, 0, 0);
+		send_pub_status(node, net_idx, src, dst, status, ele_addr, 0,
+							mod_id, 0, 0, 0, 0, 0);
 	return true;
 }
 
-static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,
+static bool config_pub_set(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size,
 					bool unreliable)
 {
@@ -228,13 +231,13 @@ static bool config_pub_set(struct mesh_node *node, uint16_t src, uint16_t dst,
 
 done:
 	if (!unreliable)
-		send_pub_status(node, src, dst, status, ele_addr, ota,
-				mod_id, idx, cred_flag, ttl, period,
-				retransmit);
+		send_pub_status(node, net_idx, src, dst, status, ele_addr, ota,
+			mod_id, idx, cred_flag, ttl, period, retransmit);
 	return true;
 }
 
-static void send_sub_status(struct mesh_node *node, uint16_t src, uint16_t dst,
+static void send_sub_status(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					uint8_t status, uint16_t ele_addr,
 					uint16_t addr, uint32_t mod)
 {
@@ -255,10 +258,12 @@ static void send_sub_status(struct mesh_node *node, uint16_t src, uint16_t dst,
 		n += 2;
 	}
 
-	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, DEFAULT_TTL, msg, n);
+	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx,
+							DEFAULT_TTL, msg, n);
 }
 
-static bool config_sub_get(struct mesh_node *node, uint16_t src, uint16_t dst,
+static bool config_sub_get(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size)
 {
 	uint16_t ele_addr;
@@ -313,7 +318,8 @@ static bool config_sub_get(struct mesh_node *node, uint16_t src, uint16_t dst,
 
 	*msg_status = (uint8_t) status;
 
-	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, DEFAULT_TTL, msg, n);
+	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx, DEFAULT_TTL,
+									msg, n);
 	return true;
 }
 
@@ -349,7 +355,8 @@ static bool save_config_sub(struct mesh_node *node, uint16_t ele_addr,
 					vendor, &db_sub);
 }
 
-static void config_sub_set(struct mesh_node *node, uint16_t src, uint16_t dst,
+static void config_sub_set(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size,
 					bool virt, uint32_t opcode)
 {
@@ -464,14 +471,15 @@ static void config_sub_set(struct mesh_node *node, uint16_t src, uint16_t dst,
 	}
 
 	if (!unreliable)
-		send_sub_status(node, src, dst, status, ele_addr, grp, mod_id);
+		send_sub_status(node, net_idx, src, dst, status, ele_addr,
+								grp, mod_id);
 
 }
 
-static void send_model_app_status(struct mesh_node *node, uint16_t src,
-					uint16_t dst, uint8_t status,
-					uint16_t addr, uint32_t id,
-					uint16_t idx)
+static void send_model_app_status(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
+					uint8_t status, uint16_t addr,
+					uint32_t id, uint16_t idx)
 {
 	uint8_t msg[12];
 	size_t n = mesh_model_opcode_set(OP_MODEL_APP_STATUS, msg);
@@ -488,10 +496,12 @@ static void send_model_app_status(struct mesh_node *node, uint16_t src,
 	l_put_le16(id, msg + n);
 	n += 2;
 
-	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, DEFAULT_TTL, msg, n);
+	mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx, DEFAULT_TTL,
+									msg, n);
 }
 
-static void model_app_list(struct mesh_node *node, uint16_t src, uint16_t dst,
+static void model_app_list(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size)
 {
 	uint16_t ele_addr;
@@ -541,14 +551,15 @@ static void model_app_list(struct mesh_node *node, uint16_t src, uint16_t dst,
 
 	if (result >= 0) {
 		*status = result;
-		mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, DEFAULT_TTL,
-								msg, n);
+		mesh_model_send(node, dst, src, APP_IDX_DEV_LOCAL, net_idx,
+							DEFAULT_TTL, msg, n);
 	}
 
 	l_free(msg);
 }
 
-static bool model_app_bind(struct mesh_node *node, uint16_t src, uint16_t dst,
+static bool model_app_bind(struct mesh_node *node, uint16_t net_idx,
+					uint16_t src, uint16_t dst,
 					const uint8_t *pkt, uint16_t size,
 					bool unbind)
 {
@@ -582,7 +593,8 @@ static bool model_app_bind(struct mesh_node *node, uint16_t src, uint16_t dst,
 	else
 		result = mesh_model_binding_add(node, ele_addr, mod_id, idx);
 
-	send_model_app_status(node, src, dst, result, ele_addr, mod_id, idx);
+	send_model_app_status(node, net_idx, src, dst, result, ele_addr,
+								mod_id, idx);
 
 	return true;
 }
@@ -737,8 +749,8 @@ static void node_reset(struct l_timeout *timeout, void *user_data)
 	l_main_quit();
 }
 
-static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
-				uint16_t unicast, uint16_t idx,
+static bool cfg_srv_pkt(uint16_t src, uint32_t dst, uint16_t unicast,
+				uint16_t app_idx, uint16_t net_idx,
 				const uint8_t *data, uint16_t size,
 				uint8_t ttl, const void *user_data)
 {
@@ -751,7 +763,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 	uint8_t msg[11];
 	uint8_t *long_msg = NULL;
 	struct mesh_net_heartbeat *hb;
-	uint16_t net_idx, app_idx;
+	uint16_t n_idx, a_idx;
 	uint8_t state, status;
 	uint8_t phase;
 	bool virt = false;
@@ -759,7 +771,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 	uint16_t interval;
 	uint16_t n;
 
-	if (idx != APP_IDX_DEV_LOCAL)
+	if (app_idx != APP_IDX_DEV_LOCAL)
 		return false;
 
 	if (mesh_model_opcode_get(pkt, size, &opcode, &n)) {
@@ -770,7 +782,8 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 
 	net = node_get_net(node);
 	hb = mesh_net_heartbeat_get(net);
-	l_debug("CONFIG-SRV-opcode 0x%x size %u idx %3.3x", opcode, size, idx);
+	l_debug("CONFIG-SRV-opcode 0x%x size %u idx %3.3x", opcode, size,
+								net_idx);
 
 	n = 0;
 
@@ -814,7 +827,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 25 && size != 27)
 			return true;
 
-		config_pub_set(node, src, unicast, pkt, size,
+		config_pub_set(node, net_idx, src, unicast, pkt, size,
 				!!(opcode & OP_UNRELIABLE));
 		break;
 
@@ -822,24 +835,26 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 11 && size != 13)
 			return true;
 
-		config_pub_set(node, src, unicast, pkt, size,
+		config_pub_set(node, net_idx, src, unicast, pkt, size,
 				!!(opcode & OP_UNRELIABLE));
 		break;
 
 	case OP_CONFIG_MODEL_PUB_GET:
-		config_pub_get(node, src, unicast, pkt, size);
+		config_pub_get(node, net_idx, src, unicast, pkt, size);
 		break;
 
 	case OP_CONFIG_VEND_MODEL_SUB_GET:
 		if (size != 6)
 			return true;
-		config_sub_get(node, src, unicast, pkt, size);
+
+		config_sub_get(node, net_idx, src, unicast, pkt, size);
 		break;
 
 	case OP_CONFIG_MODEL_SUB_GET:
 		if (size != 4)
 			return true;
-		config_sub_get(node, src, unicast, pkt, size);
+
+		config_sub_get(node, net_idx, src, unicast, pkt, size);
 		break;
 
 	case OP_CONFIG_MODEL_SUB_VIRT_OVERWRITE:
@@ -851,7 +866,8 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 	case OP_CONFIG_MODEL_SUB_DELETE:
 	case OP_CONFIG_MODEL_SUB_ADD:
 	case OP_CONFIG_MODEL_SUB_DELETE_ALL:
-		config_sub_set(node, src, unicast, pkt, size, virt, opcode);
+		config_sub_set(node, net_idx, src, unicast, pkt, size, virt,
+									opcode);
 		break;
 
 	case OP_CONFIG_RELAY_SET:
@@ -911,8 +927,8 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 3 || pkt[2] > 0x01)
 			return true;
 
-		net_idx = l_get_le16(pkt);
-		if (net_idx > 0xfff)
+		n_idx = l_get_le16(pkt);
+		if (n_idx > 0xfff)
 			return true;
 
 		/*
@@ -925,17 +941,17 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size < 2)
 			return true;
 
-		net_idx = l_get_le16(pkt);
-		if (net_idx > 0xfff)
+		n_idx = l_get_le16(pkt);
+		if (n_idx > 0xfff)
 			return true;
 
 		n = mesh_model_opcode_set(OP_NODE_IDENTITY_STATUS, msg);
 
-		status = mesh_net_get_identity_mode(net, net_idx, &state);
+		status = mesh_net_get_identity_mode(net, n_idx, &state);
 
 		msg[n++] = status;
 
-		l_put_le16(net_idx, msg + n);
+		l_put_le16(n_idx, msg + n);
 		n += 2;
 
 		msg[n++] = state;
@@ -984,13 +1000,13 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 2)
 			return true;
 
-		net_idx = l_get_le16(pkt);
+		n_idx = l_get_le16(pkt);
 
 		n = mesh_model_opcode_set(OP_CONFIG_KEY_REFRESH_PHASE_STATUS,
 						msg);
 
 		/* State: 0x00-0x03 phase of key refresh */
-		status = mesh_net_key_refresh_phase_get(net, net_idx,
+		status = mesh_net_key_refresh_phase_get(net, n_idx,
 							&phase);
 		if (status != MESH_STATUS_SUCCESS) {
 			b_res = status;
@@ -998,7 +1014,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		}
 
 		msg[n++] = b_res;
-		l_put_le16(net_idx, msg + n);
+		l_put_le16(n_idx, msg + n);
 		n += 2;
 		msg[n++] = phase;
 
@@ -1010,18 +1026,18 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 19)
 			return true;
 
-		net_idx = l_get_le16(pkt) & 0xfff;
-		app_idx = l_get_le16(pkt + 1) >> 4;
+		n_idx = l_get_le16(pkt) & 0xfff;
+		a_idx = l_get_le16(pkt + 1) >> 4;
 
 		if (opcode == OP_APPKEY_ADD)
-			b_res = appkey_key_add(net, net_idx, app_idx, pkt + 3);
+			b_res = appkey_key_add(net, n_idx, a_idx, pkt + 3);
 		else
-			b_res = appkey_key_update(net, net_idx, app_idx,
+			b_res = appkey_key_update(net, n_idx, a_idx,
 								pkt + 3);
 
 		l_debug("Add/Update AppKey %s: Net_Idx %3.3x, App_Idx %3.3x",
 			(b_res == MESH_STATUS_SUCCESS) ? "success" : "fail",
-							net_idx, app_idx);
+							n_idx, a_idx);
 
 
 		n = mesh_model_opcode_set(OP_APPKEY_STATUS, msg);
@@ -1034,15 +1050,14 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 
 	case OP_APPKEY_DELETE:
 		if (size != 3)
-			return
-				true;
+			return true;
 
-		net_idx = l_get_le16(pkt) & 0xfff;
-		app_idx = l_get_le16(pkt + 1) >> 4;
-		b_res = appkey_key_delete(net, net_idx, app_idx);
+		n_idx = l_get_le16(pkt) & 0xfff;
+		a_idx = l_get_le16(pkt + 1) >> 4;
+		b_res = appkey_key_delete(net, n_idx, a_idx);
 		l_debug("Delete AppKey %s Net_Idx %3.3x to App_Idx %3.3x",
 			(b_res == MESH_STATUS_SUCCESS) ? "success" : "fail",
-							net_idx, app_idx);
+							n_idx, a_idx);
 
 		n = mesh_model_opcode_set(OP_APPKEY_STATUS, msg);
 		msg[n++] = b_res;
@@ -1054,16 +1069,17 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 	case OP_APPKEY_GET:
 		if (size != 2)
 			return true;
-		net_idx = l_get_le16(pkt);
+
+		n_idx = l_get_le16(pkt);
 
 		long_msg = l_malloc(CFG_MAX_MSG_LEN);
 		n = mesh_model_opcode_set(OP_APPKEY_LIST, long_msg);
 
-		status = appkey_list(net, net_idx, long_msg + n + 3,
+		status = appkey_list(net, n_idx, long_msg + n + 3,
 						CFG_MAX_MSG_LEN - n - 3, &size);
 
 		long_msg[n] = status;
-		l_put_le16(net_idx, long_msg + n + 1);
+		l_put_le16(n_idx, long_msg + n + 1);
 		n += (size + 3);
 		break;
 
@@ -1072,12 +1088,12 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 		if (size != 18)
 			return true;
 
-		net_idx = l_get_le16(pkt);
+		n_idx = l_get_le16(pkt);
 
 		if (opcode == OP_NETKEY_ADD)
-			b_res = mesh_net_add_key(net, net_idx, pkt + 2);
+			b_res = mesh_net_add_key(net, n_idx, pkt + 2);
 		else
-			b_res = mesh_net_update_key(net, net_idx, pkt + 2);
+			b_res = mesh_net_update_key(net, n_idx, pkt + 2);
 
 		l_debug("NetKey Add/Update %s",
 			(b_res == MESH_STATUS_SUCCESS) ? "success" : "fail");
@@ -1116,20 +1132,20 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 
 	case OP_MODEL_APP_BIND:
 	case OP_MODEL_APP_UNBIND:
-		model_app_bind(node, src, unicast, pkt, size,
+		model_app_bind(node, net_idx, src, unicast, pkt, size,
 				opcode != OP_MODEL_APP_BIND);
 		break;
 
 	case OP_VEND_MODEL_APP_GET:
 		if (size != 6)
 			return true;
-		model_app_list(node, src, unicast, pkt, size);
+		model_app_list(node, net_idx, src, unicast, pkt, size);
 		break;
 
 	case OP_MODEL_APP_GET:
 		if (size != 4)
 			return true;
-		model_app_list(node, src, unicast, pkt, size);
+		model_app_list(node, net_idx, src, unicast, pkt, size);
 		break;
 
 	case OP_CONFIG_HEARTBEAT_PUB_SET:
@@ -1260,7 +1276,7 @@ static bool cfg_srv_pkt(uint16_t src, uint32_t dst,
 	if (n) {
 		/* print_packet("App Tx", long_msg ? long_msg : msg, n); */
 		mesh_model_send(node, unicast, src,
-				APP_IDX_DEV_LOCAL, DEFAULT_TTL,
+				APP_IDX_DEV_LOCAL, net_idx, DEFAULT_TTL,
 				long_msg ? long_msg : msg, n);
 	}
 	l_free(long_msg);
mesh/model.c +63 -66

251 lines not on this page · raw message

mesh/model.h +6 -4
index a6951293f..fa031f598 100644
@@ -67,7 +67,8 @@ struct mesh_model_pub {
 
 typedef void (*mesh_model_unregister)(void *user_data);
 typedef bool (*mesh_model_recv_cb)(uint16_t src, uint32_t dst, uint16_t unicast,
-					uint16_t app_idx, const uint8_t *data,
+					uint16_t app_idx, uint16_t net_idx,
+					const uint8_t *data,
 					uint16_t len, uint8_t ttl,
 					const void *user_data);
 typedef int (*mesh_model_bind_cb)(uint16_t app_idx, int action);
@@ -116,14 +117,15 @@ int mesh_model_sub_get(struct mesh_node *node, uint16_t addr, uint32_t id,
 			uint8_t *buf, uint16_t buf_size, uint16_t *size);
 uint16_t mesh_model_cfg_blk(uint8_t *pkt);
 bool mesh_model_send(struct mesh_node *node, uint16_t src, uint16_t dst,
-					uint16_t app_idx, uint8_t ttl,
+					uint16_t app_idx, uint16_t net_idx,
+					uint8_t ttl,
 					const void *msg, uint16_t msg_len);
 int mesh_model_publish(struct mesh_node *node, uint32_t mod_id, uint16_t src,
 				uint8_t ttl, const void *msg, uint16_t msg_len);
 bool mesh_model_rx(struct mesh_node *node, bool szmict, uint32_t seq0,
 			uint32_t seq, uint32_t iv_index, uint8_t ttl,
-			uint16_t src, uint16_t dst, uint8_t key_id,
-			const uint8_t *data, uint16_t size);
+			uint16_t net_idx, uint16_t src, uint16_t dst,
+			uint8_t key_aid, const uint8_t *data, uint16_t size);
 void mesh_model_app_key_generate_new(struct mesh_node *node, uint16_t net_idx);
 void mesh_model_app_key_delete(struct mesh_node *node, struct l_queue *models,
 								uint16_t idx);
mesh/net.c +52 -40

234 lines not on this page · raw message

mesh/net.h +3 -3
index 7b0733578..834a7351a 100644
@@ -300,9 +300,9 @@ void mesh_net_transport_send(struct mesh_net *net, uint32_t key_id,
 				const uint8_t *msg, uint16_t msg_len);
 
 bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,
-				uint16_t dst, uint8_t key_id, uint8_t ttl,
-				uint32_t seq, uint32_t iv_index, bool szmic,
-				const void *msg, uint16_t msg_len,
+				uint16_t dst, uint8_t key_id, uint16_t net_idx,
+				uint8_t ttl, uint32_t seq, uint32_t iv_index,
+				bool szmic, const void *msg, uint16_t msg_len,
 				mesh_net_status_func_t status_func,
 				void *user_data);
 void mesh_net_ack_send(struct mesh_net *net, uint32_t key_id,
mesh/node.c +5 -5
index 27235ef34..40d103327 100644
@@ -1790,12 +1790,12 @@ static struct l_dbus_message *send_call(struct l_dbus *dbus,
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
 							"Incorrect data");
 
-	if ((app_idx & APP_IDX_MASK) != app_idx)
+	if (app_idx & ~APP_IDX_MASK)
 		return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
 						"Invalid key_index");
 
-	if (!mesh_model_send(node, src, dst, app_idx & APP_IDX_MASK,
-				mesh_net_get_default_ttl(node->net), data, len))
+	if (!mesh_model_send(node, src, dst, app_idx, 0, DEFAULT_TTL,
+								data, len))
 		return dbus_error(msg, MESH_ERROR_FAILED, NULL);
 
 	return l_dbus_message_new_method_return(msg);
@@ -1837,8 +1837,8 @@ static struct l_dbus_message *dev_key_send_call(struct l_dbus *dbus,
 							"Incorrect data");
 
 	/* TODO: use net_idx */
-	if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE,
-				mesh_net_get_default_ttl(node->net), data, len))
+	if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx,
+							DEFAULT_TTL, data, len))
 		return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
 
 	return l_dbus_message_new_method_return(msg);
Signature · 1 line
2.14.5
#4 lore raw

Re: [PATCH BlueZ 3/3] mesh: Propagate Net Index up Rx message chain

Hi Brian,

Quoted 15 lines · Brian Gix

When a model receives a message, it is required by the spec to respond using the same credentials. When an App Key is used, this is trivial because App keys are bound to Net keys, so only the App Index is required. Messages received on a Device key however, need the Net Index preserved from original message for re-use during the response.

---
 mesh/cfgmod-server.c | 154 ++++++++++++++++++++++++++++-----------------------
 mesh/model.c         | 129 +++++++++++++++++++++---------------------
 mesh/model.h         |  10 ++--
 mesh/net.c           |  92 +++++++++++++++++-------------
 mesh/net.h           |   6 +-
 mesh/node.c          |  10 ++--
 6 files changed, 214 insertions(+), 187 deletions(-)

(...)

Quoted 44 lines
mesh/net.c +3 -5
index a5693f154..ba52867c4 100644
@@ -119,7 +119,6 @@ struct mesh_net {
 	unsigned int pkt_id;
 	unsigned int bea_id;
 	unsigned int beacon_id;
-	unsigned int key_id_next;
 	unsigned int sar_id_next;
 
 	bool friend_enable;
@@ -203,7 +202,7 @@ struct mesh_sar {
 	bool frnd_cred;
 	uint8_t ttl;
 	uint8_t last_seg;
-	uint8_t key_id;
+	uint8_t key_aid;
 	uint8_t buf[4]; /* Large enough for ACK-Flags and MIC */
 };
 
@@ -225,7 +224,7 @@ struct msg_rx {
 	union {
 		struct {
 			uint16_t app_idx;
-			uint8_t key_id;
+			uint8_t key_aid;
 		} m;
 		struct {
 			uint16_t seq0;
@@ -668,7 +667,6 @@ struct mesh_net *mesh_net_new(struct mesh_node *node)
 	net->node = node;
 	net->pkt_id = 0;
 	net->bea_id = 0;
-	net->key_id_next = 0;
 
 	net->beacon_enable = true;
 	net->proxy_enable = false;
@@ -676,7 +674,7 @@ struct mesh_net *mesh_net_new(struct mesh_node *node)
 
 	net->seq_num = 0x000000;
 	net->src_addr = 0x0000;
-	net->default_ttl = 0x00;
+	net->default_ttl = 0x7f;

(...)

mesh/net.c bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src, · line 3425

Quoted 7 lines
mesh/net.c +1 -1
@@ -3408,18 +3421,17 @@ bool mesh_net_app_send(struct mesh_net *net, bool frnd_cred, uint16_t src,
 	if (!src || !dst)
 		return false;
 
-	if (ttl == 0xff)
+	if (ttl == DEFAULT_TTL)
 		ttl = net->default_ttl;

(...)

I would keep these in a separate patch, it's not really related to key ids/indexes.

Otherwise, LGTM!

Signature · 3 lines
Michał Lowas-Rzechonek <[email protected]>
Silvair http://silvair.com
Jasnogórska 44, 31-358 Krakow, POLAND
author
#5 lore raw
In reply to Brian Gix

Patch-set applied

Quoted 32 lines · Brian Gix

Two major flaws are corrected by this patch-set:

1. The Spec defined Access Layer AID was being confused with the internally used key_id which is used to identify Network keys for multiple networks, sub-networks, and friendships. This confused ID was then being used instead of the correct Network Key ID for outgoing messages.

2. The specification requires *responses* to all standard Server messages to be sent with the same credentials as the received command that triggered them. This requires more complete propagation of the Net Index value on received messages, so that the same Network key is used in the reverse direction.

This additionally now uses the requested Net Index when sending Device Key secured messages through the D-Bus interface.

Brian Gix (3):
mesh: Add bound Net Index lookup for App Keys
mesh: Normalize Access Key AID naming convention
mesh: Propagate Net Index up Rx message chain

 mesh/appkey.c        |  59 +++++++++++++-------
 mesh/appkey.h        |   1 +
 mesh/cfgmod-server.c | 154 ++++++++++++++++++++++++++++-----------------------
 mesh/crypto.c        |  37 ++++++-------
 mesh/crypto.h        |   8 +--
 mesh/model.c         | 129 +++++++++++++++++++++---------------------
 mesh/model.h         |  10 ++--
 mesh/net.c           |  92 +++++++++++++++++-------------
 mesh/net.h           |  10 ++--
 mesh/node.c          |  10 ++--
 10 files changed, 276 insertions(+), 234 deletions(-)