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 able to run a command in a terminal.
Why the names matter
Each layer wraps the data it receives from above in its own header. The result is called a protocol data unit (PDU). A PDU is the unit of data that one layer hands to its peer layer on another machine. The name of the PDU tells you which layer someone is talking about. If a colleague says "the packet was dropped," you need to know whether they mean an IP packet or something else.
The four common names
| Layer (TCP/IP) | OSI layers | PDU name | Example header |
|---|---|---|---|
| Application | 5-7 | Message or data | HTTP request, DNS query |
| Transport | 4 | Segment (TCP) or datagram (UDP) | TCP or UDP header with ports |
| Internet | 3 | Packet | IP header with addresses |
| Link | 1-2 | Frame | Ethernet header and trailer |
Here is what each name means:
- A frame is the unit at the link layer. An Ethernet frame has a destination and source MAC address, a type field, the payload, and a frame check sequence (FCS) at the end.
- A packet is the unit at the internet layer. An IP packet carries source and destination IP addresses and a time-to-live (TTL) value.
- A segment is the unit of TCP. It carries sequence numbers, acknowledgment numbers, and flags such as SYN and ACK.
- A datagram is a self-contained unit sent without a prior connection. A UDP datagram is the standard example. The word is also used for an IP packet, because IP is connectionless too.
How the units nest
Each PDU sits inside the payload of the one below it. A TCP segment is the payload of an IP packet. The IP packet is the payload of an Ethernet frame.
Worked example: a UDP message
This program sends five bytes over UDP. It 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 rest:
- The application data is
hello, 5 bytes. - The transport layer adds an 8-byte UDP header. The result is a 13-byte UDP datagram.
- The internet layer adds a 20-byte IPv4 header, assuming no options. The result is a 33-byte IP packet.
- The link layer adds a 14-byte Ethernet header and a 4-byte FCS on a real Ethernet link. The result is a 51-byte frame. Ethernet pads short frames to a minimum size, so the wire size is larger.
Traffic to 127.0.0.1 uses the loopback interface, which has no Ethernet header. To see all the layers, send to another host on your network.
Seeing the names in a capture
You can build a packet yourself with Scapy. The / operator stacks one layer on top of the next.
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 (frame), an IP section (packet), and a TCP section (segment), in that order from outside to inside.
Common errors
- Calling everything a packet. In casual speech this is fine. In a troubleshooting report, "packet" should mean the IP-layer unit. Use "frame" when you mean MAC addresses and "segment" when you mean TCP sequence numbers.
- Calling a TCP unit a datagram. TCP is a byte stream. Its units are segments.
- Treating the names as strict. Some documents use "packet" for any PDU. Read the context.
Exercise
A capture shows a unit with source port 51514, destination port 443, a SYN flag, and a sequence number. Which layer is this, and what is the unit called?
Show answer
The transport layer. Because it is TCP (it has a SYN flag and sequence number), the unit is a segment. A UDP unit would be a datagram.
Exercise
You send 100 bytes of application data over UDP on IPv4 and Ethernet, with no IP options. How large is the frame before any padding, and how did you get that?
Show answer
146 bytes. 100 data + 8 UDP header = 108 bytes. Adding the 20-byte IPv4 header gives 128 bytes. Adding the 14-byte Ethernet header and 4-byte FCS gives 146 bytes.