#pf #firewall #macos #packet #filter


Library for interfacing with the Packet Filter (PF) firewall on macOS

6 releases (3 breaking)

new 0.4.1 Feb 23, 2021
0.4.0 Jun 9, 2020
0.3.0 Sep 13, 2019
0.2.0 Jun 25, 2018
0.1.0 Dec 20, 2017

#21 in macOS APIs

Download history 187/week @ 2020-11-02 209/week @ 2020-11-09 158/week @ 2020-11-16 118/week @ 2020-11-23 181/week @ 2020-11-30 180/week @ 2020-12-07 164/week @ 2020-12-14 84/week @ 2020-12-21 67/week @ 2020-12-28 141/week @ 2021-01-04 123/week @ 2021-01-11 133/week @ 2021-01-18 203/week @ 2021-01-25 133/week @ 2021-02-01 147/week @ 2021-02-08 136/week @ 2021-02-15

582 downloads per month




Library for interfacing with the Packet Filter (PF) firewall on macOS.

Allows controlling the PF firewall on macOS through ioctl syscalls and the /dev/pf device.

Reading and writing to /dev/pf requires root permissions. So any program using this crate must run as the superuser, otherwise creating the PfCtl instance will fail with a "Permission denied" error.

OS Compatibility

PF is the firewall used in most (all?) BSD systems, but this crate only supports the macOS variant for now. If it can be made to work on more BSD systems that would be great, but no work has been put into that so far.

Usage and examples

A lot of examples of how to use the various features of this crate can be found in the integration tests in and examples.

Here is a simple example showing how to enable the firewall and add a packet filtering rule:

extern crate pfctl;

// Create a PfCtl instance to control PF with:
let mut pf = pfctl::PfCtl::new().unwrap();

// Enable the firewall, equivalent to the command "pfctl -e":

// Add an anchor rule for packet filtering rules into PF. This will fail if it already exists,
// use `try_add_anchor` to avoid that:
let anchor_name = "testing-out-pfctl";
pf.add_anchor(anchor_name, pfctl::AnchorKind::Filter).unwrap();

// Create a packet filtering rule matching all packets on the "lo0" interface and allowing
// them to pass:
let rule = pfctl::FilterRuleBuilder::default()

// Add the filterig rule to the anchor we just created.
pf.add_rule(anchor_name, &rule).unwrap();

License: MIT/Apache-2.0


~43K SLoC