Search code examples
socketstcptime-wait

Is there any chance of data of old TCP connection to sneak into new TCP connection on same port


I am setting SO_REUSEADDR option on sockets. Suppose a socket is closed from one end.

And socket descriptor got reassigned to other process.

Is there any chance of data from old TCP connection to sneak into new TCP connection?

Did anybody observe old data sneaking into new TCP connection especially on Solaris?


Solution

  • No.

    If you re-use the local port, but either the remote host or port changes in the subsequent connection, then this is impossible.

    For the case of reconnecting back to the same remote IP/port from the same local IP/port, also known as TIME-WAIT Assasination, there are some rules for the TCP stack to abide by. Mainly - starting out with a higher sequence number than the previous connection. You can read the fine print in RFC 1337. But here's a better link and quote that outlines how the sequence number is adjusted on subsequent connections.

    http://blogs.technet.com/b/networking/archive/2010/08/11/how-tcp-time-wait-assassination-works.aspx

    In a situation where the server side socket goes to a TIME-WAIT state and the client reconnects to the server within 2MSL (default TIME-WAIT time), there are 2 things that can happen:

    1. The server will not respond to the SYN packets from the client because the socket is in the TIME-WAIT state.

    2. The server may accept the SYN from the client and change the state of the socket from TIME-WAIT to ESTABLISHED. This is known as TIME-WAIT assassination, or incarnation of a previous connection.

    The key to scenario ‘2’ above is that the ISN (Initial Sequence number) of the SYN sent needs to be higher than the highest sequence number used in the previous session. If the ISN is not as expected, the server will not respond to the SYN and the socket will wait for 2MSL before being available for use again.