tcpdump command doesn't have a direct way to filter based on the inner IP addresses inside a GRE tunnel using simple syntax. This is because tcpdump works primarily with the outer headers when dealing with GRE encapsulation. However, you can use some tricks to filter and inspect the encapsulated IP headers within a GRE tunnel by leveraging ip[]
expressions.
The GRE tunnel introduces some complexity when trying to filter based on the inner IP address because the inner packet (the one being encapsulated) is inside the GRE header and the outer IP header.
GRE packet has the following structure:
To filter based on the encapsulated IP addresses (i.e., the inner IP packet), you need to look at the byte offset in the GRE encapsulated packet. Specifically:
You can use tcpdump
with the following approach:
Filter GRE packets with a specific source IP (inner IP): To capture GRE packets where the source IP of the encapsulated packet is 10.120.0.111
:
Explanation:
ip[36:4] = 0x0a78006f
matches the source IP 10.120.0.111
(converted to hex 0x0a78006f).Filter GRE packets with a specific destination IP (inner IP): To capture GRE packets where the destination IP of the encapsulated packet is 10.120.0.111
:
Explanation:
ip[40:4] = 0x0a78006f
matches the destination IP 10.120.0.111
(converted to hex 0x0a78006f).Filter GRE packets for specific protocol types (e.g., TCP inside GRE): To capture GRE packets that encapsulate TCP packets (where protocol 0x06
is TCP):
Filter GRE packets for a specific source IP inside GRE (combining source IP and protocol): To capture GRE packets with a specific source IP 10.120.0.111
and protocol TCP
:
Filter GRE packets for a specific IP inside GRE (Two way traffic): To capture GRE packets with a specific IP 10.120.0.111:
To use in tcpdump
, you need to convert the IP addresses into hexadecimal format:
0xc0a80101
0x0a010101
Convert each octet of the IP to its hexadecimal form and join them together.
192
→ c0
, 168
→ a8
, 1
→ 01
, 1
→ 01
, so 192.168.1.1
becomes 0xc0a80101
.