OSI layers, TCP/IP layers and encapsulation

Follow 11 bytes through TCP, IP and Ethernet headers and across a router that rebuilds the frame, then place the seven OSI and four Internet layers. Layers and encapsulation · Computer networks.

You type READ memo/7 into a memo app on your laptop. The memo server sits on another network, behind one office router. The 11 bytes fit in one segment and reach the server unchanged. The router sends a new frame. What does each layer add, which parts travel the whole way together, and which wrapper does the router rebuild?

Three questions, three headers

A header is a block of control fields placed before the data it describes.

Which program? A laptop runs many programs at once. The transport header carries ports; along with addresses and protocol, they help identify the communication endpoint. TCP's header is 20 bytes (without options) and holds source port 50210 and destination port 8080 (on an already-open connection; see the handshake lesson). The 11 bytes plus that header are a segment. UDP, the other common transport protocol, has an 8-byte header and calls its unit a datagram.

Which machine, perhaps on another network? The IP header carries the source and destination addresses that routers use. IPv4's header is 20 bytes (without options): source 10.1.5.20, destination 10.9.2.80, a TTL (time to live, lowered by at least 1 by each forwarding router) of 64, and protocol number 6, meaning TCP. Segment plus IP header is a packet: 20 + 31 = 51 bytes.

Which neighbour, over this one link? A link joins one interface to the next on the path, so a frame names both ends of that link by their MAC addresses, link-layer addresses assigned to network interfaces. Ethernet adds a 14-byte header (destination MAC, source MAC and the EtherType 0x0800, which says an IPv4 packet is inside) and a 4-byte trailer, the FCS (frame check sequence), which detects transmission errors. The frame is 14 + 51 + 4 = 69 bytes (the preamble is physical-layer overhead and is not counted).

Adding a header is encapsulation. Removing it at the receiving end is decapsulation.

What the router changes

The server is on another network, so the laptop hands the packet to its default gateway, the router. The laptop's Ethernet interface (MAC 3c:52:82:aa:10:01) builds the first frame, addressed to the router's interface :02.

Eleven bytes across one router

Read the steps as text

  1. Laptop will send 11 bytes of application data to Memo server, across 2 Ethernet links and 1 router.
  2. The application on Laptop hands down 11 bytes: "READ memo/7". Nothing has been added yet.
  3. Laptop's TCP layer adds a 20-byte header holding source port 50210 and destination port 8080. The segment is 31 bytes (20 + 11).
  4. Laptop's IPv4 layer adds a 20-byte header: source 10.1.5.20, destination 10.9.2.80, TTL 64, protocol 6 (TCP). The packet is 51 bytes (20 + 31).
  5. Laptop's eth0 wraps the packet in an Ethernet frame: source 3c:52:82:aa:10:01, destination 3c:52:82:aa:10:02, type 0x0800. The frame is 69 bytes (14 + 51 + 4).
  6. The frame crosses link 1 (Ethernet) from Laptop to Office router without changing.
  7. Office router checks the frame and removes the frame's 14-byte header and 4-byte FCS, leaving the 51-byte IP packet.
  8. Office router reads only the IP header: destination 10.9.2.80 picks the next hop. TTL falls from 64 to 63. The TCP header and the payload are not opened.
  9. Office router builds a NEW frame on eth1 (Ethernet): source 3c:52:82:aa:10:03, destination 3c:52:82:aa:10:04, the next hop. The Ethernet frame is 69 bytes (14 + 51 + 4).
  10. The frame crosses link 2 (Ethernet) from Office router to Memo server without changing.
  11. Memo server checks the frame and removes the frame's 14-byte header and 4-byte FCS; type 0x0800 says an IPv4 packet is inside.
  12. Memo server removes the 20-byte IPv4 header; protocol 6 says a TCP segment is inside.
  13. Memo server removes the 20-byte TCP header; destination port 8080 picks the receiving application.
  14. The application on Memo server receives all 11 bytes: "READ memo/7".

The office router checks and removes the frame. For this example, assume ordinary destination-based forwarding with no inspection or translation. The destination IP 10.9.2.80 determines the next link. It lowers the TTL from 64 to 63 and recomputes the IPv4 header checksum. Then it builds a new frame for link 2, with its own outgoing interface (:03) as source and the next hop's interface (:04, the server itself) as destination. The 31-byte segment stays intact. Both links are Ethernet, so the new frame is again 69 bytes.

MAC, IP and ARP explains how the router finds the next hop's MAC address, and longest-prefix routing how it chooses the link.

At the server the layers are removed in the reverse order. The frame comes off, and its EtherType 0x0800 says IPv4 is inside. The IPv4 header comes off, and protocol 6 says TCP. The TCP header comes off, and port 8080 helps select the receiving endpoint; the application receives its 11 bytes.

The same rule on other wrappers

