Sub topic 7
How switches learn and forward by MAC address
This chapter assumes you know the layout of a MAC address and the Ethernet II frame from earlier chapters. It also assumes you know the difference between unicast, multicast, and broadcast destinations. Here you will see how a switch uses the source and destination MAC fields to deliver frames without any configuration.
What a switch does
A switch is a link-layer device that connects several hosts in one LAN. A hub repeats every frame out of every port. A switch instead keeps a MAC address table (also called a CAM table or forwarding table). The table maps each MAC address to the port where that address was last seen.
A switch makes one decision per frame, using only the Ethernet header. It does not read IP addresses.
Learning: read the source MAC
A switch learns by watching the source address of every frame that arrives. The table starts empty.
- A frame arrives on port 1 with source MAC
aa:aa:aa:aa:aa:01. - The switch records
aa:aa:aa:aa:aa:01 → port 1. - If the entry already exists, the switch refreshes its age timer.
- If the address was last seen on a different port, the switch updates the entry. This is how the table follows a device that moves.
Entries expire after an aging time of 300 seconds by default on many switches. Aging keeps the table from filling with devices that have left.
Forwarding: read the destination MAC
After learning, the switch looks up the destination MAC and picks one of four actions.
| Destination | Table result | Action |
|---|---|---|
| Unicast | Found, on a different port | Forward out that port only |
| Unicast | Found, on the arrival port | Drop (filter) the frame |
| Unicast | Not found ("unknown unicast") | Flood out all ports except the arrival port |
| Broadcast or multicast | Not applicable | Flood out all ports except the arrival port |
Flooding is safe because only the intended host accepts the frame. The other hosts see a destination MAC that is not theirs and ignore it. The switch never sends a frame back out the port it came in on.
Broadcast and multicast frames are always flooded by a basic switch. A switch with multicast snooping can limit multicast, but that is an optional feature.
A worked example
Hosts A, B, and C connect to ports 1, 2, and 3. The table is empty.
From the second frame onward, traffic between A and B stays on their two ports. C no longer sees it. The following Python sketch models this logic:
class Switch:
def __init__(self, ports):
self.ports = ports
self.table = {} # MAC -> port
def receive(self, in_port, src, dst):
self.table[src] = in_port # learn or update
broadcast = dst == 'ff:ff:ff:ff:ff:ff'
if not broadcast and dst in self.table:
out = self.table[dst]
return [] if out == in_port else [out]
return [p for p in self.ports if p != in_port] # flood
sw = Switch([1, 2, 3])
print(sw.receive(1, 'aa:aa:aa:aa:aa:01', 'aa:aa:aa:aa:aa:02')) # [2, 3]
print(sw.receive(2, 'aa:aa:aa:aa:aa:02', 'aa:aa:aa:aa:aa:01')) # [1]
print(sw.receive(1, 'aa:aa:aa:aa:aa:01', 'aa:aa:aa:aa:aa:02')) # [2]On a managed Linux bridge, bridge fdb show lists the learned entries.
Common errors and gotchas
- Broadcast is never learned. A source address is never
ff:ff:ff:ff:ff:ff, so broadcast never appears in the table. Frames sent to it always flood. - Table size is finite. If an attacker sends frames with thousands of fake source MACs, the table fills. The switch may then flood most traffic, which lets a nearby host see frames meant for others. This is called MAC flooding. Port security limits the number of MACs per port.
- Loops break the model. If two switches are connected by two cables, a flooded frame circulates forever, and the same MAC appears to jump between ports. Spanning Tree Protocol blocks redundant links to prevent this.
- Idle hosts disappear. After the aging time, a quiet host is forgotten. The next frame to it floods once, then the table relearns it when the host replies.
Exercise
A switch has an empty MAC table. Host A (port 1) sends a unicast frame to host B (port 2), and B has never sent anything. Which ports does the switch send the frame out of, and what does it learn?
Show answer
It floods the frame out every port except port 1, because B's MAC is unknown. It learns only A's MAC on port 1, taken from the frame's source address. It learns B's MAC only when B sends a frame.
Exercise
Why can a switch never learn a broadcast or multicast address from a frame it receives?
Show answer
The switch learns from the source MAC field, and a source address must be a single unicast device. Broadcast and multicast addresses are only valid as destinations, so they never show up as a source.