Fixing a Midnight DNS Loop: When nftables Drops Queries on VPN Bridge

Midnight DNS Loop: When nftables Drops Queries on a VPN Bridge

When a VPN bridge sits in front of a local DNS server, a single nftables rule can silently swallow all DNS traffic. The result is a “midnight DNS loop” – clients resolve fine during the day but hit a wall after 00:00 because the bridge’s default policy flips. Below I walk through the why, how to spot it, and a hardened fix that keeps the bridge tight without breaking the DNS that everyone actually needs.


Why the Loop Happens

A VPN bridge usually looks like this:

tun0 ──┐
       │
       ▼
     br0   ── eth0

Most people add a blanket drop rule to the bridge’s forward chain to stop stray packets from leaking out:

table inet vpn {
    chain forward {
        type filter hook forward priority 0; policy drop;
    }
}

If the bridge also forwards DNS (port 53) to a local resolver, that drop rule swallows the packets. Some setups then add a time‑based exception:

table inet vpn {
    chain forward {
        type filter hook forward priority 0; policy drop;
        ip dport 53 counter accept ct state established,related
        # Accept during daytime only
        ip dport 53 ip saddr @allowed set accept
    }
}

The @allowed set is populated by a cron job that runs at 06:00 and clears it at 00:00. When the set is empty, every DNS query hits the drop rule, and clients see a DNS loop that only resolves again after the next cron run.


Detecting the Drop

  1. Check nftables state

    nft list table inet vpn
    

    Look for a policy drop on the forward chain and any time‑based rules.

  2. Trace the packet path

    tcpdump -i br0 udp port 53
    

    If you see queries but no responses, the bridge is dropping them.

  3. Examine the bridge’s default policy

    iptables -t mangle -L FORWARD -v -n
    

    (nftables replaces iptables, but legacy chains may still exist.)

  4. Log dropped packets
    Temporarily change the policy to log to capture the drop reason:

    nft add rule inet vpn forward log prefix "DNS drop: "
    

Hardened Fix

Instead of a blanket drop, use a more granular approach that preserves security while allowing DNS.

1. Separate the DNS interface

Create a dedicated bridge for VPN clients that only forwards necessary ports. For example, br-vpn for VPN traffic and br-dns for DNS.

ip link add name br-dns type bridge
ip link set br-dns up
ip link set eth0 master br-dns
ip link set tun0 master br-dns

Move the DNS resolver to br-dns and drop all other traffic on that bridge.

2. Use nftables with stateful rules

table inet vpn {
    chain forward {
        type filter hook forward priority 0; policy drop;
        # Allow established/related traffic
        ct state established,related accept
        # Allow DNS from VPN clients
        ip dport 53 ip saddr @vpn_clients accept
        # Drop everything else
        ip protocol icmp drop
    }
}

Define the @vpn_clients set once, e.g.:

set vpn_clients {
    type ipv4_addr
    elements = { 10.8.0.0/24 }
}

This rule set is static; no cron job is needed.

3. Keep the resolver reachable

If the resolver runs on the host, bind it to 127.0.0.1 and forward queries from the bridge to localhost:

nft add rule inet vpn forward ip daddr 127.0.0.1 ip dport 53 accept

Or use dnsmasq with the --interface=br-dns option.

4. Verify with nft list ruleset

nft list ruleset | grep -A5 forward

You should see only the three rules above, no time‑based logic.


Trade‑offs

ApproachProsCons
Time‑based setSimple to implement, no extra interfacesRisk of silent drops, hard to audit
Dedicated bridgeClear separation of duties, easier to auditRequires additional interface, more complex routing
Stateful rulesKeeps default drop, allows only needed trafficRequires careful rule ordering, may miss edge cases

For production environments, the dedicated bridge plus stateful rules is the safest. It eliminates the need for cron jobs, reduces attack surface, and keeps DNS traffic visible for logging.


Common Pitfalls

  • Over‑dropping ICMP: Some VPN clients rely on ICMP for reachability checks. If you drop all ICMP, clients may think the network is unreachable. Add an explicit allow rule if needed.
  • Missing NAT: If the VPN bridge uses NAT, ensure iptables -t nat rules are updated to reflect the new bridge names.
  • Resolver binding: If the resolver listens only on 127.0.0.1, ensure the bridge forwards to localhost; otherwise, clients will get NXDOMAIN.

Quick Checklist

  • nft list table inet vpn shows no time‑based rules.
  • DNS traffic appears in tcpdump -i br-dns udp port 53.
  • No log entries for dropped DNS packets.
  • Firewall policy remains drop for non‑DNS traffic.
  • Clients can resolve names after midnight without reboot.

Further Reading

See also