CloudAcademia
EN

Networking · Module 09 · Switching and VLANs

Switching and VLANs

Lesson50 minBeginnerMarkdown

Showing raw Markdown: the renderer could not load.

## Why this matters
A small company has one office floor: 40 staff laptops, a dozen IP phones, printers, two servers and a guest Wi-Fi access point, all plugged into two switches. Everything sits in one network. A visitor on the guest Wi-Fi can see the printers and the file server. Every Address Resolution Protocol (ARP) broadcast reaches every device. One day someone plugs both ends of a spare cable into the same switch, and within seconds the whole office network stops working.

None of this involves a router. It all happens at Layer 2, inside the switches. In this lesson you learn how a switch decides where to send each frame, how **virtual LANs (VLANs)** split one set of switches into separate networks, how traffic gets from one VLAN to another, why a single looped cable can take a network down, and how attackers abuse Layer 2 and how you stop them. The same ideas show up in data centres, in virtualisation hosts and in the virtual switches of cloud platforms.

## Learning objectives
- Explain how a switch learns MAC addresses, forwards and floods frames, and what a broadcast domain is.
- Describe VLANs, IEEE 802.1Q tagging, and the difference between access and trunk ports.
- Compare inter-VLAN routing options: router-on-a-stick and Layer 3 switches.
- Explain why switching loops happen and what the Spanning Tree Protocol (STP) and Rapid STP do about them.
- Assess Layer 2 attacks (MAC flooding, VLAN hopping) and the defences against them (port security, trunk hardening).

## Key concepts

> [!NOTE]
> This lesson builds on [04 — The OSI Model](../04-osi-model/README.md) and [06 — Subnetting](../06-subnetting/README.md). Quick recap: Ethernet works at Layer 2 and moves **frames**. Every network interface has a 48-bit **Media Access Control (MAC) address**, written as six hexadecimal bytes such as `00:00:5e:00:53:0a`. A frame carries a destination MAC, a source MAC and a payload. `ff:ff:ff:ff:ff:ff` is the broadcast address: "every device on this network". The MAC addresses in this lesson come from the block that RFC 7042 reserves for documentation, `00:00:5e:00:53:00` to `00:00:5e:00:53:ff`.

### How a switch forwards frames
Think of a hotel receptionist on their first day. They don't know which guest is in which room. When a letter arrives for an unknown guest, they slip a copy under every door. But every time a guest walks past the desk, the receptionist notes their room number. After a while, letters go straight to the right room.

A **switch** works the same way. It connects devices on the same network and forwards each frame based on its destination MAC address. It keeps a **MAC address table** (also called the content-addressable memory, or CAM, table, or the forwarding database): a list of "this MAC address was last seen on this port". For every frame it receives, the switch does two things:

1. **Learn.** It reads the *source* MAC and records it against the port the frame came in on. If the entry already exists, it refreshes the timer. If the device moved to another port, it updates the port.
2. **Forward, flood or filter.** It looks up the *destination* MAC:
   - **Known unicast:** the MAC is in the table on another port. The switch **forwards** the frame out of that port only.
   - **Unknown unicast:** the MAC isn't in the table. The switch **floods** the frame: it sends a copy out of every port except the one it came in on.
   - **Broadcast** (`ff:ff:ff:ff:ff:ff`) and, unless the switch is configured otherwise, multicast: also flooded.
   - **Same port:** if the destination was learned on the port the frame arrived on, the switch **filters** (drops) it, because the destination has already received it.

Entries don't live forever. If a MAC address isn't seen for a while, its entry **ages out**. A default of 300 seconds is common, but the value depends on the platform. Aging keeps the table small and lets it follow devices that move.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 560" width="940" height="560" role="img" aria-labelledby="switching-mac-learning-title switching-mac-learning-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="switching-mac-learning-title">How a switch learns MAC addresses and floods unknown destinations</title>
<desc id="switching-mac-learning-desc">Switch SW1 has four hosts: A on port 1 (00:00:5e:00:53:0a), B on port 2 (00:00:5e:00:53:0b), C on port 3 (00:00:5e:00:53:0c) and D on port 4 (00:00:5e:00:53:0d). A sends a frame to C. SW1 records A&#x27;s source MAC on port 1. C&#x27;s MAC is not in the MAC address table, so SW1 floods copies out of ports 2, 3 and 4. The table shows A on port 1 (just learned), B on port 2 (learned earlier, ages out after about 300 seconds idle) and no entry for C.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="559" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><path class="dg-stroke" d="M380 140V154L128 220V236" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M374 153l6-8 6 8" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M440 140V154L356 220V236" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M350 227l6 8 6-8" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M500 140V154L584 220V236" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M578 227l6 8 6-8" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M560 140V154L812 220V236" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-warn"><path class="dg-stroke" d="M806 227l6 8 6-8" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-card" x="320" y="20" width="300" height="120" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(336 36) 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="378" y="57" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Switch SW1</text></g>
<text x="338" y="88" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Learns source MACs per port</text>
<text x="338" y="108" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Floods unknown destinations</text>
<text x="380" y="134" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle">1</text>
<text x="440" y="134" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle">2</text>
<text x="500" y="134" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle">3</text>
<text x="560" y="134" font-size="11" class="dg-muted" fill="#5c5c68" font-weight="700" text-anchor="middle">4</text>
<g class="dg-l1"><rect class="dg-bg-plain" x="208.04" y="175" width="91.92" height="24" rx="12" fill="#ffffff"/><text x="254" y="191.5" font-size="12" class="dg-ink" fill="#0369a1" font-weight="600" text-anchor="middle">frame A → C</text></g>
<g class="dg-warn"><rect class="dg-bg-plain" x="348.68" y="175" width="98.64" height="24" rx="12" fill="#ffffff"/><text x="398" y="191.5" font-size="12" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">flooded copy</text></g>
<g class="dg-warn"><rect class="dg-bg-plain" x="492.68" y="175" width="98.64" height="24" rx="12" fill="#ffffff"/><text x="542" y="191.5" font-size="12" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">flooded copy</text></g>
<g class="dg-warn"><rect class="dg-bg-plain" x="636.68" y="175" width="98.64" height="24" rx="12" fill="#ffffff"/><text x="686" y="191.5" font-size="12" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">flooded copy</text></g>
<rect class="dg-card" x="24" y="240" width="208" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(40 256) 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="277" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">Host A · port 1</text></g>
<text x="42" y="308" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0a</text>
<rect class="dg-card" x="252" y="240" width="208" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(268 256) scale(1.2500)" fill="none" stroke="#17171c" 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>
<text x="310" y="277" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Host B · port 2</text>
<text x="270" y="308" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0b</text>
<rect class="dg-card" x="480" y="240" width="208" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(496 256) scale(1.2500)" fill="none" stroke="#17171c" 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>
<text x="538" y="277" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Host C · port 3</text>
<text x="498" y="308" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0c</text>
<rect class="dg-card" x="708" y="240" width="208" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(724 256) scale(1.2500)" fill="none" stroke="#17171c" 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>
<text x="766" y="277" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Host D · port 4</text>
<text x="726" y="308" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0d</text>
<rect class="dg-card" x="24" y="370" width="892" height="166" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="44" y="398" font-size="14" class="dg-text" fill="#17171c" font-weight="700">MAC address table · VLAN 1</text>
<text x="44" y="426" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="700">MAC address</text>
<text x="250" y="426" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="700">Port</text>
<text x="330" y="426" font-size="12" class="dg-muted" fill="#5c5c68" font-weight="700">Entry</text>
<path class="dg-line" d="M44 436H896" stroke="#cfcfc8" stroke-width="1" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<g class="dg-ok"><text x="44" y="462" font-size="13" class="dg-ink" fill="#15803d" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0a</text></g>
<g class="dg-ok"><text x="260" y="462" font-size="13" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-ok"><text x="330" y="462" font-size="13" class="dg-ink" fill="#15803d" font-weight="600">just learned from the source of A&#x27;s frame</text></g>
<text x="44" y="488" font-size="13" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0b</text>
<text x="260" y="488" font-size="13" class="dg-text" fill="#17171c" font-weight="600" text-anchor="middle">2</text>
<text x="330" y="488" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">learned earlier · ages out after ~300 s idle</text>
<g class="dg-warn"><text x="44" y="514" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="600" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">00:00:5e:00:53:0c</text></g>
<g class="dg-warn"><text x="260" y="514" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="700" text-anchor="middle">—</text></g>
<g class="dg-warn"><text x="330" y="514" font-size="13" class="dg-ink" fill="#b91c1c" font-weight="600">not in the table yet → flood to every other port</text></g>
</svg></figure>

