Skip to content

Commit a3aa38b

Browse files
Tero Heinonendeepakvenugopal
authored andcommitted
Thread extension commission updates (#1870)
1 parent 3e89d0a commit a3aa38b

File tree

5 files changed

+122
-37
lines changed

5 files changed

+122
-37
lines changed

source/6LoWPAN/Thread/thread_extension.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,7 @@ static void thread_extension_reset_timeout_cb(void *arg)
210210
// Delete all domain stuff and start discovery.
211211
thread_extension_bootstrap_network_certificate_set(cur, NULL, 0);
212212
thread_extension_bootstrap_network_private_key_set(cur, NULL, 0);
213-
thread_nvm_store_active_configuration_remove();
214213
thread_nvm_store_mleid_rloc_map_remove();
215-
thread_nvm_store_pending_configuration_remove();
216214
thread_nvm_store_link_info_clear();
217215
thread_joiner_application_link_configuration_delete(cur->id);
218216
thread_bootstrap_connection_error(cur->id, CON_ERROR_NETWORK_KICK, NULL);
@@ -295,8 +293,6 @@ static int thread_extension_reenroll_req_cb(int8_t service_id, uint8_t source_ad
295293
goto send_response;
296294
}
297295

298-
299-
300296
thread_extension_bootstrap_reenrollment_start(cur, service_id, pbbr_addr);
301297

302298
send_response:

source/6LoWPAN/Thread/thread_extension_bbr.c

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
typedef struct {
7070
uint8_t pbbr_multicast_address[16];
7171
uint8_t tri_address[16];
72+
uint8_t registrar_address[16];
7273
uint8_t domain_prefix[8];
7374
uint32_t mlr_timeout;
7475
uint32_t delay_timer;
@@ -317,6 +318,7 @@ static int thread_pbbr_data_req_recv_cb(int8_t service_id, uint8_t source_addres
317318
(void) source_port;
318319

319320
uint8_t payload_ptr[18] = {0};
321+
uint8_t *address_ptr;
320322
uint8_t *request_tlv_ptr;
321323
uint16_t request_tlv_len;
322324
uint8_t *ptr = payload_ptr;
@@ -326,6 +328,8 @@ static int thread_pbbr_data_req_recv_cb(int8_t service_id, uint8_t source_addres
326328
return -1;
327329
}
328330

331+
tr_debug("MGMT_BBR_GET.req received");
332+
329333
request_tlv_len = thread_tmfcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_GET, &request_tlv_ptr);
330334

331335
if (0 == request_tlv_len) {
@@ -335,7 +339,14 @@ static int thread_pbbr_data_req_recv_cb(int8_t service_id, uint8_t source_addres
335339

336340
if (thread_meshcop_tlv_list_type_available(request_tlv_ptr, request_tlv_len, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS)) {
337341
tr_debug("Registrar IPv6 address requested");
338-
ptr = thread_meshcop_tlv_data_write(ptr, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS, 16, this->tri_address);
342+
// If registrar address is not set, return TRI address
343+
if (addr_is_ipv6_unspecified(this->registrar_address)) {
344+
address_ptr = this->tri_address;
345+
} else {
346+
address_ptr = this->registrar_address;
347+
}
348+
349+
ptr = thread_meshcop_tlv_data_write(ptr, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS, 16, address_ptr);
339350
}
340351

341352
coap_service_response_send(this->coap_service_id, COAP_REQUEST_OPTIONS_NONE, request_ptr, COAP_MSG_CODE_RESPONSE_CHANGED, COAP_CT_OCTET_STREAM, payload_ptr, ptr - payload_ptr);
@@ -348,6 +359,35 @@ static int thread_pbbr_data_set_recv_cb(int8_t service_id, uint8_t source_addres
348359
(void) source_address;
349360
(void) source_port;
350361

362+
uint8_t response[3] = {0};
363+
uint8_t *ptr = response;
364+
int8_t response_code = -1;
365+
uint8_t *registrar_addr = NULL;
366+
thread_pbbr_t *this = thread_border_router_find_by_service(service_id);
367+
368+
if (!request_ptr || !this) {
369+
return -1;
370+
}
371+
372+
tr_debug("MGMT_BBR_SET.req received");
373+
374+
if (16 == thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS, &registrar_addr)) {
375+
memcpy(this->registrar_address, registrar_addr, 16);
376+
response_code = 1;
377+
}
378+
379+
ptr = thread_meshcop_tlv_data_write_uint8(response, MESHCOP_TLV_STATE, response_code);
380+
381+
coap_service_response_send(service_id, COAP_REQUEST_OPTIONS_NONE, request_ptr, COAP_MSG_CODE_RESPONSE_CHANGED, COAP_CT_OCTET_STREAM, response, ptr - response);
382+
383+
return 0;
384+
}
385+
386+
static int thread_pbbr_sec_data_set_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
387+
{
388+
(void) source_address;
389+
(void) source_port;
390+
351391
uint8_t response[3] = {0};
352392
uint8_t *ptr = response;
353393

@@ -1144,7 +1184,9 @@ static int thread_extension_bbr_pbbr_start(thread_pbbr_t *this)
11441184
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_NMK_RX_NTF, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_pbbr_nmkp_relay_rx_recv_cb);
11451185
// Register BBR data request URI
11461186
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_DATA_REQ, COAP_SERVICE_ACCESS_GET_ALLOWED, thread_pbbr_data_req_recv_cb);
1147-
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_DATA_SET, COAP_SERVICE_ACCESS_GET_ALLOWED, thread_pbbr_data_set_recv_cb);
1187+
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_DATA_SET, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_pbbr_data_set_recv_cb);
1188+
coap_service_register_uri(this->coap_service_id, THREAD_URI_MGMT_SEC_PENDING_SET, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_pbbr_sec_data_set_recv_cb);
1189+
11481190

