Berkeley Packet Filter
Table of Contents
Berkeley Packet Filter
The Berkeley Packet Filter (BPF) is a powerful and efficient mechanism for filtering raw network packets at the data link layer of the network stack in various Unix-like operating systems (including Linux and macOS). It allows user-space programs to capture only the network traffic they are interested in, significantly reducing overhead and improving performance compared to receiving all packets and filtering in the application.
Core Concepts
- Virtual Machine: BPF operates as a small, in-kernel virtual machine. Filter expressions are compiled into bytecode that is executed by this VM for each incoming packet.
- Filter Expression: A human-readable string that describes the criteria for matching packets. This expression is then translated into BPF bytecode.
- Bytecode: The low-level instructions executed by the BPF virtual machine.
- Packet Matching: For each incoming packet, the BPF VM executes the filter bytecode. If the bytecode returns a non-zero value, the packet matches the filter and is passed to the capturing application. Otherwise, the Network Packet is dropped.
- Kernel-Level Filtering: Because filtering happens within the kernel, only the packets that match the filter are copied to user space, minimizing context switches and data copying, leading to high performance.
- BPF Extensions (eBPF): Modern Linux systems use Extended BPF (eBPF), which is a significant enhancement over the original BPF. eBPF offers more capabilities, including tracing, performance monitoring, and network traffic control. However, the fundamental filtering concepts remain similar.
Structure of BPF Filter Expressions
BPF filter expressions typically consist of one or more primitives combined using logical operators.
Primitives
Primitives specify a particular field or attribute of a packet to examine. Common primitives include:
host <hostname>orhost <ipaddr>: Matches packets with the specified source or destination host.net <network>ornet <network> mask <mask>ornet <network>/<len>: Matches packets with a source or destination IP Address within the specified network.port <port>: Matches packets with the specified source or destination port.portrange <port1>-<port2>: Matches packets with a source or destination port within the specified range.proto <protocol>: Matches packets of the specified protocol (tcp,udp,icmp,ip,arp,ether, etc.).ether host <eaddr>: Matches packets with the specified source or destination Ethernet address.ether proto <protocol>: Matches packets of the specified Ethernet protocol (e.g.,ether proto arp).srcanddstmodifiers: Can be used withhost,net, andportto specify direction (e.g.,src host 192.168.1.100).less <length>,greater <length>,len <length>: Matches packets of a specific length.ip,tcp,udp,icmp,arp,etherkeywords: Act as protocol qualifiers, e.g.,tcp port 80only matches TCP packets on port 80.- Byte offsets and arithmetic/bitwise operations: Allow for more complex filtering based on specific byte patterns within packet headers or payload (less common for basic filtering).
Logical Operators
Primitives can be combined using the following logical operators:
andor&&(implicit): Both preceding and following primitives must be true.oror||: Either the preceding or the following primitive must be true.notor!: Negates the following primitive.
Examples
- Capture all traffic to or from host 192.168.1.100:
host 192.168.1.100
- Capture TCP traffic on port 80:
tcp port 80
- Capture UDP traffic to port 53 (DNS):
udp dst port 53
- Capture ICMP packets:
icmp
- Capture traffic from network 192.168.1.0/24:
src net 192.168.1.0/24
- Capture traffic to port 80 or port 443:
port 80 or port 443
- Capture traffic not destined for the local network 192.168.1.0/24:
not dst net 192.168.1.0/24
- Capture TCP traffic to a specific host on a specific port:
tcp dst host 10.0.0.5 and dst port 22
Use Cases
BPF filters are used extensively in various Networking tools:
- Packet Capture Tools (tcpdump, Wireshark/Tshark): To specify which packets to capture and display, reducing the amount of data to process.
- Network Monitoring Tools: To filter specific types of traffic for analysis and alerting.
- Intrusion Detection/Prevention Systems (IDS/IPS): To identify potentially malicious traffic patterns.
- Traffic Shapers and QoS Tools: To classify and prioritize network traffic based on specific criteria.
- Network Debugging Utilities: To isolate specific communication flows for troubleshooting.
Advantages
- Kernel-Level Efficiency: Filtering in the kernel significantly reduces overhead.
- Flexibility: The filter expression language allows for complex matching criteria.
- Wide Adoption: Supported by numerous Networking tools across different Unix-like systems.
Limitations
- Limited to Packet Headers (Mostly): While eBPF allows for more complex operations and access to packet data, traditional BPF primarily operates on packet headers. Deep packet inspection often requires passing packets to user space.
- Expression Complexity: Writing complex BPF filters can be challenging.
- Security Considerations (eBPF): While offering great power, eBPF requires careful management to prevent potential security risks from malicious or buggy bytecode.