2

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?

Chandu
  • 1,837
  • 7
  • 30
  • 51

2 Answers2

2

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.

selbie
  • 100,020
  • 15
  • 103
  • 173
  • So in the 2nd case u mentioned above there is a remote possibility of data of old connection sneaking into to new connection if sequence number of old connection is more than that of new connection. – Chandu Mar 11 '13 at 09:14
  • Short summary is that you should not have to worry about this. But whatever protocol you've built on top of TCP should be robust enough to handle "bad data" from a misbehaving client. That is, if you actually think it's possible, then code defensively. In the second case above, the socket will be closed. And even if the TCP layer puts the connection back into the ESTABLISHED state, it will be bubbled up to the application as a new socket returned from accept(). – selbie Mar 11 '13 at 09:30
1

That's what the TIME_WAIT state is for. It lasts for twice the maximum segment lifetime, so that any data sent to an old connection will expire before a new connection between the same IP:port pairs can be formed.

user207421
  • 305,947
  • 44
  • 307
  • 483