custom ip routing when using IP_NAPT
Posted: Fri Dec 19, 2025 5:06 pm
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?
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?