Skip to content
Tutorials Center
My learning

Topics

Lesson 18 of 79 ยท Data Link Layer

VLANs and ARP

Learn how VLANs split a switch into separate networks with 802.1Q tags, and how ARP maps IP addresses to MAC addresses, including ARP spoofing, with simulations.

Two ideas finish the data link layer. The first is VLANs: a way to carve one physical switch (or a whole campus of switches) into several separate logical networks, so that a broadcast sent by the accounting department never reaches the guest Wi-Fi. The second is ARP, the small protocol that answers a question every host asks all day long: "I know the IP address I want to talk to, so what is its MAC address?"

They belong together because they both depend on the same fact from the last lesson: a switch floods broadcasts to every port in its broadcast domain. A VLAN defines where that domain stops, and ARP is the most common broadcast that travels inside it. By the end you will be able to read a tagged frame, explain access and trunk ports, follow an ARP exchange, and see why ARP can be abused.

Why VLANs?

Without VLANs, one switch is one broadcast domain. If you want two separate networks, say offices and cameras, you would need two switches, or two sets of cables. A virtual LAN (VLAN) lets the switch keep a separate set of ports for each network. Frames never cross from one VLAN to another at layer 2. Hosts in different VLANs talk only through a router, where you can apply a firewall policy.

The benefits are practical:

  • Smaller broadcast domains. Less broadcast noise per host.
  • Security and isolation. Guests, servers, phones and building systems are kept apart.
  • Flexibility. Moving a user to another department means changing a port setting rather than recabling.
  • Reuse of hardware. One switch behaves like several.

Each VLAN is identified by a VLAN ID (VID), a number from 1 to 4094. Each VLAN is normally given its own IP subnet, so "VLAN 20 is 10.0.20.0/24" is a common arrangement (see subnetting).

Access ports and trunk ports

A switch port can be configured in one of two main ways.

  • An access port belongs to exactly one VLAN. It connects an end host such as a laptop or a printer. The host sends and receives ordinary untagged frames and does not know that VLANs exist. The switch adds the VLAN membership internally.
  • A trunk port carries traffic of several VLANs over a single link, normally between two switches or between a switch and a router. Because the frames of different VLANs share one cable, each frame must say which VLAN it belongs to. That is the job of the tag.
flowchart LR
    subgraph SW1["Switch 1"]
      P1["port 1<br/>access VLAN 10"]
      P2["port 2<br/>access VLAN 20"]
      T1["port 24<br/>trunk 10, 20"]
    end
    subgraph SW2["Switch 2"]
      T2["port 24<br/>trunk 10, 20"]
      P3["port 1<br/>access VLAN 10"]
      P4["port 2<br/>access VLAN 20"]
    end
    H1["Laptop A (VLAN 10)"] --- P1
    H2["Camera (VLAN 20)"] --- P2
    T1 ===|"802.1Q tagged frames"| T2
    P3 --- H3["Laptop B (VLAN 10)"]
    P4 --- H4["Recorder (VLAN 20)"]

One trunk carries both VLANs between the switches. Laptop A can reach Laptop B, but not the camera.

802.1Q tagging

The IEEE standard 802.1Q defines how a frame is marked. The switch inserts a 4-byte tag between the source MAC address and the original EtherType field:

Part Size Meaning
TPID 16 bits Tag protocol identifier: always 0x8100. It sits where the EtherType used to be, which is how receivers recognise a tagged frame.
PCP 3 bits Priority code point: a class of service value from 0 to 7 (see QoS).
DEI 1 bit Drop eligible indicator: this frame may be dropped first when there is congestion.
VID 12 bits The VLAN ID.

The real EtherType follows the tag. Because the frame grows by four bytes, the maximum Ethernet frame becomes 1,522 bytes, and the FCS is recalculated by the switch that inserts or removes the tag.

One trunk VLAN can be the native VLAN, whose frames are sent untagged; by default that is VLAN 1. Mismatched native VLANs on the two ends of a trunk cause leaks between VLANs and are a well-known misconfiguration. Good practice is to prune trunks so they carry only the VLANs that are needed and to avoid using VLAN 1 for user traffic.

Python Build and parse an 802.1Q tag, then forward by VLAN

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Traffic between VLANs