*Figure 1. The switch learns from the source address and floods when it doesn't know the destination. When C replies, the switch learns C on port 3, and from then on frames to C go out of port 3 only.*

A switch is transparent: hosts don't know it exists and never address frames to it. That's why switches are also called **bridges** in the IEEE standards and in Linux. A bridge is the original two-port version of the same idea.

> [!CHECK]
> A switch receives a frame on port 2 with destination MAC `00:00:5e:00:53:0c`, which its table lists on port 7. What does the switch do?
>
> - [ ] Floods it out of every port except port 2, to be safe
> - [x] Records the frame's source MAC on port 2 and forwards the frame out of port 7 only
> - [ ] Forwards it out of port 7 and records `00:00:5e:00:53:0c` on port 2
>
> The switch always learns from the *source* address of a frame, against the port it arrived on. A known destination means a single, targeted copy. Flooding is only for unknown, broadcast and multicast destinations.

### Collision domains and broadcast domains
Two terms describe how far traffic spreads.

A **collision domain** is the set of devices whose transmissions can collide on a shared medium. With old hubs, every device shared one collision domain. On a switch, every port is its own collision domain, and with full-duplex links, which are the norm today, collisions don't happen at all.

A **broadcast domain** is the set of devices that receive each other's broadcasts. A switch floods broadcasts out of every port, so all devices on a switch, and on any switches connected to it, form one broadcast domain. A **router** stops broadcasts: each router interface is the edge of a broadcast domain. In IP terms, one broadcast domain is usually one subnet.

Why care? Broadcasts are part of normal life. ARP, Dynamic Host Configuration Protocol (DHCP) discovery and many discovery protocols use them. But every device must process every broadcast in its domain. With a few hundred devices, that's background noise. With thousands, it wastes bandwidth and CPU time, and it lets any device see and talk to every other device directly. Smaller broadcast domains are calmer and easier to secure.

| | Hub | Switch | Router |
|---|---|---|---|
| Works at | Layer 1 | Layer 2 | Layer 3 |
| Collision domains | One for all ports | One per port | One per interface |
| Broadcast domains | One | One (per VLAN, see below) | One per interface |

> [!CHECK]
> Twenty laptops are connected to two switches, which are connected to each other. There are no routers and no VLANs. How many broadcast domains are there?
>
> - [x] One
> - [ ] Two, one per switch
> - [ ] Twenty, one per switch port
>
> Switches flood broadcasts out of every port, including the link between them, so all twenty laptops receive each other's broadcasts. Each switch *port* is a separate collision domain, but only a router or a VLAN boundary splits a broadcast domain.

### VLANs and IEEE 802.1Q tagging
Back to the office. You want guests, staff and phones in separate networks, but you don't want to buy three sets of switches and run three sets of cables. A **virtual LAN (VLAN)** solves this. It's a logical broadcast domain inside a switch: the switch keeps a separate MAC address table for each VLAN and never forwards or floods a frame from one VLAN into another. One physical switch behaves like several independent switches.

Each VLAN has a number, the **VLAN ID (VID)**. Most switches put every port in VLAN 1 by default, which is why everything works out of the box.

Within one switch, the VLAN of a frame is simply the VLAN of the port it came in on. But what happens when two switches are connected and both carry several VLANs? The receiving switch needs to know which VLAN each frame belongs to. The standard answer is **IEEE 802.1Q**: the sending switch inserts a 4-byte **VLAN tag** into the frame, right after the source MAC address.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 340" width="940" height="340" role="img" aria-labelledby="vlan-8021q-tag-title vlan-8021q-tag-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="vlan-8021q-tag-title">Where the IEEE 802.1Q tag sits in an Ethernet frame</title>
<desc id="vlan-8021q-tag-desc">An Ethernet frame: destination MAC (6 bytes), source MAC (6 bytes), the 802.1Q tag (4 bytes), EtherType (2 bytes), payload (42 to 1500 bytes) and frame check sequence (4 bytes). The tag is inserted after the source MAC. Inside the tag: Tag Protocol Identifier, 16 bits, value 0x8100; Priority Code Point, 3 bits, priority 0 to 7; Drop Eligible Indicator, 1 bit; VLAN identifier, 12 bits, 1 to 4094 usable, 0 and 4095 reserved.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="339" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<text x="30" y="40" font-size="14" class="dg-text" fill="#17171c" font-weight="700">Ethernet frame with an 802.1Q tag (sizes in bytes, not to scale)</text>
<rect class="dg-card" x="30" y="56" width="144" height="62" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="102" y="83" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Destination MAC</text>
<text x="102" y="103" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">6</text>
<rect class="dg-card" x="180" y="56" width="144" height="62" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="252" y="83" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Source MAC</text>
<text x="252" y="103" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">6</text>
<g class="dg-l2"><rect class="dg-tint" x="330" y="56" width="134" height="62" rx="8" fill="#e8f2ec" stroke="#8ac09e" stroke-width="1.3"/></g>
<g class="dg-l2"><text x="397" y="83" font-size="13.5" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">802.1Q tag</text></g>
<g class="dg-l2"><text x="397" y="103" font-size="12.5" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">4</text></g>
<rect class="dg-card" x="470" y="56" width="110" height="62" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="525" y="83" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">EtherType</text>
<text x="525" y="103" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">2</text>
<rect class="dg-card" x="586" y="56" width="230" height="62" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="701" y="83" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Payload</text>
<text x="701" y="103" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">42–1500</text>
<rect class="dg-card" x="822" y="56" width="74" height="62" rx="8" fill="#ffffff" stroke="#cfcfc8"/>
<text x="859" y="83" font-size="13.5" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">FCS</text>
<text x="859" y="103" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">4</text>
<g class="dg-l2"><path class="dg-stroke" d="M330 122L30 186" stroke="#15803d" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M464 122L904 186" stroke="#15803d" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<text x="470" y="160" font-size="14" class="dg-text" fill="#17171c" font-weight="700" text-anchor="middle">Inside the 4-byte tag</text>
<g class="dg-l2"><rect class="dg-tint" x="30" y="190" width="284" height="110" rx="8" fill="#e8f2ec" stroke="#8ac09e" stroke-width="1.3"/></g>
<g class="dg-l2"><text x="172" y="220" font-size="15" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">TPID · 0x8100</text></g>
<text x="172" y="246" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">16 bits</text>
<text x="172" y="272" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">says “a VLAN tag follows”</text>
<g class="dg-l2"><rect class="dg-tint" x="320" y="190" width="164" height="110" rx="8" fill="#e8f2ec" stroke="#8ac09e" stroke-width="1.3"/></g>
<g class="dg-l2"><text x="402" y="220" font-size="15" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">PCP</text></g>
<text x="402" y="246" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">3 bits</text>
<text x="402" y="272" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">priority 0–7</text>
<g class="dg-l2"><rect class="dg-tint" x="490" y="190" width="144" height="110" rx="8" fill="#e8f2ec" stroke="#8ac09e" stroke-width="1.3"/></g>
<g class="dg-l2"><text x="562" y="220" font-size="15" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">DEI</text></g>
<text x="562" y="246" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">1 bit</text>
<text x="562" y="272" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">drop eligible</text>
<g class="dg-l2"><rect class="dg-tint" x="640" y="190" width="264" height="110" rx="8" fill="#e8f2ec" stroke="#8ac09e" stroke-width="1.3"/></g>
<g class="dg-l2"><text x="772" y="220" font-size="15" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">VID · VLAN ID</text></g>
<text x="772" y="246" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="600" text-anchor="middle" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">12 bits</text>
<text x="772" y="272" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" text-anchor="middle">1–4094 usable (0, 4095 reserved)</text>
</svg></figure>

