6. VLANs

One physical switch, several separate networks, and the four bytes that keep them apart

Remember the receptionist from the Ethernet article? She sits in the middle of an open-plan office with a notepad, learns which desk each voice comes from, and walks messages straight to the right person. When she doesn't know where someone sits, she shouts the message to the whole room.

Now the office is shared by three tenants: your own team, a company that makes smart light bulbs, and a drop-in coworking desk for visitors. You don't want visitors overhearing your team's shouted messages, and you certainly don't want the light-bulb people's gadgets reading your files. Building walls would mean three rooms, three receptionists and a lot of moving desks around every time somebody changes team.

So instead you hand out coloured lanyards: teal for your team, orange for the light-bulb company, purple for visitors. The receptionist keeps one notepad per colour, and when she has to shout, she shouts only to people wearing the same colour as the sender. When a message has to go to the receptionist on the floor below, she clips a small coloured tag to the envelope so her colleague knows which group it belongs to. Nothing moved, no walls went up, and yet there are now three separate offices.

That is a VLAN: a virtual LAN, one physical network carved into several logical ones by nothing more than switch configuration and a 4-byte tag. At the end of the IPv6 article, our home network had plenty of addresses but only one LAN: one broadcast domain, one set of Router Advertisements, and nothing stopping a guest's phone from talking to the NAS. This article fixes that.

One Big Room: Why Flat Networks Hurt

A switch gives every port its own collision domain, but as we saw in the Ethernet article, it does nothing about broadcasts. Every frame to ff:ff:ff:ff:ff:ff is flooded out of every port, and so is every frame to an address the switch hasn't learned yet. The whole switched network is one broadcast domain. On a four-device home LAN that's harmless. As a network grows, three problems grow with it.

Broadcast load. A surprising amount of everyday traffic is broadcast or multicast: every ARP request ("who has 192.168.1.1?"), every DHCP discover, every mDNS announcement from the printer, and on a switch without multicast snooping, every IPv6 Neighbor Solicitation and Router Advertisement too. Each one interrupts every host on the LAN, which has to look at it and throw it away. The cost grows with the square of the network's size:

If each of NN hosts sends rr broadcasts per second, every host receives (N−1) r(N-1)\,r of them, and the network as a whole delivers N(N−1) rN(N-1)\,r copies per second.

A thousand hosts on one LAN

Suppose each host sends a modest one broadcast every ten seconds (r = 0.1). With N = 1,000 on one flat network, each host has to process 999 × 0.1 ≈ 100 broadcasts per second that are almost never for it, and the switches deliver about 99,900 copies per second. Split the same thousand hosts into ten VLANs of 100 and each host sees 99 × 0.1 ≈ 10 per second; the total falls to 9,900. Ten times fewer, without changing a single cable.

Security. The ARP article showed that anybody on the LAN can poison caches and sit in the middle of other people's traffic, and that the attack "only works from inside the broadcast domain". In a flat home network, inside includes the guest's phone, the cheap smart plug with firmware from 2019, and the TV that phones home every five minutes. Everything can scan everything else, and the router never gets a chance to say no, because traffic between two devices on the same LAN goes through the switch and never touches the router at all.

Policy. For the same reason, there is nowhere to put rules. "Guests may reach the Internet but nothing else" and "the printer may receive print jobs but never start a connection" are both firewall rules, and a firewall can only filter traffic that passes through it.

The classic fix is physical: one switch per group, each cabled to its own router port. That works, and it's what networks looked like in the early 1990s, but it means buying switches by the group instead of by the port, and recabling every time someone changes department. VLANs do the same job in configuration.

What a VLAN Is

A VLAN is a broadcast domain defined by switch configuration instead of by cabling. You tell the switch which ports belong to which VLAN, and from then on it behaves as if it were several separate switches that happen to share a box and a power supply.

What you plug into How it behaves Laptop NAS Printer TV Guest Phone 1 2 3 4 5 6 One switch, three VLANs VLAN 10 VLAN 20 VLAN 30 A broadcast from the printer (port 3) reaches the TV (port 4) and nobody else. VLAN 10 switch Laptop NAS VLAN 20 switch Printer TV VLAN 30 switch Guest Phone No wire between them: frames never cross from one VLAN to another.

The rule that makes this work fits in one sentence:

The VLAN rule

A frame that enters the switch in VLAN v can only ever leave through ports that are members of VLAN v. Flooding, forwarding and learning all happen inside one VLAN at a time.

There is no exception for broadcasts, and no exception for "but I know where that MAC address is". As far as Layer 2 is concerned, VLAN 10 and VLAN 20 are on different planets. The only way across is up through Layer 3, via a router, which is exactly where we want the firewall to be.

VLAN IDs

Each VLAN is identified by a number, the VLAN ID or VID. It's a 12-bit field (we'll see where it lives shortly), so there are 212 = 4,096 possible values. Two are reserved, leaving 4,094 usable VLANs:

VID Hex Meaning
0 000 No VLAN: a "priority tag", which carries only a priority value (more on that below)
1 001 The default VLAN. Out of the box, every port on a managed switch is in VLAN 1
2–4094 002–ffe Yours to use
4095 fff Reserved for implementations; never configured or sent on the wire

VLAN 1 is special only by convention, but that convention is strong. A new switch is a plain, flat switch precisely because every port starts in VLAN 1. Some vendors reserve a few more numbers for their own use; Cisco, for example, keeps 1002–1005 for historical reasons.

