E · 02 Computer Networks & Security

Layers & Packets

Layers & Packets

Every packet on the internet wears four envelopes. In this episode we open each one: what's written on it, who reads it, and why the internet is built in layers at all. OSI vs TCP/IP, real TCP/IP/Ethernet headers, MTU, and the one security rule every engineer should know: encryption protects every layer above it, never below.

Chapters

Transcript

In the last episode, a tiny question, where is example.com, left your laptop wrapped in four envelopes. We moved fast. Today, we slow down and open every single one. What's written on each envelope? Who reads it?

And why would anyone design the internet as a stack of nested envelopes in the first place? This is also a classic interview question: explain the OSI model. Most people recite seven names and stop. By the end of this video, you'll be able to explain why the layers exist, and what each one actually writes on the packet.

This is Just Known. From unknown, to known. Episode two: layers and packets. Start with the why. Sending data across the internet is a pile of hard problems.

Which program should get this data? Which computer, out of billions? How do bits physically cross a wire, or the air? And what happens when something gets lost along the way? Solving all of that in one giant program would be a nightmare.

So the internet splits it into layers. Each layer solves one problem, and trusts the layer below it to handle the rest. If you write software, you already know this idea. It's abstraction. Your browser has no idea whether you're on Wi-Fi, fiber, or a phone network, and it doesn't need to.

You can swap the bottom of the stack without touching the top. That's why the same web page loads over a coffee-shop hotspot and a cable under the ocean. The model the internet actually runs on is called TCP/IP, and it has four layers. Each one answers a single question. At the top, the application layer: what are we saying?

That's HTTP, DNS, email. Your actual message. Next, transport: which program on the other machine should get this, and does it need to arrive reliably? That's TCP or UDP, and it's where port numbers live. Then the network layer, sometimes called the internet layer: which computer, anywhere in the world?

That's IP, and IP addresses. And at the bottom, the link layer: which device is next, on this particular wire or radio? That's Ethernet or Wi-Fi, with hardware addresses called MAC addresses. Under all of it, the bits themselves: voltage on copper, pulses of light in fiber, or radio waves in the air. Then there's the model interviewers love: OSI, with seven layers.

From the bottom: physical, data link, network, transport, session, presentation, and application. One mnemonic: Please Do Not Throw Sausage Pizza Away. OSI came out of a standards effort in the late seventies and eighties. Its protocols mostly lost to TCP/IP, but its vocabulary won. When engineers talk about a layer two switch, a layer three router, or a layer seven load balancer, those are OSI numbers.

The mapping is simple. OSI's bottom two layers roughly make up the link layer. Network and transport line up one to one. And OSI's top three, session, presentation, and application, all collapse into TCP/IP's application layer. Where does TLS fit?

Honestly, not neatly. In practice, it sits between TCP and HTTP. Models are maps, not territory. Now let's build a real packet. Your browser wants to send its HTTP request over the connection from episode one.

Thanks to TLS, it's already encrypted: a few hundred bytes that look like noise to anyone watching. That's the application data, envelope number one. The operating system hands it to TCP, which adds a header of its own: twenty bytes, at minimum. Inside: the source port, a number your laptop picked for this connection, say 51544. The destination port, 443, for HTTPS.

A sequence number and an acknowledgment number, so the bytes can be put back in order, and missing ones resent. A few flags, like SYN and ACK from the handshake. A window size, saying how much more data this side can accept. And a checksum, to catch corruption. Data plus a TCP header is called a segment.

With UDP instead, it's a datagram, with a much simpler, eight-byte header. Next, IP. Another twenty bytes, at least. The version: four. The time to live, often starting at 64.

A protocol field set to six, which means: TCP is inside. And the two addresses that matter most: source 192.168.1.23, destination 203.0.113.10. Now it's called a packet. Finally, the link layer wraps it in a frame. On Ethernet, the frame's header holds the destination hardware address, which is your router, the source, which is your laptop, and a type field: 0800 in hex, meaning an IPv4 packet is inside.

At the very end goes a short trailer: a checksum, so the next device can tell if the frame was damaged on the way. Wi-Fi frames carry a few more addresses, but the idea is identical. Then the network card turns the whole frame into bits on the wire, or radio waves in the air. Four envelopes, one signal. Here's a clever detail: each envelope says what's inside the next one.

