From 06263efe0be36eb1592611cfb13e4a503f0541c8 Mon Sep 17 00:00:00 2001 From: David Cermak Date: Thu, 17 Mar 2022 12:43:57 +0100 Subject: [PATCH] lwip: Add IPv4 route hook to the esp32 port --- .../port/esp32/hooks/lwip_default_hooks.c | 50 +++++++++++++++++++ .../port/esp32/include/lwip_default_hooks.h | 3 ++ 2 files changed, 53 insertions(+) diff --git a/components/lwip/port/esp32/hooks/lwip_default_hooks.c b/components/lwip/port/esp32/hooks/lwip_default_hooks.c index f053c16f15..db34443334 100644 --- a/components/lwip/port/esp32/hooks/lwip_default_hooks.c +++ b/components/lwip/port/esp32/hooks/lwip_default_hooks.c @@ -50,3 +50,53 @@ int __weak lwip_hook_ip6_input(struct pbuf *p, struct netif *inp) return 0; } #endif + +#ifdef LWIP_HOOK_IP4_ROUTE_SRC +#if ESP_IP4_ROUTE +#include "lwip/netif.h" + +bool ip4_netif_exist(const ip4_addr_t *src, const ip4_addr_t *dest) +{ + struct netif *netif = NULL; + + for (netif = netif_list; netif != NULL; netif = netif->next) { + /* is the netif up, does it have a link and a valid address? */ + if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { + /* source netif and dest netif match? */ + if (ip4_addr_netcmp(src, netif_ip4_addr(netif), netif_ip4_netmask(netif)) || ip4_addr_netcmp(dest, netif_ip4_addr(netif), netif_ip4_netmask(netif))) { + /* return false when both netif don't match */ + return true; + } + } + } + + return false; +} + +/** + * Source based IPv4 routing hook function. + */ +struct netif * +ip4_route_src_hook(const ip4_addr_t *src,const ip4_addr_t *dest) +{ + struct netif *netif = NULL; + + /* destination IP is broadcast IP? */ + if ((src != NULL) && !ip4_addr_isany(src)) { + /* iterate through netifs */ + for (netif = netif_list; netif != NULL; netif = netif->next) { + /* is the netif up, does it have a link and a valid address? */ + if (netif_is_up(netif) && netif_is_link_up(netif) && !ip4_addr_isany_val(*netif_ip4_addr(netif))) { + /* source IP matches? */ + if (ip4_addr_cmp(src, netif_ip4_addr(netif))) { + /* return netif on which to forward IP packet */ + + return netif; + } + } + } + } + return netif; +} +#endif +#endif /* LWIP_HOOK_IP4_ROUTE_SRC */ diff --git a/components/lwip/port/esp32/include/lwip_default_hooks.h b/components/lwip/port/esp32/include/lwip_default_hooks.h index 2388d4f3a0..82f30c8bbb 100644 --- a/components/lwip/port/esp32/include/lwip_default_hooks.h +++ b/components/lwip/port/esp32/include/lwip_default_hooks.h @@ -53,6 +53,9 @@ int lwip_hook_ip6_input(struct pbuf *p, struct netif *inp); #define LWIP_HOOK_IP6_INPUT lwip_hook_ip6_input #endif /* CONFIG_LWIP_HOOK_IP6_INPUT_CUSTIOM... */ +struct netif * +ip4_route_src_hook(const ip4_addr_t *src,const ip4_addr_t *dest); + #ifdef __cplusplus } #endif