Skip to content

Expert · Network security · topic 44 of 57 · 35 min

Layer 2 security: port security, DHCP snooping, DAI

Explanation

Access switch ports are where users, and sometimes attackers, plug in. Port security limits which MAC addresses may use a port. DHCP snooping lets only trusted ports send DHCP server replies and builds a table of who got which address. Dynamic ARP Inspection uses that table to drop fake ARP messages. Disabling DTP and changing the native VLAN stops VLAN hopping.

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.

output
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 restrict
Cisco IOS: up to 2 MAC addresses (say an IP phone and a PC), learned automatically and saved as sticky.

Port 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 modeDrops bad framesLogs and counts the violationPort state
protectYesNoStays up
restrictYesYes (syslog, SNMP, counter goes up)Stays up
shutdown (default)YesYeserr-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.

output
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 10
Cisco IOS: snooping on for VLAN 10, the uplink trusted, and user ports limited to 10 DHCP messages per second.

Snooping 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.

output
SW1(config)# ip arp inspection vlan 10
SW1(config)# interface gigabitEthernet1/0/48
SW1(config-if)# ip arp inspection trust
Cisco IOS: DAI for VLAN 10, with the uplink trusted. DAI needs DHCP snooping (or ARP ACLs for hosts with static IPs).
Careful: Common mistake: turning on DAI in a VLAN where printers or servers have static IP addresses. They are not in the snooping table, so their ARP is dropped and they vanish from the network. Add ARP ACLs for them, or give them DHCP reservations, before enabling DAI. Also, forgetting to trust the uplink with DHCP snooping blocks every real DHCP reply, so nobody gets an address.

VLAN 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.
output
SW1(config)# interface gigabitEthernet1/0/48
SW1(config-if)# switchport mode trunk
SW1(config-if)# switchport nonegotiate
SW1(config-if)# switchport trunk native vlan 999
Cisco IOS: a trunk that does not negotiate and uses an unused native VLAN.
Tip: Practise every feature here in Packet Tracer, GNS3 or CML first. Watch show port-security, show ip dhcp snooping binding and show ip arp inspection statistics as you connect test devices. Only test on your own lab or with written permission.

Need 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

Layer 2 security

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)

Task 1 · Question

Which port security violation mode drops the bad frames AND increases the violation counter, but keeps the port up?

Task 2 · Code · Python

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"}.

Task 3 · Practice

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

Question 1

1.What is the default port security violation mode?

Question 2

2.With DHCP snooping on, which port should be trusted?

Question 3

3.Dynamic ARP Inspection checks ARP messages against what?

Question 4

4.Which configuration helps prevent VLAN hopping by switch spoofing?

Question 5

5.A port was err-disabled by port security. What brings it back up, after fixing the cause?

Read more (free)

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.

From Trunks and 802.1Q tagging

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.

From How a switch learns and forwards

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.

From VLANs: what they are and why we use them

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.

From DHCP: automatic addresses

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. 1 more topic in “Network security” before its exam: Firewalls, IPS and VPNs.
  2. Module 9 exam: Network security · 13 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Wireless networking. 57 topics left in the course before the final exam.

← Previous: Access control lists (ACLs)