The rule does not depend on TCP, IPv4 or Ethernet. A door sensor sends the 13 bytes door 7 locked in a UDP datagram of 8 + 13 = 21 bytes, inside an IPv6 packet. IPv6's header is a fixed 40 bytes, with a hop limit instead of a TTL and no header checksum. The packet is 21 + 40 = 61 bytes. The sensor's first link is Wi-Fi. Here an unprotected 802.11 three-address QoS data frame has a 26-byte MAC header and an 8-byte LLC/SNAP header carrying the EtherType (0x86DD for IPv6), then a 4-byte FCS, so the Wi-Fi frame is 34 + 61 + 4 = 99 bytes. The gateway is an access point and router in one box.

A sensor message from Wi-Fi to Ethernet

Read the steps as text

  1. Door sensor has wrapped 13 bytes of data: the UDP datagram is 21 bytes, the IPv6 packet 61 bytes and the 802.11 frame 99 bytes. It crosses 2 links (Wi-Fi, then Ethernet) and 1 router to reach Lock server.
  2. The frame crosses link 1 (Wi-Fi) from Door sensor to Wi-Fi gateway without changing. The 34 header bytes are 26 for 802.11 and 8 for LLC/SNAP.
  3. Wi-Fi gateway checks the frame and removes the frame's 34-byte header and 4-byte FCS, leaving the 61-byte IP packet.
  4. Wi-Fi gateway reads only the IP header: destination 2001:db8:9::80 picks the next hop. Hop limit falls from 64 to 63. The UDP header and the payload are not opened.
  5. Wi-Fi gateway builds a NEW frame on eth0 (Ethernet): source a4:83:e7:19:5c:23, destination a4:83:e7:19:5c:24, the next hop. The Ethernet frame is 79 bytes (14 + 61 + 4).
  6. The frame crosses link 2 (Ethernet) from Wi-Fi gateway to Lock server without changing.
  7. Lock server checks the frame and removes the frame's 14-byte header and 4-byte FCS; type 0x86DD says an IPv6 packet is inside.
  8. Lock server removes the 40-byte IPv6 header; next header 17 says a UDP datagram is inside.
  9. Lock server removes the 8-byte UDP header; destination port 5683 picks the receiving application.
  10. The application on Lock server receives all 13 bytes: "door 7 locked".

At the gateway the Wi-Fi frame is removed and a new Ethernet frame of 14 + 61 + 4 = 79 bytes is built for link 2. The frame changed technology and size; the 61-byte packet changed only its hop limit.

Large writes become several segments

TCP is a byte stream: writes do not prescribe segment boundaries. With a 1,500-byte packet limit (MTU; see fragmentation and ICMP) and 20-byte IPv4 and TCP headers, at most 1,460 data bytes fit per packet. If the sender may use that size and fills each segment, 3,000 bytes travel as 1,460, 1,460 and 80 bytes. The sender also respects the peer's advertised MSS (maximum segment size); a smaller limit or buffering can produce smaller segments.

Seven layers, four layers

OSI, the Open Systems Interconnection reference, names seven layers by their jobs.

OSI layerIts one jobIn the trace
7 Applicationthe protocol the program speaks, such as HTTPREAD memo/7
6 Presentationdata representation: encoding, compression, encryptionin the application
5 Sessionopening, keeping and recovering a dialoguein the application
4 Transportdelivery between programs by portTCP or UDP header
3 Networkaddresses and forwarding between networksIP header
2 Data linkframes on one link, MAC addresses, error detectionEthernet or Wi-Fi frame
1 Physicalbits as signals on a mediumcable or radio

RFC 1122 describes the Internet's protocols in four layers: link (OSI layers 1 and 2), internet (OSI's network layer), transport, and application (OSI layers 5 to 7). It has no separate session or presentation layer: those functions live inside applications and their libraries (TLS, for example, is usually placed between transport and application). Many textbooks teach five layers by splitting the link layer into physical and data link.

A layer also tells you what a device reads. A hub repeats bits (physical). A basic switch forwards frames by destination MAC (data link). A basic router uses the IP header to forward (network); devices can also perform other functions. A host uses every layer.

Recap

  • Ports help select an endpoint, IP addresses identify hosts, and the frame addresses the next link neighbour. The 11 bytes became a 31-byte segment, a 51-byte packet and a 69-byte frame.
  • In this trace the router removes the incoming frame, lowers the TTL by 1 and builds a new frame with a new MAC pair. It leaves the segment and IP addresses unchanged.
  • The destination removes the frame, the IP header and the transport header in that order; the EtherType and protocol number say what is inside, and the port picks the program.
  • OSI's seven layers and RFC 1122's four describe the same work. Basic router forwarding uses the network layer; session and presentation functions can be implemented by applications and libraries.
What this trace leaves out

Real paths add more. NAT rewrites addresses and ports at some routers, so the end-to-end addresses are not always unchanged. IPv4 options and IPv6 extension headers lengthen the IP header, and an 802.1Q VLAN tag adds 4 bytes to an Ethernet frame. An Ethernet payload under 46 bytes is padded. A packet too big for a link is fragmented or rejected, and a forwarding router that lowers a TTL to 0 drops the packet and normally sends an ICMP error, subject to ICMP exceptions.

Encrypted Wi-Fi frames carry extra bytes. Frames between a station and access point carry three addresses; the access point may be separate from the router.

Firewalls and load balancers read ports, so they look above the network layer.

Next: IPv4 subnetting and CIDR prefixes.