Our home network, split three ways

Let's put this to work. The house gets three VLANs, each carved from the address plans of the previous two articles: the IPv4 subnets come from 192.168.0.0/16, and the IPv6 subnets from the house's 2001:db8:1234::/56.

VLAN Purpose IPv4 subnet IPv6 subnet
10 Trusted: laptops, desktop, NAS 192.168.10.0/24 2001:db8:1234:10::/64
20 IoT: printer, TV 192.168.20.0/24 2001:db8:1234:20::/64
30 Guests 192.168.30.0/24 2001:db8:1234:30::/64

Yes, everyone gets renumbered

From here on, the old flat 192.168.1.0/24 and 2001:db8:1234:1::/64 are retired. Every device keeps its MAC address and its last octet, but moves into its VLAN's subnet: the laptop becomes 192.168.10.10, the NAS 192.168.10.30, and the printer 192.168.20.20. The router keeps its MAC, 02:00:00:cc:dd:02, but now has three addresses, one per VLAN: 192.168.10.1, 192.168.20.1 and 192.168.30.1. (We could have kept the old subnet as VLAN 1, but as you'll see in the security section, putting real devices in VLAN 1 is a bad habit.)

The house has also grown a second switch in the study, so we meet three new devices. All the MACs are locally administered, like the rest of the series:

Device MAC IPv4 VLAN
Laptop 02:00:00:aa:bb:01 192.168.10.10 10
NAS 02:00:00:aa:bb:04 192.168.10.30 10
Desktop (new) 02:00:00:aa:bb:05 192.168.10.40 10
Printer 02:00:00:aa:bb:03 192.168.20.20 20
TV (new) 02:00:00:aa:bb:06 192.168.20.50 20
Guest laptop (new) 02:00:00:aa:bb:07 192.168.30.100 30
Router 02:00:00:cc:dd:02 .1 in each VLAN all three

The 10, 20 and 30 in the IPv6 prefixes are chosen to look like the VLAN numbers, which makes life easier when you're reading logs at 2 a.m. They're hex, though: subnet 10 is subnet 16 out of the 256 that a /56 allows. Nothing in the protocols links the two numbers; it's purely a naming convention.

Access Ports and Trunk Ports

If everything lived on one switch, port membership would be the whole story. But our house has two switches, a router, and devices from all three VLANs on both switches. How does switch B know which VLAN a frame from switch A belongs to?

The answer is that switch ports come in two kinds:

Access port

Belongs to exactly one VLAN. Frames go in and out untagged, as ordinary Ethernet frames. The device on the other end (a laptop, a printer) has no idea VLANs exist: the switch assigns every frame it receives on the port to the port's VLAN. This is what you plug end devices into.

Trunk port

Carries many VLANs over one cable. Each frame gets a 4-byte tag saying which VLAN it belongs to, added on the way out and read (and removed) on the way in. This is what you use between switches, and between a switch and a router or a hypervisor that understands tags.

Here is the house wired up. One trunk joins the two switches; another joins switch A to the router.

Router …cc:dd:02 .10.1 · .20.1 · .30.1 trunk: 10, 20, 30 Switch A 7 8 Switch B 8 trunk 10, 20, 30 1 2 3 1 2 3 Laptop 192.168.10.10 …aa:bb:01 NAS 192.168.10.30 …aa:bb:04 Printer 192.168.20.20 …aa:bb:03 Desktop 192.168.10.40 …aa:bb:05 TV 192.168.20.50 …aa:bb:06 Guest 192.168.30.100 …aa:bb:07 VLAN 10 trusted VLAN 20 IoT VLAN 30 guest trunk (tagged) Coloured lines are access ports (untagged). Thick black lines are trunks.

Without trunks, connecting the two switches would take one cable per VLAN (and one router port per VLAN, too). With a trunk, one cable carries all three, and the tag on each frame tells the far end which virtual switch to put it in. Before 802.1Q was standardized, Cisco solved the same problem with its proprietary ISL, which wrapped each frame in a whole new header. The IEEE's design was leaner: squeeze four bytes into the frame that's already there.

The 802.1Q Tag

IEEE 802.1Q, approved in December 1998, defines how VLANs work on Ethernet, and the heart of it is a 4-byte tag inserted into the frame right after the source MAC address, exactly where the EtherType used to start.

Untagged Dst MAC 6 Src MAC 6 Type 2 Payload 46 – 1500 FCS 4 max 1518 bytes insert 4 bytes here Tagged Dst MAC 6 Src MAC 6 TPID 2 TCI 2 Type 2 Payload (unchanged) 42 – 1500 FCS new max 1522 TPID = 0x8100 1000 0001 0000 0000 PCP 000 0 VID = 10 0000 0000 1010 16 bits 3 bits DEI 1 bit 12 bits "a tag follows" TCI: Tag Control Information The tag for VLAN 10 at normal priority: 81 00 00 0a
Field Bits What it does
TPID 16 Tag Protocol Identifier. Always 0x8100 for an 802.1Q tag
PCP 3 Priority Code Point: 0–7, a class of service for queueing
DEI 1 Drop Eligible Indicator: 1 = "drop me first if the link is congested"
VID 12 VLAN ID, 0–4095

The two bytes after the TPID are together called the TCI, the Tag Control Information.

