Skip to content

Commit

Permalink
Merge branch 'feature/lwip_add_ip6_src_addr_select_hook' into 'master'
Browse files Browse the repository at this point in the history
lwip: add a hook for ip6 source address selection

See merge request espressif/esp-idf!23672
  • Loading branch information
zwx1995esp committed Jun 15, 2023
2 parents dccc31e + 65f7e1e commit bcb1741
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
20 changes: 20 additions & 0 deletions components/lwip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,26 @@ menu "LWIP"

endchoice

choice LWIP_HOOK_IP6_SELECT_SRC_ADDR
prompt "IPv6 source address selection Hook"
depends on LWIP_IPV6
default LWIP_HOOK_IP6_SELECT_SRC_ADDR_NONE
help
Enables custom IPv6 source address selection.
Setting this to "default" provides weak implementation
stub that could be overwritten in application code.
Setting this to "custom" provides hook's declaration
only and expects the application to implement it.

config LWIP_HOOK_IP6_SELECT_SRC_ADDR_NONE
bool "No hook declared"
config LWIP_HOOK_IP6_SELECT_SRC_ADDR_DEFAULT
bool "Default (weak) implementation"
config LWIP_HOOK_IP6_SELECT_SRC_ADDR_CUSTOM
bool "Custom implementation"

endchoice

choice LWIP_HOOK_NETCONN_EXTERNAL_RESOLVE
prompt "Netconn external resolve Hook"
default LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT if OPENTHREAD_DNS64_CLIENT
Expand Down
2 changes: 1 addition & 1 deletion components/lwip/lwip
12 changes: 11 additions & 1 deletion components/lwip/port/hooks/lwip_default_hooks.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,17 @@ const ip6_addr_t *__weak lwip_hook_nd6_get_gw(struct netif *netif, const ip6_add
LWIP_UNUSED_ARG(netif);
LWIP_UNUSED_ARG(dest);

return 0;
return NULL;
}
#endif

#ifdef CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_DEFAULT
const ip_addr_t *__weak lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
{
LWIP_UNUSED_ARG(netif);
LWIP_UNUSED_ARG(dest);

return NULL;
}
#endif

Expand Down
6 changes: 6 additions & 0 deletions components/lwip/port/include/lwip_default_hooks.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ const ip6_addr_t *lwip_hook_nd6_get_gw(struct netif *netif, const ip6_addr_t *de
#define LWIP_HOOK_ND6_GET_GW lwip_hook_nd6_get_gw
#endif /* CONFIG_LWIP_HOOK_ND6_GET_GATEWAY... */

#if defined(CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_CUSTOM) || defined(CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR_DEFAULT)
const ip_addr_t *lwip_hook_ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest);

#define LWIP_HOOK_IP6_SELECT_SRC_ADDR lwip_hook_ip6_select_source_address
#endif /* CONFIG_LWIP_HOOK_IP6_SELECT_SRC_ADDR... */

#if defined(CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_CUSTOM) || defined(CONFIG_LWIP_HOOK_NETCONN_EXT_RESOLVE_DEFAULT)
int lwip_hook_netconn_external_resolve(const char *name, ip_addr_t *addr, u8_t addrtype, err_t *err);

Expand Down
23 changes: 23 additions & 0 deletions components/openthread/src/esp_openthread_lwip_netif.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
1 change: 1 addition & 0 deletions examples/openthread/ot_br/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions examples/openthread/ot_cli/sdkconfig.defaults
Original file line number Diff line number Diff line change
Expand Up @@ -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

#
Expand Down

0 comments on commit bcb1741

Please sign in to comment.