Berkeley Packet Filter
- Martin Moghadam
As part of the agent configuration group parameters you can specify a Berkeley Packet Filter (BPF) expression. In the agent configuration it's passed as FilterExpression=<expression>
Packet filters are normally used to tell agents to only examine packets to and from specific hosts.
If no filter is applied, no packets will be rejected by the filtering mechanism. Bear in mind that packets may also be filtered out by other settings, such as the agent configuration group parameters List of Excluded local TCP ports and List of Excluded local UDP ports.
Packet Filter Syntax
The BPF expression specifies which packets should be analyzed by agents that belong to the agent configuration group that uses the packet filter.
Keyword | Description |
---|---|
Dir | dir qualifiers specify a particular transfer direction to and/or from ID. Possible directions are:
|
proto | proto qualifiers restrict the match to a particular protocol. Possible protos are: | ether| | fddi| | tr| | ip| | ip6| | arp| | rarp| | decnet| | lat| | sca| | moprc| | mopdl| | iso| | esis| | isis| | icmp| | icmp6| | tcp | | udp| | |
`ether src foo', `arp net 128.3', `tcp port 21'. If there is no proto qualifier, all protocols consistent with the type are assumed. For example, `src foo' means `(ip or arp or rarp) src foo' (except the latter isn't legal syntax), `net bar' means `(ip or arp or rarp) net bar' and `port 53' means `(tcp or udp) port 53'. `fddi' is actually an alias for `ether'; the parser treats them identically as meaning the data link level used on the specified network interface. FDDI headers contain Ethernet-like source and destination addresses, and often contain Ethernet-like packet types, so you can filter on these FDDI fields just as with the analogous Ethernet fields. FDDI headers also contain other fields, but you can't name them explicitly in a filter expression. Similarly, `tr' is an alias for `ether'. |
Primitives
There are also some special `primitive' keywords that don't follow the pattern:
*gateway, broadcast, less, greater and arithmetic expressions.
All of these are described in the following.
More complex filter expressions are built up by using the words and, or and not to combine primitives. Example:
host foo and not port ftp and not port ftp-data
To save typing, identical qualifier lists can be omitted. Example:
tcp dst port ftp or ftp-data or domain
is exactly the same as
tcp dst port ftp or tcp dst port ftp-data or tcp dst
True if either the IPv4/v6 source or destination of the packet is host. Any of the host expressions can be prepended with the keywords, ip, arp, rarp, or ip6 as in:
ip host host
which is equivalent to:
ether proto \ip and host host
If host is a name with multiple IP addresses, each address will be checked for a match.
Abbreviations for:
iso proto p
where p is one of the protocols. Note that the agent does an incomplete job of parsing these protocols.
Primitive | Description |
---|---|
ether dst ehost | True if the Ethernet destination address is ehost. Ehost may be either a name or in numeric format |
ether src ehost | True if the Ethernet source address is ehost. |
ether host ehost | True if either the Ethernet source or destination address is ehost. |
gateway host | True if the packet used host as a gateway. That is, the Ethernet source or destination address was host but neither the IP source nor the IP destination was host. |
dst net net | True if the IPv4/v6 destination address of the packet has a network number of net. Net may be either a name from /etc/networks or a network number. |
src net net | True if the IPv4/v6 source address of the packet has a network number of net. |
net net | True if either the IPv4/v6 source or destination address of the packet has a network number of net. |
dst port port | True if the packet is ip/tcp, ip/udp, ip6/tcp or ip6/udp and has a destination port value of port. The port is a number. |
src port port | True if the packet has a source port value of port. |
port port | True if either the source or destination port of the packet is port. Any of the port expressions can be prepended with the keywords, tcp or udp, as in ... |
tcp src port port | ... which matches only TCP packets whose source port is port. |
less length | True if the packet has a length less than or equal to length. This is equivalent to: len <= length. |
greater length | True if the packet has a length greater than or equal to length. This is equivalent to: len >= length. |
ip proto protocol | True if the packet is an IP packet of protocol type protocol. Protocol can be a number or one of the names icmp, icmp6, igmp, igrp, pim, ah, esp, udp, or tcp. Note that this primitive doesn't chase protocol header chain. |
ip6 proto protocol | True if the packet is an IPv6 packet of protocol type protocol. Note that this primitive doesn't chase protocol header chain. May be somewhat slow. |
ether broadcast | True if the packet is an Ethernet broadcast packet. The ether keyword is optional. |
ip broadcast | True if the packet is an IP broadcast packet. It checks for both the all-zeroes and all-ones broadcast conventions, and looks up the local subnet mask. |
ether multicast | True if the packet is an Ethernet multicast packet. The ether keyword is optional. This is shorthand for `ether[manu:0] & 1 != 0'. |
ip multicast | True if the packet is an IP multicast packet. |
ip6 multicast | True if the packet is an IPv6 multicast packet. |
ether proto protocol | True if the packet is of ether type protocol. Protocol can be a number or one of the names ip, ip6, arp, rarp, atalk, aarp, dec-net, sca, lat, mopdl, moprc, or iso. Note these identifiers are also keywords and must be escaped via backslash( ). [manu:In the case of FDDI (e.g., `fddi protocol arp'), the protocol identification comes from the 802.2 Logical Link Control (LLC) header, which is usually layered on top of the FDDI header. The agent assumes, when filtering on the protocol identifier, that all FDDI packets include an LLC header, and that the LLC header is in so-called SNAP format. The same applies to Token Ring.] |
lat, moprc, mopdl | Abbreviations for: ether proto p where p is one of the protocols. Note that tcpdump doesn't currently know how to parse these protocols. |
vlan[manu:vlan_id] | True if the packet is an IEEE 802.1Q VLAN packet. If [manu:vlan_id] is specified, only true is the packet has the specified vlan_id. Note that the first vlan keywor encountered in expression changes the decoding offsets for the remainder of expression on the assumption that the packet is a VLAN packet. |
tcp, udp, icmp | Abbreviations for: ip proto p or ip6 proto p where p is one of the protocols. |
iso proto protocol | True if the packet is an OSI packet of protocol type protocol. Protocol can be a number or one of the names clnp, esis, or isis. |
expr relop expr | True if the relation holds, where relop is one of > , < , >= , <= , = , != , and expr is an arithmetic expression composed of integer constants (expressed in standard C syntax), the normal binary operators [manu:+, -, *, /, &, |], a length operator, and special packet data accessors. To access data inside the packet, use the following syntax: |
proto [ expr : size ] | Proto is one of ether, fddi, tr, ip, arp, rarp, tcp, udp, icmp or p6, and indicates the protocol layer for the index operation. Note that tcp, udp and other upper layer protocol types only apply to IPv4, not IPv6. The byte offset, relative to the indicated pro udp index operations. For instance, tcp[manu:0] always means the first byte of the TCP header, and never means the first byte of an intervening fragment. |
Combination of Primitives
Primitives may be combined using:
A parenthesized group of primitives and operators.
Negation (`!' or `not').
Concatenation (`&&' or `and').
Alternation (`||' or `or').
Negation has highest precedence. Alternation and concatenation have equal precedence and associate left to right. Note that explicit and tokens, not juxtaposition, are now required for concatenation. If an identifier is given without a keyword, the most recent keyword is assumed. For example, not host vs and howdy is short for not host vs and host howdy which should not be confused with not ( host vs or howdy ).
Examples:
To process IP packets to or from the network 192.168.1.0/24 when the agent itself is in another network
net 192.168.1.0/24
To process IP packets to or from the network 192.168.1.0/24 when the agent is in the same network
src net 192.168.1.0/24 and dst net 192.168.1.0/24
To process all packets arriving at or departing from helios:
host helios
To process traffic between helios and either hotrod or howdy:
host helios and ( hotrod or howdy )
To process all IP packets between howdy and any host except helios:
ip host howdy and not helios
To process all traffic between local hosts and hosts at Berkeley: host.
tcp[manu:13] & 3 != 0 and not src and dst net localnet
To process IP packets longer than 576 bytes sent through gateway geezer:
gateway geezer and ip[2:2] > 576
Search this documentation
On this page
In this section