diff --git a/components/openthread/port/esp_openthread_lwip_netif.c b/components/openthread/port/esp_openthread_lwip_netif.c index b05eb2912f..29be440ae0 100644 --- a/components/openthread/port/esp_openthread_lwip_netif.c +++ b/components/openthread/port/esp_openthread_lwip_netif.c @@ -15,6 +15,7 @@ #include "lwip/esp_netif_net_stack.h" #include "lwip/netif.h" #include "lwip/pbuf.h" +#include "lwip_default_hooks.h" #include "openthread/error.h" #include "openthread/ip6.h" #include "openthread/link.h" @@ -143,3 +144,25 @@ static err_t openthread_netif_init(struct netif *netif) return ERR_OK; } + +const ip_addr_t *lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest) +{ + const ip6_addr_t *cur_addr; + uint8_t idx = 0; + // Only process with ot netif. + if (!(netif->name[0] == 'o' && netif->name[1] == 't')) { + return NULL; + } + // Currently, prefer the address with the same prefix of the destination address. + // If no address found, return NULL for selection source address using the default algorithm. + for (idx = 0; idx < LWIP_IPV6_NUM_ADDRESSES; idx++) { + if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, idx))) { + continue; + } + cur_addr = netif_ip6_addr(netif, idx); + if (ip6_addr_netcmp_zoneless(cur_addr, dest)) { + return netif_ip_addr6(netif, idx); + } + } + return NULL; +} diff --git a/examples/openthread/ot_br/sdkconfig.defaults b/examples/openthread/ot_br/sdkconfig.defaults index 369bdb00f6..d2ee6928f0 100644 --- a/examples/openthread/ot_br/sdkconfig.defaults +++ b/examples/openthread/ot_br/sdkconfig.defaults @@ -43,6 +43,7 @@ CONFIG_LWIP_NETIF_STATUS_CALLBACK=y CONFIG_LWIP_HOOK_IP6_ROUTE_DEFAULT=y CONFIG_LWIP_HOOK_ND6_GET_GW_DEFAULT=y CONFIG_LWIP_HOOK_IP6_INPUT_CUSTOM=y +CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_CUSTOM=y CONFIG_LWIP_IPV6_AUTOCONFIG=y # end of lwIP diff --git a/examples/openthread/ot_cli/sdkconfig.defaults b/examples/openthread/ot_cli/sdkconfig.defaults index 9ea24974eb..e6b4e2c933 100644 --- a/examples/openthread/ot_cli/sdkconfig.defaults +++ b/examples/openthread/ot_cli/sdkconfig.defaults @@ -43,6 +43,7 @@ CONFIG_OPENTHREAD_DNS64_CLIENT=y CONFIG_LWIP_TCPIP_TASK_STACK_SIZE=4096 CONFIG_LWIP_IPV6_NUM_ADDRESSES=8 CONFIG_LWIP_MULTICAST_PING=y +CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_CUSTOM=y # end of lwIP #