*Figure 2. The 802.1Q tag sits between the source MAC and the EtherType. Its last 12 bits carry the VLAN ID.*

The tag has four fields:

| Field | Size | Meaning |
|---|---|---|
| Tag Protocol Identifier (TPID) | 16 bits | Always `0x8100` for an 802.1Q tag. It sits where the EtherType normally is, so a receiver knows "a tag follows". |
| Priority Code Point (PCP) | 3 bits | Priority 0–7 for quality of service (QoS), for example to favour voice traffic. Often called 802.1p priority. |
| Drop Eligible Indicator (DEI) | 1 bit | Marks frames that may be dropped first under congestion. |
| VLAN identifier (VID) | 12 bits | The VLAN number. 12 bits give 4096 values: 0 and 4095 are reserved, so usable VLANs are **1 to 4094**. |

A VID of 0 means "this frame carries a priority but belongs to no particular VLAN" (a priority tag). The tag also makes the maximum frame 4 bytes longer: 1522 bytes instead of 1518. The minimum stays 64 bytes, so the minimum payload drops from 46 to 42 bytes.

> [!NOTE]
> A second standard, IEEE 802.1ad ("Q-in-Q"), stacks two tags so that a service provider can carry a customer's VLANs inside its own. Its outer tag uses TPID `0x88A8`. You won't need it in this module, but you'll recognise it in packet captures.

> [!CHECK]
> A network engineer wants to create VLAN 4095 for a new team. What happens?
>
> - [ ] It works: the VID field has 12 bits, so 4095 is the highest VLAN
> - [x] It's rejected: 4095 is reserved, and usable VLAN IDs are 1 to 4094
> - [ ] It works, but only on trunk ports
>
> The 12-bit VID field can hold 0 to 4095, but 802.1Q reserves 0 (priority tag) and 4095. Some vendors reserve a few more IDs for internal use, so check your platform's documentation too.

### Access ports and trunk ports
Hosts usually don't know anything about VLANs. A laptop sends ordinary untagged frames. So switch ports come in two kinds:

- An **access port** belongs to exactly one VLAN. Frames from the host arrive untagged, and the switch assigns them to the port's VLAN. Frames to the host leave untagged. The host never sees a tag.
- A **trunk port** carries several VLANs over one link, usually between switches, or between a switch and a router, a firewall or a virtualisation host. Frames on a trunk carry an 802.1Q tag that says which VLAN they belong to.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 420" width="940" height="420" role="img" aria-labelledby="vlan-access-trunk-title vlan-access-trunk-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="vlan-access-trunk-title">Access ports and a trunk carrying two VLANs between two switches</title>
<desc id="vlan-access-trunk-desc">Two switches, SW1 and SW2, joined by an 802.1Q trunk that carries VLAN 10 and VLAN 20 as tagged frames. On SW1, PC-A (192.168.10.11, VLAN 10) and PC-B (192.168.20.12, VLAN 20) use access ports. On SW2, PC-C (192.168.10.13, VLAN 10) and PC-D (192.168.20.14, VLAN 20) use access ports. Hosts send and receive untagged frames; only the trunk carries tags.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="419" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><rect class="dg-solid" x="24" y="22" width="132" height="26" rx="13" fill="#0369a1"/><text x="90" y="39.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">VLAN 10 · Staff</text></g>
<g class="dg-l4"><rect class="dg-solid" x="166" y="22" width="132" height="26" rx="13" fill="#c2410c"/><text x="232" y="39.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">VLAN 20 · Guest</text></g>
<g class="dg-l1"><path class="dg-stroke" d="M150 236V262L110 282V300" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M230 236V262L330 282V300" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M710 236V262L610 282V300" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M790 236V262L830 282V300" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l2"><path class="dg-stroke" d="M320 176H620" stroke="#15803d" stroke-width="4" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-card" x="60" y="140" width="260" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(76 156) 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="118" y="177" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Switch SW1</text></g>
<text x="78" y="208" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Access ports: one VLAN, untagged</text>
<rect class="dg-card" x="620" y="140" width="260" height="96" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(636 156) 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="678" y="177" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">Switch SW2</text></g>
<text x="638" y="208" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">Access ports: one VLAN, untagged</text>
<g class="dg-l2"><rect class="dg-bg-plain" x="360.2" y="164" width="219.6" height="24" rx="12" fill="#ffffff"/><text x="470" y="180.5" font-size="12" class="dg-ink" fill="#15803d" font-weight="600" text-anchor="middle">trunk · 802.1Q · tagged 10, 20</text></g>
<g class="dg-l1"><rect class="dg-solid" x="366.5" y="134" width="67" height="26" rx="13" fill="#0369a1"/><text x="400" y="151.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">tag 10</text></g>
<g class="dg-l4"><rect class="dg-solid" x="506.5" y="134" width="67" height="26" rx="13" fill="#c2410c"/><text x="540" y="151.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">tag 20</text></g>
<rect class="dg-card" x="20" y="300" width="180" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(36 316) 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="78" y="337" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">PC-A</text></g>
<text x="38" y="368" 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</text>
<text x="38" y="388" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">access port · VLAN 10</text>
<rect class="dg-card" x="240" y="300" width="180" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(256 316) 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="298" y="337" font-size="14" class="dg-ink" fill="#c2410c" font-weight="700">PC-B</text></g>
<text x="258" y="368" 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</text>
<text x="258" y="388" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">access port · VLAN 20</text>
<rect class="dg-card" x="520" y="300" width="180" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(536 316) 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="578" y="337" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">PC-C</text></g>
<text x="538" y="368" 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</text>
<text x="538" y="388" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">access port · VLAN 10</text>
<rect class="dg-card" x="740" y="300" width="180" height="100" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(756 316) 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="798" y="337" font-size="14" class="dg-ink" fill="#c2410c" font-weight="700">PC-D</text></g>
<text x="758" y="368" font-size="12.5" class="dg-text" fill="#17171c" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">192.168.20.14</text>
<text x="758" y="388" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">access port · VLAN 20</text>
</svg></figure>

*Figure 3. One trunk carries both VLANs between the switches. PC-A can reach PC-C at Layer 2, but not PC-B or PC-D, even though they're on the same switches.*

A trunk has two more settings you need to know:

- **Allowed VLANs.** The list of VLANs the trunk carries. Many switches allow all VLANs on a trunk by default. Restricting the list to the VLANs that are really needed ("pruning") keeps broadcasts and attackers out of places they don't belong.
- **Native VLAN.** The VLAN to which a trunk assigns frames that arrive *untagged*, and whose frames it sends untagged. Both ends must agree on it; if they don't, frames leak from one VLAN into another. On many switches the native VLAN is VLAN 1 by default. Linux and some vendors call this the port VLAN ID (PVID).

> [!NOTE]
> Vendors use different words for the same ideas. Some say "untagged member of VLAN 10" instead of "access port in VLAN 10", and "tagged member" instead of "trunk member". The behaviour is the same: what matters is whether frames on that port, for that VLAN, carry a tag.

> [!CHECK]
> PC-A in Figure 3 sends a frame to PC-C. Where does the frame carry an 802.1Q tag?
>
> - [ ] On every link, so each switch knows the VLAN
> - [ ] Nowhere: VLAN 10 isn't the native VLAN, so it is never tagged
> - [x] Only on the trunk between SW1 and SW2
>
> PC-A's access port receives an untagged frame and SW1 assigns it to VLAN 10. SW1 adds the tag to cross the trunk, and SW2 removes it before delivering the frame out of PC-C's access port. Hosts on access ports never see tags.

### Routing between VLANs
VLANs are separate broadcast domains, and each one is normally its own IP subnet: VLAN 10 is `192.168.10.0/24`, VLAN 20 is `192.168.20.0/24`. A switch on its own will never move a frame from one VLAN to another. To get traffic between VLANs, you need a router, or something that acts like one. Each VLAN's hosts use a router interface in their VLAN as their default gateway.

There are three common designs:

1. **One router interface per VLAN.** Connect a separate router port to an access port in each VLAN. It's simple and easy to understand, but you run out of router ports quickly.
2. **Router-on-a-stick.** Connect *one* router port to a switch trunk port. On the router, create one **subinterface** per VLAN: a logical interface that sends and receives frames tagged with that VLAN's ID, with its own IP address that serves as the gateway for the VLAN. Traffic between VLANs goes up the trunk to the router and comes back down the same trunk.
3. **Layer 3 switch.** A **Layer 3 switch** (multilayer switch) routes between VLANs itself. You create a virtual interface for each VLAN, often called a switched virtual interface (SVI) or VLAN interface, give it the gateway IP address, and the switch routes between them, usually in hardware at full speed.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 470" width="940" height="470" role="img" aria-labelledby="vlan-router-on-a-stick-title vlan-router-on-a-stick-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="vlan-router-on-a-stick-title">Router-on-a-stick: routing between VLAN 10 and VLAN 20 over one trunk</title>
<desc id="vlan-router-on-a-stick-desc">PC-A (192.168.10.11, VLAN 10, gateway 192.168.10.1) sends to PC-B (192.168.20.12, VLAN 20, gateway 192.168.20.1). 1: PC-A sends an untagged frame to SW1. 2: SW1 forwards it up the trunk tagged with VLAN 10 to R1&#x27;s subinterface eth0.10 (192.168.10.1/24). 3: R1 routes the packet and sends it back down the same trunk from subinterface eth0.20 (192.168.20.1/24), tagged with VLAN 20. 4: SW1 removes the tag and delivers the frame to PC-B.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="469" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<g class="dg-l1"><path class="dg-stroke" d="M440 216V156" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M434 163l6-8 6 8" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M500 156V216" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M494 209l6 8 6-8" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M170 340V266H316" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l1"><path class="dg-stroke" d="M309 260l8 6-8 6" stroke="#0369a1" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M624 266H770V336" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<g class="dg-l4"><path class="dg-stroke" d="M764 329l6 8 6-8" stroke="#c2410c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/></g>
<rect class="dg-card" x="300" y="24" width="340" height="126" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-icon-neutral" transform="translate(316 40) 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="61" font-size="15" class="dg-text" fill="#17171c" font-weight="700">Router R1</text>
<text x="318" y="92" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">one physical link, two subinterfaces</text>
<text x="318" y="112" 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="132" 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="320" y="222" width="300" height="90" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(336 238) 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="378" y="259" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">SW1</text></g>
<text x="338" y="290" font-size="13" class="dg-muted" fill="#5c5c68" font-weight="500">trunk port to R1 · access ports to PCs</text>
<g class="dg-l1"><rect class="dg-solid" x="366" y="172" width="62" height="26" rx="13" fill="#0369a1"/><text x="397" y="189.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">tag 10</text></g>
<g class="dg-l4"><rect class="dg-solid" x="512" y="172" width="62" height="26" rx="13" fill="#c2410c"/><text x="543" y="189.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">tag 20</text></g>
<rect class="dg-card" x="40" y="340" width="260" height="108" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l1"><g class="dg-icon" transform="translate(56 356) 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="98" y="377" font-size="14" class="dg-ink" fill="#0369a1" font-weight="700">PC-A · VLAN 10</text></g>
<text x="58" y="408" 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>
<text x="58" y="428" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">gateway 192.168.10.1</text>
<rect class="dg-card" x="640" y="340" width="260" height="108" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l4"><g class="dg-icon" transform="translate(656 356) 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="698" y="377" font-size="14" class="dg-ink" fill="#c2410c" font-weight="700">PC-B · VLAN 20</text></g>
<text x="658" y="408" 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>
<text x="658" y="428" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">gateway 192.168.20.1</text>
<g class="dg-l1"><circle class="dg-solid" cx="250" cy="266" r="11" fill="#0369a1"/><text x="250" y="270.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">1</text></g>
<g class="dg-l1"><circle class="dg-solid" cx="354" cy="185" r="11" fill="#0369a1"/><text x="354" y="189.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">2</text></g>
<g class="dg-l4"><circle class="dg-solid" cx="586" cy="185" r="11" fill="#c2410c"/><text x="586" y="189.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">3</text></g>
<g class="dg-l4"><circle class="dg-solid" cx="770" cy="300" r="11" fill="#c2410c"/><text x="770" y="304.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">4</text></g>
</svg></figure>

