|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. For example, “host foo”, “net 128.3”, and “port 20”. If there is no type qualifier,
src or dst,
src and dst,
addr4. For example,
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 some link layers, such as SLIP and the "cooked" Linux capture mode used for the "any" device and for some other device types, the
outboundqualifiers can be used to specify a desired direction.
udp. For example, “ether src foo”, “arp net 128.3”, “tcp port 21”, and “wlan addr2 0:2:3:4:5:6”. 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 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 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 expression.
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 primitives 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. For example, “host
foo and not port ftp and not port ftp-data”. To save typing,
identical qualifier lists can be omitted, so that “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
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.
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 available, this check will not work correctly.
etherkeyword is optional. This is shorthand for “ether & 1 != 0”.
stp. Note these identifiers are also keywords and must be escaped using a backslash character (\).
In the case of FDDI (such as "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:
memory(applies only to packets logged by pf(4)).
blockand, with later versions of 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.
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
ip protop or
ip6 protop, where p is one of the above protocols.
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.
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 fragment.
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.
!” 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
If an identifier is given without a keyword, the most recent keyword is assumed. For example, “not host vs and ace” is short for “not host vs and host ace”, which shouldn't be confused with “not ( host vs or ace )”.
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 25, 2019||OpenBSD-current|