Sub topic 14
Finite table size and MAC flooding
This chapter assumes you know how a switch learns source addresses, forwards by destination lookup, floods unknown unicast frames, and ages out idle entries. It covers what happens when the MAC address table runs out of room, how an attacker can force that, and how to limit the damage.
The table is a fixed-size resource
The MAC address table lives in dedicated memory, often called CAM (content-addressable memory) or a hash table in the switching chip. Its size is set by the hardware. Typical figures:
| Device class | Typical MAC table size |
|---|---|
| Small unmanaged or home switch | 1,000 to 8,000 entries |
| Enterprise access switch | 8,000 to 32,000 entries |
| Data center or core switch | 100,000 to several hundred thousand entries |
Several things can make the usable size smaller than the number on the data sheet:
- Shared memory. On some platforms the same memory also holds other tables, so the MAC limit depends on how the switch is configured.
- Hash collisions. Many chips store entries in hash buckets. If several addresses hash to the same full bucket, the new address cannot be stored even though the table as a whole has free space.
- Per-VLAN entries. The table is keyed by VLAN and MAC address. A host that appears in three VLANs uses three entries.
Normal networks stay far below the limit. Large Layer 2 domains with virtualization are the usual exception, because every virtual machine has its own MAC address and each one counts as an entry.
What happens when the table is full
Learning is an optimization. Forwarding still works without it, because an unknown destination is flooded. So a full table does not stop the switch. It makes the switch worse at its job.
The exact behavior depends on the vendor and model:
- Some switches stop learning new addresses and keep the existing entries until they age out.
- Some evict an old entry to make room for the new one.
- Some hash-based tables reject only the entries that land in a full bucket.
In every case, frames sent to an address that is not in the table are flooded out of all ports in the VLAN except the ingress port. The cost is bandwidth, and every host in the VLAN receives traffic that was meant for one host. Learning also stops being reliable. If a host's entry ages out while the table is full, the switch may not be able to learn it again until space frees up.
The following program models the "stop learning when full" behavior. It is self-contained and runs as shown.
BROADCAST = 'ff:ff:ff:ff:ff:ff'
def is_group(mac):
# The least significant bit of the first octet marks multicast/broadcast.
return int(mac.split(':')[0], 16) & 1 == 1
def fake_mac(n):
# First octet 0x02: unicast, locally administered.
return '02:00:%02x:%02x:%02x:%02x' % (
(n >> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255)
class CappedSwitch:
def __init__(self, ports, capacity):
self.ports = set(ports)
self.capacity = capacity
self.table = {} # mac -> port (aging omitted for brevity)
self.dropped_learns = 0
def handle(self, in_port, src, dst):
if not is_group(src):
if src in self.table or len(self.table) < self.capacity:
self.table[src] = in_port
else:
self.dropped_learns += 1 # table full: source not learned
out = None if is_group(dst) else self.table.get(dst)
if out is None:
return sorted(self.ports - {in_port}) # flood
return [] if out == in_port else [out]
sw = CappedSwitch(ports=[1, 2, 3], capacity=4)
A = '00:00:00:00:00:0a' # host on port 1
B = '00:00:00:00:00:0b' # host on port 2
C = '00:00:00:00:00:0c' # new host on port 1, arrives later
print(sw.handle(1, A, B)) # B unknown: flood -> [2, 3]
print(sw.handle(2, B, A)) # A known -> [1]
for n in range(10): # attacker on port 3 sends 10 fake sources
sw.handle(3, fake_mac(n), BROADCAST)
print(sw.handle(1, C, B)) # B known -> [2], but C is not learned
print(sw.handle(2, B, C)) # C unknown: flooded -> [1, 3]
print(len(sw.table), sw.dropped_learns) # 4 9The last reply from B to C reaches port 3 even though the attacker is not the destination. The table holds A, B and two fake addresses, so C never gets an entry.
The MAC flooding attack
MAC flooding (also called CAM table overflow) exploits this behavior. The attacker connects to a switch port and sends frames with a different random source MAC address each time. Tools such as macof can generate tens of thousands of such frames per second. Each frame teaches the switch a new address on the attacker's port.
The attack works in these steps:
- The attacker sends frames with random source addresses, and the switch learns each one.
- The table fills with fake entries on the attacker's port.
- Real hosts' entries age out, or real hosts that are new to the switch cannot be learned.
- Unicast frames to addresses that are not in the table are flooded out of every port in the VLAN, including the attacker's port.
- The attacker captures traffic that should never have reached that port.
Limits of the attack
MAC flooding is noisy and has real constraints:
- VLAN scope. Flooding stays inside the VLAN of the attacker's port. The attacker cannot see traffic in other VLANs.
- Rate against aging. Entries for active hosts are refreshed constantly. The attacker must fill the table faster than the switch can age out and recycle entries, and must keep sending to hold it full.
- Only unlearned destinations leak. Conversations whose entries are already in the table keep flowing to one port. Traffic leaks once an entry expires or a new host appears.
- Collateral damage. The flood of unknown unicast traffic slows the whole VLAN, so the attack is often noticed as a performance problem first.
Because of these limits, the attack is considered a mid-level threat. It is still cheap to run, and most access switches are exposed to it by default.
Spotting a full or flooded table
Watch the number of entries, not just the entries themselves.
On a Cisco IOS-style switch:
Switch# show mac address-table count
Switch# show mac address-table dynamic interface GigabitEthernet0/3The first command shows the totals per VLAN and the space left. The second shows what one port has learned. A single access port that owns thousands of dynamic entries is a strong sign of flooding or of a misconfigured downstream device.
On a Linux bridge, count the dynamic entries on a port:
bridge fdb show br br0 | grep -c "dev eth2"Recent Linux kernels can also cap the number of learned entries per bridge or port. Check your kernel and ip link documentation for the exact option on your version.
Other signs include log messages about MAC move or flap events, switch CPU or link utilization spikes without a matching increase in legitimate traffic, and many hosts reporting traffic they should not see.
Limiting the damage
The standard defense is to limit how many addresses a port may learn. This is port security. The next chapter covers it in detail. Here is the minimum needed to counter flooding on an access port:
interface GigabitEthernet0/2
switchport mode access
switchport port-security
switchport port-security maximum 2
switchport port-security violation restrict
switchport port-security mac-address stickyThe maximum value is the number of addresses the port may learn. Set it to the number of devices you expect, such as one PC or a PC plus a phone. When a port sees an address beyond the limit, the violation mode decides what happens:
| Mode | Offending frames | Log message | Port state |
|---|---|---|---|
| protect | Dropped | No | Stays up |
| restrict | Dropped | Yes, with a violation counter | Stays up |
| shutdown | Dropped | Yes | Port goes to the err-disabled state |
sticky makes the port keep the learned addresses in the running configuration. Other protections work alongside it:
- Storm control limits the rate of unknown unicast, broadcast and multicast traffic on a port.
- 802.1X admits only authenticated devices to the port at all.
- Smaller VLANs reduce the number of hosts that could receive flooded traffic.
Key points
- The MAC table has a fixed size, and the usable size can be lower than the advertised size.
- A full table does not stop forwarding. It causes unknown destinations to be flooded and stops new hosts from being learned.
- MAC flooding fills the table with fake source addresses so that frames are flooded to the attacker's port.
- The attack is limited to one VLAN and mainly exposes traffic whose entries are missing from the table.
- Per-port address limits are the main defense, with storm control and authentication as additions.
Exercise
A switch has a 4,000-entry MAC table that an attacker fills with fake addresses from port 7. A user on port 2 has an existing entry, and keeps talking to a server on port 3, which also has an entry. A new laptop on port 4 then connects and tries to reach the same server. Which frames are visible on port 7, and why?
Show answer
Frames between the user on port 2 and the server on port 3 are forwarded only to those two ports, because both addresses are already in the table and their entries are refreshed by traffic. The attacker does not see them. The laptop on port 4 cannot be learned because the table is full, so the server's replies to the laptop have an unknown destination. The switch floods them to every other port in the VLAN, including port 7, so the attacker sees the server's traffic to the laptop. The laptop's own frames to the server are forwarded to port 3 only, since the server's address is known.
Exercise
An access port is configured with switchport port-security maximum 2 and violation shutdown. An attacker runs a MAC flooding tool on that port. What happens, and why is this better than leaving the port unrestricted?
Show answer
The port learns two addresses. When the third source address arrives, the switch registers a violation, drops the frame, logs it, and puts the port into the err-disabled state, which stops all traffic through it. The attacker can add at most two entries instead of filling the table, so unknown unicast traffic does not start leaking to the attacker's port. The cost is that an administrator must re-enable the port, or configure err-disable recovery, after a violation.