*Figure 4. With router-on-a-stick, inter-VLAN traffic crosses the same trunk twice: up in VLAN 10, down in VLAN 20.*

| | Router-on-a-stick | Layer 3 switch |
|---|---|---|
| Hardware | Any router or firewall with 802.1Q support, plus a Layer 2 switch | A switch with routing features |
| Cost | Low: one router port serves many VLANs | Higher per device, but often already in place in larger networks |
| Performance | Limited by one link, which carries each routed packet twice | Routes at or near switching speed |
| Typical use | Small offices, labs, branches; when a firewall must inspect inter-VLAN traffic | Campus and data centre networks with lots of inter-VLAN traffic |
| Single point of failure | The router and its one link | The switch (usually deployed in redundant pairs) |

> [!TIP]
> In many networks the inter-VLAN "router" is actually a firewall, often in a router-on-a-stick design. That's deliberate: every packet between VLANs passes a place where you can apply security policy.

> [!CHECK]
> An office routes between eight VLANs with router-on-a-stick over one 1 Gbit/s link. Users complain that copying files between the Staff and Servers VLANs is slow, while traffic within each VLAN is fast. What is the most likely cause?
>
> - [ ] The trunk can carry only one VLAN at a time
> - [x] All inter-VLAN traffic crosses the single trunk link twice, so that link is a bottleneck
> - [ ] The switch floods all inter-VLAN traffic to every port
>
> Each routed packet goes up the trunk to the router and back down again, so the link carries it twice and is shared by all eight VLANs. A Layer 3 switch, or a faster or aggregated link to the router, removes the bottleneck.

### Switching loops and the Spanning Tree Protocol
Networks need redundancy: if a switch or a cable fails, traffic should take another path. But redundant links between switches create **loops**, and at Layer 2 that's dangerous. An IP packet has a Time to Live (TTL) field that is decreased at every router, so a packet in a routing loop eventually dies. An Ethernet frame has no such field. A frame in a loop circulates for ever.

Take three switches connected in a triangle. One host sends a broadcast. Each switch floods it out of every other port, so copies travel round the triangle in both directions, and every switch floods every copy again. Within seconds you get:

- a **broadcast storm**: the links fill up with endless copies of the same frames, and devices spend all their CPU time processing them;
- **MAC table instability**: the switch sees the same source MAC arrive on different ports and keeps rewriting its table ("MAC flapping");
- **duplicate frames** delivered to hosts.

The network becomes unusable until the loop is broken. That's what happened in the office when someone looped a spare cable.

The **Spanning Tree Protocol (STP)**, defined in IEEE 802.1D, keeps the redundant links but logically disables just enough ports to remove every loop. The result is a tree: exactly one active path between any two switches. When an active link fails, STP unblocks a backup port.

Switches running STP exchange **Bridge Protocol Data Units (BPDUs)**, small frames sent to the multicast address `01:80:c2:00:00:00`. With them, they build the tree in three steps:

1. **Elect a root bridge.** Each switch has a **bridge ID**: a configurable priority (default 32768) followed by its MAC address. The lowest bridge ID wins. With equal priorities, the lowest MAC wins, which is why you set the priority by hand on the switch you *want* as root.
2. **Choose a root port on every other switch.** Each non-root switch picks the port with the lowest total path cost to the root. Faster links have lower costs.
3. **Choose a designated port on every link.** On each link, the port closest to the root forwards traffic onto it. Any port that is neither a root port nor a designated port is **blocked**: it receives BPDUs but doesn't forward frames.

<figure class="diagram" style="--dg-w: 940px"><svg xmlns="http://www.w3.org/2000/svg" class="dg" viewBox="0 0 940 490" width="940" height="490" role="img" aria-labelledby="switching-stp-loop-title switching-stp-loop-desc" font-family="Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif" xml:lang="en">
<title id="switching-stp-loop-title">Spanning Tree breaks a three-switch loop by blocking one port</title>
<desc id="switching-stp-loop-desc">Three switches in a triangle. SW1 has the lowest bridge ID (priority 32768, MAC 00:00:5e:00:53:a1) and becomes the root bridge; both its ports are designated ports. SW2 (00:00:5e:00:53:a2) and SW3 (00:00:5e:00:53:a3) each use their link to SW1 as root port. On the SW2–SW3 link, SW2&#x27;s port is designated because SW2 has the lower bridge ID, and SW3&#x27;s port is blocked (an alternate, discarding port), so there is no loop.</desc>
<rect class="dg-bg" x="0.5" y="0.5" width="939" height="489" rx="16" fill="#ffffff" stroke="#e3e3dd"/>
<path class="dg-line" d="M380 140L174 300" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M560 140L766 300" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<path class="dg-line" d="M324 358H470" stroke="#cfcfc8" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>
<g class="dg-warn"><path class="dg-stroke" d="M470 358H616" stroke="#b91c1c" stroke-width="2.5" fill="none" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="7 6"/></g>
<rect class="dg-card" x="320" y="20" width="300" height="116" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-ok"><g class="dg-icon" transform="translate(336 36) 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-ok"><text x="378" y="57" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">SW1 · root bridge</text></g>
<text x="338" y="88" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">Bridge priority 32768</text>
<text x="338" y="108" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">MAC 00:00:5e:00:53:a1</text>
<rect class="dg-card" x="24" y="300" width="300" height="116" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(40 316) 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="82" y="337" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">SW2</text></g>
<text x="42" y="368" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">Bridge priority 32768</text>
<text x="42" y="388" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">MAC 00:00:5e:00:53:a2</text>
<rect class="dg-card" x="616" y="300" width="300" height="116" rx="12" fill="#ffffff" stroke="#cfcfc8"/>
<g class="dg-l2"><g class="dg-icon" transform="translate(632 316) 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="674" y="337" font-size="15" class="dg-ink" fill="#15803d" font-weight="700">SW3</text></g>
<text x="634" y="368" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">Bridge priority 32768</text>
<text x="634" y="388" font-size="12.5" class="dg-muted" fill="#5c5c68" font-weight="500" font-family="JetBrains Mono, ui-monospace, Menlo, Consolas, monospace">MAC 00:00:5e:00:53:a3</text>
<g class="dg-ok"><rect class="dg-solid" x="357" y="127" width="46" height="26" rx="13" fill="#15803d"/><text x="380" y="144.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">DP</text></g>
<g class="dg-ok"><rect class="dg-solid" x="537" y="127" width="46" height="26" rx="13" fill="#15803d"/><text x="560" y="144.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">DP</text></g>
<g class="dg-l2"><rect class="dg-solid" x="151" y="287" width="46" height="26" rx="13" fill="#15803d"/><text x="174" y="304.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">RP</text></g>
<g class="dg-l2"><rect class="dg-solid" x="743" y="287" width="46" height="26" rx="13" fill="#15803d"/><text x="766" y="304.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">RP</text></g>
<g class="dg-ok"><rect class="dg-solid" x="333" y="345" width="46" height="26" rx="13" fill="#15803d"/><text x="356" y="362.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">DP</text></g>
<g class="dg-warn"><rect class="dg-solid" x="530" y="345" width="80" height="26" rx="13" fill="#b91c1c"/><text x="570" y="362.5" font-size="12" class="dg-on" fill="#ffffff" font-weight="700" text-anchor="middle">Blocked</text></g>
<g class="dg-warn"><rect class="dg-bg-plain" x="373.64" y="380" width="192.72" height="24" rx="12" fill="#ffffff"/><text x="470" y="396.5" font-size="12" class="dg-ink" fill="#b91c1c" font-weight="600" text-anchor="middle">no loop: SW3 discards here</text></g>
<g class="dg-l2"><rect class="dg-tint" x="24" y="440" width="259" height="26" rx="13" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="153.5" y="457.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">RP = root port (best path to root)</text></g>
<g class="dg-ok"><rect class="dg-tint" x="293" y="440" width="238" height="26" rx="13" fill="#e8f2ec" stroke="#96c6a8" stroke-width="1.2"/><text x="412" y="457.5" font-size="11.5" class="dg-ink" fill="#15803d" font-weight="700" text-anchor="middle">DP = designated port (forwards)</text></g>
<g class="dg-warn"><rect class="dg-tint" x="541" y="440" width="279" height="26" rx="13" fill="#f8e8e8" stroke="#e09999" stroke-width="1.2"/><text x="680.5" y="457.5" font-size="11.5" class="dg-ink" fill="#b91c1c" font-weight="700" text-anchor="middle">Blocked = alternate port (discarding)</text></g>
</svg></figure>

