# How networks work: layers, packets and the next hop

The four layers every request passes through, what a packet really contains, and the two decisions every machine makes to send it on. Read your own machine with ip and watch a packet with tcpdump.

Course: [Networking for DevOps](https://devops.rajeev.pro/courses/networking), lesson 1. About 25 minutes, beginner. Source: https://devops.rajeev.pro/courses/networking/01-how-networks-work

## What you will learn

- Name the four TCP/IP layers, their OSI numbers, and the tool you check each with
- Explain encapsulation using a real captured packet
- Read your machine's addresses, routes and ARP cache with ip
- Explain why a packet's destination MAC is the gateway, not the server

> **Lab status.** **Verified lab:** `ip` on a Linux host (WSL 2) and a `tcpdump` capture of a real HTTP request from an Alpine container. The output shown is real. <br />**Cloud lab:** none needed.

"The network is down" is the start of most incident calls, and it's almost never the whole network. Something
broke at one layer. This lesson gives you the map, so you can say which.

## Four layers

> **Figure.** Four rows. Application, OSI 5 to 7: HTTP, DNS, TLS, SSH, checked with curl -v, dig and openssl s_client; breaks as 404 or 500, wrong DNS answer, expired certificate. Transport, OSI 4: TCP, UDP and ports, checked with ss -tlnp and nc -zv; breaks as connection refused. Internet, OSI 3: IP addresses and routing, checked with ip addr, ip route, ping and traceroute; breaks as no route or a firewall drop. Link, OSI 1 and 2: Ethernet, Wi-Fi, MAC and ARP, checked with ip link and ip neigh; breaks as interface down or no ARP reply.

The internet runs on the four-layer **TCP/IP** model. People still say "layer 4" or "layer 7" from the older
seven-layer **OSI** model, so you need both numbers: an "L4 load balancer" works with TCP ports, an "L7" one reads
HTTP. Each layer only talks to the layers next to it, and each one has its own tools and its own ways of breaking.
This whole course follows the stack upward.

## What a packet contains

Every request is wrapped in headers, one per layer. This is a real HTTP request, captured with `tcpdump -e` in an
Alpine container:

> **Figure.** Nested boxes. The HTTP request GET / Host: example.com sits inside a TCP segment from port 34410 to port 80, inside an IP packet from 172.17.0.2 to 104.20.23.154, inside an Ethernet frame from MAC 0e:89:79:78:14:35 to 8e:25:82:58:7f:16, the gateway.

```text
$ tcpdump -i eth0 -e -n -c 6 tcp port 80 & curl -s -o /dev/null http://example.com
0e:89:79:78:14:35 > 8e:25:82:58:7f:16, ethertype IPv4, length 74: 172.17.0.2.34410 > 104.20.23.154.80: Flags [S] ...
8e:25:82:58:7f:16 > 0e:89:79:78:14:35, ethertype IPv4, length 74: 104.20.23.154.80 > 172.17.0.2.34410: Flags [S.] ...
0e:89:79:78:14:35 > 8e:25:82:58:7f:16, ethertype IPv4, length 66: 172.17.0.2.34410 > 104.20.23.154.80: Flags [.] ...
0e:89:79:78:14:35 > 8e:25:82:58:7f:16, ethertype IPv4, length 141: 172.17.0.2.34410 > 104.20.23.154.80: Flags [P.] ...
```

(Trimmed.) One line per packet, read left to right: link (MACs), internet (IPs), transport (ports and TCP flags).
The first three lines are TCP's handshake (lesson 3); the fourth, 75 bytes longer, carries the HTTP request.

## The next hop

Look at the destination MAC: `8e:25:82:58:7f:16`. That isn't example.com. It's the container's **gateway**:

> **Figure.** Four hops left to right: the container 172.17.0.2, docker0 on the host 172.17.0.1, the host's gateway 172.29.80.1, and example.com 104.20.23.154. Below, two decisions: is the destination on my subnet (no, so use the default route via 172.17.0.1), and what is the gateway's MAC (ARP answers 8e:25:82:58:7f:16).

Every machine makes the same two decisions for every packet:

1. **Where next?** The route table says: not on my subnet, so send it to the default gateway.
2. **Which MAC is that?** **ARP** asks the local network "who has 172.17.0.1?" once and caches the answer.

```text
$ ip neigh        # inside the container
172.17.0.1 dev eth0 lladdr 8e:25:82:58:7f:16 ref 1 used 0/0/0 probes 4 REACHABLE
```

Same MAC as in the capture. Routers along the way repeat both decisions, rewrapping the frame with new MACs each
time. The IP addresses stay the same end to end, until NAT changes them (lesson 7).

## Read your own network

Three commands show everything above on your own machine. From a Linux host (WSL 2 here):

```text
$ ip -br addr
lo               UNKNOWN        127.0.0.1/8 ::1/128
eth0             UP             172.29.94.205/20 fe80::215:5dff:fedd:4d64/64
docker0          DOWN           172.17.0.1/16
$ ip route
default via 172.29.80.1 dev eth0 proto kernel onlink
172.17.0.0/16 dev docker0 proto kernel scope link src 172.17.0.1 linkdown
172.29.80.0/20 dev eth0 proto kernel scope link src 172.29.94.205
$ ip neigh
172.29.80.1 dev eth0 lladdr 00:15:5d:6c:9c:81 REACHABLE
```

(Trimmed.) `eth0` has address `172.29.94.205` on a `/20` network (lesson 2 decodes that). Anything not on a listed
network goes to the **default** route, `172.29.80.1`, and `ip neigh` holds that gateway's MAC. `docker0` is the
bridge that containers use, which is why the container's gateway was `172.17.0.1`. Lesson 11 takes it apart.

> **Old commands.** `ifconfig`, `route` and `arp` still appear in older guides. They come from the deprecated net-tools package; `ip addr`, `ip route` and `ip neigh` replace them and are installed everywhere.

## Mission: Map your own machine

Record your main interface and address, your default gateway and its MAC. Then capture one HTTP request in a container and show that the packet's destination MAC is the container's gateway.

- You need: A Linux shell (or WSL 2) and Docker
- You'll have: ~/my-network.txt

```sh
$ { ip -br addr; ip route; ip neigh; } > ~/my-network.txt
$ docker run --rm --cap-add NET_ADMIN --cap-add NET_RAW alpine:3.22 sh -c '
    apk add -q tcpdump curl
    tcpdump -i eth0 -e -n -c 4 tcp port 80 & sleep 1
    curl -s -o /dev/null http://example.com; sleep 2; ip neigh' >> ~/my-network.txt
```

The destination MAC in the outgoing packets should match the `ip neigh` line at the end.

## Try it

- Run `ip -br addr` and find your main interface and its address.
- Run `ip route` and find your default gateway.
- Run `ip neigh` and find the gateway's MAC address.
- Capture one HTTP request with `tcpdump -e` in a container and match the MACs to `ip neigh`.
- Do the mini mission and keep `~/my-network.txt`.

## Quiz

1. A website returns 503. Which layer is that?
   - Link
   - Transport
   - **Application** (answer)
   - Internet
   Why: An HTTP status code means TCP connected and the server answered. The problem is in the application or behind it.

2. In a captured packet going to example.com, whose MAC address is the destination?
   - example.com's
   - **The next hop's, usually your default gateway** (answer)
   - Your own
   - The DNS server's
   Why: MAC addresses only matter on the local link. Each router rewraps the frame for its next hop while the IP destination stays the same.

3. What does ip route tell you?
   - Which MAC belongs to an IP
   - **Where packets for each destination go next** (answer)
   - Which ports are open
   - The DNS server
   Why: The route table answers "where next?". ip neigh (ARP) answers "which MAC is that next hop?".

## Resources

- [The TCP/IP model (RFC 1122, section 1.1.3)](https://www.rfc-editor.org/rfc/rfc1122#section-1.1.3) (IETF)
- [ip(8) manual](https://man7.org/linux/man-pages/man8/ip.8.html) (man7.org)
- [tcpdump tutorial and primer](https://danielmiessler.com/blog/tcpdump) (Daniel Miessler)