Since a host in VLAN 10 and a host in VLAN 20 are in different broadcast domains and different IP subnets, their packets must be routed. Two designs are common. In router on a stick, one router interface is connected to a trunk and uses one subinterface per VLAN, each with an address that serves as the default gateway for its VLAN. In a layer 3 switch the routing happens inside the switch itself, on virtual interfaces (SVIs), which is faster and the usual choice in larger networks.

ARP: from IP address to MAC address

When your computer wants to send a packet to 192.168.1.20, the IP layer decides which next hop to use, as you will see in routing and forwarding. But the frame that leaves your network card needs a destination MAC address, and the IP layer only knows an IP address. The Address Resolution Protocol (ARP) bridges the gap for IPv4 on Ethernet. (IPv6 uses Neighbor Discovery in ICMPv6 for the same purpose.)

If the destination is on your own subnet, ARP is used to find the destination's MAC address. If it is on another network, ARP is used to find the MAC address of the default gateway, because the frame goes to the router, and the router forwards the IP packet onward.

The exchange

  1. The sender checks its ARP cache for the IP address. If it finds a MAC address, it uses it and the process ends.
  2. Otherwise it sends an ARP request. It is a broadcast Ethernet frame (destination ff:ff:ff:ff:ff:ff, EtherType 0x0806) that effectively says: "Who has 192.168.1.20? Tell 192.168.1.10."
  3. Every host on the segment receives it, but only the owner of 192.168.1.20 answers. The owner sends an ARP reply directly (unicast) to the asker: "192.168.1.20 is at aa:bb:cc:00:00:20."
  4. The sender stores the answer in its cache and sends the original packet. Many hosts also note the sender's address from the request they just received.
sequenceDiagram
    participant A as Host A 192.168.1.10
    participant L as LAN (broadcast)
    participant B as Host B 192.168.1.20
    A->>L: ARP request: who has 192.168.1.20?
    L->>B: request reaches every host
    B->>A: ARP reply: 192.168.1.20 is at aa:bb:cc:00:00:20
    Note over A: cache B, then send the IP packet

An ARP exchange costs one broadcast and one unicast frame, then the answer is cached.

An ARP message is 28 bytes for IPv4 over Ethernet: hardware type (1 = Ethernet), protocol type (0x0800), address lengths (6 and 4), the operation (1 = request, 2 = reply), then the sender's MAC and IP and the target's MAC and IP. In a request, the target MAC is all zeros because that is what we are asking for.

The ARP cache

Each host keeps an ARP cache (or neighbor table) of recent IP-to-MAC mappings. Entries expire after a short time, anywhere from seconds to a few minutes depending on the operating system, so the table follows devices that change. You can see yours with arp -a or ip neigh. The cache is why you only see ARP traffic when a host talks to a new neighbor.

Gratuitous ARP

A gratuitous ARP is a request or reply that nobody asked for, in which a host announces its own IP-to-MAC mapping (the sender and target IP are both its own). It has real uses:

  • Duplicate address detection. A host that has just configured an address sends an ARP probe for it; if someone replies, the address is already taken.
  • Cache refresh after a change. When a server's network card is replaced, or a high-availability pair fails over and a standby takes over the shared IP address, the new owner announces itself so that neighbors update their caches at once.

ARP has no authentication

Every host trusts ARP replies, even ones it never asked for, and there is no way to check them. An attacker on the same LAN can therefore send forged replies claiming that the gateway's IP address belongs to the attacker's MAC address. Victims overwrite their cache entry and start sending their traffic to the attacker, who can read it and forward it on, a man-in-the-middle attack. This is ARP spoofing (or ARP poisoning). It works only inside one broadcast domain, which is one more reason to keep untrusted devices in a separate VLAN.

Defences include dynamic ARP inspection on switches (which checks ARP messages against a table of trusted bindings, often built from DHCP snooping), static entries for critical hosts such as gateways, port security, and above all encryption at higher layers so that intercepted traffic is useless (TLS). See common attacks and defenses for the wider picture.

Python ARP request, reply, cache and spoofing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Real operating systems are a little pickier than this toy host (many ignore unsolicited replies for unknown entries, and some tools disable that safety check), but the underlying weakness is the same: ARP carries no proof of identity.

Historical cousins: RARP, BOOTP and Inverse ARP

ARP answers "I know the IP address, what is the MAC address?" Three older protocols answer related questions, and each one explains something about how modern networks configure themselves.

