18.5. Troubleshooting Firewall Behavior

A misbehaving firewall produces symptoms - timeouts, half-open connections, services visible from one network and invisible from another - and the tools of the last three sections identify the cause in a fixed order. Figure 18.3 fixes that order.

Narrowing down an unexpected drop

Figure 18.3. A conceptual inspection order for a failing connection: the failure's shape first, then counters and logs for the rule, then a per-packet trace, then the return path - each step naming what the next one needs.

18.5.1. Tracing Packets Through the Ruleset

Counters say which rule a packet matched; a trace shows the whole path. The kernel records a per-packet trace when any rule sets the packet's meta nftrace flag, and nft monitor trace streams the record. Trace one flow on fwclient - the echo request it sends to the router. In the root shell, insert the tracing rule; it prints nothing on success:

# nft insert rule inet filter output ip daddr 192.168.218.2 icmp type echo-request meta nftrace set 1

The rule matches echo requests to one address and sets the flag - it makes no verdict of its own, so the insert position is safe anywhere. Confirm it and note its handle:

# nft -a list chain inet filter output
table inet filter {
    chain output { # handle 3
        type filter hook output priority filter; policy accept;
        ip daddr 192.168.218.2 icmp type echo-request meta nftrace set 1 # handle 23
    }
}

Start the monitor in one terminal, as root; it prints a line per traced event and keeps running until stopped with Ctrl-C:

# nft monitor trace
trace id d5fab4d3 inet filter output packet: oif "eth1" ip saddr 192.168.218.10 ip daddr 192.168.218.2 ip dscp cs0 ip ecn not-ect ip ttl 64 ip id 4628 ip protocol icmp ip length 84 icmp type echo-request icmp code 0 icmp id 1 icmp sequence 1
trace id d5fab4d3 inet filter output rule ip daddr 192.168.218.2 icmp type echo-request meta nftrace set 1 (verdict continue)
trace id d5fab4d3 inet filter output policy accept

In a second session on fwclient, as john, generate the traffic:

$ ping -c 1 192.168.218.2
PING 192.168.218.2 (192.168.218.2) 56(84) bytes of data.
64 bytes from 192.168.218.2: icmp_seq=1 ttl=64 time=0.360 ms

--- 192.168.218.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.360/0.360/0.360/0.000 ms

The three trace lines share one trace id and read in order: the packet line shows the kernel's view of the packet entering the output chain - interface, both addresses, protocol details; the rule line shows the traced rule matching with verdict continue (it set the flag and passed the packet on); the policy line shows the chain's accept policy ending the evaluation. Nothing between the rule and the policy dropped it, so the trace has already answered the question this tool exists for. The echo reply does not appear because the trace rule matches only the request's direction; tracing the inbound side means adding a matching rule in the input chain, which sees the reply as ct state established. Stop the monitor with Ctrl-C and remove the tracing rule by the handle listed above; the delete is silent:

# nft delete rule inet filter output handle 23
# nft list chain inet filter output
table inet filter {
    chain output {
        type filter hook output priority filter; policy accept;
    }
}

The chain is back to its installed state. The trace tool's cost is proportional to what it matches - a rule matching a whole port range under load will trace every packet in it - so trace rules are temporary, matched as narrowly as the question, and deleted when the answer is read, exactly as done here.

18.5.2. Inspecting Connection Tracking and Rule Counters

The conntrack command from the conntrack-tools package reads the flow table directly, which makes it the tool for questions like "did my SYN arrive?", "did the server's answer leave?", and "is the translation being applied?" Install it on the hosts that need it with apt install conntrack-tools. Its options as used here:

conntrack option Description
-L List tracked flows; the main inspection view.
-S Show per-CPU statistics counters for the tracking subsystem.
-p proto Restrict the listing to one protocol.
--dport port Restrict the listing to one destination port.

First make the flows to look at. On fwclient, as john:

$ curl -sS --max-time 3 http://192.168.220.10/
hello from server 80

On fwremote, as john:

$ curl -sS --max-time 3 http://192.168.219.2:8080/
hello from server 80

On fwrouter, in the root shell, list the tracked web flows:

