Sub topic 14
Finite table size and MAC flooding
This chapter assumes you know how a switch learns source addresses, floods unknown unicast frames, and ages out idle entries. It covers what happens when the table fills up and how an attacker can force that.
The table has a fixed size
The MAC address table lives in special memory, often called CAM (content-addressable memory). That memory is fast but limited. Entry counts range from a few thousand on small access switches to hundreds of thousands on large ones.
Normally the limit never matters. A switch only stores addresses it has seen recently, and aging removes idle ones. Problems start when new source addresses arrive faster than old ones expire.
What happens when the table is full
A full table cannot store new addresses. The switch still forwards, but it behaves differently for the addresses it cannot record:
- Frames to destinations already in the table are still forwarded to one port.
- Frames from a new source are not learned.
- Frames to a destination that was never learned are treated as unknown unicast and flooded.
Flooding is correct behavior, so connectivity still works. The costs are wasted bandwidth and loss of isolation. Traffic that was private to two ports now reaches every port in the VLAN.
MAC flooding
MAC flooding is an attack that exploits this behavior. The attacker sends frames with many different forged source addresses, often thousands per second. Each frame is learned as a new entry. The table fills with junk, and legitimate hosts' entries age out or cannot be added.
Once the table is full, frames for legitimate hosts are flooded out of every port in the VLAN. The attacker, connected to one of those ports, can capture traffic meant for others with an ordinary packet sniffer. The switch then acts like a hub for the affected traffic.
Seeing it in a simulation
The LearningSwitch from earlier in this tutorial has no size limit. This subclass adds one, so you can watch the failure happen.
import time
AGING_SECONDS = 300
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class LearningSwitch:
def __init__(self, ports, aging=AGING_SECONDS, capacity=None):
self.ports = set(ports)
self.aging = aging
self.capacity = capacity
self.table = {} # mac -> (port, last_seen)
def handle(self, in_port, src, dst, now=None):
now = time.monotonic() if now is None else now
if not is_group(src):
known = src in self.table
if known or self.capacity is None or len(self.table) < self.capacity:
self.table[src] = (in_port, now)
entry = None if is_group(dst) else self.table.get(dst)
if entry is not None and now - entry[1] > self.aging:
del self.table[dst]
entry = None
if entry is None:
return sorted(self.ports - {in_port})
return [] if entry[0] == in_port else [entry[1 - 1]]
sw = LearningSwitch(ports=[1, 2, 3], capacity=4)
A = '00:00:00:00:00:0a'
B = '00:00:00:00:00:0b'
# Attacker on port 3 fills the table with forged sources.
for i in range(4):
sw.handle(3, f'02:00:00:00:00:{i:02x}', A, now=0)
print(sw.handle(2, B, A, now=1)) # A unknown, B cannot be learned: flood -> [1, 3]
print(sw.handle(1, A, B, now=2)) # B was never learned: flood -> [2, 3]Both frames are flooded, so port 3 sees traffic between A and B.
Limiting the damage
Port security caps the number of source addresses allowed on a port. A typical access port allows one to a few. When a port exceeds its limit, the switch applies a violation action: drop the offending frames, drop them and log, or shut the port down. Capping per-port entries means no single port can fill the whole table.
Some switches also offer aging adjustments and alerts when the table nears capacity. Static entries, covered in the next chapter, are not removed by aging, but they also consume table space.
Exercise
A switch's MAC table is full because of a MAC flooding attack. Host B sends a frame to host A. A's address was learned before the attack and is still in the table. Is the frame flooded? What if A's entry had aged out?
Show answer
If A's entry is still in the table, the frame is forwarded only to A's port, because the lookup succeeds. If A's entry aged out, the switch cannot relearn A until A sends a frame and a table slot is free. A is then unknown unicast and the frame is flooded to all other ports in the VLAN, including the attacker's.