Nftables log‑filter tricks to find hidden routing loops on a bridged LAN

Bridged LANs, Routing Loops, and Nftables Log‑Filtering

Bridged LANs are everywhere—from a tiny home lab to a mid‑size office. When a bridge gets a wrong port or a VLAN slips through, packets can start circling forever. The symptoms are subtle: a CPU spike on the bridge, packet loss, and a traffic burst that looks like a DoS. The problem is that the packets never hit a firewall rule that drops them—they just bounce, so you’re left staring at a wall of traffic with no obvious culprit.

Nftables, the successor to iptables, lets us capture the packets that are stuck in a loop without drowning the system in logs. In this post I’ll walk through a minimal, production‑ready log‑filter that isolates hidden routing loops on a bridged LAN, discuss the trade‑offs, and show how to troubleshoot when the logs don’t tell the whole story.


Why Nftables Over Other Tools?

  • Fine‑grained control – nft can match on bridge interfaces, packet counters, and even custom expressions.
  • Low overhead – the kernel implements nft, so matching is cheap compared to userspace tools like tcpdump.
  • Unified syntax – one tool for IPv4, IPv6, and raw Ethernet frames.
  • Extensibility – Lua scripts or the nft API let you automate rule changes.

Other options (tc, iproute2, bridge-utils) can capture traffic but lack the ability to filter on forwarding state or log only the packets that belong to a loop without custom scripting.


Prerequisites

ItemVersionNotes
Linux kernel≥ 6.0Nftables is fully supported; older kernels may miss some bridge expressions.
nftables≥ 1.0.0Usually installed by default on modern distros.
bridge-utils≥ 1.10For brctl commands, though ip link can also be used.
syslog or journalctlAnyWhere logs will be written.

1. Identify the Bridge and Its Ports

$ ip link show type bridge

Assume we have a bridge br0 with ports eth0, eth1, and a virtual interface veth0. The loop could involve any of these.


2. Enable Bridge Forwarding Counters

The kernel keeps per‑interface packet counters. In a loop the same packet will be forwarded many times, so the counter on the bridge interface will grow rapidly.

# Make bridge packets visible to nftables
$ sysctl -w net.bridge.bridge-nf-call-iptables=1
$ sysctl -w net.bridge.bridge-nf-call-ip6tables=1

3. Create a Dedicated Nftables Table

Keep the loop‑detection rules isolated so they don’t interfere with existing firewall policies.

$ nft add table inet loop_detect

4. Define a Counter‑Based Logging Rule

The core idea: log packets that are forwarded more than N times in a short window. We’ll use a counter expression to track how many times a packet has traversed the bridge. Because nftables can’t directly count per‑packet across hops, we approximate by logging packets that hit the bridge interface more than a threshold within a minute.

$ nft add chain inet loop_detect forward { type filter hook forward priority 0 \; policy accept \; }

Now add the rule:

$ nft add rule inet loop_detect forward \
    iifname br0 counter packets 1000 \
    log prefix "LOOP:" level warning

Explanation

  • iifname br0 – match packets entering the bridge.
  • counter packets 1000 – only log if the counter for this rule has exceeded 1000 packets since the last reset.
  • log – writes to the kernel log with the specified prefix.

Trade‑off: A low threshold (e.g., 100) will catch loops early but may produce many false positives during normal high‑traffic bursts. A high threshold (e.g., 10,000) reduces noise but delays detection.


5. Reset the Counter Periodically

We need to reset the counter to avoid a single spike from flooding the logs forever. Use a cron job or systemd timer that runs every minute:

$ cat /etc/systemd/system/nft-loop-reset.timer
[Unit]
Description=Reset nft counter for loop detection

[Timer]
OnBootSec=30s
OnUnitActiveSec=60s

[Install]
WantedBy=timers.target
$ cat /etc/systemd/system/nft-loop-reset.service
[Unit]
Description=Reset nft counter for loop detection

[Service]
Type=oneshot
ExecStart=/usr/sbin/nft flush counter inet loop_detect forward

Enable and start:

$ systemctl enable --now nft-loop-reset.timer

6. Capture the Looping Packets

When the log line appears, capture the packet with tcpdump or wireshark for deeper analysis:

$ sudo tcpdump -i br0 -nn -vvv -c 50 'ether src xx:xx:xx:xx:xx:xx'

Replace the MAC with the one seen in the log. The capture will show the packet bouncing between ports.


