Skip to content

Commit

Permalink
ip6: add a hook for ip6 source address selection
Browse files Browse the repository at this point in the history
  • Loading branch information
zwx1995esp authored and david-cermak committed May 30, 2023
1 parent dafc822 commit 57c2964
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/core/ipv6/ip6.c
Expand Up @@ -290,6 +290,14 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
u8_t best_pref = 0;
u8_t best_bits = 0;

best_addr = NULL;

#ifdef LWIP_HOOK_IP6_SELECT_SRC_ADDR
if ((best_addr = LWIP_HOOK_IP6_SELECT_SRC_ADDR(netif, dest)) != NULL) {
return best_addr;
}
#endif

/* Start by determining the scope of the given destination address. These
* tests are hopefully (roughly) in order of likeliness to match. */
if (ip6_addr_isglobal(dest)) {
Expand All @@ -307,8 +315,6 @@ ip6_select_source_address(struct netif *netif, const ip6_addr_t *dest)
dest_scope = IP6_MULTICAST_SCOPE_GLOBAL;
}

best_addr = NULL;

for (i = 0; i < LWIP_IPV6_NUM_ADDRESSES; i++) {
/* Consider only valid (= preferred and deprecated) addresses. */
if (!ip6_addr_isvalid(netif_ip6_addr_state(netif, i))) {
Expand Down
22 changes: 22 additions & 0 deletions src/include/lwip/opt.h
Expand Up @@ -3027,6 +3027,28 @@
#define LWIP_HOOK_ND6_GET_GW(netif, dest)
#endif

/**
* LWIP_HOOK_IP6_SELECT_SRC_ADDR(netif, dest):
* Called from ip6_select_source_address() (IPv6)
* Signature:\code{.c}
* const ip6_addr_t *my_hook(struct netif *netif, const ip6_addr_t *dest);
* \endcode
* Arguments:
* - netif: the netif used for selecting
* - dest: the destination IPv6 address
* Return values:
* - the preferred source IPv6 address of the specified destination IPv6 address
* - NULL, in which case none source address is preferred by the hook, lwip
* will determine a source address based RFC 6724.
*
* The returned address MUST be on the specified netif!
* This function is meant to implement advanced IPv6 source address selection with
* LWIP_HOOK_IP6_SELECT_SRC_ADDR().
*/
#ifdef __DOXYGEN__
#define LWIP_HOOK_IP6_SELECT_SRC_ADDR(netif, dest)
#endif

/**
* LWIP_HOOK_VLAN_CHECK(netif, eth_hdr, vlan_hdr):
* Called from ethernet_input() if VLAN support is enabled
Expand Down

0 comments on commit 57c2964

Please sign in to comment.