Sub topic 10
Naming the data at each layer: frame, packet, segment, datagram
This chapter assumes you know the four TCP/IP layers (link, internet, transport, application) and the idea of encapsulation from earlier chapters. You should also be comfortable running a command in a terminal and reading a few lines of Python.
Why the names matter
Each layer wraps the data it receives in its own header. The result is a protocol data unit (PDU), which is the chunk of data a layer hands to the layer below it. Each layer has a conventional name for its PDU. Engineers use these names to say where in the stack something happens.
Compare two bug reports:
- "The network dropped a packet."
- "The switch dropped a frame with a bad checksum."
The second report tells you the layer (link), the device type (a switch works with frames), and the failure (a frame check error). The first tells you almost nothing. Using the right name narrows the search.
The four names
| Layer (TCP/IP) | OSI layer | PDU name | Addressing used | Example header |
|---|---|---|---|---|
| Application | 5-7 | message, or just "data" | URLs, names, application IDs | HTTP request line, DNS query |
| Transport | 4 | segment (TCP), datagram (UDP) | Port numbers | TCP header, UDP header |
| Internet | 3 | packet (or datagram) | IP addresses | IPv4 or IPv6 header |
| Link | 1-2 | frame | MAC addresses | Ethernet header and trailer |
Two points in the table cause most of the confusion, so the rest of the chapter covers them in detail.
Frame: the link layer unit
A frame is the unit the link layer sends across one physical or wireless hop. An Ethernet frame has three parts:
- A header with the destination MAC address, the source MAC address, and an EtherType field. The EtherType says what the payload is, for example
0x0800for IPv4,0x86DDfor IPv6, and0x0806for ARP. - A payload, which is the packet from the layer above.
- A trailer containing the frame check sequence (FCS), a 32-bit CRC that lets the receiver detect corruption.
The Ethernet trailer is the only common place where a layer adds data at the end. Most capture tools do not show the FCS, because the network card usually checks it and strips it before the operating system sees the frame.
A frame lives for exactly one hop. A router receives a frame, removes the frame header and trailer, reads the packet inside, decides where it goes next, and builds a new frame with new MAC addresses for the next hop. The packet inside stays the same (apart from a few IP header fields such as TTL), but the frame is replaced at every hop.
The maximum size of the payload in a standard Ethernet frame is 1500 bytes. This limit is the maximum transmission unit (MTU) of the link.
Packet: the internet layer unit
A packet is the unit the internet layer routes from the source host to the destination host. Its header carries the source and destination IP addresses, a TTL (IPv4) or hop limit (IPv6), and a protocol number that says what the payload is. For example, protocol 6 means TCP, 17 means UDP, and 1 means ICMP.
Unlike a frame, a packet is end to end. The source and destination IP addresses normally stay the same from sender to receiver. Exceptions exist, such as network address translation (NAT), which rewrites addresses in transit.
If a packet is larger than the MTU of a link, IPv4 routers can split it into fragments. Each fragment is itself a packet with its own IP header. IPv6 routers do not fragment. Only the sending host may do so.
Segment and datagram: the transport layer units
The transport layer has two common protocols, and they use different names.
- A TCP segment is the PDU of TCP. It carries a sequence number, an acknowledgment number, flags such as SYN and ACK, and a window size. TCP treats the data as a stream of bytes and cuts that stream into segments.
- A UDP datagram is the PDU of UDP. UDP adds only source port, destination port, length, and checksum. Each
sendcall produces one datagram, and message boundaries are preserved.
The size of a TCP segment is bounded by the maximum segment size (MSS), which is normally the MTU minus 40 bytes of IPv4 and TCP headers. On standard Ethernet this gives an MSS of 1460 bytes.
The overloaded word "datagram"
The word "datagram" is used at two layers. RFC 768 calls the UDP PDU a user datagram. RFC 791 describes IP as moving "internet datagrams". In informal use, many people say "packet" for any unit at layer 3 and above.
You will therefore see these usages:
- "UDP datagram" always means the transport-layer unit.
- "IP datagram" means the same thing as an IP packet.
- "Packet" is also used loosely for any unit on the wire, including frames and segments.
When precision matters, give the protocol name: "TCP segment", "UDP datagram", "IPv4 packet", "Ethernet frame". Those four phrases are unambiguous.
How the names nest
The diagram shows one HTTP request sent over TCP, IPv4, and Ethernet. Each PDU contains the one above it as its payload.
Read the diagram from top to bottom to follow decapsulation on the receiver, and from bottom to top to follow encapsulation on the sender.
Seeing the names in code
The Scapy library builds each layer as a separate object and stacks them with the / operator. The order of the operands matches the nesting in the diagram: the outermost header comes first.
from scapy.all import Ether, IP, TCP
pkt = Ether() / IP(dst="192.0.2.10") / TCP(dport=80, flags="S")
pkt.show()The output lists an Ether section (the frame header), an IP section (the packet header), and a TCP section (the segment header). Scapy does not use the strict PDU names, but the structure is the same one the table above describes. Here the segment carries no application data, because a SYN segment only opens a connection.
The next example sends one UDP datagram. The program supplies only the data, an address, and a port.
import socket
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as s:
s.sendto(b"hello", ("127.0.0.1", 9999))The operating system builds the UDP datagram, wraps it in an IP packet, and passes it to the link layer. On the loopback address, the system usually skips real Ethernet framing, so a capture on lo may show a different link-layer header than a capture on a physical interface.
Matching names to a capture
A capture lets you check each name against real bytes. This command shows five TCP packets on port 80, with link-layer addresses (-e) and IP and TCP detail (-v):
sudo tcpdump -nn -e -v -c 5 'tcp port 80'Each line of output follows the same order:
- The first fields show the source and destination MAC addresses and the EtherType. These belong to the frame.
- The next fields show the IP version, TTL, total length, protocol, and the two IP addresses. These belong to the packet.
- The last fields show ports, flags, sequence numbers, and window size. These belong to the segment.
Note that tcpdump calls each captured item a "packet" even though it records the whole frame. This is the loose usage described earlier. You can still recover the layers by reading the headers in order.
Common errors
- Calling every unit a packet. This is not always wrong in casual speech, but it hides the layer. In a design review or a bug report, use the specific name.
- Saying "TCP datagram". TCP produces segments. The word datagram goes with UDP and IP.
- Expecting message boundaries in TCP. Two
sendcalls of 100 bytes may arrive as one 200-byte read, or as three reads. TCP segments are an internal detail and do not match application messages. - Expecting a UDP datagram to be split. A UDP datagram is sent whole. If it is larger than the MTU, IP fragments it, and the loss of any fragment loses the entire datagram.
- Assuming the frame survives the whole trip. The frame is rebuilt at every router, so the source MAC address you see at the destination is that of the last router, not the original sender.
- Forgetting the application layer has no agreed name. HTTP calls its units requests and responses, DNS calls them queries and responses, and TLS calls them records.
Size limits at a glance
| Unit | Typical limit | Where the limit comes from |
|---|---|---|
| Ethernet frame payload | 1500 bytes | Link MTU |
| IPv4 packet | 65,535 bytes | 16-bit total length field |
| TCP segment payload | 1460 bytes on Ethernet | MSS, MTU minus IP and TCP headers |
| UDP datagram payload | 65,507 bytes | IPv4 maximum minus 20-byte IP header and 8-byte UDP header |
Only the link limit is usually reached in practice. Larger IP packets are cut up or never built, because TCP chooses a segment size that fits the path.
Exercise
A host sends 3,000 bytes with one TCP send call over standard Ethernet with an MSS of 1460. Name the unit at each of the transport and link layers, and say roughly how many of each are sent.
Show answer
At the transport layer the data is split into TCP segments. 3,000 bytes need three segments (1460 + 1460 + 80 bytes of payload). Each segment goes into its own IP packet, and each packet goes into its own Ethernet frame, so about three frames are sent, plus any frames for acknowledgments coming back.
Exercise
A packet travels from a laptop through two routers to a server. How many times is the Ethernet frame built, and which addresses change between hops?
Show answer
The frame is built three times, once for each hop (laptop to router 1, router 1 to router 2, router 2 to server). The source and destination MAC addresses change at every hop. The source and destination IP addresses stay the same unless NAT rewrites them.
Exercise
Why is "TCP datagram" an incorrect term, and what is the correct name for the same unit?
Show answer
TCP's unit is called a segment, because TCP works on a byte stream that it cuts into segments. The word datagram is used for UDP and for IP. The correct term is "TCP segment".