Reviewer · Networking
Scope network rules precisely
When writing firewall/NAT rules or mutating low-level network state (neighbor tables, FDB entries, port bindings, conntrack flushes), scope the match criteria exactly to the traffic you intend to affect — interface, protocol, address family, and NAT/DNAT condition — rather than relying on incidental context that happens to be true today. For example, a...
When writing firewall/NAT rules or mutating low-level network state (neighbor tables, FDB entries, port bindings, conntrack flushes), scope the match criteria exactly to the traffic you intend to affect — interface, protocol, address family, and NAT/DNAT condition — rather than relying on incidental context that happens to be true today. For example, a UDP-specific conntrack flush must check the protocol explicitly rather than assume it, and a hairpin-forwarding rule reached only through a per-bridge chain still needs an iifname match if it sits ahead of a rule that otherwise distinguishes local vs. external traffic; missing that match silently changes what a counter counts or what gets accepted, even if the end result looks correct today.
For stateful mutations against the kernel (adding/deleting neighbors, FDB entries, pools, etc.), validate preconditions — “is this still mine to touch”, “did my own prior write actually succeed” — before performing the irreversible kernel operation, not after. Checking after the fact means a failed precondition is reported only once the mutation has already happened, potentially deleting state owned by a different, unrelated entry (e.g. two peers sharing an IP but differing in MAC). Likewise, be deliberate about changing error-wrapping (%v → %w): verify every caller that pattern-matches on the wrapped error still takes the intended branch, since an unwrapped-vs-wrapped change can silently redirect control flow (e.g. skipping a restore path). When an invariant used to justify an ordering (such as “state here was only ever written by a successful call”) only holds for part of a resource’s lifetime (e.g. it’s reset on sandbox rebuild), say so explicitly in the comment so future changes don’t rely on it blindly. Finally, when changing how previously-logged/relied-upon cleanup paths behave (e.g. a double free/deallocate becoming a silent no-op), add a debug log and call out the behavior change in the commit message so it isn’t mistaken for a regression later.