Stateful filtering and connection tracking
Quick answer
A stateful firewall evaluates packets using both packet attributes and information about tracked flows. Connection tracking records flow state; filtering rules decide whether traffic is accepted or dropped.
Connection state and application authorization are different: a tracked flow may still carry an unauthorized request. Evaluate permitted new flows and return traffic separately, and validate both allowed and denied cases in an isolated lab.
Prepare the practice environment
Run python3 lab-fixtures/firewalls/setup.py from the extracted root as an ordinary user. Enter the printed fresh private path and check pwd. Use fresh fixtures, Bash with set -e -o pipefail, LC_ALL=C and umask 077. Run each snippet in a separate Bash process, not your interactive shell. Only private files are used; optional examples require an authorized snapshot VM. Do not modify firewall, interfaces or routing. The teaching ruleset must never be loaded. Review the lab guide.
Prerequisites
Review: Network configuration across distributions, Secure boot, encryption and secrets.
How it works
An input chain handles packets destined for the local host, a forward chain handles routed traffic, and an output chain handles locally generated traffic. In a simple input policy, accept loopback, reject invalid state by dropping, accept established and related packets, allow explicitly authorized new service connections, then use a default drop policy. NEW is not synonymous with TCP SYN; UDP also has tracked flows. ESTABLISHED describes tracking state, not user authentication. RELATED refers to traffic associated with an existing flow, such as certain ICMP errors. UNTRACKED means tracking was bypassed. A drop silently discards; reject attempts to return an error. Real nftables may have multiple base chains and hook priorities, and an accept in one chain does not guarantee final acceptance. The lab is a simplified input-only model using preassigned states, not a kernel emulator.
Try the examples
The fixture models replies, a permitted HTTPS port, an unlisted port, invalid state, loopback and related traffic. It does not derive connection state.
Expected stdout:
reply acceptweb acceptother dropinvalid droploop acceptrelated acceptpython3 model.py filter | awk '$2=="drop" {n++} END {print n+0}'The count covers six fictional packets only; it is not a real firewall counter.
Expected stdout:
2Optional read-only privileged inspection of an authorized isolated VM; output varies and may contain sensitive addresses. Do not copy real flow records into course files.
Manual example; not executed locally. Follow its stated environment restrictions.
Common mistakes
Never infer application identity from an established flow or assume a permitted port guarantees that a service listens.
Practice
Predict the verdict for each packet in packets.json before running the model. Explain why the unlisted port and invalid state are dropped.
Related references
Study: nft, conntrack. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Stateful filtering and connection tracking — Knowledge Check
Apply firewall and network concepts to synthetic fixtures and VM-only scenarios.
What does ESTABLISHED represent?
What does a default drop policy do?
Which chain handles traffic routed through a host?
What does the supplied model test?