Page 1 of 1

custom ip routing when using IP_NAPT

Posted: Fri Dec 19, 2025 5:06 pm
by crc1021
I have an application for ESP32 that uses NAT on the SoftAP interface. The IP_NAPT code works well to route packets from the AP through another interface to the internet; in this application there can be 3 interfaces (ethernet, wifi station, and cellular using PPPoS) that are active to route to the internet.
I have two issues:
* I don't see a mechanism for the application to select which interface use when multiple interfaces are active
* I don't see a mechanism to filter or control packets being routed via NAT through an interface. Basically, if PPPos is the only active interface, the application would restrict what packets could be routed.

I looks like the function that handles the routing is ip4_route_src() in ip4.c, which calls a macro LWIP_HOOK_IP4_ROUTE_SRC(), that macro is defined to be ip4_route_src_hook in port/include/lwipopts.h, which is implemented in port/hooks/lwip_default_hooks.c. Unlike many functions in that file, it is does not have the __weak attribute

Questions:
Is this the proper place to put in the needed functionality? What is the proper way to have a custom implementation of that routing function for the port to ESP32 without changing the idf files?

Re: custom ip routing when using IP_NAPT

Posted: Mon Dec 22, 2025 1:55 pm
by lichurbagan
ip4_route_src() is where lwIP decides which netif an outgoing IPv4 packet should use, and NAT (NAPT) relies on that routing decision.
In ESP-IDF:

Code: Select all

struct netif *ip4_route_src(const ip4_addr_t *src, const ip4_addr_t *dest)
{
    struct netif *netif;
    LWIP_HOOK_IP4_ROUTE_SRC(src, dest, netif);
    ...
}
Filtering/blocking specific packets is not ideal here.

Re: custom ip routing when using IP_NAPT

Posted: Fri Dec 26, 2025 5:27 pm
by crc1021
yes, I see ip4_route_src() is the place, which calls the macro LWIP_HOOK_IP4_ROUTE_SRC(), which is defined as ip4_route_src_hook(). But, ip4_route_src_hook() is not weakly linked, so it can not be overridden.

Where is the more ideal place to do this?

Can Espressif make changes to the idf to allow for applications to do operations like this even if it is not ideal?

Thanks