# conntrack -L -p tcp --dport 80
tcp      6 79 TIME_WAIT src=2001:db8:18:1::10 dst=2001:db8:18:2::20 sport=38574 dport=80 src=2001:db8:18:2::20 dst=2001:db8:18:1::10 sport=80 dport=38574 [ASSURED] mark=0 use=1
tcp      6 115 TIME_WAIT src=192.168.218.10 dst=192.168.220.10 sport=41960 dport=80 src=192.168.220.10 dst=192.168.218.10 sport=80 dport=41960 [ASSURED] mark=0 use=1
tcp      6 40 TIME_WAIT src=192.168.218.10 dst=192.168.220.10 sport=43272 dport=80 src=192.168.220.10 dst=192.168.218.10 sport=80 dport=43272 [ASSURED] mark=0 use=1
tcp      6 34 TIME_WAIT src=192.168.218.10 dst=192.168.219.20 sport=43390 dport=80 src=192.168.219.20 dst=192.168.219.2 sport=80 dport=43390 [ASSURED] mark=0 use=1
tcp      6 73 TIME_WAIT src=2001:db8:18:1::10 dst=2001:db8:18:3::10 sport=45404 dport=80 src=2001:db8:18:3::10 dst=2001:db8:18:1::10 sport=80 dport=45404 [ASSURED] mark=0 use=1
conntrack v1.4.8 (conntrack-tools): 5 flow entries have been shown.

Each line holds two tuples: the original direction (src= ... sport= ... dst= ... dport=) and, after the second src=, the reply direction as the far endpoint sees it. Read the fourth line, the masqueraded flow from Section 18.4.2: the original tuple says the client is 192.168.218.10, and the reply tuple says the server's answers go to 192.168.219.2 - the router's WAN address. That is the stored NAT rewriting, visible in the table that every later packet of the flow consults. Compare the second and third lines, routed to the DMZ: original and reply tuples are mirror images, because nothing translated them. All five flows sit in TIME_WAIT - finished connections held open for the kernel's grace period - and [ASSURED] marks flows that carried traffic in both directions. The IPv6 lines appear here for the same reason as their IPv4 counterparts: one family, one tracking table.

The statistics view answers a different question - whether tracking itself is failing under load:

# conntrack -S
cpu=0       found=0 invalid=0 insert=0 insert_failed=0 drop=0 early_drop=0 error=0 search_restart=0 clash_resolve=0 chaintoolong=0

All zeros is the healthy idle state. The fields to know are insert_failed and drop, which count flows the kernel could not track because the table was full or the chain too long, and early_drop, which counts healthy-flow evictions the kernel made to free space for new ones. A host under connection flood shows these moving before anything else does.

The client sees its own half of the same picture, with no rewriting anywhere. On fwclient, in the root shell:

# conntrack -L -p tcp --dport 80
tcp      6 75 TIME_WAIT src=2001:db8:18:1::10 dst=2001:db8:18:2::20 sport=38574 dport=80 src=2001:db8:18:2::20 dst=2001:db8:18:1::10 sport=80 dport=38574 [ASSURED] mark=0 use=1
tcp      6 69 TIME_WAIT src=2001:db8:18:1::10 dst=2001:db8:18:3::10 sport=45404 dport=80 src=2001:db8:18:3::10 dst=2001:db8:18:1::10 sport=80 dport=45404 [ASSURED] mark=0 use=1
tcp      6 111 TIME_WAIT src=192.168.218.10 dst=192.168.220.10 sport=41960 dport=80 src=192.168.220.10 dst=192.168.218.10 sport=80 dport=41960 [ASSURED] mark=0 use=1
tcp      6 36 TIME_WAIT src=192.168.218.10 dst=192.168.220.10 sport=43272 dport=80 src=192.168.220.10 dst=192.168.218.10 sport=80 dport=43272 [ASSURED] mark=0 use=1
tcp      6 31 TIME_WAIT src=192.168.218.10 dst=192.168.219.20 sport=43390 dport=80 src=192.168.219.20 dst=192.168.218.10 sport=80 dport=43390 [ASSURED] mark=0 use=1
conntrack v1.4.8 (conntrack-tools): 5 flow entries have been shown.

The same five flows from the client's end, and here every line is symmetric - nothing was rewritten on fwclient, so no line shows it. Set the two listings side by side and the masqueraded flow is the one that differs: the router's table expects the replies addressed to 192.168.219.2, while the client's table expects them addressed to itself. Both are true at once, and the router's conntrack entry is what reconciles them into one working connection. The rule counter that bookends the look:

# nft list counter inet filter forwarded_web
table inet filter {
    counter forwarded_web {
        packets 5 bytes 300
    }
}

Five accepted connections since the ruleset loaded: the three of Section 18.4.2 and the two just repeated. Counter, tracking table, and log together give the router a complete account of what it forwarded, dropped, and rewrote.

18.5.3. Diagnosing Asymmetric Paths and Unexpected Drops

The last failure mode is the one that makes administrators distrust working firewalls: every rule says accept, the trace says accept, and the connection still dies. The cause is usually the return path - the kernel answered, and the answer never came back. A route can produce that on a two-network lab, so the demonstration breaks a return route deliberately and reads the evidence in both places.

Start from health. On fwserver, as john, ask the kernel which way replies to fwclient go:

