Sub topic 10
Routing between VLANs
This chapter assumes you know Ethernet frames, how a switch learns MAC addresses, IPv4 subnetting, and basic router and switch command-line use. It also assumes you can already configure access and trunk ports.
Why VLANs need a router
A VLAN is a separate Layer 2 broadcast domain. A switch never forwards a frame from one VLAN into another, even when the two hosts plug into the same switch. A host in VLAN 10 that wants to reach a host in VLAN 20 must send its traffic to a Layer 3 device. That device routes the packet between the two subnets.
The usual design maps one VLAN to one IP subnet. VLAN 10 uses 192.168.10.0/24 and VLAN 20 uses 192.168.20.0/24. Each subnet needs a default gateway, which is an IP address on the Layer 3 device inside that subnet.
There are three common ways to build this:
| Method | How it works | Typical use |
|---|---|---|
| Router with one physical interface per VLAN | Each VLAN connects to its own router port by an access link | Very small or legacy networks, since it uses many ports |
| Router-on-a-stick | One router interface connects to a trunk and is split into subinterfaces | Small sites, labs, low traffic |
| Switched virtual interface (SVI) on a Layer 3 switch | The switch routes between VLANs in hardware | Most campus and data center access and distribution layers |
This chapter covers the second and third methods in detail.
How a packet crosses VLANs
Before the configuration, trace one packet. Host A (192.168.10.5, VLAN 10) pings host B (192.168.20.7, VLAN 20).
- Host A sees that 192.168.20.7 is outside its own /24. It decides to send the packet to its default gateway, 192.168.10.1.
- Host A uses ARP to find the gateway's MAC address, then sends an Ethernet frame to that MAC. The frame carries the destination IP of host B.
- The frame reaches the router in VLAN 10. The router removes the Ethernet header, looks up 192.168.20.7 in its routing table, and finds a connected route out of the VLAN 20 interface.
- The router ARPs for host B on VLAN 20, builds a new Ethernet frame with its own VLAN 20 MAC as the source, and sends it.
- Host B receives the packet.
The IP addresses stay the same from end to end. The Ethernet source and destination MAC addresses change at the router, and so does the VLAN the frame travels in.
Router-on-a-stick
In router-on-a-stick, one physical router interface connects to a switch trunk port. The router splits that interface into logical subinterfaces, one per VLAN. Each subinterface does two jobs: it matches frames tagged with one VLAN ID, and it holds the IP address that acts as the default gateway for that VLAN.
Switch side, the port facing the router is a normal trunk:
interface GigabitEthernet0/24
switchport mode trunk
switchport trunk native vlan 999
switchport trunk allowed vlan 10,20
switchport nonegotiateRouter side:
interface GigabitEthernet0/0
no shutdown
!
interface GigabitEthernet0/0.10
encapsulation dot1Q 10
ip address 192.168.10.1 255.255.255.0
!
interface GigabitEthernet0/0.20
encapsulation dot1Q 20
ip address 192.168.20.1 255.255.255.0Key points about this configuration:
- The physical interface has no IP address. It only needs
no shutdown. If it stays shut down, every subinterface is down too. encapsulation dot1Q 10tells the subinterface to accept frames tagged with VLAN 10 and to add that tag to frames it sends out.- The number after the dot in
Gi0/0.10is only a label. Many engineers match it to the VLAN ID for readability, but theencapsulationline is what counts. - Each subinterface creates a connected route automatically. No static routes are needed between the directly attached VLANs.
- The router does not need the VLANs created in a VLAN database. It only reads the tags.
Native VLAN on the router
In the switch example, the native VLAN is 999, and that VLAN is not in the allowed list, so no untagged traffic needs a subinterface. If you instead leave the native VLAN as 1 and want to route it, the router needs a subinterface with the native keyword, for example encapsulation dot1Q 1 native. Without it, untagged frames arriving on the trunk have nowhere to go. The native VLAN on both ends of the trunk must match.
Limits of router-on-a-stick
All inter-VLAN traffic enters and leaves through the same physical link, in both directions. The link carries each routed packet twice: once toward the router and once back. A 1 Gb/s link therefore limits routed throughput to well under 1 Gb/s per direction in practice, and the router's forwarding speed adds another limit. This is why the design suits small sites but not busy campus cores.
Switched virtual interfaces on a Layer 3 switch
A Layer 3 switch can route between VLANs itself, using dedicated hardware. It does this with a switched virtual interface (SVI), which is a virtual Layer 3 interface tied to one VLAN. The SVI IP address becomes the default gateway for hosts in that VLAN.
ip routing
!
vlan 10
name USERS
vlan 20
name SERVERS
!
interface Vlan10
ip address 192.168.10.1 255.255.255.0
no shutdown
!
interface Vlan20
ip address 192.168.20.1 255.255.255.0
no shutdownNotes on this configuration:
ip routingturns on the Layer 3 forwarding function. Without it, the switch has SVIs but does not route between them. This is a common mistake.- The VLAN must exist in the VLAN database. If it does not, the SVI stays down.
- An SVI comes up only when its VLAN is active and at least one port in that VLAN is up. That port can be an access port or a trunk that carries the VLAN. This is sometimes called the "line-protocol up" rule. Where you need an SVI to stay up regardless, platform-specific settings such as
autostate excludeexist. - Routed traffic between SVIs does not cross any physical link. The switch forwards it internally at hardware speed, which removes the bandwidth bottleneck of router-on-a-stick.
Layer 3 switches also offer routed ports. The command no switchport turns a physical port into a Layer 3 interface with its own IP address. Engineers use this to connect a Layer 3 switch to an upstream router or another Layer 3 switch without involving a VLAN.
Choosing between the methods
- Use router-on-a-stick when you have a router and a small amount of inter-VLAN traffic, or when you are learning in a lab.
- Use SVIs when you have a Layer 3 switch, or when routed traffic volume is high.
- Many networks combine them: SVIs route internally, and a routed port or trunk links to a router or firewall for traffic leaving the site.
Some designs send inter-VLAN traffic to a firewall on purpose, so the firewall can apply policy between VLANs. In that case, the firewall holds the gateway addresses, often on subinterfaces just like the router example, and the switches act only as Layer 2 devices.
Host configuration
A host needs an IP address in its VLAN's subnet, the correct subnet mask, and the VLAN's gateway as its default gateway. Most hosts sit on access ports and see no tags at all. A Linux server on a trunk port can create a tagged subinterface itself:
sudo ip link add link eth0 name eth0.10 type vlan id 10
sudo ip addr add 192.168.10.5/24 dev eth0.10
sudo ip link set eth0.10 upVerifying and troubleshooting inter-VLAN routing
Work from the bottom of the stack upward. Check the Layer 2 state first, then the Layer 3 state.
show vlan brief
show interfaces trunk
show interfaces GigabitEthernet0/24 switchportOn the router or Layer 3 switch, also check:
show ip interface brief
show ip route connectedCommon faults and what they look like:
| Symptom | Likely cause | What to check | Column |
|---|---|---|---|
| Host reaches its own VLAN but not the gateway | Wrong access VLAN on the host port, or the VLAN missing from the trunk's allowed list | `show vlan brief`, `show interfaces trunk` | |
| Subinterface is up but no traffic flows | Wrong VLAN ID in `encapsulation dot1Q` | Compare with the VLAN ID on the switch | |
| All subinterfaces are down | Physical interface is shut down, or the trunk is down | `show ip interface brief` | |
| SVI is down/down | VLAN does not exist, or no active port carries it | `show vlan brief`, `show interfaces trunk` | |
| Hosts reach the gateway but not other VLANs | `ip routing` missing on the Layer 3 switch | `show running-config \ | include ip routing` |
| Pings work in one direction only | Host has the wrong default gateway or wrong mask | Host IP settings | |
| Untagged traffic lost on the router trunk | Native VLAN mismatch or no `native` subinterface | `show interfaces trunk` on both ends |
A short test sequence helps: ping the host's own gateway, then the gateway of the other VLAN, then a host in the other VLAN. The point where the ping first fails tells you which layer to inspect.
You can also build a tagged test frame with Scapy on a lab network to see how the tag looks on the wire:
from scapy.all import Ether, Dot1Q, IP, ICMP
frame = (
Ether(dst="ff:ff:ff:ff:ff:ff")
/ Dot1Q(vlan=10, prio=0)
/ IP(dst="192.168.10.255")
/ ICMP()
)
frame.show()Exercise
A Layer 3 switch has VLAN 30 configured with interface Vlan30 and the address 10.0.30.1/24. Hosts on VLAN 30 can ping 10.0.30.1, but they cannot reach hosts in VLAN 40, which has a working SVI. Name the first command you would check for, and one other reason the routing might fail.
Show answer
First check that ip routing is enabled, for example with show running-config | include ip routing. Without it, the switch has SVIs but does not route between them. Another cause is a host with the wrong default gateway. An access list on one of the SVIs that blocks the traffic could also be the reason.
Exercise
A router has interface GigabitEthernet0/0.20 with encapsulation dot1Q 200 and the address 192.168.20.1/24. The switch trunk allows VLAN 20, and hosts in VLAN 20 cannot reach the gateway. What is wrong and how do you fix it?
Show answer
The subinterface expects frames tagged VLAN 200, but the hosts are in VLAN 20, so the router ignores their frames. Change the line to encapsulation dot1Q 20. The number in the subinterface name does not matter. Only the encapsulation value counts.