Sunday, September 23, 2012

TCP Connection Termination

Source: http://www.tcpipguide.com/free/t_TCPConnectionTermination-2.htm

Normal Connection Termination
In the normal case, each side terminates its end of the connection by sending a special message with the FIN (finish) bit set. This message, sometimes called a FIN, serves as a connection termination request to the other device, while also possibly carrying data like a regular segment. The device receiving the FIN responds with an acknowledgment to the FIN to indicate that it was received. The connection as a whole is not considered terminated until both sides have finished the shut down procedure by sending a FIN and receiving an ACK.
Thus, termination isn't a three-way handshake like establishment: it is a pair of two-way handshakes. The states that the two devices in the connection move through during a normal connection shutdown are different because the device initiating the shutdown must behave differently than the one that receives the termination request. In particular, the TCP on the device receiving the initial termination request must inform its application process and wait for a signal that the process is ready to proceed. The initiating device doesn't need to do this, since the application is what started the ball rolling in the first place.
Key Concept: A TCP connection is normally terminating using a special procedure where each side independently closes its end of the link. It normally begins with one of the application processes signalling to its TCP layer that the session is no longer needed. That device sends a FIN message to tell the other device that it wants to end the connection, which is acknowledged. When the responding device is ready, it too sends a FIN that is acknowledged; after waiting a period of time for the ACK to be received, the session is closed.

Table 154 describes in detail how the connection termination process works; the progression of states and messages exchanged can also be seen in Figure 214. The table is adapted from Table 151, describing the TCP finite state machine, but shows what happens for both the server and the client over time during connection shutdown. Either device can initiate connection termination; in this example I am assuming the client does it. Each row shows the state each device begins in, what action it takes in that state and what state to which it transitions. I have also shown the send and receive stages of both of the steps for each of the client and server’s close operations.


Table 154: TCP Connection Termination Procedure
Client
Server
Start State
Action
Transitions To State
Start State
Action
Transitions To State
ESTABLISHED
Client Close Step #1 Transmit: The application using TCP signals that the connection is no longer needed. The client TCP sends a segment with the FIN bit set to request that the connection be closed.
FIN-WAIT-1
ESTABLISHED
At this stage the server is still in normal operating mode.
FIN-WAIT-1
The client, having sent a FIN, is waiting for it to both be acknowledged and for the serve to send its own FIN. In this state the client can still receive data from the server but will no longer accept data from its local application to be sent to the server.
ESTABLISHED
Client Close Step #1 Receive and Step #2 Transmit: The server receives the client's FIN. It sends an ACK to acknowledge the FIN. The server must wait for the application using it to be told the other end is closing, so the application here can finish what it is doing.
CLOSE-WAIT
FIN-WAIT-1
Client Close Step #2 Receive: The client receives the ACK for its FIN. It must now wait for the server to close.
FIN-WAIT-2
CLOSE-WAIT
The server waits for the application process on its end to signal that it is ready to close.
FIN-WAIT-2
The client is waiting for the server's FIN.
CLOSE-WAIT
Server Close Step #1 Transmit: The server's TCP receives notice from the local application that it is done. The server sends its FIN to the client.
LAST-ACK
FIN-WAIT-2
Server Close Step #1 Receive and Step #2 Transmit: The client receives the server's FIN and sends back an ACK.
TIME-WAIT
LAST-ACK
The server is waiting for an ACK for the FIN it sent.
TIME-WAIT
The client waits for a period of time equal to double the maximum segment life (MSL) time, to ensure the ACK it sent was received.
LAST-ACK
Server Close Step #2 Receive: The server receives the ACK to its FIN and closes the connection.
CLOSED
TIME-WAIT
The timer expires after double the MSL time.
CLOSED
CLOSED
The connection is closed on the server's end.

CLOSED
The connection is closed.

CLOSED
The connection is closed.






No comments: