Sub topic 3
Encapsulation and headers as data moves down the stack
This chapter assumes you know the layer names from the previous chapter (application, transport, network, link, physical) and can convert between bits and bytes. The aim is to trace what happens to a message as each layer wraps it, and to compute the size cost of that wrapping.
The core idea: each layer wraps what it receives
A layer does not look inside the data handed to it by the layer above. It treats that data as an opaque payload, adds its own control information, and passes the result down. The control information at the front is a header. Some layers also add a trailer at the end. A header or trailer, together with its payload, forms the layer's protocol data unit (PDU).
Wrapping on the way down is encapsulation. Unwrapping on the way up is decapsulation. Each layer on the receiver reads only the header written by its peer layer on the sender, removes it, and hands the payload upward.
| Layer | PDU name | Header or trailer added (TCP/IP over Ethernet) | Typical size |
|---|---|---|---|
| Application | Message | Protocol-specific (for example HTTP) | Varies |
| Transport | TCP segment, UDP datagram | TCP header, or UDP header | 20 bytes (TCP, no options), 8 bytes (UDP) |
| Network | IP packet (datagram) | IPv4 header | 20 bytes (no options) |
| Link | Frame | Ethernet header and FCS trailer | 14 bytes + 4 bytes |
| Physical | Bits (or symbols) | Preamble for synchronization | 8 bytes on Ethernet |
What the headers contain
Headers exist so the peer layer can do its job. Each field answers a question that layer must solve.
- Link header (Ethernet): destination MAC address, source MAC address, and an EtherType field. EtherType tells the receiver which network-layer protocol is inside (0x0800 means IPv4).
- Link trailer (FCS): a CRC computed over the frame, used to detect bit errors. A frame that fails the check is dropped.
- Network header (IPv4): source and destination IP addresses, TTL, a protocol field, total length, and fragmentation fields (identification, flags, fragment offset). The protocol field says what the payload is (6 means TCP, 17 means UDP).
- Transport header (TCP): source and destination port numbers, sequence number, acknowledgment number, flags, window size, and checksum.
Each "which protocol is inside" field is a demultiplexing key. The receiving stack uses EtherType to choose IP, the IP protocol field to choose TCP or UDP, and the destination port to choose the application socket. Without these keys, the receiver would not know which module should read the next header.
Addresses that change and addresses that do not
When a packet crosses several links, the layers behave differently.
- The IP addresses stay the same from source host to destination host (ignoring NAT, which rewrites them).
- The MAC addresses are replaced at every hop. Each router removes the old frame, reads the IP header to choose the next hop, and builds a new frame with new MAC addresses.
- The router decrements the IP TTL and recomputes the IP header checksum. It never reads the TCP header in normal forwarding.
This is why a router is called a layer-3 device, as the next-but-one chapter formalizes. It decapsulates up to the IP header, makes a decision, and encapsulates again.
Worked example: size at each layer
Suppose an application hands TCP 1000 bytes of data. TCP adds a 20-byte header, IP adds 20 bytes, and Ethernet adds 14 bytes of header and 4 bytes of FCS.
- Segment: 1000 + 20 = 1020 bytes
- Packet: 1020 + 20 = 1040 bytes
- Frame: 1040 + 14 + 4 = 1058 bytes
The frame carries 58 bytes of overhead for 1000 bytes of data, so the fraction of useful bytes is 1000/1058, about 94.5%.
Segmentation and the MSS
Ethernet limits the payload of a frame to 1500 bytes. This limit is the maximum transmission unit (MTU) of the link. Because the IP packet must fit in the frame payload, the TCP data in one segment is limited to
MSS = MTU − IP header − TCP header = 1500 − 20 − 20 = 1460 bytes.
The maximum segment size (MSS) is the largest amount of application data in one TCP segment. A larger message is cut into several segments, and each segment gets its own full set of headers. The code below computes the cost of sending a message this way.
import math
def stack_cost(app_bytes, mss=1460, tcp=20, ip=20, eth_header=14, eth_fcs=4):
"""Bytes sent for app_bytes of data, with per-segment headers.
Preamble and inter-frame gap are not counted."""
segments = math.ceil(app_bytes / mss)
per_frame_overhead = tcp + ip + eth_header + eth_fcs
total = app_bytes + segments * per_frame_overhead
return segments, total, app_bytes / total
segments, total, efficiency = stack_cost(10_000)
print(segments, total, round(efficiency, 4)) # 7 10406 0.961Ten thousand bytes need ceil(10000/1460) = 7 segments, because six full segments carry 8760 bytes and a seventh carries the remaining 1240. Seven frames add 7 × 58 = 406 bytes of overhead, giving 10 406 bytes on the wire and an efficiency of about 96.1%.
The efficiency depends on the payload size. A tiny payload pays the same 58 bytes as a large one, so efficiency falls sharply for small messages. Sending 1 byte over TCP/IP/Ethernet gives 1/59, under 2%. (Real Ethernet also pads very short frames to a 64-byte minimum, which lowers efficiency further.)
Fragmentation at the IP layer
The MSS rule avoids fragmentation when TCP knows the MTU. A router may still meet a link with a smaller MTU than the packet size. In IPv4, the router can split the packet into fragments. Each fragment is a complete IP packet with its own header, and only the destination host reassembles them.
The fragmentation fields work like this:
- Identification is the same value in all fragments of one original packet.
- More Fragments (MF) is 1 in every fragment except the last.
- Fragment offset gives where the fragment's data begins in the original data, in units of 8 bytes.
Because the offset is in 8-byte units, the data in every fragment except the last must be a multiple of 8 bytes.
Take an IP packet of 4000 bytes (20-byte header, 3980 bytes of data) that meets a link with MTU 1500.
- Maximum data per fragment: 1500 − 20 = 1480 bytes, and 1480 is a multiple of 8 (1480/8 = 185), so it is allowed.
- Fragment 1: 1480 bytes of data, offset 0, MF = 1.
- Fragment 2: 1480 bytes of data, offset 185, MF = 1.
- Fragment 3: 3980 − 2960 = 1020 bytes of data, offset 370, MF = 0.
The three fragments total 4000 + 2 × 20 = 4040 bytes of IP packets, because each extra fragment repeats the 20-byte header. If the MTU were 1500 but the link allowed only 1498 bytes of data per fragment's payload, you would round the data down to the nearest multiple of 8 (1496 here, before subtracting nothing else) so the offsets stay whole numbers.
Common errors
- Adding headers once instead of per segment or fragment. Every segment gets its own TCP, IP and link headers. Every fragment gets its own IP header.
- Forgetting that the IP total length includes the IP header. Subtract 20 bytes to find the data.
- Using bytes for offsets. The fragment offset field counts 8-byte blocks.
- Assuming a switch or router reads all headers. A switch reads the link header. A router reads the link header, then the IP header. Neither reads the TCP header during forwarding.
- Mixing PDU names. "Segment" is TCP, "packet" is IP, "frame" is the link layer. GATE questions use these words with exact meaning.
Why this matters for later chapters
Overhead feeds directly into performance calculations. A header adds to the number of bits to transmit, so it raises transmission delay (packet bits divided by link rate). In the delay chapters you will see a packet size that includes headers, and the segmentation trade-off: smaller packets pipeline better across store-and-forward links, but each one carries a full header.
Exercise
An application sends 5000 bytes over TCP/IP/Ethernet. Use a 20-byte TCP header, a 20-byte IP header, a 14-byte Ethernet header, a 4-byte FCS, and an MTU of 1500 bytes. How many frames are needed, how many bytes are sent in total, and what is the efficiency (useful bytes divided by total bytes)?
Show answer
MSS = 1500 − 40 = 1460. Segments = ceil(5000/1460) = 4 (three full segments carry 4380 bytes, and the fourth carries 620). Overhead per frame = 20 + 20 + 14 + 4 = 58 bytes, so total overhead = 4 × 58 = 232 bytes. Total sent = 5000 + 232 = 5232 bytes. Efficiency = 5000/5232 ≈ 0.9557, about 95.6%.
Exercise
An IP packet of 3020 bytes (20-byte header) must cross a link with MTU 1000. Give the number of fragments, and the data length, fragment offset and MF flag of each.
Show answer
Maximum data per fragment = 1000 − 20 = 980, which is not a multiple of 8, so round down to 976 (976/8 = 122). The original data is 3000 bytes. Fragment 1: 976 bytes, offset 0, MF = 1. Fragment 2: 976 bytes, offset 122, MF = 1. Fragment 3: 976 bytes, offset 244, MF = 1. Fragment 4: 3000 − 2928 = 72 bytes, offset 366, MF = 0. There are 4 fragments.
Exercise
A router forwards a packet from host A to host B over two different Ethernet links. Which of these fields change at the router: source MAC, destination MAC, source IP, destination IP, TCP source port, IP TTL?
Show answer
The source MAC, destination MAC and IP TTL change. The source IP, destination IP and TCP source port stay the same, assuming no NAT.