diff --git a/src/core/ipv6/ip6.c b/src/core/ipv6/ip6.c index 4fc119940..13097b31b 100644 --- a/src/core/ipv6/ip6.c +++ b/src/core/ipv6/ip6.c @@ -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)) { @@ -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))) { diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index 1071bcf21..a495e8ad5 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -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