Skip to content

Commit 927c329

Browse files
author
Tero Heinonen
authored
Thread extension commissioning updates (ARMmbed#1828)
1 parent 160ef0a commit 927c329

File tree

5 files changed

+319
-26
lines changed

5 files changed

+319
-26
lines changed

source/6LoWPAN/Thread/thread_extension.c

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include "6LoWPAN/Thread/thread_extension_bootstrap.h"
5454
#include "6LoWPAN/Thread/thread_extension_constants.h"
5555
#include "6LoWPAN/Thread/thread_neighbor_class.h"
56+
#include "6LoWPAN/Thread/thread_nvm_store.h"
5657
#include "6LoWPAN/MAC/mac_helper.h"
5758
#include "Common_Protocols/icmpv6.h"
5859
#include "NWK_INTERFACE/Include/protocol.h"
@@ -74,6 +75,7 @@ typedef struct thread_extension_info {
7475
uint8_t sequence_number;
7576
uint32_t delay_timer;
7677
uint32_t mlr_timer;
78+
timeout_t *reset_timeout;
7779
uint16_t rloc;
7880
uint16_t relay_port_ae;
7981
uint16_t relay_port_nmkp;
@@ -195,6 +197,114 @@ static int thread_extension_mtd_dua_ntf_cb(int8_t service_id, uint8_t source_add
195197
return 0;
196198
}
197199

200+
static void thread_extension_reset_timeout_cb(void *arg)
201+
{
202+
protocol_interface_info_entry_t *cur = (protocol_interface_info_entry_t *)arg;
203+
204+
if (!cur) {
205+
return;
206+
}
207+
208+
// Delete all domain stuff and start discovery.
209+
thread_extension_bootstrap_network_certificate_set(cur, NULL, 0);
210+
thread_extension_bootstrap_network_private_key_set(cur, NULL, 0);
211+
thread_nvm_store_active_configuration_remove();
212+
thread_nvm_store_mleid_rloc_map_remove();
213+
thread_nvm_store_pending_configuration_remove();
214+
thread_nvm_store_link_info_clear();
215+
thread_joiner_application_link_configuration_delete(cur->id);
216+
thread_bootstrap_connection_error(cur->id, CON_ERROR_NETWORK_KICK, NULL);
217+
}
218+
219+
/*
220+
* Commissioner requests to leave this domain
221+
*/
222+
static int thread_extension_reset_req_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
223+
{
224+
(void) source_address;
225+
(void) source_port;
226+
227+
uint8_t payload[3] = {0};
228+
uint8_t *ptr = payload;
229+
uint8_t *signature_ptr = NULL;
230+
uint16_t session_id = 0;
231+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_management_server_interface_id_get(service_id));
232+
233+
if (!cur || !request_ptr) {
234+
return -1;
235+
}
236+
237+
tr_debug("Received MGMT_RESET.req");
238+
239+
// Verify request TLV's: Commissioner Session ID TLV - Commissioner Token TLV (optional) - Commissioner Signature TLV
240+
thread_meshcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_COMMISSIONER_SESSION_ID, &session_id);
241+
thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_COMM_SIGNATURE, &signature_ptr);
242+
243+
if ((session_id != thread_info(cur)->registered_commissioner.session_id) /*|| (signature_ptr == NULL)*/) { // todo: signature may not come in this early phase of implementors
244+
tr_debug("Request parse failed");
245+
ptr = thread_meshcop_tlv_data_write_uint8(ptr, MESHCOP_TLV_STATE, -1);
246+
}
247+
248+
// Downgrade if router
249+
if (thread_am_router(cur)) {
250+
thread_bootstrap_attached_downgrade_router(cur);
251+
}
252+
253+
// Delete all data and start reattach
254+
// Get some time to send response and keep the Commissioner happy
255+
cur->thread_info->extension_info->reset_timeout = eventOS_timeout_ms(thread_extension_reset_timeout_cb, 5000, cur);
256+
257+
// Send response
258+
coap_service_response_send(service_id, COAP_REQUEST_OPTIONS_NONE, request_ptr, COAP_MSG_CODE_RESPONSE_CHANGED, COAP_CT_NONE, payload, ptr - payload);
259+
260+
return 0;
261+
}
262+
263+
264+
/*
265+
* Commissioner requests to get new certificate from Registrar, but to stay in the same domain
266+
*/
267+
static int thread_extension_reenroll_req_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
268+
{
269+
(void) source_address;
270+
(void) source_port;
271+
272+
uint8_t pbbr_addr[16] = {0};
273+
uint8_t status_tlv[3] = {0};
274+
uint8_t *ptr = status_tlv;
275+
uint8_t *signature_ptr = NULL;
276+
uint16_t session_id = 0;
277+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(thread_management_server_interface_id_get(service_id));
278+
279+
if (!cur || !request_ptr) {
280+
return -1;
281+
}
282+
283+
tr_debug("Received MGMT_REENROLL.req");
284+
285+
// Verify request TLV's: Commissioner Session ID TLV - Commissioner Token TLV (optional) - Commissioner Signature TLV
286+
thread_meshcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_COMMISSIONER_SESSION_ID, &session_id);
287+
thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_COMM_SIGNATURE, &signature_ptr);
288+
289+
if ((session_id != thread_info(cur)->registered_commissioner.session_id) ||/* (signature_ptr == NULL) || */
290+
thread_extension_primary_bbr_get(cur, pbbr_addr, NULL, NULL, NULL)) {
291+
tr_debug("Request parse failed");
292+
ptr = thread_meshcop_tlv_data_write_uint8(ptr, MESHCOP_TLV_STATE, -1);
293+
goto send_response;
294+
}
295+
296+
297+
298+
thread_extension_bootstrap_reenrollment_start(cur, service_id, pbbr_addr);
299+
300+
send_response:
301+
302+
// send response
303+
coap_service_response_send(service_id, COAP_REQUEST_OPTIONS_NONE, request_ptr, COAP_MSG_CODE_RESPONSE_CHANGED, COAP_CT_NONE, status_tlv, ptr-status_tlv);
304+
return 0;
305+
}
306+
307+
198308
static int thread_extension_primary_bbr_update_needed(struct protocol_interface_info_entry *cur, uint16_t rloc, uint8_t seq)
199309
{
200310
if (!cur->thread_info->extension_info) {
@@ -444,6 +554,8 @@ void thread_extension_init(int8_t interface_id, int8_t coap_service_id)
444554
return;
445555
}
446556
cur->thread_info->extension_info->coap_service_id = coap_service_id;
557+
coap_service_register_uri(thread_info(cur)->extension_info->coap_service_id, THREAD_URI_RESET_REQ, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_extension_reset_req_cb);
558+
coap_service_register_uri(thread_info(cur)->extension_info->coap_service_id, THREAD_URI_REENROLL_REQ, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_extension_reenroll_req_cb);
447559
}
448560

