CloudAcademia
EN

Networking · Module 09 · Switching and VLANs

Lab: Building a VLAN-Aware Switch with Linux Namespaces

Lab60 minBeginnerMarkdown

Showing raw Markdown: the renderer could not load.

## Goal
You build a small switched network on one Linux machine: a VLAN-aware Linux bridge acts as the switch, and network namespaces act as three PCs and a router. You watch the switch learn MAC addresses and flood unknown frames, split the PCs into two VLANs, carry both VLANs over a trunk, see the 802.1Q tags in a capture, and route between the VLANs with a router-on-a-stick.

## Objectives
- Observe MAC address learning and flooding in a switch's forwarding database.
- Configure access ports in two VLANs and show that hosts in different VLANs can't reach each other at Layer 2, even in the same subnet.
- Configure a trunk port that carries VLANs 10 and 20 tagged, and read the 802.1Q tags in a packet capture.
- Route between VLANs with a router-on-a-stick built from VLAN subinterfaces.

## Prerequisites
- A Linux machine or VM where you can use `sudo`. Any current distribution works (tested on Fedora with Linux 6.x).
- iproute2 5.0 or later, which provides the `ip` and `bridge` commands (tested with 6.17). Check with `ip -V`.
- tcpdump 4.9 or later (tested with 4.99.6). Install it with your package manager if `tcpdump --version` fails, for example `sudo dnf install tcpdump` or `sudo apt install tcpdump`.
- Two terminal windows: you run captures in the second one.
- The [lesson](lesson.md) of this module, especially MAC learning, access and trunk ports, and router-on-a-stick.

