TCP Max SEQ Number Explained

tcp

I'm reading about TCP at the moment and there is a maximum sequence number 2 ^ 32, my question is what happens when the other host tries to send an acknowledgement for that sequence number 2 ^ 32 + 1?

Does TCP handle this by resetting or does it just crash out?

(Also bonus question, is the first sequence number generated at random?, I keep reading different things about it)

Best Answer

The sequence numbers in TCP wrap around. This means that after 2^32-1 (4294967295), the sequence numbers continue with 0.
You might think that this could pose problems with distinguishing between old and new data with the same sequence number, but that doesn't happen, because TCP also has the concept of a window of acceptable sequence numbers and that window is at most 2^16 sequence numbers wide. This means that long before you need to re-use sequence numbers, they have fallen outside the window of acceptable sequence numbers.

The initial sequence number can be selected in multiple ways.
The original TCP specification leaves it fairly open, but does specify that the initial sequence number should not have been used in a preceding connection (on the same port) in the last few hours. One example given is that the initial sequence numbers are chosen based on a bit-clock with a period of 4.55 hours (after that time the sequence numbers start to repeat).
It is also possible to select the initial sequence numbers more randomly, as long as there is no chance that the packets of the new connection can't be confused with those from a previous connection.