*Figure 5. STP keeps all three cables but blocks one port, so the triangle becomes a tree. If the SW1–SW2 link fails, SW3's blocked port starts forwarding.*

Classic 802.1D STP is slow. When the topology changes, a port goes through listening and learning states before it forwards, and with the default timers that takes 30 to 50 seconds. **Rapid Spanning Tree Protocol (RSTP)** fixes this. It was first published as IEEE 802.1w, then folded into 802.1D-2004, and is now part of IEEE 802.1Q. RSTP uses the same root bridge, root port and designated port ideas, but switches negotiate directly with their neighbours instead of waiting for timers. It typically converges in a few seconds or less. RSTP adds:

- **Alternate ports**: blocked ports that are a ready-made backup for the root port, so failover is almost immediate.
- **Edge ports**: ports connected to hosts, not switches. They start forwarding at once instead of waiting. (Some vendors call this "PortFast".)
- Fewer port states: **discarding**, learning and forwarding.

RSTP is backward compatible with STP, and it is the sensible default today. Multiple Spanning Tree Protocol (MSTP), also in 802.1Q, runs separate trees for groups of VLANs so that different VLANs can use different links.

> [!IMPORTANT]
> STP is a safety net, not a design. Keep the Layer 2 topology simple, choose the root bridge on purpose (usually the core or distribution switch), and never disable STP to "make a port come up faster": configure the port as an edge port instead.

> [!CHECK]
> Three switches have the same bridge priority. Their MAC addresses are `00:00:5e:00:53:a5`, `00:00:5e:00:53:a2` and `00:00:5e:00:53:b1`. Which one becomes the root bridge?
>
> - [ ] The one with `00:00:5e:00:53:b1`, because the highest MAC wins
> - [x] The one with `00:00:5e:00:53:a2`, because the lowest bridge ID wins
> - [ ] The one with the most ports connected
>
> The bridge ID is the priority followed by the MAC address, and the lowest wins. With equal priorities, the lowest MAC decides. To control the election, lower the priority of the switch you want as root.

## How it works
Here is the full journey of one packet in the office network of Figure 3, with a router-on-a-stick R1 connected to a trunk port on SW1. PC-A (`192.168.10.11`, VLAN 10, on SW1) sends a packet to PC-D (`192.168.20.14`, VLAN 20, on SW2). Assume all MAC tables and ARP caches are empty and STP has finished.

