Skip to content

Commit aca957c

Browse files
committed
Merge branch 'fix_ot_src_addr_select' into 'master'
Fix ot src addr select See merge request espressif/esp-idf!40768
2 parents 5181747 + bf12839 commit aca957c

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

components/openthread/src/esp_openthread_lwip_netif.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -153,21 +153,29 @@ static err_t openthread_netif_init(struct netif *netif)
153153

154154
const ip_addr_t *lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
155155
{
156-
const ip6_addr_t *cur_addr;
156+
ip6_addr_t cand_addr = { 0 };
157157
uint8_t idx = 0;
158+
otError err = OT_ERROR_NONE;
158159
// Only process with ot netif.
159160
if (!(netif->name[0] == 'o' && netif->name[1] == 't')) {
160161
return NULL;
161162
}
162-
// Currently, prefer the address with the same prefix of the destination address.
163-
// If no address found, return NULL for selection source address using the default algorithm.
164-
for (idx = 0; idx < LWIP_IPV6_NUM_ADDRESSES; idx++) {
165-
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, idx))) {
166-
continue;
167-
}
168-
cur_addr = netif_ip6_addr(netif, idx);
169-
if (ip6_addr_netcmp_zoneless(cur_addr, dest)) {
170-
return netif_ip_addr6(netif, idx);
163+
otMessageInfo message_info = { 0 };
164+
memcpy(message_info.mPeerAddr.mFields.m32, dest->addr, sizeof(message_info.mPeerAddr.mFields.m32));
165+
otInstance *instance = esp_openthread_get_instance();
166+
esp_openthread_task_switching_lock_acquire(portMAX_DELAY);
167+
err = otIp6SelectSourceAddress(instance, &message_info);
168+
esp_openthread_task_switching_lock_release();
169+
if (err == OT_ERROR_NONE) {
170+
// If a Src address was selected by the OT stack, use this address.
171+
memcpy(cand_addr.addr, message_info.mSockAddr.mFields.m32, sizeof(cand_addr.addr));
172+
for (idx = 0; idx < LWIP_IPV6_NUM_ADDRESSES; idx++) {
173+
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, idx))) {
174+
continue;
175+
}
176+
if (ip6_addr_zoneless_eq(netif_ip6_addr(netif, idx), &cand_addr)) {
177+
return netif_ip_addr6(netif, idx);
178+
}
171179
}
172180
}
173181
return NULL;

components/openthread/src/esp_openthread_netif_glue.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2021-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2021-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -106,9 +106,13 @@ static void process_thread_address(const otIp6AddressInfo *address_info, bool is
106106
} else {
107107
ip_event_add_ip6_t add_addr;
108108
add_addr.addr = addr;
109-
// if an address is not mesh local or link local, we set preferred for this address.
110-
add_addr.preferred =
111-
is_mesh_local_addr(address_info->mAddress) || is_link_local_addr(address_info->mAddress) ? 0 : 1;
109+
// Only mark the address as preferred if
110+
// it is marked preferred by OpenThread and it is neither a mesh-local nor a link-local address.
111+
if (address_info->mPreferred == 0 || is_mesh_local_addr(address_info->mAddress) || is_link_local_addr(address_info->mAddress)) {
112+
add_addr.preferred = 0;
113+
} else {
114+
add_addr.preferred = 1;
115+
}
112116
if (esp_event_post(OPENTHREAD_EVENT, OPENTHREAD_EVENT_GOT_IP6, &add_addr, sizeof(add_addr), 0) != ESP_OK) {
113117
ESP_LOGE(OT_PLAT_LOG_TAG, "Failed to post OpenThread got ip6 address event");
114118
}

0 commit comments

Comments
 (0)