TCP -- coinciding sequence numbers in multiple TCP connections - tcp

In TCP, Initial Sequence Numbers are set on a clock and are incremented every 4 microseconds.
Suppose (IP#1, port#1) and (IP#2, port#2) set up a TCP connection and with the respective Initial Sequence Number-s as
ISN1=100, ISN2=500.
A millisecond after, (IP#1, port#1) and (IP#2, port#2) are setting up another TCP connection
with the applying ISN numbers. In this case, the sequence numbers of the first and the
second connections are likely to coincide.
How does TCP handle this? It can reset and thus re-synchronize a connection under
certain circumstances along the way when the sequence numbers are odd (not synchronizing), but this may not cover every case.
How are the sequence numbers handled in such a case when these numbers of
2 different TCP connections may interfere with those of one another?
TIA.

Each TCP connection is identified by a tuple of [ local address, remote address, local port, remote port]. The sequence number is part of the state associated with that key therefore it doesn't matter at all if sequence numbers overlap.
Your particular example is impossible. You cannot setup two different TCP connections with identical local and remote addresses and ports.

Related

TCP packet sequence number generation

I'm creating pseudo TCP packet from scratch (based on some existing data) that can be later analysed using Wireshark.
How should I fill out sequence/acknowledgement number so that in Wireshark we can see a clean flow? Currently, I'm seeing TCP retransmission/out of order/previous segment not captured etc.
What I've tried: increment sequence number with the current TCP packet's length, but it didn't work.
(It doesn't necessarily need to makes sense, I should just get rid of the warnings)

If TCP runs out of its sequence number, what will happen ? if it is 0 again, will that byte not be considered duplicate?

If TCP runs out of its sequence number, what will happen?
If it again turns to 0 as the sequence number of the next byte, won't that be considered "duplicate" by the receiver?
If yes, then it has to ignore that byte.
If not, why?
I think, i found the answer.
The answer of this query, lies on one of the TCP option field known as "timestamp". It's in every TCP segment (including data and ACK segments).
Therefore to identify a unique tcp segment, we look for a combination of "timestamp" and "sequence number".
The basic idea is that a segment can be discarded as an old duplicate if it is
received with a timestamp less than some timestamp recently received on this connection.
Example :
Two segments 400:12001 and 700:12001 definitely belongs to two different incarnations.
And this mechanism is known as "PAWS" or protection against wrapped sequence numbers.
Reference: https://www.rfc-editor.org/rfc/rfc1323#page-17

Does the SYN bit remains 1 in TCP header segment after a successful 3 way hand shake connection between two hosts?

Or does it flip back to zero, i know that SYN bit is set to one during the connection formation but what happens to it after that ?
The SYN bit indicates that you wish to synchronize sequence numbers. Since this is only done one time by each side in the conversation, the bit is reset to zero after the first two packets in the three-way handshake

Why is an empty TCP segment at right edge of receive window not acceptable?

The TCPv4 specification (RFC 793) classifies a received segment as unacceptable if it has zero length, a sequence number equal to RCV.NXT+RCV.WND while the receive window is not zero (second row in the table).
This essentially means that the segment will be discarded, other than possibly sending an ACK. No ACK processing or send window update will be done.
What is the justification of this?
Consider this scenario:
Host A sends all possible data segments to host B, just exhausting the receive window of B.
Host A shortly also sends an empty segment, e.g. a window update or acknowledgement of received data. This segment has sequence number equal to the right edge of the receive window of host B (RCV.NXT+RCV.WND), since it was set to the latest SND.NXT of host A.
The mentioned data packets are lost in the network or delayed, and host B receives the empty segment first.
Host B will classify the empty segment as not acceptable, and drop it, ignoring any acknowledgement or window update.
Is there some part that I am not understanding correctly? Is this scenario really possible?
note: I ask here instead of on networkengineering.stackexchange.com since I encountered the issue while implementing a TCP/IP stack and these protocol details seem closer to programming than what is commonly understood as network engineering.

Maximum value of TCP sequence number

I'm trying to capture packets and reorganize packets for obtaining original HTTP request.
I'm capturing packets by IPQUEUE(by iptables rule), and I figured out that packets are not captured in order.
I already know that in TCP protocol, packets have to be re-sequenced, so I'm trying to re-sequence packets by sequence number.
According to Wikipedia, the sequence number of TCP is 32 bits number. Then, what happens if sequence number reaches to MAX 32bits number?
Because sequence number of SYN packet is random number, I think this limitation can be reached very fast.
If anybody has a commend on it, or has some links helpful, please leave me a answer.
From RFC-1185
Avoiding reuse of sequence numbers within the same connection is
simple in principle: enforce a segment lifetime shorter than the
time it takes to cycle the sequence space, whose size is
effectively 2**31.
If the maximum effective bandwidth at which TCP
is able to transmit over a particular path is B bytes per second,
then the following constraint must be satisfied for error-free
operation:
2**31 / B > MSL (secs)
So In simpler words TCP will take care of it.
In addition of this condition TCP also has concept of Timestamps to handle sequence number wrap around condition. From the same above RFC
Timestamps carried from sender to receiver in TCP Echo options can
also be used to prevent data corruption caused by sequence number
wrap-around, as this section describes.
Specifically TCP uses PAWS mechanism to handle TCP wrap around case.
You can find more information about PAWS in RFC-1323
RFC793 Section 3.3:
It is essential to remember that the actual sequence number space is
finite, though very large. This space ranges from 0 to 2*32 - 1.
Since the space is finite, all arithmetic dealing with sequence
numbers must be performed modulo 2*32. This unsigned arithmetic
preserves the relationship of sequence numbers as they cycle from
2**32 - 1 to 0 again. There are some subtleties to computer modulo
arithmetic, so great care should be taken in programming the
comparison of such values.
Any arithmetics done on the sequence number are modulo 2^32
In simple terms, the 32-bit unsigned number will wrap around:
...
0xFFFFFFFE
0xFFFFFFFF
0x00000000
0x00000001
...
have a look at tcp timestamps section of https://en.wikipedia.org/wiki/Transmission_Control_Protocol

Resources