RARP: the reverse question

A Reverse ARP (RFC 903, 1984) client asks the opposite: "I know my own MAC address, what is my IP address?" It was made for diskless workstations. Such a machine had no disk to store an IP address in, but its network card always knew its burned-in MAC address. At boot it broadcast a RARP request (EtherType 0x8035, ARP operation codes 3 for the request and 4 for the reply), and a RARP server that held a table of MAC-to-IP pairs sent back a unicast reply.

RARP had serious limits. It ran directly on Ethernet, so a request never crossed a router, and every subnet needed its own RARP server. A reply carried only an IP address: no subnet mask, no default gateway, no DNS server, so the machine still needed another way to learn the rest. The administrator also had to type in every MAC address by hand.

BOOTP: configuration over UDP

BOOTP (RFC 951, 1985) fixed both problems. It runs over UDP (client port 68, server port 67) and therefore over IP, so one server can serve many subnets through relay agents that forward the client's broadcast as a unicast. The reply carries much more than an address: the server's address, a default gateway and the name of a boot file that the client then downloads with TFTP (see TFTP). The table was still static, though: an administrator had to enter each MAC address and its fixed IP address in advance.

DHCP (RFC 2131, 1997) keeps the BOOTP message format and its relay mechanism, and adds dynamic leases: a pool of addresses handed out on demand and returned on expiry, plus options for the mask, DNS servers and more. That is why a DHCP packet still has BOOTP-style fields, and why RARP has vanished from real networks.

Inverse ARP: the other side of a virtual circuit

Inverse ARP (RFC 2390) is a cousin used on Frame Relay and similar WAN technologies. There, a router knows the layer 2 identifier of a virtual circuit (a DLCI) but not the IP address at the far end. It sends an Inverse ARP request down that circuit and the neighbor answers with its own IP address, so the router can build its map automatically. On Ethernet LANs you never see it.

Protocol Question it answers Runs over Status
ARP IP address to MAC address Ethernet directly Alive (IPv4)
RARP MAC address to IP address Ethernet directly Obsolete
BOOTP Which IP, gateway and boot file? UDP 67/68 Obsolete, lives on inside DHCP
Inverse ARP Layer 2 circuit to IP address Frame Relay and similar Rare

The toy below plays both roles: a workstation that knows only its MAC address, and a server with the table. Notice how little the reply contains compared with a DHCP offer.

Python A toy RARP server

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does an access port do with frames sent by the attached host?

    An access port belongs to one VLAN and the host is unaware of VLANs. The switch places the untagged frames in the port's VLAN.
  2. 2. How many bytes does an 802.1Q tag add to an Ethernet frame, and which value does the TPID have?

    The tag is 4 bytes: a 2-byte TPID of 0x8100 followed by 2 bytes holding PCP, DEI and the 12-bit VLAN ID.
  3. 3. Host A in VLAN 10 wants to reach host B in VLAN 20. What is required?

    Different VLANs are different broadcast domains and normally different IP subnets, so only a layer 3 device can pass traffic between them.
  4. 4. Why is an ARP request sent to ff:ff:ff:ff:ff:ff?

    The whole point of ARP is to find an unknown MAC address, so the question is asked of every host on the segment and only the owner replies.
  5. 5. What makes ARP spoofing possible?

    Any host on the LAN can claim any IP address, and receivers have no means of verifying the claim.

Summary

  • VLANs split a switched network into separate broadcast domains, usually one IP subnet each. Traffic between VLANs needs a router or layer 3 switch.
  • Access ports carry one VLAN untagged to end hosts; trunk ports carry many VLANs between network devices using a 4-byte 802.1Q tag (TPID 0x8100, 12-bit VID, 3-bit priority).
  • ARP resolves an IPv4 address to a MAC address with a broadcast request and a unicast reply, and caches the result for a short time. For off-subnet destinations it resolves the gateway.
  • A gratuitous ARP announces a mapping without being asked, for duplicate detection and failover.
  • ARP has no authentication, so forged replies can poison caches. Dynamic ARP inspection, VLAN separation and encryption reduce the risk.
  • RARP, BOOTP and Inverse ARP are older address-resolution and configuration protocols; DHCP replaced RARP and BOOTP, while Inverse ARP lives on in some Frame Relay networks.

You have reached the end of the data link layer. Continue upward to the network layer with IPv4 addresses.