Part 1 · Foundations

Ethernet, switches, VLANs and ARP: the link layer explained

lesson 03 / 1927 min1 labpublished updated
Course contents
On this page
  1. MAC addresses
  2. The Ethernet frame
  3. Switches: getting a frame to the right port
  4. ARP: from an IP address to a MAC address
  5. Bigger networks: VLANs, loops and bundled links
  6. Under the link layer: cables and Wi-Fi

The previous lesson said the link layer’s job is to deliver data to the next machine on the same local network. This lesson shows how. It answers three questions:

  1. Every network card has a hardware address, the MAC address. Why do we need it when we already have IP addresses?
  2. What does a switch do with the data it receives, and how does it know where to send it?
  3. Your laptop knows the router’s IP address (you found it with ip route in lesson 1), but the frame it sends needs the router’s MAC address. How does it find it? (The answer is ARP.)

Then you build a small local network with three virtual machines and a virtual switch on your own computer, and watch all of it happen. At the end there is a short tour of what bigger networks add (VLANs, loop protection) and of the cables and Wi-Fi underneath.

MAC addresses

Every network card, wired or Wi-Fi, has a MAC address (media access control address): 48 bits, written as six bytes in hexadecimal, like 3c:22:fb:7a:10:4e. It is set in the factory, and the first three bytes identify the manufacturer.

See yours (this only reads, it changes nothing):

ip -br link        # one line per interface: name, state, MAC address
lo               UNKNOWN        00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
wlan0            UP             3c:22:fb:7a:10:4e <BROADCAST,MULTICAST,UP,LOWER_UP>

Why two kinds of address? Because they answer different questions:

  • The IP address says where the packet is finally going, anywhere in the world. It stays the same for the whole trip.
  • The MAC address says which card on this local network should pick up the frame right now. It only means something on one cable or one Wi-Fi network.

When your laptop sends a packet to a server across the Internet, the IP destination is the server, but the MAC destination of the frame is your router, because the router is the next machine on your local network. The router takes the packet out of the frame, puts it in a new frame for its next hop, and so on. The IP addresses stay the same all the way; the MAC addresses change at every hop. It is like a parcel that keeps its final address while each courier hands it to the next courier.

A few special cases:

  • Broadcast: ff:ff:ff:ff:ff:ff means “everyone on this local network”. You will see it used by ARP in a moment.
  • Made-up addresses: software can invent MAC addresses (virtual machines and containers do; Docker’s start with 02:42), and phones and laptops use a random MAC on each Wi-Fi network for privacy.

The Ethernet frame

Ethernet is the standard for the link layer on cables (IEEE 802.3), and Wi-Fi frames are converted to it at the access point, so from here on “frame” means an Ethernet frame. A frame is a small envelope around one IP packet:

Preamble + SFD8 BDestination MAC6 BSource MAC6 B802.1Q VLAN tag (optional)4 BEtherType2 BPayload (e.g. an IP packet)46 - 1500 BFCS (CRC-32)4 Bheader14 B(18 Btagged)MTU = max payload = 1500 B
The structure of an Ethernet II frame, with the size of each field in bytes.
FieldSizeWhat it is
Destination MAC6 bytesWhich card should pick this frame up
Source MAC6 bytesWhich card sent it
EtherType2 bytesWhat’s inside: 0x0800 IPv4, 0x86DD IPv6, 0x0806 ARP
Payload46 to 1,500 bytesUsually one IP packet; 1,500 is the MTU you met in lesson 2
FCS4 bytesA checksum, to detect frames damaged on the way

(On the wire there are also 8 bytes of synchronization pattern in front, the preamble, but the network card strips them, so you never see them in tcpdump.)

If the checksum doesn’t match, the frame was damaged on the way and the receiver silently drops it. Ethernet doesn’t resend anything; if the data matters, a higher layer (usually TCP) notices it is missing and sends it again.

Switches: getting a frame to the right port

A switch is a box with many ports that connects the machines of one local network. Its only job is to move each frame out of the port where its destination MAC address lives. To do that, it keeps a table, the MAC address table, that maps “MAC address → port”, and it fills the table by itself:

  1. A frame arrives on port 3 with source MAC A. The switch writes down: A is behind port 3.
  2. It looks up the destination MAC, B. If B is in the table, the frame goes out of that one port only (forwarding).
  3. If B isn’t in the table yet, or the destination is the broadcast address, the frame goes out of every port except the one it came in on (flooding). When B answers, the switch learns where B is, too.
  4. Entries that see no traffic for a while (usually 5 minutes) are removed (aging), so a machine that moves to another port is found again.

