Skip to content

Lesson 6 of 9 · 27m

PF Firewall Engineering

Write readable pf.conf policy, use anchors and tables safely, NAT deliberately, and validate rules before locking yourself out.

PF is one of the strongest arguments for FreeBSD infrastructure. Its configuration language reads like policy rather than packet-filter assembler. That readability is a security control: operators can review what traffic is allowed, why NAT exists, and which state is created. The danger is that PF is simple enough to write confidently and powerful enough to lock you out completely.

A firewall policy starts with intent

A small host policy might look like this:

ext_if = "vtnet0"
table <trusted_ops> persist { 198.51.100.10, 203.0.113.44 }

set skip on lo0
block log all

pass in on $ext_if proto tcp from <trusted_ops> to port ssh
pass in on $ext_if proto tcp to port { 80, 443 }
pass out on $ext_if keep state

The first rule of PF is not syntax; it is default posture. block log all makes absence of permission visible. Explicit passes describe the service contract. Comments and tables should name administrative intent, because nobody remembers why an orphan IP was allowed three years later.

Validate before loading

Never learn firewall syntax during a production outage:

pfctl -nf /etc/pf.conf
pfctl -f /etc/pf.conf
pfctl -sr
pfctl -ss

-n parses without loading. This is the minimum check before enabling PF in rc.conf:

sysrc pf_enable=YES
service pf start

On remote systems, use a scheduled rollback or at minimum keep an established SSH session open while testing. PF state may preserve an existing connection, but new connections can still be blocked. Console access remains the adult solution.

NAT and redirection are explicit policy

For jails using private addresses, NAT often looks like:

jail_net = "10.7.0.0/24"
nat on $ext_if from $jail_net to any -> ($ext_if)
rdr pass on $ext_if proto tcp to port 443 -> 10.7.0.10

Read it literally: outbound traffic from the jail network is translated to the external interface address; inbound HTTPS is redirected to the jail. rdr pass combines redirection and permission; without pass, you still need a matching pass rule depending on rule structure and version behavior.

Tables and anchors keep automation sane

Tables are ideal for dynamic sets such as trusted operators, blocked abusers, or customer edge networks:

pfctl -t trusted_ops -T add 192.0.2.20
pfctl -t trusted_ops -T show

Anchors allow controlled insertion points for application-managed rules. They are useful when a service needs to adjust a small slice of policy without rewriting all of /etc/pf.conf. They are also dangerous when nobody documents ownership; an orphaned anchor can quietly pass traffic long after its service was removed.

State is behavior, not bookkeeping

keep state is how legitimate return traffic works. Inspect state during incidents with pfctl -ss and counters with pfctl -si. If a service works from one network but not another, compare rule evaluation order, NAT translation, routing, and state creation before blaming the application.

Field notes

  • PF rules are generally evaluated with the last matching rule winning for filter rules; use quick deliberately, not habitually.
  • Log is not an intrusion detection system; high-volume logging can create its own denial of service.
  • Test IPv6 separately. A perfect IPv4 policy beside an unintended IPv6 pass rule is not a secure perimeter.
  • Firewall syntax valid on one release may still behave differently across NAT ordering or jail networking choices. Test the traffic, not only the parser.

Was this lesson helpful?

Sign in to rate