1. **PC-A decides where to send it.** `192.168.20.14` is outside PC-A's subnet `192.168.10.0/24`, so PC-A must send the packet to its default gateway, `192.168.10.1`. It needs the gateway's MAC address, so it broadcasts an ARP request.
2. **SW1 learns and floods.** The broadcast arrives untagged on PC-A's access port. SW1 assigns it to VLAN 10, learns PC-A's MAC on that port in the VLAN 10 table, and floods the broadcast to every other port that is a member of VLAN 10: PC-C's direction (tagged on the trunk to SW2) and R1's trunk port (tagged 10). PC-B and PC-D, in VLAN 20, never see it.
3. **R1 answers.** R1's subinterface for VLAN 10 owns `192.168.10.1`, so it replies with its MAC. The reply goes back tagged 10, SW1 learns R1's MAC on the trunk port in VLAN 10, and forwards the reply to PC-A only, because it now knows where PC-A is.
4. **PC-A sends the packet.** The frame is addressed to R1's MAC but carries the IP destination `192.168.20.14`. SW1 forwards it up the trunk, tagged 10.
5. **R1 routes.** R1 removes the Layer 2 header, sees that `192.168.20.14` is on its VLAN 20 subinterface (`192.168.20.0/24`), decreases the TTL, and ARPs for PC-D in VLAN 20. It then sends the packet in a new frame, from its own MAC to PC-D's MAC, tagged 20.
6. **The switches deliver it.** SW1 receives a frame tagged 20. The destination MAC is now known in its VLAN 20 table (learned from PC-D's ARP reply), so SW1 forwards it across the trunk to SW2, still tagged 20. SW2 looks up its VLAN 20 table, removes the tag, and sends the frame out of PC-D's access port.

Note what changed along the way. The source and destination IP addresses stayed the same end to end. The MAC addresses changed at the router, and the VLAN tag was added and removed at each trunk edge. The same MAC address (R1's) appears in both VLAN 10 and VLAN 20 tables, because each VLAN has its own table.

> [!CHECK]
> In step 5, which MAC addresses does R1 put in the frame it sends towards PC-D?
>
> - [ ] Source PC-A's MAC, destination PC-D's MAC, because switches keep MAC addresses end to end
> - [x] Source R1's MAC, destination PC-D's MAC
> - [ ] Source R1's MAC, destination the broadcast address, because PC-D is in another VLAN
>
> A router builds a new Layer 2 frame for each hop. The IP addresses stay PC-A to PC-D, but the frame on VLAN 20 goes from R1's MAC to PC-D's MAC, which R1 found with ARP.

## Worked example
A small office has two access switches, SW1 and SW2, and a firewall that routes between VLANs with router-on-a-stick. You plan three VLANs, each with its own private subnet (RFC 1918):

| VLAN | Name | Subnet | Gateway (firewall subinterface) |
|---|---|---|---|
| 10 | Staff | `192.168.10.0/24` | `192.168.10.1` |
| 20 | Guest | `192.168.20.0/24` | `192.168.20.1` |
| 99 | Management | `192.168.99.0/24` | `192.168.99.1` |
| 999 | Native (unused) | none | none |

VLAN 99 holds the switches' own management addresses. VLAN 999 exists only as the native VLAN of the trunks: no host is ever placed in it, so nothing legitimate travels untagged on a trunk. (The [Security considerations](#security-considerations) explain why.)

**Port plan for SW1** (SW2 is similar):

| Port | Connected to | Mode | VLANs |
|---|---|---|---|
| 1–20 | Staff desks | Access | 10 |
| 21–22 | Guest Wi-Fi access points | Access | 20 |
| 23 | Firewall | Trunk | Allowed 10, 20, 99; native 999 |
| 24 | SW2 | Trunk | Allowed 10, 20, 99; native 999 |
| Unused ports | nothing | Access, shut down | 999 |

Note that the access points sit on access ports in VLAN 20 only because they serve just the guest network. An access point that broadcasts several wireless networks would need a trunk.

**What the MAC table looks like.** After some traffic, SW1's table might look like this (columns vary by vendor):

```text
VLAN  MAC address         Port  Type
10    00:00:5e:00:53:11   1     dynamic
10    00:00:5e:00:53:12   2     dynamic
10    00:00:5e:00:53:fe   23    dynamic
20    00:00:5e:00:53:fe   23    dynamic
20    00:00:5e:00:53:21   21    dynamic
10    00:00:5e:00:53:13   24    dynamic
```

The firewall's MAC `00:00:5e:00:53:fe` appears twice, once per VLAN, both on the trunk port 23. `00:00:5e:00:53:13` is a Staff laptop connected to SW2: from SW1's point of view, it lives behind port 24.

**Configuration.** Every platform has its own syntax. On Linux, which you use in the lab, a VLAN-aware bridge with an access port in VLAN 10 and a trunk carrying VLANs 10, 20 and 99 looks like this:

```bash
sudo ip link add br0 type bridge vlan_filtering 1
sudo bridge vlan add dev eth1 vid 10 pvid untagged   # access port in VLAN 10
sudo bridge vlan del dev eth1 vid 1
sudo bridge vlan add dev eth24 vid 10                # trunk: tagged 10, 20, 99
sudo bridge vlan add dev eth24 vid 20
sudo bridge vlan add dev eth24 vid 99
sudo bridge vlan del dev eth24 vid 1
```

Here `eth1` and `eth24` stand for the physical ports added to the bridge. The `pvid untagged` keywords make the port an access port: untagged frames coming in join VLAN 10, and frames going out leave untagged. Ports added without them carry tagged frames only.

For comparison, the same two ports in a Cisco IOS-style syntax, as one vendor example:

```text
interface GigabitEthernet1/0/1
 switchport mode access
 switchport access vlan 10
!
interface GigabitEthernet1/0/24
 switchport mode trunk
 switchport trunk allowed vlan 10,20,99
 switchport trunk native vlan 999
 switchport nonegotiate
```

> [!NOTE]
> Exact commands depend on the vendor, the model and the software version. For example, some older switches that also support a pre-standard tagging method need `switchport trunk encapsulation dot1q` before `switchport mode trunk`. Check your platform's documentation.

## Common pitfalls
- **Native VLAN mismatch.** If one end of a trunk uses native VLAN 1 and the other uses 999, untagged frames from one VLAN arrive in the other. Some switches detect this through a neighbour discovery protocol and log a warning; read the logs.
- **VLAN not allowed on the trunk.** Hosts in the same VLAN on two switches can't reach each other because the VLAN is missing from the trunk's allowed list, or doesn't exist on one switch. Check both ends.
- **Access port in the wrong VLAN.** The host gets an IP address from the wrong DHCP scope, or none at all. Compare the port's VLAN with the documentation.
- **Missing gateway for a VLAN.** Hosts can reach their own VLAN but nothing else. Check that the router or Layer 3 switch has an interface with the gateway address in that VLAN, and that it's up.
- **Expecting a VLAN to stop traffic between subnets.** VLANs separate Layer 2. Once a router connects them, all traffic flows unless you filter it with access control lists (ACLs) or a firewall.
- **Leaving the root bridge to chance.** With default priorities, the oldest switch (often with the lowest MAC) may become root and carry traffic it was never meant to. Set the priority on the switch you want as root.
- **Disabling STP to fix slow port start-up.** It works until someone creates a loop. Use edge ports instead.

## Security considerations
Layer 2 trusts by default: switches learn any source MAC, flood unknown frames and, on some platforms, negotiate trunks automatically. Attackers with a foot on one access port can abuse that. The attacks below are explained so you can recognise and prevent them. Only ever reproduce them in an isolated lab you own.

- **MAC flooding.** The MAC address table has a limited size. An attacker sends thousands of frames with random source MACs and fills it. Once it's full, the switch can't learn legitimate addresses, so it floods their traffic out of every port in the VLAN, including the attacker's, who can now capture it. **Defence: port security**, which limits how many MAC addresses a port may learn (for example one or two on a desk port) and shuts the port down or drops frames when the limit is exceeded. Port-based authentication with IEEE 802.1X is a stronger control: a device must authenticate before its port forwards traffic.
- **VLAN hopping by switch spoofing.** Some switches can negotiate trunks automatically (for example Cisco's proprietary Dynamic Trunking Protocol, DTP). If a desk port is left in an automatic mode, an attacker's machine can pretend to be a switch, form a trunk, and reach every VLAN. **Defence:** configure every host-facing port explicitly as an access port, configure trunks explicitly, and disable trunk negotiation.
- **VLAN hopping by double tagging.** An attacker on an access port whose VLAN is also the native VLAN of a trunk sends a frame with two 802.1Q tags: the outer one is the native VLAN, the inner one is the target VLAN. The first switch removes the outer tag and, because the native VLAN goes untagged, sends the frame across the trunk with only the inner tag. The next switch reads that tag and delivers the frame into the target VLAN. It only works one way (replies don't come back), but that's enough for some attacks. **Defence: native VLAN hygiene.** Use a dedicated, unused native VLAN that no access port belongs to (VLAN 999 in the worked example), never use VLAN 1 for users, and, where the platform supports it, tag the native VLAN too.
- **Trunk hardening in general.** Allow only the VLANs each trunk needs, shut down unused ports and put them in an unused VLAN, and keep management interfaces in their own VLAN, reachable only from administrator machines.
- **Spanning Tree attacks.** A device that sends BPDUs with a very low priority can become the root bridge and pull traffic through itself, or trigger constant recalculations. **Defence: BPDU guard** on edge ports shuts the port down if it receives any BPDU, since a desk port should never see one. Root guard on ports facing downstream switches stops them from becoming root.
- **Rogue DHCP servers and ARP spoofing.** A device that answers DHCP requests can hand out itself as the default gateway. **Defence: DHCP snooping** marks ports as trusted (towards the real DHCP server) or untrusted (everything else) and drops DHCP server replies from untrusted ports. The bindings it records also feed Dynamic ARP Inspection (DAI), which drops forged ARP replies.

> [!IMPORTANT]
> VLANs are a segmentation tool, not a security boundary on their own. They only isolate traffic if the switches are hardened as above and the router or firewall between VLANs filters traffic. Treat every access port as untrusted.

## Summary
- A switch learns source MAC addresses per port, forwards frames to known destinations, floods broadcasts and unknown destinations, and ages out idle entries (often after 300 seconds).
- Each switch port is a collision domain; all ports in a VLAN form one broadcast domain. Routers and VLAN boundaries split broadcast domains.
- A VLAN is a separate broadcast domain inside a switch. IEEE 802.1Q tags frames with a 4-byte tag (TPID `0x8100`, PCP, DEI, 12-bit VID); usable VLAN IDs are 1–4094.
- Access ports carry one VLAN, untagged, to hosts. Trunk ports carry several VLANs, tagged, between switches and routers; untagged frames on a trunk belong to the native VLAN.
- Traffic between VLANs needs routing: router-on-a-stick uses one trunk and subinterfaces, cheaply but with a bottleneck; a Layer 3 switch routes between VLAN interfaces at switching speed.
- Ethernet frames have no TTL, so Layer 2 loops cause broadcast storms. STP (802.1D) elects a root bridge and blocks redundant ports; RSTP (802.1w, now in 802.1Q) converges much faster.
- Defend Layer 2 with port security, explicit access and trunk ports, an unused native VLAN, pruned trunks, BPDU guard and DHCP snooping.

## Glossary
| Term | Definition |
|---|---|
| Frame | The unit of data at Layer 2, with destination and source MAC addresses, a type field, a payload and a checksum. |
| Media Access Control (MAC) address | A 48-bit hardware address that identifies a network interface on a Layer 2 network. |
| Switch (bridge) | A Layer 2 device that forwards frames between its ports based on their destination MAC addresses. |
| MAC address table (CAM table, forwarding database) | The switch's list of which MAC address was last seen on which port, per VLAN. |
| Learn | Record a frame's source MAC address against the port it arrived on. |
| Forward | Send a frame out of the single port where its destination MAC is known. |
| Flood | Send a frame out of every port in its VLAN except the one it came in on; used for broadcast, multicast and unknown unicast. |
| Filter | Drop a frame whose destination is on the same port it arrived on. |
| Age out | Remove a MAC table entry that hasn't been refreshed for the aging time. |
| Collision domain | The devices whose transmissions can collide on a shared medium; one per switch port. |
| Broadcast domain | The devices that receive each other's broadcasts; one per VLAN, bounded by routers. |
| Router | A Layer 3 device that forwards packets between networks and stops broadcasts. |
| Virtual LAN (VLAN) | A logical broadcast domain inside one or more switches, with its own MAC address table. |
| VLAN ID (VID) | The 12-bit number that identifies a VLAN; 1–4094 are usable. |
| IEEE 802.1Q | The standard for VLAN tagging (and, today, bridging and spanning tree in general). |
| VLAN tag | The 4-byte 802.1Q field inserted after the source MAC: TPID, PCP, DEI and VID. |
| Access port | A switch port in a single VLAN that sends and receives untagged frames, usually to a host. |
| Trunk port | A switch port that carries several VLANs, using 802.1Q tags to tell them apart. |
| Native VLAN | The VLAN that a trunk port assigns to untagged frames; called the PVID on Linux. |
| Subinterface | A logical router interface bound to one VLAN on a physical interface, with its own IP address. |
| Router-on-a-stick | Inter-VLAN routing with one router interface connected to a trunk and one subinterface per VLAN. |
| Layer 3 switch | A switch that can also route between VLANs, using one virtual interface per VLAN. |
| Loop | Two or more active Layer 2 paths between the same switches, which lets frames circulate endlessly. |
| Broadcast storm | Broadcast frames multiplied by a Layer 2 loop until they saturate the network. |
| Spanning Tree Protocol (STP) | IEEE 802.1D protocol that blocks redundant switch ports to make a loop-free tree. |
| Bridge Protocol Data Unit (BPDU) | The frames switches exchange to run STP or RSTP. |
| Bridge ID | A switch's priority followed by its MAC address; the lowest becomes the root bridge. |
| Root bridge | The switch at the centre of the spanning tree. |
| Root port | On a non-root switch, the port with the best path to the root bridge. |
| Designated port | On each link, the port that forwards traffic onto that link. |
| Blocked port | A port that STP keeps from forwarding frames to prevent a loop (an alternate port in RSTP). |
| Rapid Spanning Tree Protocol (RSTP) | The faster successor of STP, first IEEE 802.1w, now part of IEEE 802.1Q. |
| Alternate port | An RSTP blocked port that is a ready backup path to the root. |
| Edge port | An RSTP port connected to a host, which starts forwarding immediately. |
| Discarding | The RSTP port state in which a port doesn't forward frames. |
| MAC flooding | An attack that fills a switch's MAC table so that it floods traffic to the attacker. |
| Port security | A switch feature that limits which or how many MAC addresses a port may use. |
| VLAN hopping | Attacks that send traffic into a VLAN the attacker doesn't belong to (switch spoofing, double tagging). |
| Switch spoofing | VLAN hopping by negotiating a trunk with a switch port left in an automatic mode. |
| Double tagging | VLAN hopping by sending a frame with two 802.1Q tags through the native VLAN of a trunk. |
| BPDU guard | A feature that shuts down an edge port if it receives a BPDU. |
| DHCP snooping | A switch feature that drops DHCP server messages arriving on untrusted ports. |

## Check your understanding
1. Why does a switch learn from the *source* MAC address of a frame and not from the destination?
2. A colleague says: "We bought a bigger switch, so now we have fewer broadcast problems." Is that right? What would actually reduce broadcast traffic?
3. Two switches are connected by a trunk. Hosts in VLAN 20 on both switches can't reach each other, but VLAN 10 works fine. List three things you would check.
4. When would you choose router-on-a-stick over a Layer 3 switch, even in a network that could afford both?
5. Why does a loop at Layer 2 cause a storm, while a loop at Layer 3 eventually stops? What does STP do about it?
6. An attacker is plugged into a desk port in VLAN 1. Which configuration choices on the trunks make a double-tagging attack possible, and which ones stop it?

## References
- IEEE 802.1Q — Bridges and Bridged Networks (VLAN tagging, and the current home of RSTP and MSTP).
- IEEE 802.1D — MAC Bridges (original Spanning Tree Protocol; the 2004 edition replaced STP with RSTP).
- IEEE 802.1w — Rapid Reconfiguration of Spanning Tree (original RSTP amendment).
- IEEE 802.1ad — Provider Bridges (stacked "Q-in-Q" tags).
- IEEE 802.1X — Port-Based Network Access Control.
- [RFC 7042](https://www.rfc-editor.org/rfc/rfc7042) — IANA Considerations and IETF Protocol and Documentation Usage for IEEE 802 Parameters (documentation MAC addresses).
- [RFC 1918](https://www.rfc-editor.org/rfc/rfc1918) — Address Allocation for Private Internets.
- [Linux kernel documentation: Ethernet bridging](https://docs.kernel.org/networking/bridge.html).
- [`bridge(8)` manual page](https://man7.org/linux/man-pages/man8/bridge.8.html) — iproute2 bridge and VLAN configuration.