Sub topic 5
GRE: a simple tunnel with no protection
This chapter assumes you know basic IP addressing and subnets, how a routing table and default route work, and the difference between TCP and UDP. You should also be comfortable at a Linux command line. It builds on the previous chapters on encapsulation and MTU.
What GRE is
Generic Routing Encapsulation (GRE) is a protocol that wraps one network packet inside another IP packet. The outer packet carries IP protocol number 47. GRE does one job: it moves packets across a network that would not route them natively. It adds no authentication, no integrity check and no encryption.
A GRE tunnel is a plain tunnel, not a VPN. Anyone on the path between the two endpoints can read the inner packets, change them, or inject new ones.
How the wrapping works
The sending host takes an inner packet, adds a GRE header (4 bytes in the basic form), and then adds a new outer IP header. The outer header uses the tunnel endpoints' public addresses. Routers on the path only look at the outer header. The receiving endpoint removes both headers and forwards the inner packet.
Building a GRE tunnel on Linux
On Linux, a tunnel is an ordinary network interface. You create it, give it an address, bring it up, and add routes that point at it.
# On host A (public address 192.0.2.1); host B mirrors this with local/remote swapped
sudo ip tunnel add gre1 mode gre local 192.0.2.1 remote 198.51.100.1 ttl 255
sudo ip addr add 10.10.10.1/30 dev gre1
sudo ip link set gre1 up
sudo ip route add 10.20.0.0/16 via 10.10.10.2 dev gre1Each line has a purpose:
ip tunnel addcreates thegre1interface.localandremoteare the outer addresses.ip addr addgives the tunnel its inner address. The /30 leaves room for exactly two hosts, one at each end.ip link set ... upactivates the interface.ip route addsends traffic for the remote subnet to the far end of the tunnel. Without this route, nothing uses the tunnel.
On host B, swap local and remote, use 10.10.10.2/30 as the inner address, and route the subnet behind host A.
Overhead and MTU
Basic GRE adds 24 bytes to every packet: 20 for the outer IPv4 header and 4 for the GRE header. On a 1500-byte link, the inner packet can be at most 1476 bytes. Larger packets must be fragmented, or dropped if the Don't Fragment bit is set. Linux normally sets the tunnel MTU to 1476 for you. If a firewall drops the ICMP "fragmentation needed" messages, large transfers can stall while small packets still work.
When GRE is useful
GRE is stateless. It has no handshake and no session, so it is cheap and easy to set up. It can carry multicast and non-IP protocols, which is why routing protocols such as OSPF are often run over GRE tunnels. Typical uses are:
- Linking two sites over a network you already trust or control.
- Carrying routing protocols between sites.
- Combining with IPsec, so GRE provides the flexible tunnel and IPsec provides the protection.
Limits
Because GRE has no protection, never use it alone across the public internet for sensitive data. It also has no peer authentication. A host that can send packets with the right outer addresses can inject traffic into the tunnel. Firewalls and NAT can also block protocol 47, since it has no ports to translate.
Exercise
Host A and host B are connected by a GRE tunnel. A packet from A's inner address passes through a router on the internet. Which addresses can that router see, and can it read the payload of the inner packet?
Show answer
The router sees the outer IP header, which holds the public addresses of the two tunnel endpoints. GRE does not encrypt, so the inner packet, including its addresses and payload, is still readable by anyone who can capture the traffic. The router just doesn't use the inner header when forwarding.
Exercise
A GRE tunnel is up and small pings work, but large file transfers hang. What is the likely cause, and what command from the earlier material helps test it?
Show answer
The likely cause is an MTU problem: the 24-byte GRE overhead makes full-size packets too big, and the ICMP "fragmentation needed" messages are probably being blocked. Use ping with the Don't Fragment option and different sizes, for example ping -M do -s 1372 10.10.10.2, and lower the size until it succeeds to find the usable path size.