Skip to content

Commit 58a60e7

Browse files
address query logic implemented(#1667)
1 parent c25df78 commit 58a60e7

File tree

5 files changed

+92
-45
lines changed

5 files changed

+92
-45
lines changed

source/6LoWPAN/Thread/thread_extension_bbr.c

Lines changed: 82 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ static void thread_border_router_bb_ans_send(thread_pbbr_t *this, uint8_t *desti
229229
return;
230230
}
231231

232-
static void thread_border_router_bb_qry_send(thread_pbbr_t *this, uint8_t *target_eid_ptr, uint16_t *rloc_ptr)
232+
static void thread_border_router_bb_qry_send(thread_pbbr_t *this, const uint8_t *target_eid_ptr, uint16_t *rloc_ptr)
233233
{
234234
uint8_t *ptr;
235235
uint8_t payload[22];
@@ -446,7 +446,7 @@ static int thread_pbbr_bb_qry_cb(int8_t service_id, uint8_t source_address[16],
446446
if (!link_configuration_ptr || !cur) {
447447
return -1;
448448
}
449-
if (addr_is_assigned_to_interface(cur,source_address)) {
449+
if (addr_interface_address_compare(cur, source_address) == 0) {
450450
// Received from own address no need to process
451451
return 0;
452452
}
@@ -473,15 +473,57 @@ static int thread_pbbr_bb_qry_cb(int8_t service_id, uint8_t source_address[16],
473473

474474
return 0;
475475
}
476+
477+
static int thread_pbbr_dua_duplicate_address_detection(int8_t service_id, uint8_t *addr_data_ptr, uint8_t *ml_eid_ptr)
478+
{
479+
thread_pbbr_t *this = thread_border_router_find_by_service(service_id);
480+
481+
if (!this) {
482+
return -1;
483+
}
484+
485+
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(this->interface_id);
486+
duplicate_dua_tr_t *tr_ptr = thread_border_router_dup_tr_find(this->interface_id,addr_data_ptr);
487+
488+
if (!tr_ptr) {
489+
return -1;
490+
}
491+
492+
ipv6_route_t *route = ipv6_route_choose_next_hop(addr_data_ptr, this->interface_id, NULL);
493+
if (!route || route->prefix_len != 128 || !route->on_link || route->info.source != ROUTE_THREAD_PROXIED_HOST) {
494+
// Not found
495+
tr_debug("route not found");
496+
return 0;
497+
}
498+
499+
// We have pending request and received answer
500+
if (memcmp(ml_eid_ptr,tr_ptr->ml_eid, 8) != 0){
501+
// Different ml_eid but same address means duplicate address detected
502+
thread_resolution_client_address_error(this->interface_id, tr_ptr->source_address, tr_ptr->target_eid, tr_ptr->ml_eid);
503+
ipv6_neighbour_t *neighbour_entry;
504+
neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, tr_ptr->target_eid);
505+
if (neighbour_entry) {
506+
tr_debug("Remove from neigh Cache: %s", tr_ipv6(tr_ptr->target_eid));
507+
ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry);
508+
}
509+
ipv6_route_delete(route->prefix, route->prefix_len, this->interface_id, route->info.next_hop_addr, ROUTE_THREAD_PROXIED_HOST);
510+
}
511+
return 0;
512+
513+
// TODO check last transaction time for migrated device if this answer is newer delete entry
514+
// ipv6_route_delete(route->prefix, route->prefix_len, this->interface_id, route->info.next_hop_addr, ROUTE_THREAD_PROXIED_HOST);
515+
}
516+
476517
static int thread_pbbr_bb_ans_cb(int8_t service_id, uint8_t source_address[16], uint16_t source_port, sn_coap_hdr_s *request_ptr)
477518
{
478519
(void)service_id;
479520
(void)source_port;
480521
(void)request_ptr;
481-
uint16_t addr_len, ml_eid_len, last_transaction_time_len, network_name_len;
522+
uint16_t addr_len, ml_eid_len, last_transaction_time_len;
482523
uint8_t *addr_data_ptr;
524+
uint16_t rloc16;
525+
uint8_t destination_address[16] = {0};
483526
uint8_t *ml_eid_ptr;
484-
uint8_t *network_name_ptr;
485527
uint32_t last_transaction_time;
486528
tr_info("Thread BBR BB_ANS.ntf receive");
487529
thread_pbbr_t *this = thread_border_router_find_by_service(service_id);
@@ -495,54 +537,51 @@ static int thread_pbbr_bb_ans_cb(int8_t service_id, uint8_t source_address[16],
495537
if (!link_configuration_ptr || !cur) {
496538
return -1;
497539
}
498-
if (addr_is_assigned_to_interface(cur,source_address)) {
540+
if (addr_interface_address_compare(cur, source_address) == 0) {
499541
// Received from own address no need to process
500542
return 0;
501543
}
502544

503545
addr_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_TARGET_EID, &addr_data_ptr);
504546
ml_eid_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_ML_EID, &ml_eid_ptr);
505547
last_transaction_time_len = thread_meshcop_tlv_data_get_uint32(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_LAST_TRANSACTION_TIME, &last_transaction_time);
506-
network_name_len = thread_meshcop_tlv_find(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_LAST_TRANSACTION_TIME, &network_name_ptr);
507548

508549
if ( addr_len < 16|| ml_eid_len < 8 || last_transaction_time_len < 4 ) {
509550
tr_err("Invalid message");
510551
return 0;
511552
}
512553

513-
// Network name must match
514-
if(stringlen((char *)link_configuration_ptr->name,16) != network_name_len || memcmp(link_configuration_ptr->name,network_name_ptr,network_name_len) != 0) {
554+
if ((thread_pbbr_dua_duplicate_address_detection(service_id, addr_data_ptr, ml_eid_ptr) == 0)) {
515555
return 0;
516556
}
517557

518-
ipv6_route_t *route = ipv6_route_choose_next_hop(addr_data_ptr, this->interface_id, NULL);
519-
if (!route || route->prefix_len != 128 || !route->on_link || route->info.source != ROUTE_THREAD_PROXIED_HOST) {
520-
// Not found
558+
// If rloc16 is present then a/an is sent to the thread device with the rloc
559+
if (thread_tmfcop_tlv_data_get_uint16(request_ptr->payload_ptr, request_ptr->payload_len, TMFCOP_TLV_RLOC16, &rloc16) != 2) {
521560
return 0;
522561
}
523562

524-
duplicate_dua_tr_t *tr_ptr = thread_border_router_dup_tr_find(this->interface_id,addr_data_ptr);
525-
if (tr_ptr) {
526-
// We have pending request and received answer
527-
if (memcmp(ml_eid_ptr,tr_ptr->ml_eid, 8) != 0){
528-
// Different ml_eid but same address means duplicate address detected
529-
thread_resolution_client_address_error(this->interface_id, tr_ptr->source_address, tr_ptr->target_eid, tr_ptr->ml_eid);
530-
ipv6_neighbour_t *neighbour_entry;
531-
neighbour_entry = ipv6_neighbour_lookup(&cur->ipv6_neighbour_cache, tr_ptr->target_eid);
532-
if (neighbour_entry) {
533-
tr_debug("Remove from neigh Cache: %s", tr_ipv6(tr_ptr->target_eid));
534-
ipv6_neighbour_entry_remove(&cur->ipv6_neighbour_cache, neighbour_entry);
535-
}
536-
ipv6_route_delete(route->prefix, route->prefix_len, this->interface_id, route->info.next_hop_addr, ROUTE_THREAD_PROXIED_HOST);
537-
} else {
538-
// Roaming case lets inform other pbbr that we have newest
539-
thread_border_router_bb_ans_send(this, source_address, tr_ptr->target_eid, tr_ptr->ml_eid, 0, link_configuration_ptr->name, NULL);
540-
}
541-
} else {
542-
ipv6_route_delete(route->prefix, route->prefix_len, this->interface_id, route->info.next_hop_addr, ROUTE_THREAD_PROXIED_HOST);
543-
}
563+
// form the destination address to which the a/an is sent
564+
thread_addr_write_mesh_local_16(destination_address, rloc16, cur->thread_info);
544565

545-
// If delayed DUA registration entry is present send duplicate error code to DUA.response
566+
// Address query pending send address notification, include tlvs: target eid, rloc16 tlv of bbr, mleid, and last transaction time
567+
uint8_t payload[16 + 2 + 8 + 4]; // Target eid + Rloc16 + MLEID + transaction time
568+
uint8_t *ptr;
569+
ptr = payload;
570+
ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_TARGET_EID, 16, addr_data_ptr);
571+
ptr = thread_tmfcop_tlv_data_write_uint16(ptr, TMFCOP_TLV_RLOC16, cur->thread_info->routerShortAddress);
572+
ptr = thread_tmfcop_tlv_data_write(ptr, TMFCOP_TLV_ML_EID, 8, ml_eid_ptr);
573+
ptr = thread_tmfcop_tlv_data_write_uint32(ptr, TMFCOP_TLV_LAST_TRANSACTION_TIME, last_transaction_time);
574+
575+
// XXX "Accepted" response?
576+
577+
/* We don't require a response, so we don't specify a callback. Library
578+
* should retry itself until it gets an ACK.
579+
*/
580+
coap_service_request_send(this->coap_service_id, COAP_REQUEST_OPTIONS_ADDRESS_SHORT,
581+
destination_address, THREAD_MANAGEMENT_PORT,
582+
COAP_MSG_TYPE_CONFIRMABLE, COAP_MSG_CODE_REQUEST_POST,
583+
THREAD_URI_ADDRESS_NOTIFICATION, COAP_CT_OCTET_STREAM,
584+
payload, ptr - payload, NULL);
546585
return 0;
547586
}
548587
static int thread_extension_bbr_bmlr_req_send(int8_t service_id, const uint8_t br_addr[16], const uint8_t *address_ptr, uint8_t addr_len, uint32_t timeout)
@@ -997,7 +1036,7 @@ void thread_extension_bbr_delete(int8_t interface_id)
9971036
ns_dyn_mem_free(this);
9981037
}
9991038

1000-
bool thread_extension_bbr_nd_query_process(protocol_interface_info_entry_t *cur, const uint8_t *target_addr)
1039+
bool thread_extension_bbr_nd_query_process(protocol_interface_info_entry_t *cur, const uint8_t *target_addr, uint16_t rloc)
10011040
{
10021041
uint8_t domain_prefix[8];
10031042
if (thread_version < THREAD_VERSION_1_2) {
@@ -1010,12 +1049,17 @@ bool thread_extension_bbr_nd_query_process(protocol_interface_info_entry_t *cur,
10101049
if (!this->pbbr_started) {
10111050
return false;
10121051
}
1013-
// if we have DUA addressing enabled
1014-
if ( thread_extension_network_prefix_get(cur->id, NULL, domain_prefix, NULL) == 0 &&
1015-
bitsequal(domain_prefix,target_addr,64) ) {
1016-
return true;
1052+
// if we do not have DUA addressing enabled
1053+
if ( !thread_extension_network_prefix_get(cur->id, NULL, domain_prefix, NULL) == 0 ||
1054+
!bitsequal(domain_prefix,target_addr,64) ) {
1055+
return false;
1056+
10171057
}
1018-
return false;
1058+
1059+
// SEND BB_QRY
1060+
thread_border_router_bb_qry_send(this,target_addr,&rloc);
1061+
return true;
1062+
10191063
}
10201064

10211065

source/6LoWPAN/Thread/thread_extension_bbr.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ extern "C" {
4141
#ifdef HAVE_THREAD_V2
4242
int8_t thread_extension_bbr_init(int8_t interface_id, int8_t backbone_interface_id);
4343
void thread_extension_bbr_delete(int8_t interface_id);
44-
bool thread_extension_bbr_nd_query_process(protocol_interface_info_entry_t *cur, const uint8_t *target_addr);
44+
bool thread_extension_bbr_nd_query_process(protocol_interface_info_entry_t *cur, const uint8_t *target_addr, uint16_t rloc);
4545
void thread_extension_bbr_seconds_timer(int8_t interface_id, uint32_t seconds);
4646
int thread_extension_bbr_timeout_set(int8_t interface_id, uint32_t timeout_a, uint32_t timeout_b, uint32_t delay);
4747
int thread_extension_bbr_address_set(int8_t interface_id, const uint8_t *addr_ptr, uint16_t port);
@@ -51,7 +51,7 @@ int thread_extension_bbr_address_set(int8_t interface_id, const uint8_t *addr_pt
5151

5252
#define thread_extension_bbr_init(interface_id, backbone_interface_id)
5353
#define thread_extension_bbr_delete(interface_id)
54-
#define thread_extension_bbr_nd_query_process(cur, target_addr) false
54+
#define thread_extension_bbr_nd_query_process(cur, target_addr, rloc) false
5555
#define thread_extension_bbr_seconds_timer(interface_id, seconds)
5656
#define thread_extension_bbr_timeout_set(interface_id, timeout_a, timeout_b, delay)
5757
#define thread_extension_bbr_address_set(interface_id, addr_ptr, port) (-1)

source/6LoWPAN/Thread/thread_nd.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ static mle_neigh_table_entry_t *thread_nd_child_mleid_get(int8_t interface_id, u
209209
return NULL;
210210
}
211211

212-
static int thread_nd_address_query_lookup(int8_t interface_id, const uint8_t target_addr[static 16], uint16_t *addr_out, bool *proxy, uint32_t *last_transaction_time, uint8_t *mleid_ptr)
212+
static int thread_nd_address_query_lookup(int8_t interface_id, const uint8_t target_addr[static 16], uint16_t *rloc, uint16_t *addr_out, bool *proxy, uint32_t *last_transaction_time, uint8_t *mleid_ptr)
213213
{
214214
protocol_interface_info_entry_t *cur = protocol_stack_interface_info_get_by_id(interface_id);
215215
if (!cur) {
@@ -260,8 +260,8 @@ static int thread_nd_address_query_lookup(int8_t interface_id, const uint8_t tar
260260
can_route_of_mesh = true;
261261
}
262262

263-
if (thread_extension_bbr_nd_query_process(cur,target_addr)) {
264-
can_route_of_mesh = true;
263+
if (thread_extension_bbr_nd_query_process(cur,target_addr, *rloc)){
264+
return -1;
265265
}
266266

267267
if (can_route_of_mesh) {

source/6LoWPAN/Thread/thread_resolution_server.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static int thread_resolution_server_query_cb(int8_t service_id, uint8_t source_a
9191
uint8_t mlEID[8];
9292
bool proxy;
9393
uint16_t rloc16;
94+
uint16_t requestor_rloc;
9495
uint32_t last_transaction_time = 0;
9596
uint8_t *ptr;
9697
(void)source_port;
@@ -118,9 +119,11 @@ static int thread_resolution_server_query_cb(int8_t service_id, uint8_t source_a
118119
return -1;
119120
}
120121

122+
requestor_rloc = common_read_16_bit(source_address + 14);
123+
121124
tr_debug("Thread address query %s", trace_ipv6(target_ip_ptr));
122125

123-
int ret = this->query_cb_ptr(this->interface_id, target_ip_ptr, &rloc16, &proxy, &last_transaction_time, mlEID);
126+
int ret = this->query_cb_ptr(this->interface_id, target_ip_ptr, &requestor_rloc, &rloc16, &proxy, &last_transaction_time, mlEID);
124127
if (ret < 0) {
125128
/* XXX "Forbidden" response? */
126129
return -1;

source/6LoWPAN/Thread/thread_resolution_server.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" {
5858
*
5959
* /return return 0 for success, negative if unable to respond.
6060
*/
61-
typedef int thread_resolution_server_addr_query_cb(int8_t interface_id, const uint8_t target_addr[static 16], uint16_t *addr_out, bool *proxy, uint32_t *last_transaction_time, uint8_t *mleid_ptr);
61+
typedef int thread_resolution_server_addr_query_cb(int8_t interface_id, const uint8_t target_addr[static 16], uint16_t *rloc, uint16_t *addr_out, bool *proxy, uint32_t *last_transaction_time, uint8_t *mleid_ptr);
6262

6363
#ifdef HAVE_THREAD_NEIGHBOR_DISCOVERY
6464

0 commit comments

Comments
 (0)