Layer 4 Models
How devices process UDP segments
This procedure explains how a device sends and receives UDP
segments.
-
When the device receives a segment:
-
It de-encapsulates it and examines the UDP header for port information.
-
It then maps the local port information and sends the payload up to a higher
layer (the application layer) for processing.
-
If it cannot find the upper process based on the port information, it drops the segment.
-
When the device wants to send a segment:
-
It encapsulates the payload with a UDP header.
-
It sends the segment to the lower layer for processing.
How TCP Connection handles outgoing data
When TCP Connection is ready to send data in the out buffer: (flowchart here):
- It checks if the TCP connection State is Established:
- If no, it drops the data.
- If yes,
- It adds the new data onto the out buffer.
- It checks if the Nagle service is turned on:
- If yes, it checks if the size of the out buffer is greater than the MSS:
- If yes, it starts the initial data sending step.
- If no, it checks if there is any unacknowledged data:
- If no, it starts the initial data sending step.
- If yes, it continues to wait for more incoming data.
- If no, it starts the initial data sending step.
- The initial data sending step:
- TCP checks if there are any segments to be retransmitted:
- If yes, it stops and waits.
- If no, it checks if the usable window is smaller than the MSS, and there is more data in the out buffer than usable window size:
- If yes, it stops and waits.
- If no, it starts the new segment sending step.
- The new segment sending step:
- It checks if there are any segments to be retransmitted:
- If yes, it stops and waits.
- If no, it goes through the following steps:
- It updates and checks the unacknowledged data buffer for any segments needed to be retransmitted and transmits those first.
- Otherwise, it prepares the data in the out buffer into TCP segments, and it will eventually send all segments out.
How TCP Connection handles incoming TCP segments
When TCP connection receives a TCP segment (flowchart here):
- It updates the Receive-Window variable from the TCP header.
- If the connection is not in the LISTEN state:
- It resets the inactivity timer.
- If the sequence number is equal to the ReceiveNext variable:
- The TCP segment is expected.
- Proceeds to the next step.
- Else
- If the sequence number in the received TCP header is less than the Receive-Next variable.
- If connection is not ESTABLISHED, the segment is a duplicate.
- If the header is not a RST.
- Clears the ACK timer.
- Resets the received ACK counter.
- Sends an ACK out.
- Else
- This is an unexpected segment.
- If the header is a RST, processes RESET.
- Drops this segment.
- If the connection is in the LISTEN state:
- If this is a SYN segment, the server port accepts the connection.
- Else the server port sends back a RST and drops the segment.
- If the connection is in the SYN_SENT state:
- If the TCP header is a SYN or ACK:
- Clears the timer for retransmitting control data.
- Calculates the Receive-MSS and Send-MSS variables from the advertised MSS in the TCP header.
- Updates the local IP.
- Updates the Receive-Next with the ACK number in the TCP header.
- Sets the connection's state to ESTABLISHED.
- If the TCP header is a FIN:
- The TCP connection is refused.
- Processes FIN.
- If the TCP header is a RST:
- The TCP connection was refused.
- Processes RESET.
- If the connection is in the SYN_RECEIVED state:
- If the header is a SYN or RST
- The connection was reset.
- Processes RESET.
- If the TCP header is FIN
- If the TCP header is an ACK
- Clears the timer for retransmitting control data
- Sets the connection's state to ESTABLISHED.
- If the connection is in the ESTABLISHED state:
- If the header is a SYN or RST
- The connection was reset.
- Processes RESET.
- If the header is a FIN:
- The TCP connection was disconnected.
- Increments the Receive-Next variable.
- Processes FIN.
- If it is an ACK:
- Processes ACK.
- Clears the timer for retransmitting data.
- If the header is an ACK and it's not a RST:
- Checks if it is a duplicate segment.
- If it's not duplicate and not an empty ACK:
- Increments the Receive-Next variable with the size of the received data segment.
- Processes ACK.
- If it's duplicate and not an empty ACK:
- Drops the duplicate segment.
- Clears the timer for sending ACK.
- Sends an ACK.
- Else
- If it is not an empty ACK:
- If the connection is ESTABLISHED:
- If acked some packets, and nothing is being retransmitted, and there is buffer data,
- If it is not an empty ACK:
- Checks if an ACK needs to be sent out.
- If the connection is in the FIN_WAIT_1 state:
- If the header is a SYN or RST
- If the TCP header is a FIN:
- Clears the timer for retransmitting control data.
- Sets the connection's state to CLOSING.
- Sends an ACK.
- If the TCP header is an ACK:
- Clears the timer for retransmitting control data.
- Sets the connection's state to FIN_WAIT_2.
- If the connection is in the FIN_WAIT_2 state:
- If the header is a SYN or RST
- If the TCP header is a FIN
- Sets the connection's state to TIMED_WAIT.
- If the connection is in the LAST_ACK state:
- If the header is a SYN or RST
- If the TCP header is an ACK or FIN:
- Processes ACK.
- Clears the timer for retransmitting control data.
- Sets the connection's state to CLOSED.
- If the connection is in the CLOSING state:
- If the header is a SYN or RST
- If the TCP header is an ACK
- Sets the connection's state to TIMED_WAIT.
- If the connection is in the CLOSED state:
- Clears the timer for retransmitting control data.