Expert · Network security · topic 44 of 57 · 35 min
Layer 2 security: port security, DHCP snooping, DAI
Explanation
Why Layer 2 needs its own protection
Firewalls and ACLs usually sit at Layer 3, between networks. But inside one VLAN, devices talk directly through the switch, and the old Layer 2 protocols (Ethernet MAC learning, DHCP, ARP) trust whatever they hear. Someone already plugged into an access port can abuse that trust. Cisco switches have features that remove this blind trust. You configure them on the switch, at the edge, where users connect.
Port security
The threat: a switch learns MAC addresses into a table of limited size. A MAC flooding attack sends frames from thousands of fake MAC addresses until the table is full, and the switch then floods frames out of all ports, where they can be captured. Port security limits how many MAC addresses a port may learn, and optionally which ones. It also stops someone unplugging a desk PC and plugging in their own laptop.
SW1(config)# interface gigabitEthernet1/0/5
SW1(config-if)# switchport mode access
SW1(config-if)# switchport access vlan 10
SW1(config-if)# switchport port-security
SW1(config-if)# switchport port-security maximum 2
SW1(config-if)# switchport port-security mac-address sticky
SW1(config-if)# switchport port-security violation restrictPort security works only on a port set statically to access or trunk, not one left in a dynamic DTP mode. The default is maximum 1 and violation shutdown. Sticky learning adds the learned MAC addresses to the running configuration, so they survive a reboot once you save the config.
| Violation mode | Drops bad frames | Logs and counts the violation | Port state |
|---|---|---|---|
| protect | Yes | No | Stays up |
| restrict | Yes | Yes (syslog, SNMP, counter goes up) | Stays up |
| shutdown (default) | Yes | Yes | err-disabled: the port goes down |
An err-disabled port stays down until an engineer brings it back. Fix the cause first, then enter shutdown and no shutdown on the interface. You can also let the switch recover by itself with errdisable recovery cause psecure-violation and errdisable recovery interval 300 (seconds). Check status with show port-security interface g1/0/5.
DHCP snooping
The threat: a fake (rogue) DHCP server on the LAN answers clients first and hands out its own address as the default gateway or DNS server. Traffic then flows through the attacker, a man-in-the-middle. Sometimes it is not even an attack, just a home Wi-Fi router someone plugged in backwards. DHCP snooping splits ports into trusted and untrusted. Untrusted ports (users) may send DHCP client messages but not server messages (OFFER, ACK); those are dropped. Trusted ports are the uplinks towards the real DHCP server.
SW1(config)# ip dhcp snooping
SW1(config)# ip dhcp snooping vlan 10
SW1(config)# interface gigabitEthernet1/0/48
SW1(config-if)# ip dhcp snooping trust
SW1(config-if)# exit
SW1(config)# interface range gigabitEthernet1/0/1 - 24
SW1(config-if-range)# ip dhcp snooping limit rate 10Snooping also builds the binding table: for every lease it records the client MAC, IP address, VLAN, port and lease time. See it with show ip dhcp snooping binding. The rate limit stops a DHCP starvation attack, where one port sends floods of requests to use up every address in the pool; a port that goes over the limit is err-disabled.
Dynamic ARP Inspection (DAI)
The threat: ARP poisoning. ARP (Address Resolution Protocol) maps an IP address to a MAC address, and hosts believe any ARP reply they receive. An attacker can send fake replies saying "the gateway IP is at my MAC", and quietly receive everyone's traffic. DAI checks every ARP message on untrusted ports against the DHCP snooping binding table. If the IP and MAC pair is not in the table, the ARP message is dropped and logged.
SW1(config)# ip arp inspection vlan 10
SW1(config)# interface gigabitEthernet1/0/48
SW1(config-if)# ip arp inspection trustVLAN hopping and how to stop it
VLAN hopping means sending traffic into a VLAN you are not supposed to reach. There are two classic forms. In switch spoofing, a host pretends to be a switch using DTP (Dynamic Trunking Protocol) so the port becomes a trunk carrying all VLANs. In double tagging, a frame carries two 802.1Q tags and abuses the native VLAN (the VLAN sent untagged on a trunk). The prevention is simple configuration:
- Set user ports to switchport mode access, which turns off trunk negotiation.
- On real trunks, use switchport mode trunk with switchport nonegotiate, so DTP is not used at all.
- Change the native VLAN on trunks to an unused VLAN (not VLAN 1 and not a user VLAN), the same on both ends.
- Shut down unused ports and put them in an unused VLAN.
SW1(config)# interface gigabitEthernet1/0/48
SW1(config-if)# switchport mode trunk
SW1(config-if)# switchport nonegotiate
SW1(config-if)# switchport trunk native vlan 999Need help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
Layer 2 security: port security, DHCP snooping, DAI · 1/7
This lesson protects the access ports where users plug in, at Layer 2.
Prefer a video by a teacher? Find free videos on “CCNA port security DHCP snooping dynamic ARP inspection configuration” on YouTube ↗
In real life: A rogue router at a BPO floor
At a BPO in Gurugram, half the agents on one floor suddenly got 192.168.0.x addresses and lost access to the CRM. Someone had plugged a home Wi-Fi router into a desk port, and it was answering DHCP. The network team enabled DHCP snooping on the access switches with only the uplinks trusted, added DAI, and set port security to maximum 2 with violation restrict so the logs show any new device. The next time a stray router was plugged in, its DHCP offers were dropped and the port showed up in the logs within a minute.
Practice (0/3 done)
Which port security violation mode drops the bad frames AND increases the violation counter, but keeps the port up?
Simulate port security. Write simulate(macs, maximum, mode) where macs is the list of source MAC addresses of frames arriving on one port, in order, and mode is "protect", "restrict" or "shutdown". Rules: a MAC already learned is forwarded. A new MAC is learned and forwarded if fewer than maximum are learned. Otherwise it is a violation: protect drops it silently; restrict drops it and adds 1 to violations; shutdown adds 1 to violations and the port becomes "err-disabled". Once err-disabled, every later frame is dropped and not counted. Return a dict: {"forwarded": number, "violations": number, "status": "up" or "err-disabled"}.
Packet Tracer lab: one switch, a server running DHCP on the uplink port (Gi0/1), and three PCs in VLAN 10. Add a second "rogue" DHCP server on a user port. Configure DHCP snooping so only the real server works, then show the binding table.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Firewalls, IPS and VPNsNetwork security · same module
- Securing device access: secrets and SSHNetwork security · same module
- VLANs: what they are and why we use themSwitching and VLANs
- DHCP: automatic addressesIP services: DHCP, DNS, NAT and more
- ARP, the default gateway and pingEthernet, MAC addresses and the Cisco CLI
- Inter-VLAN routingSwitching and VLANs
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
What is the default port security violation mode?
Answer: shutdown
By default a violation err-disables the port (shutdown mode), with a maximum of 1 MAC address.
With DHCP snooping on, which port should be trusted?
Answer: The uplink towards the real DHCP server
Only ports that lead to legitimate DHCP servers (or relays) should be trusted.
Dynamic ARP Inspection checks ARP messages against what?
Answer: The DHCP snooping binding table
DAI compares the IP and MAC in each ARP message with the bindings learned by DHCP snooping.
Which configuration helps prevent VLAN hopping by switch spoofing?
Answer: switchport mode access on user ports
An access port does not negotiate a trunk, so a host cannot use DTP to turn it into one.
A port was err-disabled by port security. What brings it back up, after fixing the cause?
Answer: shutdown, then no shutdown
An err-disabled port is recovered with shutdown and no shutdown, or automatically with errdisable recovery.
How are frames in the native VLAN sent on an 802.1Q trunk?Related
Answer: Untagged
The native VLAN is the one VLAN whose frames cross the trunk without a tag.
A frame to ffff.ffff.ffff arrives on port 5 of a 24-port switch (all in one VLAN). Where does it go?Related
Answer: All ports except port 5
Broadcasts are flooded out of every port except the incoming one.
An IP phone and a PC share one switch port. Which command puts the phone traffic in VLAN 50?Related
Answer: switchport voice vlan 50
switchport voice vlan 50 tells the phone to tag voice frames with VLAN 50; the PC stays in the access VLAN.
Which UDP port does a DHCP server listen on?Related
Answer: 67
The server uses UDP 67 and the client UDP 68. 53 is DNS and 69 is TFTP.
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 1 more topic in “Network security” before its exam: Firewalls, IPS and VPNs.
- Module 9 exam: Network security · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Wireless networking. 57 topics left in the course before the final exam.