$ ip route get 192.168.218.10
192.168.218.10 via 192.168.220.2 dev eth1 src 192.168.220.10 uid 1001
    cache

Replies leave through 192.168.220.2, the router's DMZ address - the correct next hop. The demonstration replaces that with a next hop that does not exist. On fwserver, in a root shell:

# ip route replace 192.168.218.10/32 via 192.168.220.254 dev eth1

The command prints nothing; the 254 host does not exist on the DMZ link. Confirm the change, as john:

$ ip route get 192.168.218.10
192.168.218.10 via 192.168.220.254 dev eth1 src 192.168.220.10 uid 1001
    cache

On fwclient, as john, a request that worked in Section 18.4.2 now times out:

$ curl -sS --max-time 4 http://192.168.220.10/
curl: (28) Connection timed out after 4002 milliseconds

The server's input chain would accept it - nothing about the firewall changed. On fwserver, in the root shell, look for the half-open flow; the pipeline filters the tracking table down to the interesting entry - conntrack -L lists TCP flows, the first grep keeps connections stuck in SYN_RECV, the second keeps this client's, and head bounds the output:

# conntrack -L -p tcp --dport 80 | grep SYN_RECV | grep 192.168.218.10 | head -n 3
tcp      6 57 SYN_RECV src=192.168.218.10 dst=192.168.220.10 sport=39896 dport=80 src=192.168.220.10 dst=192.168.218.10 sport=80 dport=39896 mark=0 use=3
conntrack v1.4.8 (conntrack-tools): 6 flow entries have been shown.

There it is: the server received the SYN - the original tuple proves it - and its kernel answered with a SYN-ACK, the reply tuple shows. SYN_RECV means the handshake never completed: the SYN-ACK went to 192.168.220.254, a next hop that does not exist, so the client never saw it. The evidence splits the fault precisely in half: the SYN arrived (so no inbound drop), the server replied (so its firewall allowed the reply), and the reply vanished between the server's output chain and the client - a network problem wearing a firewall's symptoms. Restore the route, in the root shell; the command is silent:

# ip route del 192.168.218.10/32

The host route is gone, so replies follow the DMZ network's original route again. Confirm, as john:

$ ip route get 192.168.218.10
192.168.218.10 via 192.168.220.2 dev eth1 src 192.168.220.10 uid 1001
    cache

On fwclient, as john, the same request succeeds:

$ curl -sS --max-time 4 http://192.168.220.10/
hello from server 80

The full account of the chapter's deliberate drops, read off the two machines that did the dropping. On fwrouter, in the root shell:

# nft list counter inet filter forward_drops
table inet filter {
    counter forward_drops {
        packets 12 bytes 780
    }
}

Twelve packets: the nine IPv4 SYNs of the three denied requests in Section 18.4.2, plus the three IPv6 SYNs of the denied request in Section 18.4.3. On fwclient, in the root shell:

# nft list counter inet filter dropped_packets
table inet filter {
    counter dropped_packets {
        packets 17 bytes 1020
    }
}

Seventeen: the fourteen accounted for in Section 18.3.3 plus the three SYNs of the request against the deleted temporary rule in Section 18.3.4. Every deliberate drop in the chapter has a number, and the two counters add up exactly.

Two more facts complete the asymmetric-path picture. First, the timeout-versus-refused distinction from Section 18.2.4 is what directs this whole investigation: a refused connection means the packet reached a host and something answered, so the fault is the service; a timeout means silence, and silence is where routes and drops hide. Second, the kernel itself drops asymmetrically routed packets when reverse-path filtering is strict: Debian's default is the loose mode recorded in the sysctl output of Section 18.4.1 - net.ipv4.conf.*.rp_filter = 2 - which accepts a packet as long as some route to its source exists. Sites that tighten rp_filter to strict mode (1) in the name of spoofing protection can thereby break multi-homed and asymmetric setups that worked the day before; the interaction of reverse-path filtering with policy routing is Chapter 19's subject, and the firewall lesson is only that the drop can come from the routing layer while every filtering rule says accept.

The chapter's end state is a working arrangement, not a cleanup: the three rulesets stay loaded and enabled, the responders keep running, and the fourth network keeps carrying administration that no policy here can lock out. The patterns worth carrying forward are the ones the exercises repeated: check then load, one transaction per file, a timed rollback before any risky change, counters on every interesting rule, and a diagnosis that reads the failure's shape before it touches the ruleset. Where the next chapters take the network - bridges and VLANs and WireGuard in Chapter 19, hardened SSH in Chapter 20, and the exposed-service audit that should accompany any firewall in Chapter 26 - the same netfilter hooks and the same ruleset discipline are what those changes will pass through.