11491191
// create secure service for Network master key provisioning
11501192
this->coap_nmkp_virtual_service_id = coap_service_initialize(this->interface_id, THREAD_MANAGEMENT_PORT, COAP_SERVICE_OPTIONS_SECURE | COAP_SERVICE_OPTIONS_VIRTUAL_SOCKET, thread_pbbr_pskd_security_start_cb, NULL);
@@ -1181,7 +1223,8 @@ int8_t thread_extension_bbr_init(int8_t interface_id, int8_t backbone_interface_
11811223
this->delay_timer = THREAD_BBR_DUA_REGISTRATION_DELAY;
11821224
this->pbbr_started = false;
11831225
memcpy(this->pbbr_multicast_address, ADDR_LINK_LOCAL_ALL_ROUTERS, 16);
1184-
this->pbbr_port = THREAD_BBR_BACKBONE_PORT;
1226+
this->pbbr_port = THREAD_BBR_BACKBONE_PORT;
1227+
memset(this->registrar_address, 0, 16);
11851228
memcpy(this->tri_address, ADDR_LINK_LOCAL_ALL_ROUTERS, 16);
11861229
this->tri_port = THREAD_BBR_BACKBONE_PORT;
11871230
this->joiner_router_rloc = 0xffff;

source/6LoWPAN/Thread/thread_extension_bootstrap.c

Lines changed: 75 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "6LoWPAN/Thread/thread_extension_bootstrap.h"
4747
#include "6LoWPAN/Thread/thread_extension_constants.h"
4848
#include "6LoWPAN/Thread/thread_management_server.h"
49+
#include "6LoWPAN/Thread/thread_nvm_store.h"
4950

5051

5152
/*
@@ -71,12 +72,14 @@ typedef struct thread_extension_credentials {
7172

7273
int8_t coap_service_secure_session_id;
7374
int8_t interface_id;
75+
bool reattach_ongoing;
7476

7577
ns_list_link_t link;
7678
} thread_ccm_credentials_t;
7779

7880
#ifdef HAVE_THREAD_V2
7981

82+
#if 1
8083
/* Hardcoded CSR request */
8184
static const unsigned char csr_request[215] = {
8285
0x30, 0x81, 0xd4, 0x30, 0x7c, 0x02, 0x01, 0x00, 0x30, 0x1a, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03,
@@ -94,6 +97,25 @@ static const unsigned char csr_request[215] = {
9497
0x63, 0xea, 0xe3, 0xd2, 0xf1, 0x50, 0x48, 0x56, 0xdf, 0x6b, 0xcf, 0xc4, 0x31, 0xc4, 0xcf, 0xbc,
9598
0x26, 0xe3, 0x5a, 0x74, 0x62, 0x0f, 0x70
9699
};
100+
#else
101+
/* CSR request with CBOR header*/
102+
static const unsigned char csr_request[244] = {
103+
0x58, 0xf2, 0x30, 0x81, 0xef, 0x30, 0x81, 0x95, 0x02, 0x01, 0x01, 0x30, 0x33, 0x31, 0x1c, 0x30, 0x1a, 0x06,
104+
0x03, 0x55, 0x04, 0x03, 0x0c, 0x13, 0x4c, 0x69, 0x67, 0x68, 0x74, 0x69, 0x66, 0x79, 0x20, 0x50, 0x72, 0x6f,
105+
0x20, 0x4d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x0a,
106+
0x4f, 0x53, 0x52, 0x41, 0x4d, 0x20, 0x47, 0x6d, 0x62, 0x48, 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2a, 0x86,
107+
0x48, 0xce, 0x3d, 0x02, 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03, 0x42, 0x00,
108+
0x04, 0xd8, 0x19, 0x64, 0x07, 0xca, 0x38, 0x01, 0x62, 0xfd, 0x7e, 0xe7, 0x07, 0x8d, 0x21, 0x50, 0x0b, 0x9f,
109+
0x00, 0x71, 0x26, 0xaa, 0x55, 0x2a, 0x44, 0x9b, 0xe8, 0xfd, 0xfb, 0x0e, 0x8d, 0x41, 0x01, 0xf2, 0x7b, 0x2e,
110+
0x7b, 0xe4, 0x4f, 0x35, 0x00, 0x0b, 0x1f, 0xbc, 0x86, 0x57, 0xa1, 0x69, 0x32, 0x49, 0xcf, 0xd7, 0x2f, 0x0b,
111+
0xfa, 0x22, 0x44, 0x0b, 0x6f, 0xf4, 0xb5, 0xbd, 0x0f, 0x20, 0xab, 0xa0, 0x00, 0x30, 0x0a, 0x06, 0x08, 0x2a,
112+
0x86, 0x48, 0xce, 0x3d, 0x04, 0x03, 0x02, 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xb5, 0xa2, 0x8b,
113+
0xf9, 0xbf, 0x7d, 0x2c, 0x72, 0x3e, 0xf0, 0xad, 0x1e, 0x38, 0x28, 0xc0, 0xa3, 0xe8, 0xd6, 0x70, 0x9a, 0x2a,
114+
0xf8, 0x1d, 0x33, 0x9d, 0xbb, 0x6c, 0x4f, 0x7c, 0x81, 0xb6, 0x71, 0x02, 0x21, 0x00, 0xba, 0x74, 0x50, 0xad,
115+
0x27, 0x2e, 0x00, 0x71, 0x68, 0x7f, 0xe0, 0x2c, 0x8c, 0x1b, 0x6f, 0x95, 0x8c, 0x58, 0x1e, 0xe7, 0xe3, 0xa5,
116+
0x50, 0xca, 0x12, 0x0a, 0x60, 0x56, 0xd2, 0x3a, 0xe2, 0xeb
117+
}
118+
#endif
97119

98120
/*
99121
* Private key for certificate m_device_nxp_sn_and_8021ar.cert.pem -> test registrar will return this as default
@@ -155,19 +177,29 @@ static void thread_extension_bootstrap_attach_cb(void *arg)
155177
if (!this) {
156178
return;
157179
}
158-
// Cleaning up the joining information
159-
if (this->ccm_done_cb) {// This is successfull
160-
this->ccm_done_cb(this->interface_id);
180+
if (this->reattach_ongoing) {
181+
this->reattach_ongoing = false;
182+
thread_nvm_store_mleid_rloc_map_remove();
183+
thread_nvm_store_link_info_clear();
184+
thread_joiner_application_link_configuration_delete(this->interface_id);
185+
thread_bootstrap_connection_error(this->interface_id, CON_ERROR_NETWORK_KICK, NULL);
186+
} else {
187+
// Cleaning up the joining information
188+
if (this->ccm_done_cb) {// This is successfull
189+
this->ccm_done_cb(this->interface_id);
190+
}
161191
}
162192
}
163193

164-
int8_t thread_extension_bootstrap_network_reattach(int8_t interface_id, uint16_t timeout)
194+
int8_t thread_extension_bootstrap_network_reattach(int8_t service_id, uint16_t timeout, bool clear_data)
165195
{
166-
thread_ccm_credentials_t *this = thread_extension_bootstrap_find_by_service(interface_id);
196+
thread_ccm_credentials_t *this = thread_extension_bootstrap_find_by_service(service_id);
167197
if (!this) {
168198
return -1;
169199
}
170200

201+
this->reattach_ongoing = clear_data;
202+
171203
// re-attach in any case and close the secure connection
172204
this->attach_timeout = eventOS_timeout_ms(thread_extension_bootstrap_attach_cb, timeout, this);
173205

@@ -185,26 +217,14 @@ static int commercial_bootstrap_security_start_cb(int8_t service_id, uint8_t add
185217

186218
return 0;
187219
}
188-
static int thread_joiner_application_simple_enroll_response_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr)
220+
221+
static int thread_extension_bootstrap_enroll_parse(protocol_interface_info_entry_t *cur, uint8_t *payload_ptr, uint16_t payload_len)
189222
{
190-
(void) source_address;
191-
(void) source_port;
192223
uint8_t *ptr;
193224
uint16_t len, flen;
194225

195-
// re-attach in any case and close the secure connection
196-
thread_extension_bootstrap_network_reattach(service_id, 1000);
197-
coap_service_close_secure_connection(service_id, source_address, source_port);
198-
199-
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_extension_bootstrap_find_id_by_service(service_id));
200-
201-
tr_debug("Simple enrollment received len:%d - %s", response_ptr->payload_len, trace_array(response_ptr->payload_ptr, response_ptr->payload_len));
202-
203-
if (!cur || !cur->thread_info || !response_ptr) {
204-
return -1;
205-
}
206-
ptr = response_ptr->payload_ptr;
207-
len = response_ptr->payload_len;
226+
ptr = payload_ptr;
227+
len = payload_len;
208228
// CBOR format check
209229
if (*ptr == 0x58) {
210230
flen = *(ptr + 1);
@@ -224,6 +244,27 @@ static int thread_joiner_application_simple_enroll_response_cb(int8_t service_id
224244
tr_warn("ae response parse failed, len %d != %d", len, flen);
225245
}
226246

247+
return 0;
248+
}
249+
250+
static int thread_joiner_application_simple_enroll_response_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *response_ptr)
251+
{
252+
(void) source_address;
253+
(void) source_port;
254+
255+
// re-attach in any case and close the secure connection
256+
thread_extension_bootstrap_network_reattach(service_id, 1000, false);
257+
coap_service_close_secure_connection(service_id, source_address, source_port);
258+
259+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_extension_bootstrap_find_id_by_service(service_id));
260+
261+
tr_debug("Simple enrollment received len:%d - %s",response_ptr->payload_len,trace_array(response_ptr->payload_ptr, response_ptr->payload_len));
262+
263+
if (!cur || !cur->thread_info || !response_ptr) {
264+
return -1;
265+
}
266+
267+
thread_extension_bootstrap_enroll_parse(cur, response_ptr->payload_ptr, response_ptr->payload_len);
227268

228269
return 0;
229270
}
@@ -398,7 +439,7 @@ static int thread_joiner_application_nmkp_response_cb(int8_t service_id, uint8_t
398439
tr_debug("nmkp provisioning done");
399440

400441
// re-attach in any case and close the secure connection
401-
thread_extension_bootstrap_network_reattach(service_id, 1000);
442+
thread_extension_bootstrap_network_reattach(service_id, 1000, false);
402443
coap_service_close_secure_connection(service_id, source_address, source_port);
403444

404445
// CoAP message failed - try to reattach
@@ -682,18 +723,24 @@ static int thread_extension_reenroll_resp_cb(int8_t service_id, uint8_t source_a
682723
(void) service_id;
683724
(void) source_address;
684725
(void) source_port;
726+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_extension_bootstrap_find_id_by_service(service_id));
685727

686-
if (!response_ptr) {
687-
tr_debug("No response to re-enroll req");
728+
// Close secure connection
729+
coap_service_close_secure_connection(service_id, source_address, source_port);
730+
731+
if (!response_ptr || !cur) {
732+
tr_debug("re-enroll failed");
688733
return -1;
689734
}
690735

691736
tr_debug("re-enroll resp len %d", response_ptr->payload_len);
692737

693-
// todo:check & update new certificate
694-
695-
// Close secure connection
696-
coap_service_close_secure_connection(service_id, source_address, source_port);
738+
// todo:check new certificate
739+
// Update certificate
740+
if (0 == thread_extension_bootstrap_enroll_parse(cur, response_ptr->payload_ptr, response_ptr->payload_len)) {
741+
// start NMKP with new certificates
742+
thread_extension_bootstrap_network_reattach(service_id, 5000, true);
743+
}
697744

698745
return 0;
699746
}

source/6LoWPAN/Thread/thread_extension_bootstrap.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ int thread_extension_bootstrap_network_certificate_enable(protocol_interface_inf
5858

5959
int thread_extension_bootstrap_reenrollment_start(protocol_interface_info_entry_t *cur, int8_t service_id, uint8_t *pbbr_addr);
6060

61-
int8_t thread_extension_bootstrap_network_reattach(int8_t interface_id, uint16_t timeout);
62-
6361
#else
6462
#define thread_extension_bootstrap_free(cur);
6563
#define thread_extension_bootstrap_device_certificate_set(cur, device_certificate_ptr, device_certificate_len, priv_key_ptr, priv_key_len) (-1)

source/6LoWPAN/Thread/thread_extension_constants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ typedef struct discovery_additional_info {
6969
#define THREAD_URI_BBR_BB_QRY_NTF "b/bq" //<* Backbone border router
7070
#define THREAD_URI_BBR_BB_ANS_NTF "b/ba" //<* Backbone border router
7171
#define THREAD_URI_BBR_NMKP_REQ "c/cjf" //<* Backbone border router
72+
#define THREAD_URI_MGMT_SEC_PENDING_SET "c/sp" //<* Secure Dissemination of Pending Operational Dataset
7273

7374
#define THREAD_URI_REENROLL_REQ "c/re" //<* Device re-enrollment request
7475
#define THREAD_URI_RESET_REQ "c/rt" //<* Device reset request - instruct to remove itself from Thread domain

0 commit comments

Comments
 (0)