Sub topic 15
Static entries and port security
This chapter assumes you know how a switch learns source addresses, floods unknown destinations, and ages entries out. It also assumes you know that learned entries are per VLAN. Those mechanisms trust whatever frames arrive. This chapter covers two ways to take that trust away: entries you configure by hand, and limits on what a port may learn.
Why dynamic learning needs limits
Dynamic learning has three weaknesses:
- Spoofing. Any host can send a frame with someone else's source MAC. The switch moves the entry to the attacker's port, and frames meant for the victim go to the attacker.
- Table exhaustion. The table is finite. A host that sends frames with thousands of invented source addresses can fill it. Once it is full, new legitimate hosts cannot be learned, so frames for them are flooded and can be seen on every port.
- Unplanned devices. Someone plugs an unapproved laptop or a small switch into a wall port, and the table accepts it.
Static entries and port security address these problems differently. Static entries pin specific addresses. Port security constrains what a port may learn.
Static entries
A static entry is a MAC-to-port mapping that you configure. It differs from a dynamic entry in three ways:
- It never ages out, so it survives idle periods and topology flushes.
- Learning does not overwrite it. A frame with that source address arriving on another port does not move the entry.
- It survives only as long as the configuration does. On most platforms it is stored in the running configuration, so it is lost if you do not save it before a reboot.
On a Cisco IOS-style switch, a static entry also names a VLAN, because the table is per VLAN:
Switch(config)# mac address-table static 0011.2233.4455 vlan 1 interface GigabitEthernet0/1
Switch(config)# mac address-table static 0011.2233.99aa vlan 1 drop
Switch# show mac address-table staticThe drop form discards frames to or from that address in that VLAN. It is a blunt way to silence a known-bad device.
On a Linux bridge, bridge fdb add creates an entry that is permanent by default:
bridge fdb add 00:11:22:33:44:55 dev eth1 master static
bridge fdb show br br0 | grep 00:11:22:33:44:55
bridge fdb del 00:11:22:33:44:55 dev eth1 masterIn bridge fdb show output, a static entry carries the permanent flag, as in the previous chapter's examples.
Where static entries help, and where they hurt
Static entries suit a few fixed devices, such as a server, a router interface or a firewall. The switch never floods traffic for them, and nobody can take over their address by spoofing.
They hurt when the device moves. If you replace a server's NIC or plug the server into another port, the static entry still points at the old port. Frames for that server go to the wrong place and nothing recovers on its own, because aging and learning have both been switched off for that address. When a static entry is the cause of an outage, show mac address-table makes it easy to spot: the entry's type is STATIC and its port is wrong.
Static entries also do not scale. Maintaining hundreds of them by hand is error-prone, which is the problem learning was designed to remove.
Port security
Port security is a Cisco IOS feature (other vendors have equivalents under names such as MAC limiting or sticky MAC). It sets a limit on the number of source addresses a port may use, and optionally the exact addresses allowed. The port must be statically configured as an access port or a trunk. It cannot be in a dynamic mode.
Switch(config)# interface GigabitEthernet0/2
Switch(config-if)# switchport mode access
Switch(config-if)# switchport port-security
Switch(config-if)# switchport port-security maximum 2
Switch(config-if)# switchport port-security mac-address sticky
Switch(config-if)# switchport port-security violation restrict
Switch(config-if)# switchport port-security aging time 10
Switch# show port-security interface GigabitEthernet0/2
Switch# show port-security addressThe settings work as follows:
- maximum is the number of secure addresses the port allows. The default is 1.
- Secure addresses come from three sources: static configuration (
switchport port-security mac-address <mac>), dynamic learning up to the maximum, or sticky learning. - sticky turns learned addresses into configuration lines, as if you had typed them. They persist across a reboot only if you save the configuration.
- aging time lets non-static secure addresses expire after the given number of minutes, which frees room for a new device.
What happens on a violation
A violation occurs in two cases. First, a new source address arrives when the port already holds its maximum. Second, an address secured on one port appears on a different port in the same VLAN. The violation mode decides the response.
| Mode | Offending frames | Logged and counted | Port state |
|---|---|---|---|
| protect | Dropped | No | Stays up |
| restrict | Dropped | Yes | Stays up |
| shutdown (default) | Dropped | Yes | Goes to err-disabled |
In shutdown mode, the port stays down until an administrator runs shutdown then no shutdown on it. Alternatively, you can enable automatic recovery with errdisable recovery cause psecure-violation. Frames from addresses that are already secured keep flowing in protect and restrict modes. Only the surplus addresses are dropped.
A decision flow
The following diagram shows what a port-security switch does with each arriving frame.
Modeling it in code
The following program extends the learning switch from earlier in the tutorial. It adds static entries that are never aged or moved, and a per-port address limit with a configurable violation mode. It is a simplified model of the Cisco behavior. It treats every static entry as enforced, so a frame with a static source address arriving on another port is a violation.
def is_group(mac):
return int(mac.split(':')[0], 16) & 1 == 1
class SecureSwitch:
def __init__(self, ports, aging=300, limits=None, violation="shutdown"):
self.ports = set(ports)
self.aging = aging
self.limits = limits or {} # port -> max addresses
self.violation = violation # protect | restrict | shutdown
self.table = {} # mac -> (port, last_seen, static)
self.disabled = set()
self.violations = 0
def add_static(self, mac, port):
self.table[mac] = (port, None, True)
def _expire(self, now):
stale = [m for m, (_, seen, static) in self.table.items()
if not static and now - seen > self.aging]
for mac in stale:
del self.table[mac]
def _violate(self, port):
if self.violation != "protect":
self.violations += 1
if self.violation == "shutdown":
self.disabled.add(port)
return False
def _learn(self, port, src, now):
entry = self.table.get(src)
if entry is not None and entry[2]: # static entry
return True if entry[0] == port else self._violate(port)
limit = self.limits.get(port)
is_new_here = entry is None or entry[0] != port
if limit is not None and is_new_here:
count = sum(1 for p, _, _ in self.table.values() if p == port)
if count >= limit:
return self._violate(port)
self.table[src] = (port, now, False)
return True
def handle(self, in_port, src, dst, now):
if in_port in self.disabled:
return []
self._expire(now)
if not is_group(src) and not self._learn(in_port, src, now):
return []
entry = None if is_group(dst) else self.table.get(dst)
if entry is None:
return sorted(self.ports - {in_port} - self.disabled)
out_port = entry[0]
if out_port == in_port or out_port in self.disabled:
return []
return [out_port]
A, B, SERVER = '00:00:00:00:00:0a', '00:00:00:00:00:0b', '00:00:00:00:00:5e'
sw = SecureSwitch([1, 2, 3], limits={1: 1}, violation="restrict")
sw.add_static(SERVER, 3)
print(sw.handle(1, A, SERVER, now=0)) # [3]: A learned, static dst
print(sw.handle(1, B, SERVER, now=1)) # []: port 1 is at its limit of 1
print(sw.handle(2, SERVER, A, now=2)) # []: static address on the wrong port
print(sw.handle(3, SERVER, A, now=1000)) # [1, 2]: A aged out, SERVER did not
print(sw.violations) # 2The last frame shows the difference between the two entry types. The dynamic entry for A has expired, so the frame is flooded. The static entry for the server is still present.
Linux bridges
A Linux bridge has no direct equivalent of the Cisco maximum. You can approximate port security with building blocks:
- Turn off learning on a port with
bridge link set dev eth1 learning off, then add static entries withbridge fdb add. Only the addresses you list are reachable on that port. - Recent kernels also support locked ports (
bridge link set dev eth1 locked on). A locked port drops frames from a source address that has no matching static entry. Check that your kernel version supports it before relying on it.
Limits of these defenses
- Spoofing an allowed address still works. If an attacker copies the MAC of a secured host, port security sees a permitted address. It stops a rogue second device only if the real host is also present on a different port.
- A limit of 1 breaks legitimate setups. An IP phone with a PC behind it needs a maximum of at least 2 (or 3, with a voice VLAN). Virtualization hosts need more still.
- A small limit is not a table-size defense for the whole switch. It protects the table only on ports where you configured it.
Exercise
A server with a static entry on port Gi0/1 is moved to Gi0/5 without changing the switch configuration. The server transmits, and its frames arrive on Gi0/5. Describe what happens to frames sent to the server, and name one fix.
Show answer
The static entry is not overwritten by learning, so frames for the server keep going out of Gi0/1, where nothing is connected. The server is unreachable until the entry is corrected. (If port security is enforced, its frames on Gi0/5 may also cause a violation.) The fix is to remove the static entry and add a new one on Gi0/5, or to remove it and let the switch learn the address dynamically.
Exercise
A port has maximum 2 and violation mode restrict. Two hosts are already secured on it. A third host connects and sends frames. What happens to the third host's frames, the existing hosts' frames, and the port state?
Show answer
The third host's frames are dropped, and each violation is logged and counted. The two secured hosts keep communicating normally. The port stays up because restrict does not err-disable it. In shutdown mode, the port would go into err-disabled state and the two existing hosts would lose connectivity too.