449561
void thread_extension_mtd_service_register(protocol_interface_info_entry_t *cur)

source/6LoWPAN/Thread/thread_extension_bbr.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,38 @@ static int thread_pbbr_relay_to_tri_cb(int8_t service_id, uint8_t source_address
311311
return 0;
312312
}
313313

314+
static int thread_pbbr_data_req_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
315+
{
316+
(void) source_address;
317+
(void) source_port;
318+
319+
uint8_t payload_ptr[18] = {0};
320+
uint8_t *request_tlv_ptr;
321+
uint16_t request_tlv_len;
322+
uint8_t *ptr = payload_ptr;
323+
thread_pbbr_t *this = thread_border_router_find_by_service(service_id);
324+
325+
if (!this || !request_ptr) {
326+
return -1;
327+
}
328+
329+
request_tlv_len = thread_tmfcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, MESHCOP_TLV_GET, &request_tlv_ptr);
330+
331+
if (0 == request_tlv_len) {
332+
//error handling
333+
return 0;
334+
}
335+
336+
if (thread_meshcop_tlv_list_type_available(request_tlv_ptr, request_tlv_len, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS)) {
337+
tr_debug("Registrar IPv6 address requested");
338+
ptr = thread_meshcop_tlv_data_write(ptr, MESHCOP_TLV_REGISTRAR_IPV6_ADDRESS, 16, this->tri_address);
339+
}
340+
341+
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);
342+
343+
return 0;
344+
}
345+
314346
static int thread_pbbr_nmkp_relay_rx_recv_cb(int8_t service_id, uint8_t source_address[static 16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
315347
{
316348
thread_pbbr_t *this = thread_border_router_find_by_service(service_id);
@@ -1032,6 +1064,7 @@ static int thread_extension_bbr_pbbr_stop(thread_pbbr_t *this)
10321064
coap_service_unregister_uri(this->coap_service_id, THREAD_URI_BBR_NMK_RX_NTF);
10331065
coap_service_unregister_uri(this->coap_service_id, THREAD_URI_BBR_DOMAIN_ADDRESS_REGISTRATION);
10341066
coap_service_unregister_uri(this->coap_service_id, THREAD_URI_BBR_MCAST_LISTENER_REPORT);
1067+
coap_service_unregister_uri(this->coap_service_id, THREAD_URI_BBR_DATA_REQ);
10351068

10361069
coap_service_delete(this->br_bb_service_id);
10371070
this->br_bb_service_id = -1;
@@ -1108,6 +1141,8 @@ static int thread_extension_bbr_pbbr_start(thread_pbbr_t *this)
11081141
// Register Mesh side relay URI
11091142
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_TRI_RX_NTF, COAP_SERVICE_ACCESS_POST_ALLOWED, thread_pbbr_relay_to_tri_cb);
11101143
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);
1144+
// Register BBR data request URI
1145+
coap_service_register_uri(this->coap_service_id, THREAD_URI_BBR_DATA_REQ, COAP_SERVICE_ACCESS_GET_ALLOWED, thread_pbbr_data_req_recv_cb);
11111146

11121147
// create secure service for Network master key provisioning
11131148
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);

0 commit comments

Comments
 (0)