Before switches there were hubs, which simply copied every frame out of every port. Every machine saw everyone’s traffic, and only one could talk at a time. Switches made networks both faster and more private.

All the machines that a broadcast reaches form one broadcast domain. One switch (or several switches connected together) is one broadcast domain. Routers don’t pass broadcasts on, so routers are where one broadcast domain ends and the next begins. In practice one broadcast domain is also one IP subnet, which you’ll meet in the IPv4 lesson.

ARP: from an IP address to a MAC address

Here is the gap: your laptop wants to send a packet to the router at 192.168.1.1, but to build the frame it needs the router’s MAC address. ARP (Address Resolution Protocol, RFC 826) fills that gap by simply asking:

  1. The laptop sends an ARP request to the broadcast address, so every machine on the local network gets it: “Who has 192.168.1.1? Tell 192.168.1.20.”
  2. Every machine reads it; only the one that owns 192.168.1.1 answers, directly to the laptop: “192.168.1.1 is at aa:bb:cc:dd:ee:ff.” That is the ARP reply.
  3. The laptop stores the answer in its ARP cache for a few minutes, so it doesn’t have to ask before every packet.

You can look at your ARP cache (again, read-only):

ip neigh            # "neighbors": the ARP cache
192.168.1.1 dev wlan0 lladdr aa:bb:cc:dd:ee:ff REACHABLE
192.168.1.50 dev wlan0 lladdr 11:22:33:44:55:66 STALE

lladdr is the link-layer (MAC) address. REACHABLE means recently confirmed, STALE means old but probably still right, and FAILED means nobody answered: the machine is off or not on this network.

Three things worth knowing:

  • ARP has no security. Any machine can answer “that IP is me”, and the others believe it. Abusing this (ARP spoofing) is a classic way to intercept traffic on a local network; the network security lesson comes back to it.
  • Gratuitous ARP is an announcement nobody asked for: “10.0.0.5 is now at my MAC”. Machines send one when they boot, and clusters send one to move a shared IP address from a failed server to a healthy one.
  • IPv6 doesn’t use ARP; a protocol called NDP does the same job.

You now understand a home or small office network. Company and data-center networks add three things you will hear about. You don’t need to configure them; you need to know what they are.

  • VLANs (virtual LANs, IEEE 802.1Q) split one physical switch into several separate logical switches. A company might put employees in VLAN 10, guests in VLAN 20 and security cameras in VLAN 30: one set of cables and switches, but three broadcast domains that can’t see each other. A port that belongs to one VLAN is an access port; a link that carries several VLANs between two switches is a trunk, and each frame on it carries a small 4-byte tag with its VLAN number. Traffic between VLANs has to go through a router, just like traffic between any two networks.
  • STP (Spanning Tree Protocol). If two switches are connected by two cables for redundancy, a broadcast goes round the loop forever, because Ethernet frames, unlike IP packets, have no counter that makes them expire. Within seconds the network is full of copies of the same frame (a broadcast storm) and stops working. STP finds such loops and blocks one link, and unblocks it if the other link fails.
  • Link aggregation (LACP) does the opposite: it bundles several cables between two devices into one logical link for more capacity and redundancy.

The physical layer is the actual signal. As a programmer you rarely touch it, but a few facts help when something is slow:

  • Copper cables (Cat5e, Cat6) carry 1 Gbps up to 100 meters; fiber carries much more, much farther; radio (Wi-Fi, 4G, 5G) needs no cable. Ethernet standards are named speed + medium: 1000BASE-T is 1 Gbps over copper, 10GBASE-SR is 10 Gbps over short-range fiber.
  • Wi-Fi shares one radio channel among everyone nearby, and only one device can transmit at a time, so it waits, takes turns and confirms every frame. That is why Wi-Fi has noticeably higher and more variable latency than a cable, even when its speed looks high. The 2.4 GHz band reaches farther but is crowded; 5 and 6 GHz are faster but shorter-range. For security, use WPA2 or WPA3; WEP and the original WPA are broken.
  • On a physical machine, ethtool eth0 shows the link speed and whether a cable is detected, and ethtool -S eth0 | grep -i err shows error counters. A link stuck at 100Mb/s or a growing count of CRC errors usually means a bad cable or port. (In a VM these numbers are virtual and say little.)