I am using FreeRTOS+TCP on a Renesas RA6M3 device.
The device acts as a server, and I want to allow connections only from specific client IP addresses.
Currently, I check the client IP address after FreeRTOS_accept(), and if the IP is not allowed, I call FreeRTOS_closesocket() to disconnect.
However, with this approach, the client briefly sees the connection as established before being disconnected.
My question is:
Is there a way to check the client’s IP address before the connection is fully established, and reject connections from disallowed IPs?
I agree with you, it is not elegant to pick up the phone and smash it down within a second. It is better to reply with a tcpTCP_FLAG_RST packet before the negotiation starts.
In FreeRTOS_TCP_State_Handling_IPv[46].c you find:
I would recommend to check here if the source IP-address is allowed to login.
The call to prvTCPSendReset() makes sure that the peer will receive a proper RST packet. The peer will see an errno of ECONNREFUSED, and it will not try it again.
I would check the full address: IP and port number.
What are your thoughts?
In some of my projects, I filter on the source port number: only source port 2403 was allowed. In that way I made sure that a single device could only connect one time.
Thank you for the update and for providing the sample for the TCP Accept Hook.
I have reviewed the method using ipconfigHAS_TCP_ACCEPT_HOOK.
I will consider this approach together with the previously suggested method of checking the IP address inside prvHandleListen_IPV4() before deciding which one to adopt.
I plan to test both options and evaluate which implementation fits my requirements best.
Thank you again for sharing this information.
The previous sample you provided worked correctly when I implemented and tested it.
Regarding the new TCP Accept Hook approach, I would appreciate your advice on whether it would be preferable to use this new method instead.