MA-NET1 · Module 4
Protocol layers and encapsulation
Topics: Five-layer stack · PDUs · Encapsulation · Addresses per layer
Overview
Session 4 — Protocol layers and encapsulation
By now Mira has seen the edge, the core, and the clock that measures delay. This session ties them together with the most important organising idea in networking: layers. Instead of one giant program that knows how to do everything from drawing a forecast to pushing electricity down a wire, the Internet splits the job into five stacked responsibilities. Each layer wraps the layer above it in its own envelope, a step called encapsulation, and each kind of device only opens as many envelopes as it needs.
By the end you should be able to: list the five layers in order and say what each one is responsible for; name the unit of data (message, segment, datagram, frame) at each layer; walk through encapsulation and de-encapsulation; say which layers a host, a router, and a link-layer switch process; and tell where a port number, an IP address, and a MAC address appear and which of them change hop by hop.
Scroll Concepts → Examples → Quiz. Plan on roughly 55–65 minutes. Drawing the stack on paper while you read is the single best study habit for this session.
Tip: if a paragraph feels dense, pause and say it back in your own words before moving on; that small habit saves time later.
Concepts
Core ideas — protocol layers, encapsulation, and who reads which label
Think about everything that has to happen when Mira's phone shows a forecast from api.skylark.example. Some program must decide what to ask for and how to display the answer. Something must make sure the request arrives complete and in the right order, even if parts of it go missing on the way. Something must pick a path across many networks. Something must hand the data across each individual link. And somewhere, electricity, light, or radio waves must actually move. That is a great deal of work. If all of it lived in one enormous program, nobody could ever understand, repair, or improve it. This session explains how the Internet avoids that fate: by dividing the work into layers. All names and numbers are invented for practice.
Why layers?
Look at a familiar system first: sending a parcel through a courier company. You pack the item and write an address (your job). A local depot sorts it into a van (their job). A long-distance truck takes it between cities (another job). A delivery person hands it over at the door (yet another). Each stage does its own work, relies on a simple promise from the stage below ("this van will reach the next depot"), and offers a simple promise to the stage above ("hand me a labelled box and I will move it along"). Neither you nor the delivery person needs to know how the other works. If the company swaps trucks for trains, you never find out.
Network designers use exactly the same idea. A protocol layer is a group of related functions that offers a service to the layer above while using the services of the layer below. Because each layer only talks to its neighbours through a narrow interface, we can change one layer without rewriting the others. Mira's forecast app works the same whether her data crosses fiber, copper, or radio, because the app only deals with the top layer.
There is a second advantage: layers give us names for our problems. When something fails, we can ask "at which layer did it fail?" and narrow the search quickly.
Layering has a cost too, and it is fair to name it. Each layer adds some bookkeeping information of its own, so a little extra data travels with every message, and sometimes two layers repeat a similar job, such as checking for errors. Designers accept these costs because the gain in clarity and flexibility is far larger. If that trade-off feels abstract, keep reading: the examples put real byte counts on it.
The five layers of the Internet stack
The Internet is usually described using five layers. Reading from the top, closest to the user, down to the bottom, closest to the wire:
5. Application layer. This is where network programs live and where application protocols define what messages mean: a request for a web page, a request for a forecast, a mail message, a name lookup. The data unit at this layer is called a message. Mira's app builds a message that says, in effect, "give me tomorrow's outlook for harbor-town".
4. Transport layer. This layer moves messages between application processes on two hosts. It breaks a long message into pieces, labels each piece with port numbers that say which program on each host is involved, and, depending on the transport protocol, may also add reliability, retransmitting pieces that go missing, or controlling the speed of sending. The data unit is a segment. The two best-known transport protocols are one that offers reliable, ordered delivery, and one that is simple, offers no guarantees, and is used when speed matters more than perfection.
3. Network layer. This layer moves data between hosts across many networks. It labels each unit with the IP address of the destination (and source) and defines how routers choose where to send it next. The data unit is a datagram, sometimes simply called a packet. This layer is the one that makes the "network of networks" possible: it offers the best-effort delivery service we met in Session 1.
2. Link layer. This layer moves a datagram across one link to the next device. Different links use different link-layer protocols: a wired local network, a wireless local network, a cellular link. The data unit is a frame. Frames carry link-layer addresses, also called MAC addresses, which identify devices on the same link.
1. Physical layer. This layer moves the individual bits across the physical medium, using voltages, light pulses, or radio waves. It cares about cable types, signal levels, and timing. The details differ for each medium, as Session 1 suggested.
To remember the order from top to bottom, many learners use the mnemonic "A-T-N-L-P" or invent a sentence of their own. What matters is the idea: application → transport → network → link → physical, from the user's program down to the wire.
Common confusion: The word "packet" is used loosely. Strictly, each layer has its own name for its data unit: message, segment, datagram, frame. In casual conversation, many people say "packet" for any of them. In this course, when precision matters, use the layer-specific term.
Encapsulation: envelopes inside envelopes
How do the layers actually cooperate? Through a mechanism called encapsulation, which is easiest to understand through the image of nested envelopes.
Mira's app creates a message, the actual request. It hands the message to the transport layer. The transport layer places the message inside a segment: it takes the message as the payload and adds a small header in front containing its own control information, such as the source and destination port numbers. The segment is passed down to the network layer, which wraps the entire segment as the payload of a datagram, adding a header with source and destination IP addresses. The datagram goes to the link layer, which wraps it in a frame, adding a link-layer header (with MAC addresses) and sometimes a trailer at the end for error checking. Finally, the physical layer sends the frame's bits as signals.
Each layer treats what it received from above as an opaque payload. The network layer does not look inside the segment; it simply carries it. This is exactly the courier-company arrangement, where the delivery person never opens the box.
The result is that each unit has the general shape
[ \text{unit at layer } k = \text{header}_k + \text{payload from layer } k+1\ (+ \text{ trailer, at the link layer}). ]
At the receiving host, the process runs in reverse, called de-encapsulation. The link layer strips off the frame header and trailer and passes the datagram upward; the network layer strips the IP header and passes the segment upward; the transport layer strips its header and, using the port number, hands the message to the right application; and the application reads the message. Each layer removes only its own envelope.
One practical consequence is overhead. Every layer adds header bytes, so the number of bytes sent on the wire is larger than the message. For a large message, this overhead is a small percentage; for a very small message, it can be a large fraction. We will measure this in the examples.
Who processes which layers?
Not every device on the path handles every layer. A device only needs to understand as much of the stack as its job requires, and this is one of the most testable ideas in the whole topic.
Hosts (Mira's phone, the Skylark server) implement all five layers. They create or consume messages, so they need the application layer, and they must also handle the rest of the stack to send and receive.
Routers implement the bottom three layers: network, link, and physical. A router receives a frame, strips the link header, reads the IP address in the datagram header, consults its forwarding table, and builds a new frame for the next link. It does not need to look at ports or messages; it never runs the application layer, and it normally passes segments along without reading them. A router has a physical, link, and network layer for each link attached to it.
Link-layer switches (and wireless access points acting as simple relays) implement the bottom two layers: link and physical. A switch looks at MAC addresses in frames and forwards frames within a local network. It does not read IP addresses.
You can summarise the story as a pyramid of attention: the closer to the middle of the network, the fewer layers a device inspects. This division of labour is why the core can be fast and simple while the edge hosts handle intelligence.
Common confusion: "Routers deal with ports." Ordinary routers forward based on the IP address in the datagram header and do not need the port. (Some special devices, such as firewalls and address-translating boxes, do look at ports, but those are extra functions layered on top of a router's basic job.)
Where do addresses and ports appear?
Because three different kinds of identifiers are involved, learners often scramble them. Let us place each one carefully.
A port number appears in the transport-layer header. It identifies a program (more precisely, a communication endpoint) on a host. A server program typically waits on a well-known port, and a client uses a temporary port chosen by its own operating system. Ports answer: which application on this machine?
An IP address appears in the network-layer header. It identifies a host's network interface in the global network. IP addresses answer: which machine, anywhere in the network of networks? The source and destination IP addresses normally stay the same during the whole journey from Mira's phone to the server (setting aside special devices that rewrite them).
A MAC address appears in the link-layer header. It identifies a device's interface on a single local link, and answers: which device on this link is the next recipient? MAC addresses are rewritten at every hop. When a router forwards a datagram, it puts it into a new frame whose source MAC is the router's own interface and whose destination MAC is the next device along the way. The old frame is discarded.
A helpful summary: the IP address is the street address of the final destination, the MAC address is the next door to knock on along the route, and the port number is the apartment number once you arrive.
Following a datagram across several hops
Let us trace the whole picture once in words. Mira's laptop sends a request to the Skylark server. At the laptop, the message is wrapped into a segment (with ports), the segment into a datagram (with the server's IP address), and the datagram into a frame whose destination MAC is that of the first router, the nearest relay toward the outside world. The frame crosses the first link.
At the first router, the physical and link layers receive the frame and the link layer removes the frame header. The network layer reads the destination IP address and consults the forwarding table to choose the outgoing link. The router wraps the same datagram in a new frame addressed to the next router, and sends it out. Notice that the datagram, with its IP addresses, and the segment inside it, with its ports, are untouched. Only the frame is new.
The same thing happens at each router along the path. Finally, the last link delivers a frame to the server. The server's link layer removes the frame header, the network layer removes the datagram header, the transport layer removes the segment header and uses the port to pick the right program, and the application layer reads the message.
So, at every hop: the frame changes, the datagram persists.
Layers and fault-finding
A last benefit of layering is practical. When something breaks, the layers give you an order in which to check. If the cable is unplugged, nothing above can work: a physical problem. If the cable is fine but the device cannot join the wireless network, the issue is at the link layer. If frames flow but packets cannot reach the destination, suspect the network layer: addressing or routing. If packets arrive but the program is unreachable, check the transport layer: a closed or wrong port. If everything below works but the answer is nonsense, the application protocol itself may be at fault. Working upward from the bottom, or narrowing down from symptoms, is a habit that will serve you in every later topic.
Pulling the session together
The Internet divides communication into five layers, listed from top to bottom: application (messages, where programs and application protocols live), transport (segments, process-to-process delivery with ports), network (datagrams, host-to-host delivery with IP addresses and routing), link (frames, hop-to-hop delivery with MAC addresses), and physical (bits on the medium). Encapsulation wraps each layer's payload with that layer's header on the way down; de-encapsulation unwraps on the way up. Hosts implement all five layers, routers the bottom three, and link-layer switches the bottom two. Ports live at transport, IP addresses at network, MAC addresses at link; MAC addresses change hop by hop, while IP addresses and ports ordinarily stay fixed from end to end.
In summary:
- Layers split a big problem into small parts with narrow interfaces between them.
- Order: application, transport, network, link, physical.
- Data units: message, segment, datagram, frame (and bits at the physical layer).
- Encapsulation adds a header (and sometimes a trailer) at each layer going down; each layer treats the payload as opaque.
- Hosts: five layers; routers: three; link-layer switches: two.
- Port = which program; IP address = which host; MAC address = which device on this link.
- At each router, the frame is replaced and the datagram is kept.
You now have the vocabulary for the whole course: later sessions open each layer in turn, starting from the application layer at the top.
Examples
Worked examples — wrapping, unwrapping, and tracing Mira's request
Four stories follow. Header sizes below are typical minimums, used here only to make the arithmetic concrete: a transport header of 20 bytes, a network-layer header of 20 bytes, and a link-layer header of 14 bytes plus a 4-byte trailer. Real sizes vary.
Example 1 — Wrapping a message, and counting the overhead
Scene. Mira's app sends a request message to api.skylark.example. We follow its size as each layer wraps it, then ask how much of the traffic is overhead, and then compare with a tiny message.
Numbers.
- Application message: 400 bytes.
- Transport header: 20 bytes.
- Network header: 20 bytes.
- Link header + trailer: 14 + 4 = 18 bytes.
- Outgoing link rate: 10 Mbps.
Working.
Step 1: transport layer. The message becomes the payload of a segment. Segment size = 400 + 20 = 420 bytes.
Step 2: network layer. The segment becomes the payload of a datagram. Datagram size = 420 + 20 = 440 bytes.
Step 3: link layer. The datagram becomes the payload of a frame. Frame size = 440 + 18 = 458 bytes.
Step 4: overhead. Total headers and trailer = 20 + 20 + 18 = 58 bytes. Fraction of the frame that is overhead = 58 / 458 ≈ 0.127, so about 12.7%.
Step 5: time on the link. Frame length in bits = 458 × 8 = 3,664 bits. Transmission delay = 3,664 / 10,000,000 = 0.0003664 s ≈ 0.37 ms.
Step 6: a tiny message. Now suppose the app sends only a 20-byte "ping". Frame size = 20 + 20 + 20 + 18 = 78 bytes. Overhead = 58 / 78 ≈ 0.744, about 74%.
Interpretation. For a 400-byte message, roughly one byte in eight is overhead. For a 20-byte ping, three bytes out of four are overhead. The headers are the same size in both cases, so small messages pay proportionally more.
Takeaway. Encapsulation adds a fixed cost at every layer. This is the price of layering, and it is why applications try to send fewer, larger messages when they can.
Example 2 — Tracing one request across four devices
Scene. Mira's laptop sits in a café. It reaches the outside world through a wireless access point that works as a simple link-layer relay, then a BayLink router (R1), then a SpineNet router (R2), then the Northline Relay server edge.northline.example. Labels below are invented.
Addresses (invented).
| Device | IP address | MAC address | |--------|-----------|-------------| | Mira's laptop | 198.51.100.24 | 02:00:00:00:00:0A | | Access point | (not used for forwarding) | 02:00:00:00:00:0B | | R1, café-side interface | 198.51.100.1 | 02:00:00:00:00:1A | | R1, SpineNet-side interface | 203.0.113.9 | 02:00:00:00:00:1B | | R2, R1-side interface | 203.0.113.10 | 02:00:00:00:00:2A | | R2, server-side interface | 203.0.113.65 | 02:00:00:00:00:2B | | Northline server | 203.0.113.80 | 02:00:00:00:00:3A |
Working. We list, for each of the three links, the frame addresses and the datagram addresses.
Hop 1: laptop → R1 (the access point only relays the frame). Frame: source MAC …0A, destination MAC …1A. Datagram inside: source IP 198.51.100.24, destination IP 203.0.113.80.
Hop 2: R1 → R2. R1 removed the old frame, found the destination IP, chose its outgoing link, and built a new frame. Frame: source MAC …1B, destination MAC …2A. Datagram: source IP 198.51.100.24, destination IP 203.0.113.80 (unchanged).
Hop 3: R2 → server. Frame: source MAC …2B, destination MAC …3A. Datagram: still source 198.51.100.24, destination 203.0.113.80.
Which layers does each device process?
| Device | Layers processed | Reason | |--------|------------------|--------| | Laptop | 5 (application to physical) | It is a host: it makes the message | | Access point (simple relay) | 2 (link, physical) | It forwards frames only, by MAC | | R1 | 3 (network, link, physical) | It reads IP addresses to choose a link | | R2 | 3 | Same as R1 | | Server | 5 | It is a host: it reads the message |
Interpretation. Compare the three frames: the MAC addresses on each differ, because each frame lives on one link only. Compare the three datagrams: the IP addresses are identical. The access point never touched the IP header, and the routers never touched the message.
Takeaway. MAC addresses are hop-by-hop; IP addresses are end-to-end. A device processes layers only up to the one it needs.
Example 3 — Ports tell the programs apart
Scene. Mira's phone runs the Skylark Forecast app. It opens three simultaneous connections: one to the forecast server for data, one to the Northline Relay edge for icons, and a second one to the forecast server for a background refresh. All of this traffic arrives at the same phone, and the replies have to reach the right place.
Numbers (invented).
- Phone IP address: 198.51.100.24.
- Forecast server
api.skylark.example: IP 203.0.113.40, listening on port 443. - Edge server
edge.northline.example: IP 203.0.113.80, listening on port 443. - Temporary ports chosen by the phone: 50312, 50313, 50314.
Working.
Step 1: list each connection with its four identifiers.
Connection A (forecast data): 198.51.100.24:50312 <-> 203.0.113.40:443
Connection B (icons): 198.51.100.24:50313 <-> 203.0.113.80:443
Connection C (background poll): 198.51.100.24:50314 <-> 203.0.113.40:443
Step 2: what does the forecast server see? Connections A and C both arrive at 203.0.113.40 port 443 from the same phone IP. The only thing that tells them apart is the source port (50312 versus 50314). The server's transport layer uses the full set of four values (source IP, source port, destination IP, destination port) to give each segment to the right conversation.
Step 3: what does the phone see? A reply from 203.0.113.80 with destination port 50313 can only belong to the icon connection, so the phone's transport layer delivers its payload to the part of the app handling icons.
Step 4: where do routers look? The routers between the phone and the servers consult only the destination IP address (203.0.113.40 or 203.0.113.80) to choose a link. They do not use 50312 or 443 to decide where to forward.
Interpretation. Three identifiers, three jobs. The IP address gets the datagram to the right host. The port gets the segment to the right program or conversation. The MAC address, absent from this listing, would be rewritten at every hop.
Takeaway. One host can run many conversations at once, because the transport layer multiplexes them using port numbers.
Example 4 — Which layer is the culprit?
Scene. Mira helps friends with five different complaints. For each, name the layer most likely at fault.
- "The cable from my laptop to the wall is loose, and the link light is off."
- "I am sitting next to the café's access point, but my laptop says the wireless password is rejected."
- "My laptop is connected, but every request to
203.0.113.80says there is no route to that host." - "I can reach the server's machine, but nothing answers on the port my program is using."
- "Everything connects, but the server replies 'unknown request type' to my message."
Working.
- Physical. Nothing is being signalled on the medium. No higher layer can work until bits can move.
- Link. The physical radio works, since the laptop can see the access point. The failure is in the local link's joining rules: association and authentication at the wireless link layer.
- Network. Frames can move on the local link, but routing or addressing cannot find a path to the destination IP.
- Transport. The datagram reaches the host (network layer works), but no program is listening on that port, or the port is wrong.
- Application. All lower layers deliver the message correctly; the application protocol itself rejected its content.
Interpretation. The questions run bottom-up: can bits move? can frames move on this link? can datagrams find the host? can segments find the program? is the message acceptable? The first "no" tells you the layer.
Takeaway. Layers give a ladder for diagnosis. Start at the bottom, or start from the symptom, and ask which layer's promise has broken.
Practice check — protocol layers and encapsulation
Multiple-choice check from the Matheion app lesson. Pick an answer for each question, then submit.
1.Which list gives the five Internet layers in order from the top (closest to the user's program) to the bottom?
2.Match the names: which pairing of layer and data unit is correct?
3.Mira's app produces a 600-byte message. The transport header is 20 bytes, the network header is 20 bytes, and the link layer adds 14 bytes of header and 4 bytes of trailer. How large is the frame?
4.Which layers does an ordinary router process for a datagram it is forwarding?
5.A datagram travels from Mira's laptop to a server, crossing three links. Which statement is correct about the addresses seen on the three links?
6.Two tabs on Mira's laptop both connect to the same server IP address on port 443. What lets the laptop's transport layer give each reply to the correct tab?
7.A friend can connect to the café's wireless network and receives frames fine, but every attempt to reach a particular server's IP address fails with 'no route to host'. Which layer is the most likely cause?