Skip to content

bpfilter - BPF-Based Linux Packet Filtering Cheatsheet

bpfilter - BPF-Based Linux Packet Filtering Cheatsheet

bpfilter is a daemon that converts familiar firewall rules into optimized BPF programs. The long-standing problem it addresses: iptables has well-understood syntax but a performance ceiling from sequential rule evaluation, while raw eBPF/XDP is fast but requires writing and maintaining BPF code. bpfilter bridges them — you express rules in a familiar way, and it compiles them into efficient BPF attached at the right hook (including XDP, which processes packets before they enter the network stack).

Firewall changes can lock you out of a remote host. Test on a console-accessible system and keep a rollback path.

Requirements

  • Modern Linux kernel with BPF support (6.4+ recommended)
  • Root privileges (loads BPF programs)
  • libbpf available

Installation

MethodHow
From sourcegit clone https://github.com/facebook/bpfilter && make && sudo make install
Distro packageAvailable in some newer distributions
Start daemonsudo bpfilter (or via systemd)
Verifybpfilter --version

Architecture

PieceRole
bpfilter daemonReceives rules, generates and loads BPF
bfcliNative CLI for expressing rules
iptables/nftables front endTranslate existing rules into bpfilter
BPF programsThe compiled filter attached to a hook

Attach Hooks

HookWhere packets are processed
XDPEarliest — in the driver, before the stack (fastest)
TC (traffic control)After basic parsing, ingress/egress
cgroupPer-cgroup socket filtering
NetfilterCompatible placement in the classic path

Choosing XDP gives the largest performance win, particularly for dropping unwanted traffic (DDoS mitigation), because rejected packets never traverse the stack.

Defining Rules with bfcli

# Conceptual ruleset: default drop, allow SSH and established traffic
sudo bfcli ruleset set --from-str '
chain BF_HOOK_XDP policy DROP
  rule
    ip4.proto tcp
    tcp.dport 22
    ACCEPT
  rule
    ip4.saddr 10.0.0.0/8
    ACCEPT
'
CommandDescription
bfcli ruleset set --from-file FLoad rules from a file
bfcli ruleset getShow the active ruleset
bfcli ruleset flushRemove all rules
bfcli chain setManage a single chain

Matchers

MatcherMatches
ip4.saddr / ip4.daddrSource/destination IPv4 (CIDR supported)
ip6.saddr / ip6.daddrIPv6 addresses
ip4.protoProtocol (tcp, udp, icmp)
tcp.dport / tcp.sportTCP ports
udp.dport / udp.sportUDP ports
tcp.flagsTCP flag combinations
meta.ifindexInterface

Verdicts

VerdictEffect
ACCEPTAllow the packet
DROPDiscard silently
CONTINUEFall through to the next rule

Using Existing iptables Rules

bpfilter ships translation front ends so you can keep existing tooling:

# Point the iptables-compatible front end at bpfilter
sudo iptables-bpfilter -A INPUT -p tcp --dport 22 -j ACCEPT

This is the migration path: keep your rule syntax and management scripts, gain BPF execution underneath.

Verifying

CheckCommand
Active rulesetsudo bfcli ruleset get
Loaded BPF programssudo bpftool prog list
Attached XDPip link show <iface>
CountersRule counters in bfcli ruleset get
Overheadsudo bpftop to see program cost

bpfilter vs Alternatives

Aspectbpfilteriptablesnftablesraw XDP/eBPF
PerformanceHigh (BPF/XDP)Lower (sequential)Better than iptablesHighest
SyntaxFamiliar or bfcliFamiliarModern, unifiedWrite BPF code
MaturityNewerVery matureMatureDepends on you
Best forBPF speed with familiar rulesLegacy compatibilityModern netfilterCustom datapaths

Inspect its runtime cost with bpftop; compare with iptables for the classic path.

Resources