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 devices handle TCP segments

When the device receives a TCP segment (flowchart here):

- If there is no socket listening at the destination port or no connections matching the source and destination IPs and ports:

- Drop the segment and stop.

- If the connection is not in LISTEN state:

- If the sequence number in the received TCP header is 1 less than the connection's sequence number:

- Mark this as a duplicate segment.

- If the sequence numbers are the same:

- If the TCP header is not an empty ACK:

- Increment the sequence number.

- Otherwise:

- Drop the segment and stop.

- If the ACK number in the received TCP header is 1 more than the connection's sequence number:

- Increment the connection's sequence number.
- Pop out the last sent segment in buffer.
- If there are more segments in buffer:

- Send the next segment.

- If the TCP header is not an empty ACK:

- Send an ACK.

- If this is a duplicate segment:

- Drop segment and stop.

- If the connection is in LISTEN state:

- Start a new connection.
- Send a SYN + ACK.
- Set the new connection's state to SYN_RECEIVED.

- If the connection is in SYN_SENT state:

- If the TCP header is a SYN + ACK:

- Send an ACK.
- Set the connection's state to ESTABLISHED.

- If the connection is in SYN_RECEIVED state:

- If the TCP header is an ACK:

- Set the connection's state to ESTABLISHED.

- If the connection is in ESTABLISHED state:

- If the TCP header's PUSH flag is set:

- Send the data to the higher layer.

- Otherwise, buffer the data.

- If the connection is in FIN_WAIT_1 state:

- If the TCP header is a FIN:

- Set the connection's state to CLOSING.

- If the TCP header is an ACK:

- Set the connection's state to FIN_WAIT_2.

- If the connection is in FIN_WAIT_2 state:

- If the TCP header is a FIN:

- Set the connection's state to TIMED_WAIT.

- If the connection is in LAST_ACK state:

- If the TCP header is an ACK:

- Set the connection's state to CLOSED.

- If the connection is in CLOSING state:

- If the TCP header is an ACK:

- Set the connection's state to TIMED_WAIT.