> [!WARNING]
> Run this lab only on your own machine or a VM you control, ideally a test VM rather than a production server. Everything happens inside new network namespaces, so your real interfaces, addresses and routes are not touched. The lab uses the private range `192.168.0.0/16` (RFC 1918) inside the namespaces; even if your home network uses the same addresses, there is no conflict, because the namespaces are isolated from it. Always run the [Cleanup](#cleanup) section at the end.

## Architecture
A **network namespace** is a separate copy of the Linux network stack, with its own interfaces, addresses and routing table. You create five of them. One, `sw1`, holds the switch: a Linux bridge called `br0` with VLAN filtering turned on. The others are hosts. Each host is wired to a bridge port with a **veth pair**, a virtual Ethernet cable whose two ends can live in different namespaces.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 556" width="940" height="556" role="img" aria-labelledby="vlan-lab-architecture-title vlan-lab-architecture-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="vlan-lab-architecture-title">Lab architecture: five network namespaces around a VLAN-aware Linux bridge</title>
<desc id="vlan-lab-architecture-desc">Namespace sw1 holds br0, a Linux bridge with VLAN filtering. Three host namespaces connect to it through veth pairs: pc1 (eth0, 00:00:5e:00:53:01, 192.168.10.11/24) on port p1, an access port in VLAN 10; pc2 (00:00:5e:00:53:02, 192.168.20.12/24) on p2 in VLAN 20; pc3 (00:00:5e:00:53:03, 192.168.10.13/24) on p3 in VLAN 10. Namespace r1 (00:00:5e:00:53:fe) connects to port p4, a trunk carrying VLANs 10 and 20 tagged, and routes between them with subinterfaces eth0.10 (192.168.10.1/24) and eth0.20 (192.168.20.1/24).</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="555" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l2"><path class="dg-stroke" d="M470 146V224" stroke="#15803d" stroke-width="4" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M164 326V424" stroke="#0369a1" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M470 326V424" stroke="#c2410c" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M776 326V424" stroke="#0369a1" stroke-width="3" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><rect class="dg-tint" x="80" y="188" width="780" height="162" rx="16" fill="#f1f7f3" stroke="#a1ccb1" stroke-width="1.2" stroke-dasharray="6 5"/>
<g class="dg-icon" transform="translate(96 201) scale(0.7500)" fill="none" stroke="#15803d" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16Z"/><path d="m3.3 7 8.7 5 8.7-5"/><path d="M12 22V12"/></g>
<text x="122" y="215" font-size="13" class="dg-ink" fill="#15803d" font-weight="700">Namespace sw1 · the switch</text></g>
<rect class="dg-card" x="300" y="34" width="340" height="112" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(316 50) scale(1.2500)" fill="none" stroke="#17171c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="8" x="2" y="14" rx="2"/><path d="M6.01 18H6"/><path d="M10.01 18H10"/><path d="M15 10v4"/><path d="M17.84 7.17a4 4 0 0 0-5.66 0"/><path d="M20.66 4.34a8 8 0 0 0-11.31 0"/></g>
<text x="358" y="71" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Namespace r1 · router</text>
<text x="318" y="102" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0 · 00:00:5e:00:53:fe</text>
<text x="318" y="122" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0.10 · 192.168.10.1/24</text>
<text x="318" y="142" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0.20 · 192.168.20.1/24</text>
<rect class="dg-card" x="130" y="224" width="680" height="102" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(146 240) scale(1.2500)" fill="none" stroke="#15803d" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><rect width="20" height="12" x="2" y="6" rx="2"/><path d="M6 10.5h11"/><path d="m15 8.5 2 2-2 2"/><path d="M18 13.5H7"/><path d="m9 11.5-2 2 2 2"/></g></g>
<g class="dg-l2"><text x="188" y="261" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">br0 · Linux bridge</text></g>
<text x="148" y="292" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">vlan_filtering 1 · MAC table: bridge fdb</text>
<text x="148" y="312" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">VLANs per port: bridge vlan</text>
<g class="dg-l2"><rect class="dg-bg-plain" x="373.64" y="155" width="192.72" height="24" rx="12" fill="#ffffff"/><text x="470" y="171.5" font-size="12" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">p4 · trunk · tagged 10, 20</text></g>
<g class="dg-l1"><rect class="dg-bg-plain" x="84.44" y="376" width="159.12" height="24" rx="12" fill="#ffffff"/><text x="164" y="392.5" font-size="12" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">p1 · VLAN 10 untagged</text></g>
<rect class="dg-card" x="24" y="424" width="280" height="108" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(40 440) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<g class="dg-l1"><text x="82" y="461" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">Namespace pc1</text></g>
<text x="42" y="492" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0 · 00:00:5e:00:53:01</text>
<text x="42" y="512" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">192.168.10.11/24</text>
<g class="dg-l4"><rect class="dg-bg-plain" x="390.44" y="376" width="159.12" height="24" rx="12" fill="#ffffff"/><text x="470" y="392.5" font-size="12" class="dg-ink" fill="#c2410c" font-weight="600" text-anchor="middle">p2 · VLAN 20 untagged</text></g>
<rect class="dg-card" x="330" y="424" width="280" height="108" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(346 440) scale(1.2500)" fill="none" stroke="#c2410c" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<g class="dg-l4"><text x="388" y="461" font-size="14" class="dg-ink" fill="#c2410c" font-weight="700">Namespace pc2</text></g>
<text x="348" y="492" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0 · 00:00:5e:00:53:02</text>
<text x="348" y="512" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">192.168.20.12/24</text>
<g class="dg-l1"><rect class="dg-bg-plain" x="696.44" y="376" width="159.12" height="24" rx="12" fill="#ffffff"/><text x="776" y="392.5" font-size="12" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">p3 · VLAN 10 untagged</text></g>
<rect class="dg-card" x="636" y="424" width="280" height="108" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(652 440) scale(1.2500)" fill="none" stroke="#0369a1" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round"><path d="M20 16V7a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v9m16 0H4m16 0 1.28 2.55a1 1 0 0 1-.9 1.45H3.62a1 1 0 0 1-.9-1.45L4 16"/></g></g>
<g class="dg-l1"><text x="694" y="461" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">Namespace pc3</text></g>
<text x="654" y="492" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">eth0 · 00:00:5e:00:53:03</text>
<text x="654" y="512" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">192.168.10.13/24</text>
</svg></figure>

*Figure 1. The finished lab. Blue links are VLAN 10, orange is VLAN 20, and the green trunk carries both. pc2 starts in `192.168.10.0/24` and moves to `192.168.20.0/24` in Step 8.*

The hosts use MAC addresses from the documentation block of RFC 7042 (`00:00:5e:00:53:xx`), so the outputs you see match this guide.

| Namespace | Role | Interface | MAC | IP address (final) | Bridge port |
|---|---|---|---|---|---|
| `pc1` | Host, VLAN 10 | `eth0` | `00:00:5e:00:53:01` | `192.168.10.11/24` | `p1`, access VLAN 10 |
| `pc2` | Host, VLAN 20 | `eth0` | `00:00:5e:00:53:02` | `192.168.20.12/24` | `p2`, access VLAN 20 |
| `pc3` | Host, VLAN 10 | `eth0` | `00:00:5e:00:53:03` | `192.168.10.13/24` | `p3`, access VLAN 10 |
| `r1` | Router-on-a-stick | `eth0`, `eth0.10`, `eth0.20` | `00:00:5e:00:53:fe` | `192.168.10.1/24`, `192.168.20.1/24` | `p4`, trunk 10, 20 |
| `sw1` | Switch | `br0`, `p1`–`p4` | random | none | — |

## Steps

> [!NOTE]
> Run every command in the first terminal unless the step says "terminal 2". The outputs shown come from iproute2 6.17, tcpdump 4.99.6 and iputils 20250605. Timestamps, timings and interface index numbers (such as `@if4`) will differ on your machine. A non-English locale translates some messages: start both terminals with `export LC_ALL=C` to get the same wording as this guide.

### Step 1 — Check your tools
Make sure the commands exist and are recent enough before you build anything.

```bash
ip -V
bridge -V
tcpdump --version
```

Expected output (your versions may be newer):
```text
ip utility, iproute2-6.17.0, libbpf 1.6.3
bridge utility, 6.17.0
tcpdump version 4.99.6
libpcap version 1.10.7 (64-bit time_t, with TPACKET_V3)
...
```

### Step 2 — Create the namespaces
Each namespace is one device of your network: a switch, three PCs and a router. You also turn off IPv6 inside them. That's not needed for VLANs to work, but it stops the hosts from sending IPv6 discovery messages, so the MAC table and the captures only show the traffic you generate.

```bash
for ns in sw1 pc1 pc2 pc3 r1; do
  sudo ip netns add $ns
  sudo ip netns exec $ns sysctl -qw net.ipv6.conf.all.disable_ipv6=1 net.ipv6.conf.default.disable_ipv6=1
done
ip netns list
```

Expected output:
```text
r1
pc3
pc2
pc1
sw1
```

### Step 3 — Create the switch
A Linux **bridge** is a software switch: it learns MAC addresses and forwards frames exactly as the lesson describes. With `vlan_filtering 1`, it also keeps track of VLANs per port, like a managed switch. You create it inside `sw1` with the `-n sw1` option, which runs an `ip` or `bridge` command in that namespace.

```bash
sudo ip -n sw1 link add br0 type bridge vlan_filtering 1
sudo ip -n sw1 link set br0 up
sudo ip -n sw1 -d link show br0 | grep -o -E 'vlan_filtering [0-9]+|ageing_time [0-9]+'
sudo bridge -n sw1 vlan show
```

Expected output:
```text
ageing_time 30000
vlan_filtering 1
port              vlan-id  
br0               1 PVID Egress Untagged
```

- `ageing_time 30000` is the MAC table aging time in hundredths of a second: 300 seconds, the common default from the lesson.
- `bridge vlan show` lists the VLANs of each port. For now only `br0` itself exists, in VLAN 1. **PVID** (port VLAN ID) is the VLAN that untagged incoming frames join, and **Egress Untagged** means frames of that VLAN leave the port without a tag. Together, they make a port an access port for that VLAN.

### Step 4 — Connect three PCs
Now you plug the PCs into the switch. For each PC, one command creates a veth pair: the end called `p1` (or `p2`, `p3`) stays in `sw1` and the end called `eth0` goes into the PC's namespace. You then set the PC's MAC address, attach the switch end to `br0` and bring both ends up. At first, all three PCs share one subnet, `192.168.10.0/24`.

```bash
for i in 1 2 3; do
  sudo ip -n sw1 link add p$i type veth peer name eth0 netns pc$i
  sudo ip -n pc$i link set eth0 address 00:00:5e:00:53:0$i
  sudo ip -n sw1 link set p$i master br0
  sudo ip -n sw1 link set p$i up
  sudo ip -n pc$i link set eth0 up
done
sudo ip -n pc1 addr add 192.168.10.11/24 dev eth0
sudo ip -n pc2 addr add 192.168.10.12/24 dev eth0
sudo ip -n pc3 addr add 192.168.10.13/24 dev eth0
sudo bridge -n sw1 vlan show
sudo ip -n pc2 -br addr
```

Expected output:
```text
port              vlan-id  
br0               1 PVID Egress Untagged
p1                1 PVID Egress Untagged
p2                1 PVID Egress Untagged
p3                1 PVID Egress Untagged
lo               DOWN           
eth0@if4         UP             192.168.10.12/24 
```

Every new port joins VLAN 1 as an access port, just like a new hardware switch. All three PCs are in one broadcast domain.

### Step 5 — Watch the switch learn and flood
First, look at the switch's **forwarding database** (FDB), Linux's name for the MAC address table. The `dynamic` keyword shows only learned entries and hides the bridge's own permanent ones.

```bash
sudo bridge -n sw1 fdb show br br0 dynamic
```

Expected output: nothing. No PC has sent a frame yet, so the switch hasn't learned anything.

> [!TIP]
> If your `bridge` command doesn't accept `dynamic`, run `sudo bridge -n sw1 fdb show br br0 | grep -v permanent` instead.

Now start a capture on pc3, which is not involved in the conversation you're about to start. In **terminal 2**:

```bash
sudo ip netns exec pc3 tcpdump -e -n -i eth0 arp or icmp
```

`ip netns exec pc3` runs tcpdump inside pc3. `-e` prints the MAC addresses of each frame, `-n` disables name lookups, and `arp or icmp` shows only ARP and ping traffic.

In **terminal 1**, ping pc2 from pc1:

```bash
sudo ip netns exec pc1 ping -c 2 192.168.10.12
```

Expected output (timings vary):
```text
PING 192.168.10.12 (192.168.10.12) 56(84) bytes of data.
64 bytes from 192.168.10.12: icmp_seq=1 ttl=64 time=0.054 ms
64 bytes from 192.168.10.12: icmp_seq=2 ttl=64 time=0.056 ms

--- 192.168.10.12 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1037ms
rtt min/avg/max/mdev = 0.054/0.055/0.056/0.001 ms
```

Stop the capture in terminal 2 with Ctrl+C. Expected output in terminal 2:
```text
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on eth0, link-type EN10MB (Ethernet), snapshot length 262144 bytes
22:16:36.485926 00:00:5e:00:53:01 > Broadcast, ethertype ARP (0x0806), length 42: Request who-has 192.168.10.12 tell 192.168.10.11, length 28
^C
1 packet captured
1 packet received by filter
0 packets dropped by kernel
```

pc3 saw exactly one frame: pc1's ARP request, sent to the broadcast address and therefore flooded to every port. It did **not** see the ARP reply or the four ping packets. When pc2 replied, the switch already knew pc1's port (learned from the source of the ARP request), so it forwarded the reply to `p1` only, and learned pc2's port from it.

Check the table again:

```bash
sudo bridge -n sw1 fdb show br br0 dynamic
```

Expected output:
```text
00:00:5e:00:53:01 dev p1 vlan 1 master br0 
00:00:5e:00:53:02 dev p2 vlan 1 master br0 
```

The switch learned pc1 on `p1` and pc2 on `p2`, both in VLAN 1. pc3 hasn't sent anything, so it isn't in the table yet.

### Step 6 — Split the PCs into two VLANs
Now you make `p1` and `p3` access ports in VLAN 10, and `p2` an access port in VLAN 20. `pvid untagged` makes the VLAN the port's access VLAN. Then you remove VLAN 1 from the three ports, so that each port belongs to exactly one VLAN.

```bash
sudo bridge -n sw1 vlan add dev p1 vid 10 pvid untagged
sudo bridge -n sw1 vlan add dev p3 vid 10 pvid untagged
sudo bridge -n sw1 vlan add dev p2 vid 20 pvid untagged
sudo bridge -n sw1 vlan del dev p1 vid 1
sudo bridge -n sw1 vlan del dev p2 vid 1
sudo bridge -n sw1 vlan del dev p3 vid 1
sudo bridge -n sw1 vlan show
```

Expected output:
```text
port              vlan-id  
br0               1 PVID Egress Untagged
p1                10 PVID Egress Untagged
p2                20 PVID Egress Untagged
p3                10 PVID Egress Untagged
```

pc2 still has the address `192.168.10.12/24`, in the same subnet as pc1. Clear pc1's ARP cache, so it has to ask again, then ping pc3 (same VLAN) and pc2 (other VLAN):

```bash
sudo ip -n pc1 neigh flush all
sudo ip netns exec pc1 ping -c 2 192.168.10.13
sudo ip netns exec pc1 ping -c 2 192.168.10.12
sudo ip -n pc1 neigh show
```

Expected output (timings vary):
```text
PING 192.168.10.13 (192.168.10.13) 56(84) bytes of data.
64 bytes from 192.168.10.13: icmp_seq=1 ttl=64 time=0.038 ms
64 bytes from 192.168.10.13: icmp_seq=2 ttl=64 time=0.051 ms

--- 192.168.10.13 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1009ms
rtt min/avg/max/mdev = 0.038/0.044/0.051/0.006 ms
PING 192.168.10.12 (192.168.10.12) 56(84) bytes of data.

--- 192.168.10.12 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1019ms

192.168.10.12 dev eth0 FAILED 
192.168.10.13 dev eth0 lladdr 00:00:5e:00:53:03 REACHABLE 
```

pc1 and pc2 have addresses in the same subnet, yet pc1 can't even resolve pc2's MAC address: the ARP entry is `FAILED`. The switch keeps pc1's broadcast inside VLAN 10, so it never reaches pc2's port. That's the VLAN boundary at work. Same subnet or not, different VLANs are different broadcast domains.

Look at the table once more:

```bash
sudo bridge -n sw1 fdb show br br0 dynamic
```

Expected output:
```text
00:00:5e:00:53:01 dev p1 vlan 10 master br0 
00:00:5e:00:53:03 dev p3 vlan 10 master br0 
00:00:5e:00:53:02 dev p2 vlan 20 master br0 
```

Entries now carry the VLAN: the switch keeps one table per VLAN. Removing VLAN 1 from the ports also removed their old VLAN 1 entries. The line for pc2 in VLAN 20 appears only once pc2 has sent a frame in its new VLAN (for example, an ARP probe); if it's missing on your machine, that's fine.

### Step 7 — Add a trunk port to the router
The router must reach both VLANs over one cable, so its switch port is a trunk. Add a fourth veth pair: `p4` in the switch, `eth0` in `r1`. Then make `p4` a tagged member of VLANs 10 and 20. Without the `pvid untagged` keywords, frames of these VLANs leave `p4` with an 802.1Q tag. Removing VLAN 1 means `p4` has no native VLAN: untagged frames arriving on it are dropped.

```bash
sudo ip -n sw1 link add p4 type veth peer name eth0 netns r1
sudo ip -n r1 link set eth0 address 00:00:5e:00:53:fe
sudo ip -n sw1 link set p4 master br0
sudo ip -n sw1 link set p4 up
sudo bridge -n sw1 vlan add dev p4 vid 10
sudo bridge -n sw1 vlan add dev p4 vid 20
sudo bridge -n sw1 vlan del dev p4 vid 1
sudo bridge -n sw1 vlan show dev p4
```

Expected output:
```text
port              vlan-id  
p4                10
                  20
```

No `PVID` and no `Egress Untagged`: `p4` carries both VLANs, tagged. That's a trunk.

### Step 8 — Configure the router-on-a-stick
In `r1`, you create one **VLAN subinterface** per VLAN on top of `eth0`. `eth0.10` sends and receives frames tagged with VLAN 10, and `eth0.20` frames tagged with VLAN 20. Each gets the gateway address of its VLAN. Turning on IP forwarding makes `r1` route between them.

```bash
sudo ip -n r1 link set eth0 up
sudo ip -n r1 link add link eth0 name eth0.10 type vlan id 10
sudo ip -n r1 link add link eth0 name eth0.20 type vlan id 20
sudo ip -n r1 addr add 192.168.10.1/24 dev eth0.10
sudo ip -n r1 addr add 192.168.20.1/24 dev eth0.20
sudo ip -n r1 link set eth0.10 up
sudo ip -n r1 link set eth0.20 up
sudo ip netns exec r1 sysctl -w net.ipv4.ip_forward=1
sudo ip -n r1 -d link show eth0.20 | grep -o 'vlan protocol [^ ]* id [0-9]*'
sudo ip -n r1 -br addr
```

Expected output:
```text
net.ipv4.ip_forward = 1
vlan protocol 802.1Q id 20
lo               DOWN           
eth0@if6         UP             
eth0.10@eth0     UP             192.168.10.1/24 
eth0.20@eth0     UP             192.168.20.1/24 
```

`eth0` itself has no address: it's only the physical link that carries the tagged frames.

Now give pc2 an address in VLAN 20's subnet, and give every PC a default gateway in its own VLAN:

```bash
sudo ip -n pc2 addr flush dev eth0
sudo ip -n pc2 addr add 192.168.20.12/24 dev eth0
sudo ip -n pc1 route add default via 192.168.10.1
sudo ip -n pc3 route add default via 192.168.10.1
sudo ip -n pc2 route add default via 192.168.20.1
sudo ip -n pc1 route
```

Expected output:
```text
default via 192.168.10.1 dev eth0 
192.168.10.0/24 dev eth0 proto kernel scope link src 192.168.10.11 
```

### Step 9 — See the 802.1Q tags on the trunk
Capture on the trunk port `p4`, inside the switch, while pc1 pings pc2 across VLANs. In **terminal 2**:

```bash
sudo ip netns exec sw1 tcpdump -e -n -i p4 -c 8 vlan
```

The `vlan` filter keeps only tagged frames, and `-c 8` stops after eight packets.

In **terminal 1**:

```bash
sudo ip netns exec pc1 ping -c 1 192.168.20.12
```

Expected output in terminal 1 (timings vary):
```text
PING 192.168.20.12 (192.168.20.12) 56(84) bytes of data.
64 bytes from 192.168.20.12: icmp_seq=1 ttl=63 time=0.082 ms

--- 192.168.20.12 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.082/0.082/0.082/0.000 ms
```

Expected output in terminal 2:
```text
tcpdump: verbose output suppressed, use -v[v]... for full protocol decode
listening on p4, link-type EN10MB (Ethernet), snapshot length 262144 bytes
22:16:53.622170 00:00:5e:00:53:01 > Broadcast, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP (0x0806), Request who-has 192.168.10.1 tell 192.168.10.11, length 28
22:16:53.622187 00:00:5e:00:53:fe > 00:00:5e:00:53:01, ethertype 802.1Q (0x8100), length 46: vlan 10, p 0, ethertype ARP (0x0806), Reply 192.168.10.1 is-at 00:00:5e:00:53:fe, length 28
22:16:53.622198 00:00:5e:00:53:01 > 00:00:5e:00:53:fe, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 192.168.10.11 > 192.168.20.12: ICMP echo request, id 29489, seq 1, length 64
22:16:53.622204 00:00:5e:00:53:fe > Broadcast, ethertype 802.1Q (0x8100), length 46: vlan 20, p 0, ethertype ARP (0x0806), Request who-has 192.168.20.12 tell 192.168.20.1, length 28
22:16:53.622214 00:00:5e:00:53:02 > 00:00:5e:00:53:fe, ethertype 802.1Q (0x8100), length 46: vlan 20, p 0, ethertype ARP (0x0806), Reply 192.168.20.12 is-at 00:00:5e:00:53:02, length 28
22:16:53.622216 00:00:5e:00:53:fe > 00:00:5e:00:53:02, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 192.168.10.11 > 192.168.20.12: ICMP echo request, id 29489, seq 1, length 64
22:16:53.622224 00:00:5e:00:53:02 > 00:00:5e:00:53:fe, ethertype 802.1Q (0x8100), length 102: vlan 20, p 0, ethertype IPv4 (0x0800), 192.168.20.12 > 192.168.10.11: ICMP echo reply, id 29489, seq 1, length 64
22:16:53.622226 00:00:5e:00:53:fe > 00:00:5e:00:53:01, ethertype 802.1Q (0x8100), length 102: vlan 10, p 0, ethertype IPv4 (0x0800), 192.168.20.12 > 192.168.10.11: ICMP echo reply, id 29489, seq 1, length 64
8 packets captured
8 packets received by filter
0 packets dropped by kernel
```

Read it line by line. It's Figure 4 of the lesson, captured live:

- `ethertype 802.1Q (0x8100)` is the TPID; `vlan 10` is the VID; `p 0` is the PCP priority. Then comes the real EtherType of the payload.
- **Lines 1–3, VLAN 10:** pc1 asks for its gateway's MAC address (ARP), r1 answers from `eth0.10`, and pc1 sends the ping to r1's MAC.
- **Lines 4–6, VLAN 20:** r1 routes the packet. It asks for pc2's MAC in VLAN 20 and sends the ping down the same cable, now tagged 20, from its own MAC to pc2's.
- **Lines 7–8:** the reply takes the opposite path.
- The ping's `ttl=63` in terminal 1 (instead of 64) shows that one router handled the packet.

The IP addresses (`192.168.10.11 > 192.168.20.12`) never change. The MAC addresses and the VLAN tag do. pc1 and pc2 never saw a tag: the bridge added and removed them at `p4`.

Finally, look at the switch's table:

```bash
sudo bridge -n sw1 fdb show br br0 dynamic
```

Expected output:
```text
00:00:5e:00:53:01 dev p1 vlan 10 master br0 
00:00:5e:00:53:02 dev p2 vlan 20 master br0 
00:00:5e:00:53:03 dev p3 vlan 10 master br0 
00:00:5e:00:53:fe dev p4 vlan 20 master br0 
00:00:5e:00:53:fe dev p4 vlan 10 master br0 
```

r1's MAC appears twice on the trunk port, once per VLAN, because each VLAN has its own table.

## Verification
You have completed the lab when all of these are true:

1. `sudo bridge -n sw1 vlan show` lists `p1` and `p3` as `10 PVID Egress Untagged`, `p2` as `20 PVID Egress Untagged`, and `p4` with `10` and `20` and no `PVID`.
2. Same VLAN, direct: `sudo ip netns exec pc1 ping -c 1 192.168.10.13` reports `1 received`.
3. Different VLANs, through the router: `sudo ip netns exec pc1 ping -c 1 192.168.20.12` and `sudo ip netns exec pc3 ping -c 1 192.168.20.12` both report `1 received`, with `ttl=63`.
4. The path goes through r1:
   ```bash
   sudo ip netns exec pc1 tracepath -n 192.168.20.12
   ```
   Expected output (timings vary; `tracepath` is part of iputils):
   ```text
    1?: [LOCALHOST]                      pmtu 1500
    1:  192.168.10.1                                          0.104ms 
    1:  192.168.10.1                                          0.022ms 
    2:  192.168.20.12                                         0.020ms reached
        Resume: pmtu 1500 hops 2 back 2 
   ```
5. You can explain, from your Step 9 capture, where the tag is added and removed and why the MAC addresses change at r1.

## Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| `RTNETLINK answers: Operation not permitted` or `mkdir /var/run/netns failed: Permission denied` | You ran a command that changes the network without `sudo`. | Prefix the command with `sudo`. |
| `RTNETLINK answers: File exists` or `Cannot create namespace file "/var/run/netns/sw1": File exists` | Leftovers from an earlier attempt. | Run the [Cleanup](#cleanup), then start again from Step 2. |
| `Cannot open network namespace "pc1": No such file or directory` | A step was skipped, or cleanup already ran. | Run `ip netns list` and repeat the missing creation commands. |
| Pings in the same VLAN fail after Step 6 | A port is still in VLAN 1, or its VLAN has no `pvid untagged`. | Compare `sudo bridge -n sw1 vlan show` with Verification item 1 and fix the port with `bridge vlan add` / `del`. |
| Pings across VLANs fail, but pings inside each VLAN work | IP forwarding is off in r1, a subinterface is down, a PC has no default route, or `p4` is missing a VLAN. | Check `sudo ip netns exec r1 sysctl net.ipv4.ip_forward` (must be `1`), `sudo ip -n r1 -br addr` (both subinterfaces `UP`), `sudo ip -n pc2 route`, and `sudo bridge -n sw1 vlan show dev p4`. |
| The Step 9 capture shows nothing | You captured on the wrong interface or namespace, or the ARP entries were already cached and you missed the first packets. | Capture with `-i p4` inside `sw1`, start tcpdump before the ping, and run `sudo ip -n pc1 neigh flush all` and `sudo ip -n r1 neigh flush all` to see the ARP exchange again. |
| `tcpdump: command not found` | tcpdump isn't installed. | Install it (`sudo dnf install tcpdump` or `sudo apt install tcpdump`). |
| `bridge fdb show ... dynamic` fails with a syntax or usage error | Older iproute2 without the `dynamic` keyword. | Use `sudo bridge -n sw1 fdb show br br0 \| grep -v permanent`. |

## Challenge (optional)
Build a switching loop and let Spanning Tree break it. Create a second switch namespace `sw2` with its own bridge, and connect `sw1` and `sw2` with **two** veth pairs. Before you connect the second cable, enable STP on both bridges (`ip link set br0 type bridge stp_state 1`). Wait about 30 seconds (two 15-second forward delays), then use `bridge -n sw2 link show` to find the port in the `blocking` state. Which switch became the root bridge, and why? (Hint: `ip -d link show br0` shows `root_id` and `bridge_id`.) What changes if you lower `sw2`'s priority with `ip link set br0 type bridge priority 4096`?

> [!NOTE]
> The Linux kernel bridge implements classic 802.1D STP, which is why it takes about 30 seconds. RSTP on Linux needs a separate user-space daemon (such as `mstpd`), which is beyond this lab. Run the challenge only inside namespaces, never by bridging your real network interfaces: a loop on a real network can take it down.

## Cleanup
Delete the bridge, then all the namespaces. Deleting a namespace also deletes every interface inside it, and deleting one end of a veth pair deletes the other end too, so no veth is left behind.

```bash
sudo ip -n sw1 link del br0
for ns in sw1 pc1 pc2 pc3 r1; do sudo ip netns del $ns; done
sudo ip netns del sw2 2>/dev/null   # only if you did the challenge
ip netns list
```

Expected output: `ip netns list` prints nothing (or at least doesn't list `sw1`, `sw2`, `pc1`, `pc2`, `pc3` or `r1`).

Nothing else was changed on your machine: the IPv6 and IP forwarding settings you changed lived inside the namespaces and disappeared with them.

## What you learned
- A switch learns MAC addresses from the source of each frame, floods broadcasts, and forwards known unicast to one port only, so other hosts never see it.
- VLANs split one switch into separate broadcast domains: hosts in different VLANs can't talk at Layer 2, even with addresses in the same subnet.
- An access port carries one VLAN untagged; a trunk port carries several VLANs with 802.1Q tags (`ethertype 802.1Q (0x8100)`, `vlan <id>`), which you can read in a capture.
- Router-on-a-stick routes between VLANs over a single trunk using VLAN subinterfaces; each routed packet crosses the trunk twice, with a new MAC header and a different tag.
- The switch keeps a separate MAC table per VLAN, so the same MAC can appear in several VLANs on one trunk port.