How Does a Switch Know Where to Send Data?
Hubs and switches, the MAC address, source learning, the MAC table with port and VLAN, forwarding, filtering and flooding, ageing, STP and port security.
When a computer sends data to a printer or to another PC on the same network, the Ethernet frames almost always pass through a switch. The switch does not know in advance where the devices are, and it does not have to be configured port by port: it learns on its own, by watching the traffic, and builds a table that links each MAC address to the port through which it can be reached. Let's see how this table is built, how the switch decides whether to forward, filter or flood a frame, why entries expire and what changes with VLANs, loops and attacks.
From hub to switch
Before switches became widespread, twisted-pair Ethernet networks used hubs. A hub works at the physical layer: every signal it receives on one port is repeated on all the others. Every device receives all the traffic and shares the same medium: if two of them transmit at the same moment a collision occurs, so they work in half duplex with the CSMA/CD method, which detects collisions and retransmits after a random wait.
A switch, on the other hand, works at layer 2, the data link layer. It reads the header of every frame, in particular the source and destination MAC addresses, and sends the frame only where it is needed. In terms of standards it is a bridge with many ports: bridge operation is described in IEEE 802.1D, now merged into IEEE 802.1Q together with VLANs. Each device therefore receives only the traffic addressed to it or to everyone, and several pairs of devices can communicate at the same time.
The MAC address
Every Ethernet or Wi-Fi interface has a MAC address of 48 bits, that is 6 bytes, written in hexadecimal: 00:00:5E:00:53:0A, or 00-00-5E-00-53-0A or, on some devices, 0000.5e00.530a.
The first 24 bits form the OUI, which the IEEE Registration Authority assigns to the manufacturer or organisation; the organisation assigns the remaining 24 bits to each interface. Two bits of the first byte have a special meaning: the I/G bit distinguishes individual (unicast) addresses from group (multicast) addresses, and the U/L bit distinguishes universal addresses, assigned by the manufacturer, from locally administered ones, such as the random addresses many smartphones use on Wi-Fi networks. The address with all bits set to 1, FF:FF:FF:FF:FF:FF, is the broadcast address: every device on the network receives it.
The examples in this article use the addresses 00:00:5E:00:53:xx, which RFC 7042 reserves for documentation.
How the switch learns
When it is switched on, a switch has an empty table. For every frame it receives, the switch reads the source MAC address and records three pieces of information in the table: the address, the port the frame came in on and the VLAN it belongs to. If PC A transmits from port 1, from that moment the switch knows that A can be reached through port 1.
The standard calls this table the filtering database; device documentation uses the names "MAC address table" or "CAM table", from Content Addressable Memory, the memory that finds an address in constant time. Entries learnt in this way are called dynamic; the administrator can also add static entries, which never expire.
The switch learns only from source addresses: a device that never transmits does not appear in the table. The destination, instead, is used to decide where to send the frame.
Forwarding, filtering and flooding
After learning, the switch looks up the destination address in the table of the frame's VLAN. There are four possible cases.
- Forwarding: the destination is in the table on another port, and the frame leaves only through that port.
- Filtering: the destination is in the table on the same port the frame came in on, for example because another switch sits behind that port. The frame is discarded: the recipient has already received it.
- Flooding of unknown unicast frames: the destination is not in the table. The switch sends the frame on every port of the same VLAN except the incoming one. With the recipient's reply it also learns where the recipient is: from the next frame on, forwarding will be targeted.
- Broadcast and multicast: frames to FF:FF:FF:FF:FF:FF always go to every port of the VLAN except the incoming one. Multicast frames are also sent to every port, unless the switch supports IGMP snooping (or MLD snooping for IPv6), which limits them to the ports where devices have joined the group.
| Frame received | What the switch finds | What it does |
|---|---|---|
| Unicast | Destination in the table on another port | Forwards it only to that port |
| Unicast | Destination in the table on the incoming port | Filtering: the frame is discarded |
| Unknown unicast | Destination not in the table | Floods it to every port of the VLAN except the incoming one |
| Broadcast | FF:FF:FF:FF:FF:FF | Floods it to every port of the VLAN except the incoming one |
| Multicast | Group address | Floods it within the VLAN, or sends it only to member ports with IGMP/MLD snooping |
In a real network the first exchange between two devices is often a broadcast ARP request, with which a PC looks for the MAC address matching an IP address: the switch learns the sender from the request and the recipient from the reply, so the following unicast frames find the table already prepared.
Ageing of entries
Dynamic entries do not stay in the table forever. Each entry has a timer that is reset every time the switch receives a frame from that address; if the address stays silent for the ageing time, the entry is deleted. IEEE 802.1D recommends a default value of 300 seconds, which many switches adopt; on managed devices it can be changed.
Ageing has two purposes: it frees space in the table, whose capacity is limited, and it lets the switch follow devices that move. If a laptop is moved from one socket to another, the first frame it sends from the new port updates the entry immediately; disconnected devices, instead, disappear from the table when their timer expires. When the topology changes, the Spanning Tree protocols shorten the ageing time or flush the affected entries.
Collision domain and broadcast domain
The collision domain is the set of devices whose transmissions can collide: with a hub it is the whole network, with a switch each port is a separate domain. Today links between switches and devices almost always work in full duplex: transmission and reception happen at the same time, collisions are impossible and CSMA/CD does not come into play. A 1 Gbit/s full-duplex link carries up to 1 Gbit/s in each direction.
The broadcast domain, instead, is the set of devices reached by a broadcast frame. A switch does not divide it: all the ports of the same VLAN, even across several interconnected switches, form a single broadcast domain. Separating it requires a router or VLANs.
VLANs: one table per VLAN
On a switch with IEEE 802.1Q VLANs, every table entry includes the VLAN identifier, and the lookup always takes place in the frame's VLAN. It is as if each VLAN had its own table: the same MAC address can appear in two different VLANs, on different ports, without conflict, and the flooding of an unknown or broadcast frame reaches only the ports of that VLAN. On a trunk port, which carries several VLANs with the 802.1Q tag, the switch learns addresses separately for each VLAN. Traffic between different VLANs has to pass through a router or a firewall.
Loops and broadcast storms: STP and RSTP
Two cables between two switches look like good redundancy, but without protection they create a loop. A broadcast frame enters the first switch, leaves through both links, comes back in from the other side and starts the round again. Ethernet frames have no field that limits their lifetime, like the TTL of IP packets: the copies multiply and within a few seconds saturate the links. This is a broadcast storm. Meanwhile the MAC table becomes unstable, because the same source address keeps arriving from different ports.
The Spanning Tree Protocol (STP, IEEE 802.1D) prevents the problem: switches exchange messages called BPDUs, elect a root bridge and block the redundant links, so the logical topology becomes a loop-free tree. If an active link fails, a blocked port becomes active again. The classic version can take 30 to 50 seconds to reconverge; Rapid Spanning Tree (RSTP, IEEE 802.1w, later included in 802.1D-2004 and 802.1Q) usually reacts within a few seconds or less. It is worth always leaving it enabled: a single cable plugged in by mistake between two sockets is enough to create a loop.
MAC flooding and port security
The limited capacity of the table opens the way to an attack known as MAC flooding: a device sends, in a short time, a huge number of frames with fake source addresses until the table is full. At that point the switch can no longer learn legitimate addresses and floods frames to unknown destinations, which the attacker can then receive.
The most common countermeasure is port security, available on many managed switches under different names: it limits the number of MAC addresses learnt on each port, or accepts only defined addresses, and in case of violation it drops the traffic, logs the event or disables the port. More complete protection comes from IEEE 802.1X authentication, which authorises the device before giving it access to the network.
How to view the table
On managed switches the table can be viewed from the web interface or from the command line. On many devices the command is similar to show mac address-table; the output lists, for each entry, the VLAN, the address, the type (dynamic or static) and the port. An indicative example:
VLAN MAC address Type Port
1 00:00:5E:00:53:0A dynamic 1
1 00:00:5E:00:53:0B dynamic 2
1 00:00:5E:00:53:0C dynamic 3
1 00:00:5E:00:53:0D dynamic 4
The table is an excellent diagnostic tool: it shows which port a device is on, reveals an address that appears on different ports (a sign of a loop) and shows how many addresses sit behind a port, for example when an unauthorised switch has been plugged into a socket. To find the MAC address of your own computer, use ipconfig /all on Windows or ip link on Linux. Unmanaged switches learn in the same way, but do not let you view the table.
Conclusions
A switch needs no configuration to know where to send data: it learns from source addresses, looks up destination addresses and forwards each frame only to the right port, falling back on flooding when it does not know the recipient and for broadcast and multicast. Understanding the MAC table, its ageing and its link with VLANs helps to design tidy networks and to diagnose the most common problems.
The video
Further reading
- Book: LOCAL AREA NETWORKS (LAN) — Volume 3: Switching, VLANs, and Business Networks (English edition)
- Book: LOCAL AREA NETWORKS (LAN) — Volume 1: Fundamentals, Technologies, and Infrastructure (English edition)
- Video course: Reti LAN in Pratica — Volume 1 (video course in Italian)
- Video course: Reti LAN da Zero: Switch, IP, Router, Wi-Fi e Internet (video course in Italian)
- Related article: VLANs: Why Separating Networks Improves Security and Organisation
- Related article: Cat 5e, Cat 6 and Cat 6A: Which Ethernet Cable Should You Choose?
- Specialization: LAN Networks
Technical sources
- IEEE Std 802.1D-2004 — MAC Bridges — Bridge operation: address learning, the filtering database, forwarding and filtering, ageing time (recommended default 300 s), Spanning Tree and Rapid Spanning Tree.
- IEEE Std 802.1Q — Bridges and Bridged Networks — VLANs, the 802.1Q tag and the per-VLAN filtering database; the standard into which 802.1D bridging and the Spanning Tree protocols have been merged.
- IEEE Std 802.1w-2001 — Rapid Spanning Tree Protocol (RSTP), later incorporated into IEEE 802.1D-2004.
- IEEE Std 802.3 — Ethernet: frame format, CSMA/CD in half duplex and full-duplex operation.
- IEEE Registration Authority — Guidelines for Use of Extended Unique Identifier (EUI), Organizationally Unique Identifier (OUI), and Company ID (CID) — Structure of 48-bit MAC addresses, the OUI, the I/G (individual or group) and U/L (universal or local) bits.
- RFC 7042 — MAC addresses reserved for documentation (00-00-5E-00-53-00 … 00-00-5E-00-53-FF), used in the examples.
- RFC 826 — Address Resolution Protocol (ARP): broadcast request and unicast reply.
- RFC 4541 — Considerations for IGMP and MLD snooping switches for multicast traffic.
- IEEE Std 802.1X — Port-based network access control: authenticating the device before granting access.
Standards and official documentation available from the respective organisations.