I discussed some TCP issues with my friends over the weekend. When I was looking up the book "Linux Server High Performance Programming", I found this sentence in the book: The book says that a connection in the TIME_WAIT state will return a RST message after receiving a SYN message of the same four-tuple, and the other party will disconnect after receiving it. The author of the book only mentioned this sentence without providing any source code or packet capture evidence. At first, I thought this logic made sense, but when I looked at the TCP source code myself, I found that it was not the case. So, today we will discuss this question, "During the normal TCP handshake process, what happens when a connection in the TIME_WAIT state receives a SYN of the same four-tuple?" The problem phenomenon is as follows, the left side is the server, and the right side is the client: Conclusion firstBefore analyzing the TCP source code with you, let me first tell you the conclusion. The key to this problem is to see whether the "sequence number and timestamp" of SYN is legal, because after the connection in TIME_WAIT state receives SYN, it will determine whether the "sequence number and timestamp" of SYN is legal, and then perform different processing based on different judgment results. Let me first explain to you what is a "legal" SYN?
The above SYN legal judgment is based on the scenario where both parties have enabled the TCP timestamp mechanism. If neither party has enabled the TCP timestamp mechanism, the SYN legal judgment is as follows:
Received a legitimate SYNIf a connection in the TIME_WAIT state receives a "legitimate SYN", it will reuse this four-tuple connection, skip 2MSL and change to the SYN_RECV state, and then proceed with the connection establishment process. Take the following figure as an example. Both parties have enabled the TCP timestamp mechanism. TSval is the timestamp when the message is sent: In the figure above, when the third FIN message is received, the TSval (21) of the message is recorded and saved in the ts_recent variable. Then the next expected sequence number is calculated. In this example, the next expected sequence number is 301, which is saved in the rcv_nxt variable. When a connection in the TIME_WAIT state receives a SYN, because the SYN's seq (400) is greater than rcv_nxt (301), and the SYN's TSval (30) is greater than ts_recent (21), it is a "legal SYN", so the four-tuple connection will be reused, skipping 2MSL and transitioning to the SYN_RECV state, and then the connection establishment process can be carried out. Received an illegal SYNIf a connection in the TIME_WAIT state receives an "illegal SYN", it will reply with a fourth ACK message. After receiving it, the client finds that it is not the confirmation number (ack num) it expected, so it returns a RST message to the server. Take the following figure as an example. Both parties have enabled the TCP timestamp mechanism. TSval is the timestamp when the message is sent: In the figure above, when the third FIN message is received, the TSval (21) of the message is recorded and saved in the ts_recent variable. Then the next expected sequence number is calculated. In this example, the next expected sequence number is 301, which is saved in the rcv_nxt variable. After the connection in TIME_WAIT state receives SYN, because the seq (200) of SYN is less than rcv_nxt (301), it is an "illegal SYN", and it will reply with an ACK message that is the same as the fourth wave. After receiving it, the client finds that it is not the confirmation number it expected, so it returns a RST message to the server. If the client does not receive SYN + ACK after waiting for a while, it will time out and retransmit the SYN message. When the number of retransmissions reaches the maximum value, the connection will be disconnected. PS: Here is a question, will the connection in TIME_WAIT state be disconnected if RST is received? Source code analysisThe following source code analysis is based on the Linux 4.2 kernel code. After receiving a TCP message, the Linux kernel executes the tcp_v4_rcv function. The main code related to the TIME_WAIT state in this function is as follows: int tcp_v4_rcv ( struct sk_buff * skb ) The code process:
First of all, if the received SYN is legal, the tcp_timewait_state_process() function will return TCP_TW_SYN and reuse the connection. If the received SYN is illegal, the tcp_timewait_state_process() function will return TCP_TW_ACK and send the last sent ACK. Next, let’s see how the tcp_timewait_state_process() function determines the SYN packet. enum tcp_tw_status If both parties have enabled the TCP timestamp mechanism, the tcp_paws_reject() function will be used to determine whether the timestamp has wrapped, that is, whether the "timestamp of the currently received message" is greater than the "timestamp of the last received message":
From the source code, we can see that after receiving a SYN packet, if the timestamp of the SYN packet does not wrap around, that is, the timestamp is incremented, and the sequence number of the SYN packet does not wrap around, that is, the sequence number of the SYN is "greater than" the sequence number expected to be received next time, a sequence number will be initialized, and then TCP_TW_SYN will be returned, and then the connection will be reused, which means that 2MSL will be skipped and the state will be changed to SYN_RECV, and then the connection establishment process can be carried out. If neither party has enabled the TCP timestamp mechanism, it is only necessary to determine whether the sequence number of the SYN packet has been wrapped. If the sequence number of the SYN packet is greater than the sequence number expected to be received next time, 2MSL can be skipped and the connection can be reused. If the SYN packet is illegal, TCP_TW_ACK will be returned, and then the same ACK as the last time will be sent to the other party. In the TIME_WAIT state, will the connection be terminated if a RST is received?I left a question earlier. Will a connection in TIME_WAIT state be disconnected if it receives RST? Whether the connection will be disconnected or not depends on the kernel parameter net.ipv4.tcp_rfc1337 (the default value is 0):
The source code is processed as follows: enum tcp_tw_status The connection is released when a RST message is received in the TIME_WAIT state, which is equivalent to skipping the 2MSL time. However, this is still risky. The parameter sysctl_tcp_rfc1337 is proposed in the rfc 1337 document. Its purpose is to avoid skipping the 2MSL time due to receiving a RST message in the TIME_WAIT state. The document also gives the potential problems of skipping the 2MSL time. The TIME_WAIT state lasts for 2MSL for two main purposes:
Although the TIME_WAIT state lasts a bit long and seems unfriendly, it is designed to avoid messy things. However, the book "UNIX Network Programming" says: TIME_WAIT is our friend, it is helpful to us, don't try to avoid this state, but should figure it out. Therefore, I personally think it is safer to set net.ipv4.tcp_rfc1337 to 1. SummarizeDuring the normal TCP handshake process, what happens when a connection in the TIME_WAIT state receives a SYN of the same four-tuple? If both parties have enabled the timestamp mechanism:
In the TIME_WAIT state, will the connection be terminated if a RST is received?
|
<<: 10 times faster than 5G, are you ready for 10G communications?
>>: These three essential home gigabit network upgrade strategies
ReliableSite is a business that specializes in pr...
[[340662]] This article is reprinted from the WeC...
In the ever-evolving world of technology, 2024 pr...
There is only one month left in 2020, and RAKsmar...
After making an appointment on the mobile phone A...
[51CTO.com original article] On August 27, the 5-...
Recently, I found that many of my friends have su...
At present, under the long-term goal of carbon ne...
Following personal computers and the Internet, cl...
According to 3GPP's plan, the latest version ...
The three major domestic operators have all annou...
What is the definition of a smart city? The answe...
ZJI, formerly known as Weixiang Host, is a well-k...
OneTechCloud has launched a regular promotion aft...
Although Wi-Fi 6 wireless technology has been aro...