Labs / Switching / Switches & VLANs

Lab 2 of 3 · Switches & VLANs

VLANs: one switch, two networks that can't hear each other

Stop a hotel guest borrowing a staff address on a shared switch.

Start this lab 6 steps

The idea

Marco's hotel has one Switch and no budget for a second, so guests and the key-card server share it. His contractor says different addresses keep them apart. Prove that wrong with one setting, then build a wall a guest can't talk past.

Two subnets, one broadcast
Two subnets, one broadcast
The switch never reads the IP address
The switch never reads the IP address
A number on each port
A number on each port
A broadcast stays in its VLAN
A broadcast stays in its VLAN
The tag lives on the trunk
The tag lives on the trunk
The router is the only way across
The router is the only way across

Steps

  1. Reach for the key server from a guest's laptop

    Marco runs a small hotel. keyServer programs every room key, frontDesk is the reception computer, and guests use guestLaptop and lobbyKiosk. All four plug into one Switch, the hotelSwitch, a box with a slot called a port for each cable, and this lab names each port after the machine on the end of its cable.

    His contractor gave the staff machines IP addresses, the numbers machines use to reach each other, starting 10.0.10, and the guests' machines addresses starting 10.0.20. Those are two subnets, ranges of addresses, and the contractor told Marco that the guests are now on a separate network. It sounds right, and it even looks right, so start by testing it from the guest's side.

    Try to reach the key server, at 10.0.10.20, from guestLaptop. Then test the other half of the claim, which is what a guest can hear.

    Before frontDesk can talk to keyServer, it needs the key server's hardware address, the number built into its network card. So it asks who has 10.0.10.20, an ARP question sent as a broadcast, a message addressed to everybody, and the lab records what reaches the laptop's network card.

    guestLaptop:~# ping -c 1 -W 2 10.0.10.20
    frontDesk:~# ping -c 1 -W 2 10.0.10.20
  2. Borrow a staff address

    Two facts are about to meet. A plain switch moves frames, the wrappers that carry data across a network, by hardware address and never reads an IP address. And an IP address is a setting on a machine, which whoever controls the machine can change.

    So what stopped the guest in the last step? The laptop itself, which looked at 10.0.10.20 and decided it was far away, and nothing in the network said no.

    A machine can also hold two addresses on one network card, so a guest can add one from the staff subnet without losing their own.

    Type this on guestLaptop: the address .99 isn't in use, and no password or special access is involved, only the laptop's own settings. Then ask the key server for its page.

    guestLaptop:~# ip -echo addr add 10.0.10.99/24 dev eth0
    guestLaptop:~# curl -sS -m 3 http://10.0.10.20/
  3. Split the switch in two

    For years the answer was exactly that second switch: a box for each department, and a technician in a closet moving cables each time somebody changed desks. Spare ports on one box were no use to another, and two floors in one department needed cables between them.

    A VLAN, a virtual LAN (local area network), makes those separate boxes in software. The switch keeps a number on each port and treats ports with different numbers as if they were on different switches.

    Frames, broadcasts and tables all stay inside their number. The numbers mean nothing in themselves, so Marco picks 20 for guests and 10 for staff.

    Each port starts in VLAN 1, the default. Look at that first, then move the guest's port. You're typing on hotelSwitch, since a VLAN is a setting on the switch and the laptop has no part in it.

    The switch here is a Linux machine, and its tool is called bridge, the older word for a switch. Two commands move a port: one adds the new VLAN, and one takes away the old.

    The words in the first are vid 20, the VLAN's number, pvid, which files the plain frames a machine sends into this port under VLAN 20, and untagged, which you'll understand in a later step. On a Cisco switch the same move is switchport mode access, then switchport access vlan 20.

    hotelSwitch:~# bridge vlan show
    hotelSwitch:~# bridge vlan add dev guestLaptop vid 20 pvid untagged
    hotelSwitch:~# bridge vlan del dev guestLaptop vid 1
    hotelSwitch:~# bridge vlan show
  4. Try the same trick again

    guestLaptop still has its borrowed address, 10.0.10.99, so as far as the laptop knows, nothing has changed: keyServer is a neighbor. Its first move is an ARP question, and this time that question arrives at a port in VLAN 20.

    The rule from the last step decides what happens. A broadcast goes out of every port in the same VLAN, and no others. So run the same fetch, then ask the laptop what became of the ARP question, then repeat the staff ping from the first step with the lab recording on the guest's card.

    Finish with two checks that nothing broke: lobbyKiosk should still answer the guest, and the switch's table should show what it now knows.

    guestLaptop:~# curl -sS -m 2 http://10.0.10.20/
    guestLaptop:~# ip neigh show 10.0.10.20
    frontDesk:~# ping -c 1 -W 2 10.0.10.20
    guestLaptop:~# ping -c 1 -W 2 10.0.20.20
    hotelSwitch:~# bridge fdb show dynamic
  5. Find the tag on the router's cable

    A hotel guest also wants the internet, so the guests' side has to reach hotelRouter, and so does the staff side. One cable runs from the switch to the router, and it has to carry both VLANs, which raises a question: a frame on that cable looks like any other, so how does the far end know which VLAN it's in?

    The answer came from a standard called IEEE 802.1Q, approved in 1998, and it's a tag: 4 extra bytes that a switch writes into a frame. 12 of those bits hold the VLAN number. A bit is a single 0 or 1 and each one doubles the count, so 12 bits give 4096 values, and with two reserved, VLAN numbers stop at 4094.

    Before that standard, each vendor had its own way of marking frames, and switches from different makers couldn't share a VLAN.

    A cable that carries several VLANs, told apart by their tags, is called a Trunk. The port hotelRouter is one, which is why it was listed with three numbers.

    A bare 10 or 20 on that port means a tagged VLAN. The third, 1 PVID Egress Untagged, is the one VLAN a trunk may carry with no tag, called its native VLAN, and the vlan-trunking lab takes that apart.

    Take the borrowed address off the laptop, since the guest is done being a thief. Then ping the router at 10.0.20.2, and the lab records the same ping on the guest's own card and on the router's.

    guestLaptop:~# ip -echo addr del 10.0.10.99/24 dev eth0
    guestLaptop:~# ping -c 1 -W 2 10.0.20.2
  6. Join the two VLANs on purpose

    A VLAN is a wall with no door, and in a real hotel both sides want the internet, so something has to carry traffic out. That job belongs to a Router, the machine that joins networks, and a VLAN is a network.

    hotelRouter has one cable but two faces. On top of its card eth0 sit two VLAN cards, eth0.10 and eth0.20, which add and read tags, and each has its own address.

    Each VLAN's machines use the matching address as their default gateway. A router working from one tagged cable has an old nickname, router on a stick.

    Look at those cards. Now the laptop has given back its borrowed address and has only its own, so it treats the key server as far away, and hands the request to its gateway. Ask for the page once more, and trace the path it takes.

    Then close the door with a Firewall rule on the router, and ask for the page again from the guest's side and from the staff side.

    hotelRouter:~# ip -br addr
    guestLaptop:~# curl -sS -m 3 http://10.0.10.20/
    guestLaptop:~# traceroute -n 10.0.10.20
    hotelRouter:~# iptables -A FORWARD -i eth0.20 -o eth0.10 -j DROP
    guestLaptop:~# curl -sS -m 3 http://10.0.10.20/
    frontDesk:~# curl -sS -m 3 http://10.0.10.20/

Run these steps on real machines

Keep reading

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab