|PCAP-FILTER(5)||File Formats Manual||PCAP-FILTER(5)|
packet filter syntax
pcap_compile(3) compiles pcap filters for software such as tcpdump(8). The resulting filter program can then be applied to some stream of packets to determine which packets will be supplied to pcap_loop(3), pcap_dispatch(3), pcap_next(3), or pcap_next_ex(3).
The filter expression consists of one or more primitives. Primitives usually consist of an id (name or number) preceded by one or more qualifiers. There are three different kinds of qualifier:
port. E.g., “host foo”, “net 128.3”, “port 20”. If there is no type qualifier,
src or dst,
src and dst,
addr4. E.g., “src foo”, “dst net 128.3”, “src or dst port ftp-data”. If there is no dir qualifier,
src or dstis assumed. The
addr4qualifiers are only valid for IEEE 802.11 Wireless LAN link layers. For null link layers (i.e., point-to-point protocols such as SLIP (Serial Line Internet Protocol) or the pflog(4) header), the
outboundqualifiers can be used to specify a desired direction.
wlan. E.g., “ether src foo”, “arp net 128.3”, “tcp port 21”, and “wlan addr2 0:2:3:4:5:6”. If there is no protocol qualifier, all protocols consistent with the type are assumed. E.g., “src foo” means “(ip or arp or rarp) src foo” (except the latter is not 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 (Fiber Distributed Data Interface) headers contain
Ethernet-like source and destination addresses, and often contain
Ethernet-like packet types, so it's possible to filter these FDDI fields
just as with the analogous Ethernet fields. FDDI headers also contain
other fields, but they cannot be named explicitly in a filter
wlan are aliases for
ether; the previous paragraph's statements about
FDDI headers also apply to Token Ring and 802.11 wireless LAN headers.
For 802.11 headers, the destination address is the DA field and the
source address is the SA field; the BSSID, RA, and TA fields aren't
In addition to the above, there are some special primitive
keywords that don't follow the pattern:
greater, and arithmetic expressions. All of these
are described below.
More complex filter expressions are built up by using the words
not to combine primitives e.g., “host foo and
not port ftp and not port ftp-data”. To save typing, identical
qualifier lists can be omitted e.g., “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 port domain”.
Allowable primitives are:
Any of the above host expressions can be
prepended with the keywords,
ip6, as in:
which is equivalent to:
If host is a name with multiple IP addresses, each address will be checked for a match.
and not hosthost
which can be used with either names or numbers for host/ehost. This syntax does not work in an IPv6-enabled configuration at this moment.
dst. Note that this syntax is not valid for IPv6 networks.
Any of the above port expressions can be prepended with the
tcp src portport
which matches only TCP packets whose source port is port.
tcp. Note that the identifiers
icmpare also keywords and must be escaped using a backslash character (\). Note that this primitive does not chase the protocol header chain.
etherkeyword is optional.
If the subnet mask of the interface on which the capture is being done is not known, a value of PCAP_NETMASK_UNKNOWN can be supplied; tests for IPv4 broadcast addresses will fail to compile, but all other tests in the filter program will be OK.
etherkeyword is optional. This is shorthand for “ether & 1 != 0”.
xns. These identifiers are also keywords and must be escaped using a backslash character (‘\’).
In the case of FDDI (e.g., “fddi protocol arp”), and IEEE 802.11 wireless LANS (such as “wlan protocol arp”), for most of those protocols the protocol identification comes from the 802.2 Logical Link Control (LLC) header, which is usually layered on top of the FDDI or 802.11 header.
When filtering for most protocol identifiers on FDDI or 802.11, the filter checks only the protocol ID field of an LLC header in so-called SNAP format with an Organizational Unit Identifier (OUI) of 0x000000, for encapsulated Ethernet; it doesn't check whether the packet is in SNAP format with an OUI of 0x000000. The exceptions are:
In the case of Ethernet, the filter checks the Ethernet type field for most of those protocols. The exceptions are:
synproxy(applies only to packets logged by pf(4)).
scrub(applies only to packets logged by pf(4)).
ether protop, where p is one of the above protocols.
ether protop, where p is one of the above protocols. Note that not all applications using pcap_open_live(3) currently know how to parse these protocols (ie. tcpdump(8)).
ip protop or
ip6 protop, where p is one of the above protocols.
data, or a numeric value.
If the specified wlan_type is
mgtv, then valid values for
If the specified wlan_type is
ctl, then valid values for
If the specified wlan_type is
data, then valid values for
dir. Valid directions are:
dstods, or a numeric value.
vlankeyword encountered in expression changes the decoding offsets for the remainder of expression on the assumption that the packet is a VLAN packet. This expression may be used more than once, to filter on VLAN hierarchies. Each use of that expression increments the filter offsets by 4.
For example, to filter on VLAN 200 encapsulated within VLAN 100:
vlan 100 && vlan 200
To filter IPv4 protocols encapsulated in VLAN 300 encapsulated within any higher order VLAN:
vlan && vlan 300 && ip
mplskeyword encountered in expression changes the decoding offsets for the remainder of expression on the assumption that the packet is an MPLS packet. This expression may be used more than once, to filter on MPLS labels. Each use of that expression increments the filter offsets by 4.
For example, to filter on MPLS label 42 first and requires the next label to be 12:
mpls 42 && mpls 12
To filter on network 192.0.2.0/24 transported inside packets with label 42:
mpls 42 && net 192.0.2.0/24
proto is one of
radio, and indicates the protocol layer for the
index operation (
link all refer
to the link layer;
radio refers to the
"radio header" added to some 802.11 captures). Note that
udp, and other
upper-layer protocol types only apply to IPv4, not IPv6 (this will be
fixed in the future). The byte offset, relative to the indicated
protocol layer, is given by expr.
size is optional and indicates the number of bytes
in the field of interest; it can be either one, two, or four, and
defaults to one. The length operator, indicated by the keyword
len, gives the length of the packet. The random
operator, indicated by the keyword
generates a random number.
For example, “ether & 1 != 0” catches all
multicast traffic. The expression “ip & 0xf != 5”
catches all IPv4 packets with options. The expression “ip[6:2]
& 0x1fff = 0” catches only unfragmented IPv4 datagrams and
frag zero of fragmented IPv4 datagrams. This check is implicitly applied
operations. For instance, “tcp” always means the first
byte of the TCP header, and never means the first byte of an intervening
Some offsets and field values may be expressed as names rather
than as numeric values. The following protocol header field offsets are
icmptype (ICMP type field),
icmpcode (ICMP code field), and
tcpflags (TCP flags field).
The following ICMP type field values are available:
The following TCP flags field values are available:
Primitives may be combined using a parenthesized group of primitives and operators. Parentheses are special to the shell and must be escaped. Allowable primitives and operators are:
!” or “
Negation has highest precedence. Alternation and concatenation
have equal precedence and associate left to right. Explicit
and tokens, not juxtaposition, are now required for
If an identifier is given without a keyword, the most recent keyword is assumed. For example, For example,
is short for
which should not be confused with
To select all packets arriving at or departing from “sundown”:
To select traffic between “helios” and either “hot” or “ace”:
host helios and \( hot or ace \)
To select all IP packets between “ace” and any host except “helios”:
ip host ace and not helios
To select all traffic between local hosts and hosts at Berkeley:
To select all FTP traffic through internet gateway “snup”:
gateway snup and (port ftp or ftp-data)
To select traffic neither sourced from nor destined for local network 192.168.7.0/24 (if you gateway to one other net, this stuff should never make it onto your local net):
ip and not net 192.168.7.0/24
To select the start and end packets (the SYN and FIN packets) of each TCP connection that involves a host not in local network 192.168.7.0/24:
tcp[tcpflags] & (tcp-syn|tcp-fin) != 0 and not src and dst \ net 192.168.7.0/24
To select all IPv4 HTTP packets to and from port 80, i.e. print only packets that contain data and not, for example, SYN and FIN packets and ACK-only packets (IPv6 is left as an exercise for the reader):
tcp port 80 and (((ip[2:2] - ((ip&0xf)<<2)) \ - ((tcp&0xf0)>>2)) != 0)
To select IP packets longer than 576 bytes sent through gateway “snup”:
gateway snup and ip[2:2] > 576
To select IP broadcast or multicast packets that were not sent via Ethernet broadcast or multicast:
ether & 1 = 0 and ip >= 224
To select all ICMP packets that are not echo requests/replies (i.e. not ping packets):
icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply
The original authors are Van Jacobson, Craig Leres, and Steven McCanne, all of the Lawrence Berkeley National Laboratory, University of California, Berkeley, CA.
|September 7, 2021||OpenBSD-7.0|