Tcpdump icmp practical examples filtering on icmp type field and icmp code field
The manual for tcpdump shows how to use tcpdump expressions and primitives to build traffic capturing filters based on protocol field values, like specific icmp type and specific icmp code and specific source host.
Tcpdump also offers a way to filter packets with specified value in a specific protocol byte number, ie: we know icmp header first byte (0) is icmp type and second byte (1) is the icmp code so tcpdump allows to either use builtin primitives like "icmptype" and "icmpcode" or protocol byte number like "icmp[0]" and "icmp[1]".
Per RFC792 (
https://tools.ietf.org/html/rfc792 below is icmp protocol header:
Code:
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Type | Code | Checksum |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| unused |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Internet Header + 64 bits of Original Data Datagram |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
Wikipedia page for icmp shows the full table of icmp types and icmp codes as well as their numerical codes:
http://en.wikipedia.org/wiki/Internet_Control_Message_Protocol.
Manual for tcpdumps shows us a list of available builtin primitives for icmp types and codes. Quote:
Quote:
Some offsets and field values may be expressed as names rather than as numeric values. The following protocol header field offsets are avail-
able: icmptype (ICMP type field), icmpcode (ICMP code field), and tcpflags (TCP flags field).
The following ICMP type field values are available: icmp-echoreply, icmp-unreach, icmp-sourcequench, icmp-redirect, icmp-echo, icmp-routerad-
vert, icmp-routersolicit, icmp-timxceed, icmp-paramprob, icmp-tstamp, icmp-tstampreply, icmp-ireq, icmp-ireqreply, icmp-maskreq, icmp-maskre-
ply.
Example based on the same manual:
To print all icmp packets that are not ping packets (echo requests/replies):Code:
tcpdump -nni eth0 "icmp[icmptype] != icmp-echo and icmp[icmptype] != icmp-echoreply"
or
printing icmp packets that are not ping by using byte numbers and numerical codes for icmp type and icmp code:
Code:
tcpdump -nni eth0 "icmp[0] != 8 and icmp[0] != 0"
Tcpdump print only icmp traffic:
Code:
tcpdump -nni eth0 icmp
Tcpdump print only "Destination Unreachable" icmp type, "Fragmentation required and DF bit set" icmp code
Code:
tcpdump -nni eth0 icmp[0] = 3 and icmp[1] = 4