Why the TPID sits where the EtherType was

Putting 0x8100 exactly where the EtherType used to be is the clever part. A receiver parses a frame by reading bytes 12–13. If they're 0800, an IPv4 packet starts at byte 14. If they're 8100, the receiver knows a tag is in the way: bytes 14–15 are the TCI, and the real EtherType is at bytes 16–17. Parsing stays a simple chain of "what's next?" lookups, just like the header-to-header chain in the Layers article.

It also means a device that has never heard of VLANs isn't confused, merely unhelpful. It sees an EtherType it doesn't support, 8100, and discards the frame, just as it would a protocol it doesn't speak. That's why access ports must strip tags before handing frames to ordinary hosts, and why the whole scheme could be deployed one switch at a time.

Building a tag by hand

The TCI packs three fields into 16 bits, with the priority at the top:

TCI=8192⋅PCP+4096⋅DEI+VID\text{TCI} = 8192 \cdot \text{PCP} + 4096 \cdot \text{DEI} + \text{VID}

That is, PCP shifted left 13 bits, DEI shifted left 12, and VID in the bottom 12.

  1. Encode: VLAN 10, ordinary priority

    PCP 0 is 000, DEI 0 is 0, and VID 10 in 12 bits is 0000 0000 1010. Concatenated: 0000 0000 0000 1010 = 0x000a. Prepend the TPID and the tag is 81 00 00 0a. This is the tag our laptop's frames get on the trunk.

  2. Decode: 81 00 30 1e

    The TPID is 8100, so it's an 802.1Q tag. TCI 0x301e = 0011 0000 0001 1110. The top three bits, 001, are PCP 1. The next bit, 1, is DEI. The last twelve, 0000 0001 1110, are VID 30. Read it as: guest VLAN, background priority, drop first under congestion, a sensible marking for guest traffic.

  3. Spot the oddballs

    81 00 a0 00: PCP 5, VID 0. A priority tag: the frame belongs to no VLAN in particular, and the switch puts it in the receiving port's own VLAN while still honouring the priority. 81 00 0f ff: VID 4095, which is reserved and must never appear on the wire; a switch drops it.

PCP: priorities, briefly

