Linux Transparent Bridge Firewall with nftables: Filter Layer 2 and Layer 3 Traffic Without Changing IP Subnets

Networking tutorial - IT technology blog
Networking tutorial - IT technology blog

Context & Why

Sooner or later, you inherit a network segment you simply cannot re-architect. Maybe it is a legacy 192.168.10.0/24 industrial subnet with hardcoded IPs, or a flat colocation VLAN where another team owns the upstream gateway. Putting a traditional Layer 3 router-firewall in front of those hosts means re-subnetting, updating every default gateway, and risking a messy outage.

A transparent bridge firewall solves this as an inline Layer 2 bump-in-the-wire. You drop a Linux host with two NICs directly between the switch and your protected servers. Frames cross a virtual bridge (br0) at Layer 2, so existing IPs, gateways, and ARP tables never change. Meanwhile, the kernel inspects every frame and enforces both Layer 2 (MAC, EtherType, ARP) and Layer 3/4 (IP, TCP, UDP) rules—typically adding under 50 microseconds of forwarding latency.

Years ago, building this on Linux meant juggling ebtables, iptables, and the br_netfilter module. That stack was notorious for tangled packet-flow diagrams and cryptic sysctl toggles. Modern nftables replaces all of it with a native bridge family. You get a single 30-line ruleset that runs directly in the bridge hook pipeline, with predictable behavior even on saturated 1 Gbps and 10 Gbps links.

Installation

Grab a Linux host running Debian 12, Ubuntu 24.04 LTS, or Rocky Linux 9. You want kernel 5.10 or newer for reliable bridge connection tracking, plus at least two data NICs—say, eth0 toward the upstream switch and eth1 toward your servers. Keep a third out-of-band management port (eth2) or local console handy. One bad drop rule on an inline box can cut off your SSH session in an instant.

Debian and Ubuntu users can install nftables and iproute2 in two commands:

sudo apt update
sudo apt install -y nftables iproute2

Rocky Linux, AlmaLinux, and other RHEL derivatives use dnf instead:

sudo dnf install -y nftables iproute

Next, check the installed nftables version and enable the service at boot:

nft --version
sudo systemctl enable nftables

Stateful tracking (ct state) inside the bridge family also requires the nf_conntrack_bridge kernel module. Load it now and persist it across reboots:

sudo modprobe nf_conntrack_bridge
echo "nf_conntrack_bridge" | sudo tee /etc/modules-load.d/nf_conntrack_bridge.conf

Configuration

Two steps get the firewall inline: wiring up the Layer 2 bridge and loading your nftables bridge family ruleset.

1. Create the Transparent Linux Bridge

Strip any IP addresses off eth0 and eth1, create br0, and enslave both ports. Because you manage the host via eth2, br0 stays completely unnumbered—no IP address needed.

sudo ip addr flush dev eth0
sudo ip addr flush dev eth1

sudo ip link add name br0 type bridge
sudo ip link set dev br0 type bridge stp_state 0

sudo ip link set dev eth0 master br0
sudo ip link set dev eth1 master br0

sudo ip link set dev eth0 up
sudo ip link set dev eth1 up
sudo ip link set dev br0 up

Disabling Spanning Tree Protocol (stp_state 0) makes sense for a single inline host with no redundant paths, and it avoids the classic 30-second STP forwarding delay on link up. If your switches have redundant links, leave STP or BPDU forwarding enabled so you do not create a switching loop. On Ubuntu, persist this setup in /etc/netplan/01-bridge.yaml:

network:
  version: 2
  ethernets:
    eth0:
      dhcp4: false
      dhcp6: false
    eth1:
      dhcp4: false
      dhcp6: false
  bridges:
    br0:
      interfaces: [eth0, eth1]
      dhcp4: false
      dhcp6: false
      parameters:
        stp: false
        forward-delay: 0

Run sudo netplan apply to bring the persistent bridge online.

2. Write the nftables Bridge Family Ruleset

Open /etc/nftables.conf. Unlike the inet family used for host or L3 router firewalls, the bridge family hooks directly into Layer 2 frames. Every packet moving between eth0 and eth1 passes through its forward chain.

Here is a default-drop ruleset for a protected server at 192.168.10.50. It permits ARP and stateful return traffic, locks SSH to jump host 192.168.10.10, opens HTTPS, blocks a rogue MAC, and stops unauthorized DHCP offers on eth1:

#!/usr/sbin/nft -f

flush ruleset

table bridge filter {
    chain forward {
        type filter hook forward priority 0; policy drop;

        # Allow Layer 2 ARP so hosts can resolve MAC addresses
        ether type arp accept

        # Allow established and related connections (requires nf_conntrack_bridge)
        ct state established,related accept
        ct state invalid drop

        # Block rogue DHCP offers coming from the protected server port
        iifname "eth1" udp sport 67 drop

        # Example Layer 2 MAC filter: drop a specific unauthorized device
        ether saddr aa:bb:cc:dd:ee:ff drop

        # Allow HTTPS (443) from anywhere on the LAN to the web server
        ip daddr 192.168.10.50 tcp dport 443 ct state new accept

        # Allow SSH (22) only from the admin jump host
        ip saddr 192.168.10.10 ip daddr 192.168.10.50 tcp dport 22 ct state new accept

        # Allow outbound DNS and NTP from protected servers to the gateway
        iifname "eth1" udp dport { 53, 123 } ct state new accept

        # Rate-limited logging for dropped frames
        limit rate 5/minute log prefix "BR-DROP: "
    }
}

Test the syntax before touching live traffic:

sudo nft -c -f /etc/nftables.conf

Clean output? Load the ruleset and restart the service:

sudo nft -f /etc/nftables.conf
sudo systemctl restart nftables

Verification & Monitoring

Once the box is inline, check both Layer 2 switching and Layer 3/4 rule matches.

Start by confirming that eth0 and eth1 are enslaved to br0 in the forwarding state, and verify that MAC addresses are populating the bridge forwarding database (FDB):

bridge link show
bridge fdb show br br0

Dump the active ruleset next. If you add the counter keyword to individual rules, nft list table bridge filter prints exact packet and byte counts for every match:

sudo nft list table bridge filter

When a legitimate flow breaks, skip the guesswork. Insert a temporary trace rule at the top of the forward chain (using insert so it runs before any accept or drop rule) and watch evaluation live:

sudo nft insert rule bridge filter forward ip daddr 192.168.10.50 meta nftrace set 1
sudo nft monitor trace

Delete that trace rule as soon as you find the culprit—leaving it active on a busy link will spam your terminal and burn CPU. For ongoing visibility, pull your rate-limited drop logs from journalctl:

sudo journalctl -k -g "BR-DROP:" --since "10 minutes ago"

Keep two field lessons in mind before walking away. First, never omit ether type arp accept; without ARP, hosts cannot resolve MAC addresses and every IP rule beneath it is dead weight. Second, if your network has asymmetric routing where return packets bypass br0, skip ct state for those flows and use stateless interface-plus-port rules instead. Follow those two guardrails and you can drop inline filtering into any legacy subnet in under 15 minutes.

Share: