VLANs and 802.1Q trunking
A VLAN (Virtual LAN) splits one physical switch into several logical networks, each its own broadcast domain and usually its own subnet. 802.1Q trunking lets many VLANs share one cable between switches by tagging every frame.
52.1 Why is it used?
- Without VLANs, Sales and HR on one switch share one broadcast domain: a Sales PC's ARP broadcast reaches every HR computer.
- Security problem: the HR salary server and the Sales laptops sit in the same network.
- With VLAN 10 (Sales) and VLAN 20 (HR), a Sales broadcast goes only to VLAN 10 ports; HR never sees it.
| Benefit | What it means |
|---|---|
| Security | No direct traffic between VLANs |
| Less broadcast | Each VLAN is a small broadcast domain |
| Flexibility | A team on any floor can be in the same VLAN |
| Cost | One switch, many logical networks |
52.2 Access port vs trunk port
- Access port: where a PC/printer connects; member of only one VLAN.
- On an access port the frame is a normal Ethernet frame with no tag; the PC does not even know it is in a VLAN. The switch remembers "port 1 = VLAN 10".
- Trunk port: between switches (or switch-router); carries many VLANs on one cable.
- On the trunk, frames travel tagged; the receiving switch reads the tag, forwards only to that VLAN's access ports and removes the tag there.
- Step 1
1.Sales PC 1 sends an untagged frame on its access port
- Step 2
2.Switch 1 knows port = VLAN 10 and adds an 802.1Q tag (VLAN 10)
- Step 3
3.Frame crosses the trunk tagged, alongside VLAN 20 traffic
- Step 4
4.Switch 2 reads the tag: VLAN 10
- Step 5
5.Switch 2 removes the tag and delivers it only to Sales PC 2 (VLAN 10 access port)
| Point | Access port | Trunk port |
|---|---|---|
| VLANs | One | Many |
| Tag | Untagged | 802.1Q tag on every frame (except native VLAN) |
| Connects | End devices: PC, printer | Switch to switch, switch to router |
| Device awareness | PC does not know about the VLAN | Both ends must agree on allowed VLANs and native VLAN |
52.3 Native VLAN
- A frame that arrives on a trunk without a tag is treated as native VLAN traffic (VLAN 1 by default).
- The native VLAN must be the same on both ends, otherwise untagged traffic lands in the wrong VLAN.
- Best practice: set the native VLAN to an unused VLAN with no users (protects against VLAN hopping).
52.4 The 802.1Q tag
A 4 byte tag inserted right after the source MAC: dst MAC | src MAC | 802.1Q tag | type | data.
| Field | Size | Meaning |
|---|---|---|
| TPID | 16 bits | 0x8100: tells the switch this is a tagged frame |
| PCP | 3 bits | Priority (QoS) |
| DEI | 1 bit | Drop eligible indicator |
| VLAN ID | 12 bits | 1 to 4094 usable |
52.5 Inter-VLAN routing
- A switch alone cannot pass traffic between VLAN 10 and VLAN 20: different VLAN = different subnet, which needs a Layer 3 device.
- Router on a stick: the router connects to the switch with one trunk cable and has one sub-interface (gateway) per VLAN.
- Downside: all inter-VLAN traffic crosses the same cable twice, so it can become a bottleneck.
- L3 switch: one virtual interface per VLAN (SVI); routing happens inside the switch in hardware.
- Step 1
1.Sales PC (192.168.10.11) pings HR PC (192.168.20.13)
- Step 2
2.Switch sends it up the trunk with tag 10
- Step 3
3.Router receives on eth0.10 (gateway 192.168.10.1) and routes it
- Step 4
4.Router sends it back down the same cable with tag 20 (eth0.20)
- Step 5
5.Switch removes the tag and delivers to HR PC; reply TTL = 63
TTL drops from 64 to 63 because one router was in the path
52.6 Practical: Linux bridge as a VLAN switch
Four namespace PCs in a Docker container; a Linux bridge plays the switch.
ip link add br0 type bridge vlan_filtering 1# access portsbridge vlan del dev p1 vid 1bridge vlan add dev p1 vid 10 pvid untaggedbridge vlan add dev p3 vid 20 pvid untagged# trunk portbridge vlan add dev p4 vid 10bridge vlan add dev p4 vid 20bridge vlan show$ bridge vlan showport vlan-idbr0 1 PVID Egress Untaggedp1 10 PVID Egress Untaggedp2 10 PVID Egress Untaggedp3 20 PVID Egress Untaggedp4 1 PVID Egress Untagged 10 20- vlan_filtering 1 turns on VLAN awareness on the bridge.
- p1 and p3 are access ports: pvid untagged = untagged frames join this VLAN, and the tag is removed on the way out.
- p4 is the trunk carrying VLAN 10 and 20 tagged; its native VLAN 1 stays untagged.
52.7 Practical: same VLAN vs different VLAN
ping -c 3 192.168.10.12ping -c 3 -W 1 192.168.20.13arping -c 2 -I eth0 192.168.10.99# on pc2 and pc3tcpdump -n -i eth0 arppc1$ ping -c 3 192.168.10.1264 bytes from 192.168.10.12: ttl=64 time=0.119 ms64 bytes from 192.168.10.12: ttl=64 time=0.081 ms3 packets transmitted, 3 received, 0% losspc1$ ping -c 3 -W 1 192.168.20.13PING 192.168.20.13 (192.168.20.13) 56(84) bytes3 packets transmitted, 0 received,100% packet loss, time 2024mspc2$ tcpdump -n -i eth0 arpARP, Request who-has 192.168.10.99 tell 192.168.10.11, length 28ARP, Request who-has 192.168.10.992 packets received by filterpc3$ tcpdump -n -i eth0 arp0 packets received by filter- pc2 in the same VLAN replies at once.
- pc3 in VLAN 20: 100% packet loss, because there is no router yet.
- The ARP broadcast from pc1 is seen by pc2 (VLAN 10) twice, and by pc3 (VLAN 20) zero times.
52.8 Practical: router on a stick
ip link add link eth0 name eth0.10 \ type vlan id 10ip link add link eth0 name eth0.20 \ type vlan id 20ip addr add 192.168.10.1/24 dev eth0.10ip addr add 192.168.20.1/24 dev eth0.20sysctl -w net.ipv4.ip_forward=1ip -d link show eth0.10rtr$ sysctl -w net.ipv4.ip_forward=1net.ipv4.ip_forward = 1rtr$ ip -d link show eth0.1010: eth0.10@eth0: <BROADCAST,MULTICAST,UP> mtu 1500 state UP vlan protocol 802.1Q id 10 <REORDER_HDR>rtr$ ip -br addreth0.10@eth0 UP 192.168.10.1/24eth0.20@eth0 UP 192.168.20.1/24- eth0.10 is a sub-interface on top of eth0 that handles tagged VLAN 10 frames; same for eth0.20.
- Each gets its gateway IP and IP forwarding is turned on.
- ip -d link confirms vlan protocol 802.1Q id 10.
52.9 Practical: tags on the trunk
# pc1: VLAN 10 to VLAN 20ping -c 2 192.168.20.13traceroute -n 192.168.20.13# switch: trunk p4, access p1tcpdump -e -n -i p4 icmptcpdump -e -n -i p1 icmppc1$ ping -c 2 192.168.20.1364 bytes from 192.168.20.13: ttl=6364 bytes from 192.168.20.13: ttl=632 packets transmitted, 2 received, 0% losspc1$ traceroute -n 192.168.20.13 1 192.168.10.1 0.014 ms 2 192.168.20.13 0.009 ms$ tcpdump -e -n -i p4 icmp802.1Q (0x8100), length 102: vlan 10, p 0 192.168.10.11 > 192.168.20.13: echo request802.1Q (0x8100), length 102: vlan 20, p 0 192.168.10.11 > 192.168.20.13: echo request802.1Q (0x8100), length 102: vlan 20, p 0 192.168.20.13 > 192.168.10.11: echo reply802.1Q (0x8100), length 102: vlan 10, p 0 192.168.20.13 > 192.168.10.11: echo reply$ tcpdump -e -n -i p1 icmpethertype IPv4 (0x0800), length 98: 192.168.10.11 > 192.168.20.13: echo requestethertype IPv4 (0x0800), length 98: 192.168.20.13 > 192.168.10.11: echo reply- TTL 63: a router routed the packet in between; traceroute's first hop is the VLAN 10 gateway 192.168.10.1.
- On trunk p4 the same packet appears twice: once with vlan 10, once with vlan 20 (length 102).
- On access port p1 the same packet has no tag and length 98: 4 bytes less, the size of the tag.
52.10 Common mistakes
- Myth: a VLAN is a complete security wall. It separates traffic, but without an ACL on the router one VLAN can reach another easily.
- Use firewall rules/ACLs along with VLANs.
- Leaving the native VLAN as VLAN 1 with users on it: opens the door to VLAN hopping.
- Native VLAN mismatch between the two ends of a trunk: untagged traffic leaks into the wrong VLAN.
- Expecting two VLANs on the same switch to ping each other without a router or L3 switch.
52.11 Interview tips
- Access port: one VLAN, untagged frames, for end devices like PCs and printers.
- Trunk port: many VLANs on one link, 802.1Q tag on every frame; only the native VLAN is untagged.
- 802.1Q tag = 4 bytes (TPID 0x8100, PCP, DEI, 12-bit VLAN ID: 1-4094).
- Inter-VLAN routing: router on a stick (sub-interfaces) or L3 switch SVIs.
- Each VLAN = its own broadcast domain and subnet.