The PCP field (originally specified as IEEE 802.1p, which is why you'll often hear "802.1p priority") lets a switch put urgent frames at the front of its output queues. The standard's recommended traffic types are:

PCP Code Traffic type Rank
1 BK Background 0 (lowest)
0 BE Best effort 1 (default)
2 EE Excellent effort 2
3 CA Critical applications 3
4 VI Video, under 100 ms latency and jitter 4
5 VO Voice, under 10 ms latency and jitter 5
6 IC Internetwork control 6
7 NC Network control 7 (highest)

Note the one trap in that table: PCP 1 ranks below PCP 0. Zero is "best effort", what every untagged frame gets by default; one is "background", for bulk transfers that should yield to everything else. Whether a switch honours any of this depends on its configuration, and most home networks never touch it, but IP phones routinely mark voice frames with PCP 5.

Frame sizes, revisited

The tag adds four bytes without taking any away, so the payload limit doesn't change and the maximum frame grows from 1518 to 1522 bytes, the "Max with 802.1Q tag" row from the Ethernet article. IEEE 802.3ac (1998) blessed that size. The minimum frame stays 64 bytes, so a tagged frame needs only 42 bytes of payload before padding kicks in. One consequence: a switch that strips the tag from a minimum-size frame must pad it back up to 64 bytes.

The tagged frame, byte by byte

Back to our running example: the laptop's GET / HTTP/1.1 request to the web server at 203.0.113.80, first decoded in the Layers article. Since the laptop has moved to 192.168.10.10, its frame has changed in a few places even before any VLAN tag appears: the source IP (bytes 26–29) is now c0 a8 0a 0a, which changes the IPv4 header checksum from 20 62 to 17 62, the TCP checksum (it covers the IP addresses through its pseudo-header) from 81 1c to 78 1c, and therefore the FCS. The destination MAC is still the router's, because the router is still the laptop's gateway.

This is the frame as it leaves the laptop and enters access port 1 of switch A, untagged:

Untagged, on the access port: 95 bytes

0000  02 00 00 cc dd 02 02 00 00 aa bb 01 08 00 45 00  ..............E.
0010  00 4d 1c 46 40 00 40 06 17 62 c0 a8 0a 0a cb 00  .M.F@[email protected]......
0020  71 50 c7 38 00 50 1a 2b 3c 4d 5e 6f 70 81 50 18  qP.8.P.+<M^op.P.
0030  fa f0 78 1c 00 00 47 45 54 20 2f 20 48 54 54 50  ..x...GET / HTTP
0040  2f 31 2e 31 0d 0a 48 6f 73 74 3a 20 65 78 61 6d  /1.1..Host: exam
0050  70 6c 65 2e 63 6f 6d 0d 0a 0d 0a dd d2 fc 0a     ple.com........

 Ethernet header  bytes 0–13 ·  FCS  bytes 91–94: CRC-32 0x0AFCD2DD, sent least-significant byte first. The unhighlighted bytes are the 77-byte IPv4 packet.

The router lives behind switch A's trunk port 7. Whether the switch already knows that and forwards the frame straight there, or doesn't yet and floods it (we'll watch both happen shortly), the copy that goes out of port 7 gets the VLAN 10 tag and a recomputed FCS:

Tagged, on the trunk to the router: 99 bytes

0000  02 00 00 cc dd 02 02 00 00 aa bb 01 81 00 00 0a  ................
0010  08 00 45 00 00 4d 1c 46 40 00 40 06 17 62 c0 a8  ..E..M.F@[email protected]..
0020  0a 0a cb 00 71 50 c7 38 00 50 1a 2b 3c 4d 5e 6f  ....qP.8.P.+<M^o
0030  70 81 50 18 fa f0 78 1c 00 00 47 45 54 20 2f 20  p.P...x...GET / 
0040  48 54 54 50 2f 31 2e 31 0d 0a 48 6f 73 74 3a 20  HTTP/1.1..Host: 
0050  65 78 61 6d 70 6c 65 2e 63 6f 6d 0d 0a 0d 0a 20  example.com.... 
0060  64 b0 02                                         d..

 Ethernet  bytes 0–11 and 16–17 ·  802.1Q tag  bytes 12–15 ·  new FCS  bytes 95–98: CRC-32 0x02B06420.

Compare the two dumps. The MAC addresses haven't moved. The four tag bytes sit at offsets 12–15, which pushes the original EtherType 08 00 to offsets 16–17 and every byte of the IP packet four places to the right, completely unchanged: the IPv4 checksum is still 17 62, and the TCP checksum is still 78 1c. Only the FCS is different, because a CRC covers every byte from the destination MAC to the end of the payload, and four new bytes in the middle change the result completely.

Tagging is not free for the switch

Every time a switch adds or removes a tag, it must recompute the CRC-32. A frame that enters on an access port and leaves on a trunk gets a new FCS; so does one going the other way. Switch hardware does this at line rate as the bytes stream out. A store-and-forward switch checks the old FCS on the way in, so a frame corrupted on the first link is still caught there rather than being blessed with a fresh, valid-looking checksum.

In pseudocode, the two operations are mirror images:

TPID = 0x8100

function insert_tag(frame, pcp, dei, vid):
    body = frame[0 : len(frame) - 4]                 # drop the old FCS
    tci  = (pcp << 13) | (dei << 12) | vid
    tag  = u16_be(TPID) + u16_be(tci)                # 4 bytes
    body = body[0:12] + tag + body[12:]              # after the source MAC
    return body + u32_le(crc32(body))                # new FCS, low byte first

function strip_tag(frame):
    if u16_be(frame[12:14]) != TPID:
        return (frame, None)                         # untagged already
    tci  = u16_be(frame[14:16])
    info = (tci >> 13, (tci >> 12) & 1, tci & 0x0fff)    # PCP, DEI, VID
    body = frame[0:12] + frame[16 : len(frame) - 4]
    if len(body) < 60:
        body = body + zeros(60 - len(body))          # keep the 64-byte minimum
    return (body + u32_le(crc32(body)), info)

Build a tag

The example frame, before and after

Untagged, on the access port


  

Tagged, on the trunk


The Native VLAN

There's one wrinkle in the trunk story. What should a trunk port do with a frame that arrives without a tag?

802.1Q's answer is that every trunk port has one VLAN it treats as untagged, usually called the native VLAN (the standard's term is the port's PVID). Untagged frames arriving on the trunk are placed in the native VLAN, and frames in the native VLAN leave the trunk without a tag. By default the native VLAN is VLAN 1.

It exists for compatibility. In the 1990s a trunk link might pass through a hub or an old switch that didn't understand tags, and devices on that segment still needed to talk. Today it's mostly used for switch management traffic and for the odd device, like a wireless access point, that wants its own traffic untagged and its clients' traffic tagged.

The trouble starts when the two ends disagree. If switch A's trunk has native VLAN 1 and switch B's has native VLAN 99, an untagged frame that A sends from VLAN 1 lands in VLAN 99 at B. The two VLANs have silently merged across that link. Cisco switches detect this mismatch with their discovery protocol and complain loudly in the log, and for good reason: untagged traffic on a trunk is also the opening for one of the VLAN hopping attacks below.

One MAC Table per VLAN

The learning switch from the Ethernet article learned from source addresses and forwarded on destinations, using a table of MAC → port. A VLAN-aware switch uses exactly the same two rules, with one change: the table key becomes the pair (VLAN, MAC). Each VLAN effectively gets its own table, and flooding goes only to ports that are members of the frame's VLAN.

table = {}                                   # (vlan, mac) -> (port, last_seen)

function on_frame(raw, in_port, now):
    (vlan, frame) = classify(raw, in_port)   # which VLAN? (and strip any tag)
    if vlan is None: return                  # not allowed here: drop

    # 1. Learn from the source, inside this VLAN only
    if not is_group(frame.src):
        table[(vlan, frame.src)] = (in_port, now)

    # 2. Forward by the destination, inside this VLAN only
    members = ports_in_vlan(vlan) - {in_port}
    if is_group(frame.dst) or (vlan, frame.dst) not in table:
        for p in members: egress(frame, vlan, p)       # flood within the VLAN
    else:
        out = table[(vlan, frame.dst)].port
        if out != in_port: egress(frame, vlan, out)

function classify(raw, port):
    (frame, tag) = strip_tag(raw)            # removes the outer tag only
    if port.mode == ACCESS:
        if tag is None or tag.vid == 0: return (port.access_vlan, frame)
        if tag.vid == port.access_vlan:      # hosts shouldn't tag, but many switches
            return (port.access_vlan, frame) # accept their own VLAN (see "VLAN hopping")
        return (None, frame)                 # any other tag on an access port: drop
    # trunk port
    vlan = port.native_vlan if (tag is None or tag.vid == 0) else tag.vid
    if vlan not in port.allowed_vlans: return (None, frame)
    return (vlan, frame)

function egress(frame, vlan, port):
    if port.mode == ACCESS:
        send(port, frame)                    # untagged
    else if vlan == port.native_vlan and not port.tag_native:
        send(port, frame)                    # native VLAN: untagged on the trunk
    else:
        send(port, insert_tag(frame, pcp_of(frame), 0, vlan))

Some cheap switches use one shared table for all VLANs instead, which the standard calls shared learning. It works as long as no MAC address appears in two VLANs at once, an assumption our router is about to break.

A worked example

Here is the house from the topology diagram, with both switches freshly powered on and their tables empty. Every frame below can be replayed in the simulator further down.

  1. Printer broadcasts an ARP request (in on A3, VLAN 20)

    "Who has 192.168.20.1?" goes to ff:ff:ff:ff:ff:ff. Switch A learns (20, …aa:bb:03) → port 3. It's a broadcast, so it floods within VLAN 20: out port 7 to the router and port 8 to switch B, both tagged 81 00 00 14 (VID 20). Ports 1 and 2 are in VLAN 10, so the laptop and NAS never see it. Switch B receives it on trunk port 8, reads VID 20, learns (20, …aa:bb:03) → port 8, strips the tag and floods it out its only other VLAN 20 port, port 2. The TV checks the target IP, sees it isn't 192.168.20.50, and ignores it. The desktop and the guest never knew.

  2. Router replies (in on A7, tagged VLAN 20)

    The router's VLAN 20 interface answers "192.168.20.1 is at 02:00:00:cc:dd:02", unicast to the printer. Switch A learns (20, …cc:dd:02) → port 7, finds (20, …aa:bb:03) on port 3, strips the tag and sends it out port 3 only. Switch B sees nothing.

  3. Laptop → Desktop (in on A1, VLAN 10)

    The laptop already has the desktop's MAC in its ARP cache, but the switches have never heard from the desktop. Switch A learns (10, …aa:bb:01) → port 1. The destination (10, …aa:bb:05) is unknown, so it floods within VLAN 10: port 2 (the NAS discards it), port 7 tagged VID 10 (the router discards it: not its MAC) and port 8 tagged VID 10. Switch B learns (10, …aa:bb:01) → port 8 and floods to its only other VLAN 10 port, port 1. The desktop accepts it.

  4. Desktop → Laptop (in on B1, VLAN 10)

    Switch B learns (10, …aa:bb:05) → port 1, finds the laptop on port 8, and sends it up the trunk tagged VID 10. Switch A learns (10, …aa:bb:05) → port 8 and forwards it out port 1, untagged. From now on these two talk without flooding.

After four frames, the tables look like this. Notice that switch B has learned the printer even though it has never sent the printer anything: the broadcast in step 1 taught it.

Switch VLAN MAC Port
A 10 02:00:00:aa:bb:01 1
A 10 02:00:00:aa:bb:05 8
A 20 02:00:00:aa:bb:03 3
A 20 02:00:00:cc:dd:02 7
B 10 02:00:00:aa:bb:01 8
B 10 02:00:00:aa:bb:05 1
B 20 02:00:00:aa:bb:03 8

Getting Between VLANs

The laptop wants to print. The printer is 192.168.20.20; the laptop is 192.168.10.10/24. Applying the "is it local?" test from the IPv4 Addressing article, 192.168.20.0 isn't 192.168.10.0, so the laptop does exactly what it does for a web server on the other side of the world: it sends the packet to its default gateway, 192.168.10.1. Even if the two VLANs shared a subnet, it wouldn't help: the laptop's ARP broadcast would never leave VLAN 10. Traffic between VLANs has to be routed.

Router on a stick

Our router has one Ethernet cable to the house, plugged into trunk port 7 of switch A. It handles three VLANs over that one cable using subinterfaces: virtual interfaces, one per VLAN, each with its own IP addresses, all sharing the physical port and its MAC address. Frames tagged VID 10 arriving on the cable are handed to the VLAN 10 subinterface; anything the router sends out of the VLAN 20 subinterface gets tagged VID 20. This arrangement has a wonderfully literal name: router on a stick.

On a Linux router, the configuration is just a few lines per VLAN:

Linux: one subinterface per VLAN

ip link add link eth0 name eth0.10 type vlan id 10
ip addr add 192.168.10.1/24 dev eth0.10
ip addr add 2001:db8:1234:10::1/64 dev eth0.10
ip link set eth0.10 up
# ...and the same for eth0.20 and eth0.30
sysctl -w net.ipv4.ip_forward=1    # route between them

The kernel adds and strips the tags itself (or offloads it to the network card). Cisco routers express the same idea as interface GigabitEthernet0/0.10 with encapsulation dot1Q 10.

Now watch the print job. It crosses the trunk to the router twice, once in each direction:

Router …cc:dd:02 eth0.10 192.168.10.1 eth0.20 192.168.20.1 routes 2 · tagged VID 10 dst …cc:dd:02 (router) 3 · tagged VID 20 src …cc:dd:02, dst …aa:bb:03 port 7 (trunk) Switch A port 1 port 3 Laptop 192.168.10.10 1 · untagged to the gateway's MAC Printer 192.168.20.20 4 · untagged tag stripped

Continuing the worked example from the same tables. These are frames 5 to 8 of the simulator's replay further down. The numbered arrows in the diagram are hops of a single packet, not separate frames.

  1. Laptop → gateway (in on A1, VLAN 10)

    The frame's destination is the router's MAC, …cc:dd:02. Switch A already knows that address, but only in VLAN 20 (from frame 2 of the worked example). It looks up (10, …cc:dd:02), finds nothing, and floods within VLAN 10: the NAS discards a copy, switch B passes one to the desktop, which also discards it, and the router receives it tagged VID 10 on port 7. A switch with one shared table would have sent it straight to port 7; ours has to learn the router separately in each VLAN.

  2. Router → Printer (in on A7, tagged VLAN 20)

    The router strips the Ethernet header, decrements the TTL, looks up 192.168.20.20, finds it directly connected on eth0.20, and sends a brand-new frame from …cc:dd:02 to …aa:bb:03 (it learned the printer's MAC from the ARP request in frame 1 of the worked example). Switch A refreshes (20, …cc:dd:02) → port 7, finds the printer on port 3, strips the tag and delivers it.

  3. Printer → Laptop (the reply, the same trip in reverse)

    The printer sends to its gateway 192.168.20.1, MAC …cc:dd:02: known in VLAN 20 on port 7, so no flooding. The router routes it out eth0.10, tagged VID 10, to …aa:bb:01. Switch A now learns (10, …cc:dd:02) → port 7 and forwards to port 1.

Switch A's table now has the same MAC address twice, and that's perfectly fine:

VLAN MAC Port
10 02:00:00:aa:bb:01 1
10 02:00:00:aa:bb:05 8
10 02:00:00:cc:dd:02 7
20 02:00:00:aa:bb:03 3
20 02:00:00:cc:dd:02 7

Router on a stick is cheap and simple, and it's how most home and small-office setups work. Its weakness is that one link. Every frame that goes between VLANs crosses the trunk twice, up and back, and shares that link with all of the house's Internet traffic too. For a home with a gigabit trunk, that's rarely noticeable. For an office moving backups from one VLAN to another, it's a bottleneck.

The layer-3 switch

The alternative is to move the routing into the switch. A layer-3 switch (or "multilayer switch") has, for each VLAN, a virtual routed interface called an SVI, which acts as that VLAN's default gateway. A packet from VLAN 10 to VLAN 20 enters the switch, is routed by the same forwarding chips that do the switching, and leaves on the printer's port without ever touching a cable to anywhere else.

Router on a stick

One trunk to a separate router with a subinterface per VLAN. Inter-VLAN traffic crosses the trunk twice. Cheap, and the router is a natural place for a stateful firewall. Typical of homes and small offices.

Layer-3 switch with SVIs

The switch routes between VLANs itself, in hardware, at full port speed. Something like interface Vlan10 with ip address 192.168.10.1 255.255.255.0 creates the gateway. Typical of campus and data-centre networks, often with simpler access lists instead of a full firewall.

You probably own a VLAN switch already

Open up a typical home router and you'll often find a small switch chip with one or two internal links to the main processor. The "WAN" and "LAN" ports on the back aren't wired to separate chips; the switch chip keeps them apart with VLANs, and the processor routes between them over a trunk. Router firmware such as OpenWrt exposes exactly this, which is why "add a guest network" is often a configuration change rather than a hardware one.

The router is now the checkpoint

Because nothing crosses between VLANs except through the router, the router's firewall decides what's allowed. A sensible starting policy for the house:

From To Allowed?
Trusted (10) Internet, IoT (20) Yes: browse, print, cast to the TV
IoT (20) Trusted (10) Only replies to connections Trusted started
IoT (20) Internet Yes, or only to the vendors' update servers
Guest (30) Internet only Yes, and nothing on 10 or 20

That is the payoff for all this work: the guest's phone can't scan the NAS, the smart TV can't wander into the laptop, and a compromised light bulb is stuck on its own island.

Send a frame

MAC address tables

Switch A

VLANMACPortAge

Switch B

VLANMACPortAge

Port configuration

Pick a port here or tap one on the diagram. Changing a port’s VLANs flushes the table entries learned on it.

Frame log

One VLAN, One Subnet

You may have noticed that every VLAN in our plan has exactly one IPv4 subnet and one IPv6 /64. That's not a coincidence; it's the near-universal rule of thumb.

VLAN ≈ subnet

A VLAN is a Layer 2 broadcast domain; a subnet is a Layer 3 idea of "addresses I can reach directly". Hosts assume that everything in their subnet is reachable by ARP or Neighbor Discovery, which only works inside one broadcast domain. So map them one to one: one VLAN, one IPv4 subnet, one IPv6 /64, and one gateway address in each.

Break the rule one way, with one subnet spread over two VLANs, and things fail outright: the laptop thinks the printer is local, ARPs for it, and the broadcast never reaches the other VLAN. Break it the other way, with two subnets on one VLAN, and everything works but nothing is isolated: hosts in "different" subnets still hear each other's broadcasts, and anyone can simply give themselves an address in the other range.

For IPv6, each of the router's subinterfaces sends its own Router Advertisements with its own prefix, so SLAAC from the IPv6 article just works per VLAN. The laptop hears only the VLAN 10 advertisement and builds 2001:db8:1234:10:0:ff:feaa:bb01 (plus a temporary address), while the printer builds 2001:db8:1234:20:0:ff:feaa:bb03. Link-local addresses don't change at all: the router answers as fe80::ff:fecc:dd02 on every subinterface, which is fine, because a link-local address only has to be unique on its own link, and each VLAN is a separate link.

Two practical consequences are worth knowing before you split your own network:

  • DHCP needs a scope per VLAN. A DHCP discover is a broadcast, so it stays in its VLAN. The router serves a separate address pool on each subinterface; a layer-3 switch instead uses a DHCP relay that forwards requests to a central server. The DHCP article covers both.
  • Discovery protocols stop at the border. mDNS, the way laptops find printers and phones find TVs, uses link-local multicast that never crosses a router. Put the printer in VLAN 20 and it vanishes from the laptop's printer list until you either add it by IP or run an mDNS reflector on the router (Avahi has one), which deliberately copies announcements between chosen VLANs.

VLAN Hopping

VLANs are a genuine security boundary, but only if the switches are configured so that a host can't choose its own VLAN. Two classic misconfigurations let it do exactly that, and both are called VLAN hopping. We'll stay at the level of how they work and how to stop them.

Switch spoofing. Many Cisco switches support the Dynamic Trunking Protocol, which lets two neighbouring switches agree to form a trunk automatically. On a port left in one of DTP's dynamic modes, nothing checks that the thing on the other end is actually a switch. A device that speaks DTP can ask for a trunk and get one, and since a trunk carries every allowed VLAN, by default all of them, it now receives flooded and broadcast traffic from every VLAN and can send tagged frames into any of them. Its access-port VLAN assignment no longer means anything.

Double tagging. This one abuses the native VLAN. Suppose the attacker's access port is in VLAN 1, and the trunk between the switches also has native VLAN 1, the default for both. The attacker sends a frame carrying two tags: an outer one for VLAN 1 and an inner one for the target, VLAN 20. Switch A, like many switches, accepts a tag that matches the port's own VLAN: it puts the frame in VLAN 1 and removes that outer tag, never looking at what's behind it. Then it sends the frame over the trunk. Native-VLAN frames go out untagged, so switch A adds nothing, and the frame leaves carrying only the inner tag. Switch B sees an ordinary VLAN 20 tag, and delivers the frame into VLAN 20.

Switch spoofing: the port is talked into becoming a trunk Rogue device pretends to be a switch Switch "let's be a trunk" port in a dynamic mode becomes a trunk VLAN 10 VLAN 20 VLAN 30 Every allowed VLAN now reaches the rogue device, tagged. Defence: hard-code edge ports as access ports and turn trunk negotiation off. Double tagging: the native VLAN peels off the outer tag Switch A Switch B trunk, native VLAN 1 (untagged) 20 payload Attacker access port, VLAN 1 1 20 payload two tags Target access port, VLAN 20 payload in VLAN 20 replies can't come back Defence: native VLAN = an unused ID, tag it anyway, and never put hosts in VLAN 1.

Double tagging has real limits. It only works when the attacker's own VLAN is the trunk's native VLAN, and it's strictly one-way: the victim's reply is an ordinary VLAN 20 frame headed for an address in VLAN 1, and nothing will carry it back. That's still enough to fire a single malicious packet at something that trusts its own VLAN.

Both attacks are configuration problems, so the defences are configuration too:

  1. Make every port's role explicit

    Ports facing hosts are configured as access ports in a named VLAN; ports facing switches are configured as trunks. Turn trunk negotiation (DTP) off everywhere. A port that can't negotiate can't be talked into being a trunk.

  2. Keep VLAN 1 empty

    Don't put hosts in VLAN 1, and don't use it as the native VLAN. On Cisco gear some control protocols still travel in VLAN 1 regardless, which is one more reason to keep user traffic out of it.

  3. Make the native VLAN unused, and tag it

    Set each trunk's native VLAN to an ID that no access port uses, such as 999, and enable tagging of the native VLAN where the switch supports it, so no frame ever crosses a trunk untagged. Then there's no outer tag to peel off.

  4. Prune what each trunk carries

    Allow only the VLANs that actually need to cross each trunk, rather than the default of "all". Unused ports can be shut down or parked in an unused VLAN.

On a Cisco switch, the hardened version of our switch A looks roughly like this (other vendors use different words for the same settings):

Explicit roles, no negotiation, an unused native VLAN

vlan dot1q tag native                  ! tag the native VLAN on all trunks
!
interface GigabitEthernet0/1           ! the laptop
 switchport mode access
 switchport access vlan 10
 switchport nonegotiate
!
interface GigabitEthernet0/8           ! trunk to switch B
 ! on ISL-capable models, first: switchport trunk encapsulation dot1q
 switchport mode trunk
 switchport trunk native vlan 999
 switchport trunk allowed vlan 10,20,30
 switchport nonegotiate

Finally, keep VLANs in perspective. They stop hosts from reaching each other at Layer 2; they don't encrypt anything, and they don't replace the router's firewall rules. A well-configured VLAN is the wall; the firewall is the door.

Beyond One Tag

Voice VLANs

Offices with desk phones often run a single cable to each desk: the phone plugs into the wall, and the PC plugs into a small switch built into the phone. The switch port is configured with a data VLAN for untagged traffic and a separate voice VLAN for tagged traffic, which the phone learns about through a discovery protocol such as LLDP-MED or Cisco's CDP. The PC's frames go up untagged in the data VLAN; the phone's go up tagged, say VLAN 40 at PCP 5 (81 00 a0 28), so calls can be prioritized and kept away from the PCs.

Wi-Fi

Wireless networks do the same thing with SSIDs. A home access point broadcasting "Home" and "Home-Guest" typically maps each SSID to a VLAN, and its cable to the switch is a trunk. A phone joining the guest network ends up in VLAN 30 without ever seeing a tag.

Q-in-Q (802.1ad)

A service provider that carries traffic for many customers has a problem: each customer wants its own VLANs 1 to 4094, and they all overlap. IEEE 802.1ad (approved in 2005, folded into 802.1Q in 2011) solves it by stacking a second tag in front: the provider's S-tag (service tag, TPID 0x88a8) outside the customer's own C-tag (TPID 0x8100). The provider's switches look only at the outer tag, and the customer's VLANs pass through untouched.

Dst MAC Src MAC S-tag 88a8 · VID 100 C-tag 8100 · VID 10 Type Payload FCS the provider's VLAN the customer's VLAN Each tag is 4 bytes; a reader follows the TPIDs one at a time until it reaches a real EtherType.

Notice how this is the double-tagging picture again, done on purpose and with a different TPID marking the outer tag, so nobody confuses the two.

VXLAN: beyond 4,094

Twelve bits seemed generous in 1998. In a modern data centre, where each customer of a cloud provider might want several isolated networks, 4,094 is nowhere near enough, and stretching VLANs across hundreds of switches drags along huge broadcast domains. VXLAN (RFC 7348, 2014) takes a different approach: it wraps the whole Ethernet frame in a UDP packet (destination port 4789) and sends it across an ordinary routed IP network. Its 8-byte header carries a 24-bit VNI, room for about 16.7 million segments. The price is 50 bytes of extra headers per frame (outer Ethernet 14, IPv4 20, UDP 8, VXLAN 8). The outer Ethernet header doesn't count against the IP MTU, but everything inside it does: a 1,500-byte packet, plus the inner frame's own 14-byte Ethernet header, plus VXLAN 8, UDP 8 and IPv4 20, comes to 1,550. So the physical network needs an MTU of at least 1,550 to carry ordinary 1,500-byte packets inside. The idea is the same one this whole article has been about, though: a label on each frame that says which virtual network it belongs to.

Where to Go Next

  • Spanning Tree: our two switches are joined by a single trunk. Add a second one for redundancy and you get a loop; spanning tree finds and blocks it, and in a VLAN network it often runs separately per VLAN.
  • Routing Basics: what the router actually does with that packet between eth0.10 and eth0.20: routing tables and longest-prefix match.
  • DHCP: one address pool per VLAN, and the relay agents that let one server handle them all.
  • ARP: the broadcasts and the cache poisoning that VLANs keep contained.
  • Ethernet & Switching: the learning switch and the frame format this article built on.
  • Checksums & CRC (Error Correction): why four inserted bytes change the FCS completely.

Key Takeaways

  • A switched LAN is one broadcast domain. Broadcast load grows with the square of its size, and every host can reach every other at Layer 2 without passing a firewall.
  • A VLAN is a broadcast domain defined in switch configuration. Frames that enter in one VLAN can only leave through ports in the same VLAN; crossing requires a router.
  • VLAN IDs are 12 bits: 1–4094 are usable, 0 means "priority tag only", 4095 is reserved, and VLAN 1 is the default for every port.
  • Access ports belong to one VLAN and carry untagged frames; trunk ports carry many VLANs, with a tag on each frame.
  • The 802.1Q tag is 4 bytes after the source MAC: TPID 0x8100, then PCP (3 bits), DEI (1 bit) and VID (12 bits). It raises the maximum frame to 1522 bytes, and adding or removing it means recomputing the FCS. Our laptop's frame on the trunk is 99 bytes with tag 81 00 00 0a.
  • The native VLAN (VLAN 1 by default) crosses a trunk untagged; mismatched native VLANs merge two VLANs.
  • VLAN-aware switches learn and look up by (VLAN, MAC), so the same MAC, like a router-on-a-stick's, can appear once per VLAN.
  • Inter-VLAN routing uses a router on a stick (one subinterface per VLAN over a trunk, traffic crosses it twice) or a layer-3 switch with SVIs.
  • Map each VLAN to one IPv4 subnet and one IPv6 /64. DHCP and mDNS stay inside their VLAN unless relayed.
  • VLAN hopping (switch spoofing via DTP, double tagging via the native VLAN) is prevented by explicit access/trunk configuration with negotiation off, an unused and tagged native VLAN, no hosts in VLAN 1, and pruned trunks.
  • Q-in-Q (802.1ad, TPID 0x88a8) stacks a provider tag on a customer tag; VXLAN puts frames in UDP with a 24-bit VNI for about 16.7 million segments.

Our house now has three networks on two switches, joined by one trunk. That single cable is also a single point of failure. Add a second one and Ethernet's oldest weakness comes back: a loop, and a broadcast storm within seconds. Next up: Spanning Tree, the protocol that lets switches agree which links to use.