The frame's type field says: an IP packet is in here. The IP header's protocol field says: TCP. And TCP's destination port says: this belongs to whatever is listening on 443. So when the server receives the frame, it unwraps it in reverse. The network card checks the frame and hands the packet to IP.

IP checks the address and hands the segment to TCP. TCP puts the bytes in order and hands them to the web server. That unwrapping is called decapsulation, and it happens billions of times a second across the internet. Different devices open different envelopes. A switch, the box that connects devices on your local network, only reads the outer frame: which hardware address is this for?

That's why it's called a layer two device. A router goes one level deeper. It strips off the frame, reads the IP address, picks the next hop, and wraps the packet in a brand new frame, addressed to the next router. So here's an interview favorite: at every hop, the hardware addresses change, but the IP addresses stay the same, end to end. The big exception is NAT, which we met last time.

Firewalls usually look at layers three and four: addresses and ports. A layer four load balancer routes by IP and port. A layer seven load balancer opens the application data itself, after decrypting it, and can route by URL or cookie. That's what those layer numbers mean, and we'll go deeper in episode 14. How big can one packet be?

On most Ethernet networks, the limit, called the MTU, is 1,500 bytes for everything inside the frame. Take away twenty bytes for IP and twenty for TCP, and you're left with 1,460 bytes of actual data. So a one-megabyte image travels as roughly seven hundred separate packets, each with its own full set of envelopes. What if a packet is too big for some link along the way? In IPv4, a router can split it into fragments, or, if the packet says don't fragment, drop it and send back a warning.

IPv6 routers never fragment at all. This is why a VPN, which adds its own extra headers, can occasionally make certain websites hang: the packets quietly grow past the limit. Layers also explain security. Every layer has its own attacks. At layer two, someone on your network can fake ARP replies and pull your traffic through their machine.

At layer three, packets can carry a forged source address. At layer four, a flood of half-open handshakes can exhaust a server, and port scans map out what's listening. And at layer seven, the application itself gets attacked: injection, cross-site scripting, stolen sessions. Here's the rule that ties it all together: encryption at a layer protects everything above it, and nothing below it. TLS encrypts your HTTP, but the IP addresses and ports stay visible.

A VPN wraps your whole IP packet inside an encrypted tunnel, so your local network can't see where you're going, but the VPN server can. And Wi-Fi encryption only protects the hop through the air, between your laptop and the access point. So when someone says "it's encrypted", the right question is: at which layer? You can see all four envelopes on your own machine. Open Wireshark, capture a few packets, and click on one.

You'll see a tree that's exactly this stack: the frame, Ethernet with two MAC addresses, Internet Protocol with two IP addresses, TCP with ports and sequence numbers, and on top, TLS, the encrypted application data. Prefer the terminal? Run tcpdump with dash e, and it prints the link layer too: hardware addresses, the type field, then the IP addresses and ports, all on a single line. The exact commands are in the description. Interview corner.

Explain the OSI model, and where HTTP, TCP, IP, and Ethernet fit. Pause here, and give it a try.

Here's a strong answer. The OSI model splits networking into seven layers, so each one solves one problem. Physical sends the bits. Data link moves frames to the next device, using MAC addresses; that's Ethernet. Network routes packets across the world by IP address; that's IP.

Transport delivers data to the right program using ports, and TCP adds reliability. Session, presentation, and application handle the conversation itself; HTTP lives up there. Then add the part most candidates miss: the internet actually runs on the four-layer TCP/IP model, and OSI's real value today is its vocabulary, like layer four versus layer seven load balancing. Bonus question: as a packet crosses the internet, which addresses change at every hop? The MAC addresses.

The IP addresses stay the same, unless NAT rewrites them. Let's recap. Layers split one impossible problem into four solvable ones. Each layer adds its own header, an envelope, and each envelope says what's inside the next. Devices only open the envelopes they need: switches read frames, and routers read packets.

And encryption protects every layer above it, never below. Next stop: your local network. How does your laptop get an IP address the moment it joins Wi-Fi, and how does it find the router? That's DHCP and ARP, and it's all in episode three. This is Just Known.

From unknown, to known.

Note: addresses on screen are from reserved documentation ranges (RFC 5737 IPs, RFC 7042 MACs).
Narration voice generated with ElevenLabs.