VLANs — Segmentation, Access vs Trunk Ports, 802.1Q Tagging
A plain switch gives you exactly one broadcast domain: every device hears every broadcast, and the only classic fix is buying more switches. A VLAN slices one physical switch into several independent virtual switches in software — each its own broadcast domain, each unable to reach the others at Layer 2. Two port roles and four extra bytes on the wire make the whole thing work, and end devices never find out it happened.
Key Components
- VLAN (Virtual LAN)
- A logically separate broadcast domain — a region within which a broadcast frame reaches everyone and outside which it reaches no one — created in software on a shared physical switch. The switch keeps a separate MAC address table (CAM table) per VLAN and will not forward a frame between VLANs. To the attached devices it is exactly as though they sat on different physical switches with no cable between them.
- Access port
- A switch port facing an end device (PC, printer, IP phone). It belongs to exactly one VLAN, set by port configuration, and frames on that wire are untagged. The device has no idea VLANs exist; the switch assigns VLAN membership on the device's behalf.
- Trunk port
- A switch port facing another switch (or a router). It carries many VLANs over one cable simultaneously, so every frame must be labelled with which VLAN it belongs to. Both ends must understand the labelling. The alternative — one cable per VLAN — needs 50 cables for 50 VLANs; a trunk multiplexes them onto one.
- 802.1Q tag
- The 4-byte label defined by IEEE 802.1Q, inserted directly after the source MAC address. It carries a 12-bit VLAN ID (VID) identifying the frame's VLAN. Tags exist only on trunk links: added on the way into a trunk, stripped on the way out to an access port.
- Native VLAN
- The one VLAN on a trunk whose frames are sent untagged, for backwards compatibility with equipment that does not speak 802.1Q. It defaults to VLAN 1 and is the sharp edge of the design: an untagged VLAN on a tagged link is what makes double-tagging attacks possible.
Concrete Example
Take one 24-port switch serving three departments. Without VLANs, a broadcast from an engineering workstation is delivered to every finance and HR machine on the switch — wasted bandwidth, wasted CPU on every NIC, and zero isolation. Three VLANs fix that with configuration alone:
! Create the VLAN
Switch(config)# vlan 10
Switch(config-vlan)# name ENGINEERING
! Access port — one VLAN, facing a PC
Switch(config)# interface FastEthernet0/1
Switch(config-if)# switchport mode access
Switch(config-if)# switchport access vlan 10
! Trunk port — many VLANs, facing another switch
Switch(config)# interface GigabitEthernet0/1
Switch(config-if)# switchport mode trunk
Switch(config-if)# switchport trunk allowed vlan 10,20,30
Switch(config-if)# switchport trunk native vlan 999 ! unused VLAN — hardening
Two lines carry almost all of the meaning. switchport mode access + switchport access vlan 10 says: whatever arrives on this wire untagged belongs to VLAN 10, and nothing from any other VLAN may ever leave here. switchport mode trunk says the opposite: this wire carries several VLANs, so label everything. switchport trunk allowed vlan then prunes the trunk down to the VLANs that genuinely need to cross it — a performance and a security practice at once, because a VLAN not on the allowed list simply cannot traverse that link.
The tag those trunk frames carry is four bytes wide, and it is deliberately placed where a receiver that has never heard of VLANs would look for the EtherType:
| Field | Size | Purpose |
|---|---|---|
| TPID | 2 bytes | Always 0x8100. It sits in the EtherType position, signalling "a tag follows". |
| PCP | 3 bits | Priority Code Point (802.1p) — the QoS class of this frame. |
| DEI | 1 bit | Drop-eligible indicator: mark this frame as expendable under congestion. |
| VLAN ID | 12 bits | Which VLAN the frame belongs to. This is the field that does the work. |
The TPID trick is the elegant part. A receiver reads the two bytes where the EtherType normally lives, sees 0x8100 — a value that is not a real Layer 3 protocol — and concludes "this frame is tagged; the real EtherType is four bytes further along." In-band signalling that costs nothing when unused and stays backwards compatible.
Twelve bits give 4096 IDs, of which 1–4094 are usable (0 and 4095 are reserved). VLAN 1 is the default — every port starts there — and precisely because it is the default, production traffic should not live on it. The tag also pushes the maximum Ethernet frame from 1518 to 1522 bytes, which is why switch and NIC hardware must explicitly support "baby giant" frames on trunk links.
Finally, VLANs and IP subnets are paired 1:1 almost universally:
| VLAN | Purpose | Subnet |
|---|---|---|
| 10 | Engineering | 192.168.10.0/24 |
| 20 | HR | 192.168.20.0/24 |
| 30 | Finance | 192.168.30.0/24 |
That convention is not arbitrary — it falls out of the mechanics. A VLAN is one broadcast domain; hosts in one subnet must be able to resolve each other's MAC addresses, and that resolution is a broadcast; therefore a subnet has to fit inside a single broadcast domain. VLAN boundary = broadcast boundary = subnet boundary. Crossing VLANs therefore means crossing subnets, which means routing — a Layer 3 device (a router, or a switch doing routing on a virtual interface). Switches segment; routers connect the segments.
Visual Model
Think of the tag as a visitor badge that only exists inside the corridor between two buildings. Inside a building, everyone in a department already knows where they belong — no badge needed. But the shared corridor carries people from every department at once, so at the door you get a badge stamped with your department, and at the far door the badge is taken off you before you walk in. Nobody in either building ever sees a badge. That is exactly the life of an 802.1Q tag: it is applied entering a trunk and removed leaving to an access port, so end devices stay completely VLAN-unaware.
Step through the diagram below and watch the four tag bytes physically appear on the frame as it enters the trunk and vanish as it leaves. The last two steps switch to the harder case — what happens when the destination MAC is unknown and the switch has to flood.
Loading…
Two things are worth pinning down from that walkthrough. First, the tag never touches an end device — which is the entire reason VLANs deploy against unmodified PCs, printers and appliances. Second, every lookup is scoped to a VLAN: SW2 does not consult "its MAC table", it consults the VLAN 10 table, chosen by the VID it just read off the tag. Everything else about VLAN behaviour follows from those two facts.
Deeper — Edge Cases & Gotchas
Flooding is scoped to the VLAN — and floods do cross trunks
flood set = (all access ports in VLAN X)
+ (all trunk ports that allow VLAN X)
− (the arrival port)
untagged on the access ports, tagged on the trunks
A single flood event therefore produces both untagged copies (to end devices) and tagged copies (to neighbouring switches) from the same frame. Ports in other VLANs receive nothing at all.
Three consequences fall out of that rule:
- The flood domain is the VLAN, not the switch. A VLAN spans multiple switches over trunks, so a flood crosses the trunk and continues flooding on the far switch.
switchport trunk allowed vlangates it. If VLAN 10 is not allowed on a trunk, VLAN 10 floods cannot traverse it. This is why pruning is a genuine performance practice: it literally shortens the distance flooded traffic travels.- The lookup is per-VLAN. "Unknown destination in VLAN 10" is a completely separate question from VLAN 20's table. Each VLAN floods independently of the others.
Traced concretely on the topology from the diagram — PC-A in VLAN 10 sends to a destination SW1 has never seen:
| Where | What happens |
|---|---|
| SW1 learns | Records A → port 1 in the VLAN 10 table (learning is always from the source address). |
| SW1 decides | Destination unknown in VLAN 10's table → flood within VLAN 10. |
| → PC-D access port | Sent, untagged. |
| → trunk to SW2 | Sent, tagged VID 10. |
| → PC-B access port | Not sent — PC-B is in VLAN 20. |
| SW2 receives on trunk | Reads VID 10; learns A → trunk port in its VLAN 10 table. |
| SW2 decides | Still unknown → floods within VLAN 10. |
| → PC-C access port | Sent, untagged. |
| → PC-E access port | Not sent — PC-E is in VLAN 20. |
| → back out the trunk | Never — that is the arrival port. |
Notice what SW2 learned: A lives out the trunk port. Same learn-from-source rule as always, just pointing at a trunk instead of an access port. When the reply comes back, SW2 forwards it out the trunk and SW1 forwards it out port 1 — flooding stops and ordinary unicast takes over, now spanning two switches.
And this sets up the next problem. A flooded frame propagates across every trunk in the VLAN. Give SW1 and SW2 two trunk links for redundancy and the flood goes out trunk A, SW2 floods it out trunk B, SW1 floods it back out trunk A, forever. Ethernet frames carry no TTL — that is an IP header field, and this is pure Layer 2 — so the frame never expires. The result is a broadcast storm that saturates the links and melts the switches within seconds. Preventing exactly that is the job of Spanning Tree Protocol.
The native VLAN and double-tagging
Because the native VLAN travels untagged on a link where everything else is tagged, an attacker can exploit the asymmetry with a frame carrying two stacked tags. The first switch strips the outer tag (it matches the native VLAN, so the switch treats the frame as native-VLAN traffic) and forwards what remains onto the trunk. The second switch now sees a perfectly ordinary tagged frame — the inner tag — and delivers it into a VLAN the attacker was never permitted to reach. The traffic is one-way, but for many attacks that is enough.
Switch(config-if)# switchport mode trunk
Switch(config-if)# switchport trunk allowed vlan 10,20,30
! native VLAN left implicit → VLAN 1
Why it breaks: VLAN 1 is simultaneously the default access VLAN and the default untagged VLAN on the trunk, so an attacker on any un-provisioned port is already sitting in the VLAN whose outer tag will be silently stripped. Set the native VLAN to a dedicated, unused ID that no host is ever placed in (switchport trunk native vlan 999), or configure the trunk to tag every VLAN including the native one.
Remaining traps
- "VLANs let devices in different VLANs talk." Exactly backwards — VLANs prevent Layer 2 communication between them. Crossing requires a router. The feature that does the crossing is inter-VLAN routing, implemented either as a router with tagged subinterfaces on a single trunk ("router-on-a-stick") or as virtual routed interfaces inside a Layer 3 switch.
- The VLAN must exist and be allowed on both switches. Creating VLAN 30 on SW1 and forgetting SW2, or omitting it from the trunk's allowed list, produces traffic that vanishes silently with no error anywhere.
- Access-versus-trunk misconfiguration is a classic outage. An access port where a trunk was meant drops every tagged frame; a trunk where an access port was meant hands an end device frames it cannot parse.
- Not every device is VLAN-unaware. The general rule holds for PCs and printers, but IP phones and hypervisor hosts are routinely given trunk ports and do handle tags themselves — a phone typically carries a tagged voice VLAN and passes the untagged PC traffic through.
- A VLAN is not a firewall. It is good segmentation and it stops broadcast leakage, but hopping attacks and simple misconfiguration both cross it. Treat it as a structural boundary, not a security control.
See Also
How Switches Work — MAC Learning, CAM Table, Forward/Flood/Filter The prerequisite: one switch = one broadcast domain, and the forward/flood/filter rule that VLANs re-scope per VLAN. Ethernet — Frame Format, MAC Addresses & EtherType The frame the 4-byte tag is inserted into, and the EtherType field whose position the0x8100 TPID borrows.
Spanning Tree Protocol (STP/RSTP) — Loop Prevention & Root Bridge Election
The answer to the broadcast storm that VLAN-scoped flooding creates the moment a trunk is made redundant.
ARP — Resolving IP → MAC, ARP Cache, Gratuitous ARP
Why one subnet must fit inside one VLAN: address resolution is a broadcast, so it stops dead at the VLAN edge.
OSI 7-Layer Model — What Each Layer Does and Why the Split Exists
The L2-versus-L3 split that makes "switches segment, routers connect the segments" more than a slogan.
Sources consulted
- IEEE 802.1Q — Virtual Bridged Local Area Networks
Test Yourself
PC-A is in VLAN 10 and PC-B is in VLAN 20 on the same switch, and someone has given both of them addresses inside 192.168.1.0/24. Can PC-A ping PC-B?
Why does a trunk port need tagging when an access port does not?
A frame arrives on SW1's VLAN 10 access port with a destination MAC that appears in no table. SW1 has one trunk to SW2 (VLANs 10 and 20 allowed), two other VLAN 10 access ports and three VLAN 20 access ports. Exactly which ports does the frame leave from, and in what form?
What would happen if you added a second trunk link between SW1 and SW2 for redundancy, and why does the Ethernet header make the outcome so much worse than the equivalent situation in IP?