TCP Four-Way Handshake and TIME_WAIT State
Description
The TCP four-way handshake is the process used for the normal termination of a TCP connection, involving the exchange of four specific packets (FIN and ACK) between a client and a server. Corresponding to the "three-way handshake" for connection establishment, terminating a connection typically requires four steps, hence the name "four-way handshake." A key but often misunderstood state in this process is the TIME_WAIT state. Understanding the steps of the four-way handshake and the role of the TIME_WAIT state is crucial for diagnosing network connection issues, developing high-performance network applications, and defending against related attacks (such as connection depletion attacks).
Analysis Process
We will break down the four-way handshake step by step and delve into the TIME_WAIT state.
-
Connection Termination Initiation
- Description: Assume a TCP connection is already established, and the communicating parties are referred to as "client" and "server." When one party (e.g., the client that has finished sending data) decides to close the connection, it initiates the termination process.
- Action: The client sends a close command to its TCP stack (e.g., calling
socket.close()in programming). Subsequently, the client's TCP protocol stack constructs a TCP packet with theFINflag in the header set to 1 (FINstands for "Finish," indicating the sender has no more data to send). This packet also contains a sequence number (Sequence Number, assumed to beu), which is the sequence number of the last data byte from previous communication plus 1. - State Change: After sending this
FINpacket, the client's state changes from the stable connection state (ESTABLISHED) toFIN_WAIT_1. This means the client is waiting for the other party to acknowledge itsFINrequest.
-
Server Acknowledges the FIN
- Description: Upon receiving the
FINpacket from the client, the server understands that the client wishes to close the connection. - Action: The server must acknowledge this. It sends an acknowledgment (ACK) packet. The acknowledgment number (Acknowledgment Number) of this ACK packet is set to the received
FINpacket's sequence number plus 1, i.e.,u+1. This indicates the server has successfully received all data up to sequence numberu(including thisFIN, which consumes a sequence number). - State Change:
- Upon receiving this ACK, the client knows its close request has been acknowledged by the server. The client's state thus changes from
FIN_WAIT_1toFIN_WAIT_2. At this point, the connection from the client to the server is half-closed; the client can no longer send data but can still receive data from the server. - The server's state changes from
ESTABLISHEDtoCLOSE_WAIT. This indicates the server is aware the client wants to close and is waiting for its own application to close the connection.
- Upon receiving this ACK, the client knows its close request has been acknowledged by the server. The client's state thus changes from
- Description: Upon receiving the
-
Server Sends FIN Packet
- Description: After the server-side application finishes sending data, it also decides to close the connection.
- Action: The server-side TCP stack constructs its own
FINpacket, with a sequence number (assumed to bew) based on the data stream it previously sent. This packet is sent to the client. - State Change: After sending the
FINpacket, the server's state changes fromCLOSE_WAITtoLAST_ACK. This means the server has issued its own termination request and is waiting for the final acknowledgment.
-
Client Acknowledges the Server's FIN
- Description: The client receives the
FINpacket from the server. - Action: The client must send an acknowledgment (ACK) packet with an acknowledgment number of
w+1. - State Change:
- Upon receiving this ACK, the server considers the connection normally closed. The server's state changes from
LAST_ACKtoCLOSED(initial state). - The key point is here: After sending the final ACK, the client does not immediately enter the
CLOSEDstate. Instead, it enters a state calledTIME_WAIT.
- Upon receiving this ACK, the server considers the connection normally closed. The server's state changes from
- Description: The client receives the
-
In-depth Understanding of the TIME_WAIT State
- What it is:
TIME_WAITis also known as the 2MSL wait state. MSL stands for "Maximum Segment Lifetime," the maximum time a TCP packet can exist in the network before being discarded. The duration ofTIME_WAITis typically twice the MSL. In modern operating systems, this value is often 30 seconds, 60 seconds, or 120 seconds. - Why it is needed (two core purposes):
- Reliable Connection Termination: The final ACK packet sent by the client might get lost in the network. If the server does not receive this ACK, it will assume its
FINpacket was lost and retransmit theFINpacket. If the client directly entered theCLOSEDstate after sending the ACK and reused the same port to establish a new connection, a delayedFINpacket from the previous connection arriving later might be mistakenly interpreted as a termination signal for the new connection, incorrectly closing the new connection. A client in theTIME_WAITstate, if it receives a retransmittedFINpacket, will resend the ACK, ensuring the connection is safely and reliably terminated. - Allowing Old Connection Packets to Expire in the Network: During the 2MSL wait time, all delayed packets belonging to this recently closed connection will time out and disappear from the network. This ensures that these old, delayed packets do not cause confusion with future new connections that reuse the same quadruple (source IP, source port, destination IP, destination port).
- Reliable Connection Termination: The final ACK packet sent by the client might get lost in the network. If the server does not receive this ACK, it will assume its
- What it is:
Summary
The TCP four-way handshake is a carefully designed, reliable connection termination mechanism. The TIME_WAIT state is an integral part of it. Although it temporarily occupies system port resources (potentially causing "port exhaustion" issues in high-concurrency servers), its primary goal is to ensure the reliability of the TCP protocol. When designing and optimizing network services, it is necessary to understand and properly handle the TIME_WAIT state, rather than simply attempting to bypass it.