7. Narrow Down the Culprit

Once you have the MAC address, identify the interface:

$ ip link | grep xx:xx:xx:xx:xx:xx

If the MAC belongs to a virtual interface (veth*), the loop might be caused by a mis‑configured container or VM. If it’s a physical NIC, check the switch port configuration:

  • Verify that the port is not in a trunk mode that duplicates frames.
  • Ensure that the VLAN tagging matches the bridge configuration.
  • Check for port mirroring or SPAN that could duplicate traffic.

8. Harden the Bridge to Prevent Future Loops

8.1. Spanning Tree Protocol (STP)

STP automatically blocks redundant paths. Enable it on the bridge:

$ ip link set dev br0 type bridge stp_state 1

STP is already enabled by default on most kernels, but double‑check.

8.2. Disable Port Mirroring

If you’re using bridge vlan or bridge fdb, ensure no port mirroring is configured that could duplicate frames.

8.3. Use bridge fdb to Pin MACs

If a device should only be reachable on a specific port, pin its MAC:

$ bridge fdb add xx:xx:xx:xx:xx:xx dev eth0 permanent

This prevents the MAC from being learned on other ports, stopping accidental loops.

8.4. Limit Forwarding on the Bridge

If you know the bridge should only forward a specific set of protocols (e.g., ARP, DHCP), drop everything else:

$ nft add rule inet loop_detect forward ip protocol icmp accept
$ nft add rule inet loop_detect forward ip protocol udp accept
$ nft add rule inet loop_detect forward ip protocol tcp accept
$ nft add rule inet loop_detect forward drop

This is a hardening step that reduces the attack surface.


9. Troubleshooting Common Pitfalls

SymptomLikely CauseFix
No log entries even when traffic spikesCounter threshold too highLower counter packets value
Log floods during normal trafficCounter resets too infrequentlyReduce timer interval or add counter packets 100
Loop persists after pinning MACMultiple devices share the same MAC (e.g., cloned NIC)Verify MAC uniqueness
Log shows frames from a device that never connectedDuplicate MAC in the bridge fdbRemove stale entries: bridge fdb del xx:xx:xx:xx:xx:xx
STP blocks all trafficSTP mis‑configured or disabledEnsure stp_state is 1 and bridge stp is active

10. Advanced: Using Lua for Per‑Packet Loop Detection

For environments where loops are frequent and the threshold approach is too coarse, a Lua script can inspect packet headers and maintain a per‑MAC counter in userspace.

-- /etc/nftables/lua/loop.lua
local counters = {}
local limit = 50

function packet()
    local mac = packet:eth():src()
    counters[mac] = (counters[mac] or 0) + 1
    if counters[mac] > limit then
        log("LOOP: MAC "..mac.." exceeded "..limit.." forwards")
        counters[mac] = 0
    end
    return 0
end

Load it in nft:

$ nft add rule inet loop_detect forward lua /etc/nftables/lua/loop.lua

Trade‑off: Lua scripts add userspace overhead and complexity. Use only if the simple counter method is insufficient.


11. Real‑World Example

A small office had a 24‑port managed switch. A mis‑configured port was set to trunk mode, sending all VLANs to a server that was also connected to the bridge. The server’s NIC was set to promiscuous mode, causing it to receive duplicate frames. The bridge forwarded the frames endlessly, and the server’s CPU spiked.

Using the log‑filter above, the sysadmin noticed the LOOP: log after 1,200 packets. The capture revealed the server’s MAC bouncing between eth0 and veth0. Pinning the MAC to eth0 and disabling promiscuous mode stopped the loop within minutes.


12. Summary of Trade‑offs

ApproachProsCons
Counter‑based log ruleSimple, kernel‑level, low overheadThreshold tuning required
Lua scriptFine‑grained, per‑MAC stateUserspace overhead, complexity
STPAutomatic loop preventionRequires switch support, may introduce latency
MAC pinningPrevents accidental learningRequires manual updates for new devices

13. Final Checklist

  • Verify bridge interfaces and ports.
  • Enable bridge forwarding counters.
  • Deploy the counter‑based nft rule.
  • Set up a timer to reset counters.
  • Monitor logs for LOOP: entries.
  • Capture offending packets.
  • Pin MACs or adjust STP as needed.
  • Harden the bridge with selective forwarding rules.

By combining nftables’ lightweight logging with bridge management best practices, you can detect and eliminate hidden routing loops before they become a performance or security issue.

See also