Sub topic 5
GRE: a simple tunnel with no protection
This chapter assumes you know IP addressing, subnets and routing tables, and the idea of encapsulation from the earlier chapter on tunnelling. You should also be comfortable with a Linux shell. GRE (Generic Routing Encapsulation) is the plainest tunnel in common use. It wraps one packet in another and adds nothing else. That makes it a good place to see what a tunnel is before security is added.
What GRE does
GRE is defined in RFC 2784, with extensions in RFC 2890. It takes a packet of some network protocol, called the payload or inner packet. It adds a small GRE header and puts the result inside an ordinary IP packet, the outer packet. The outer packet uses IP protocol number 47. GRE does not use TCP or UDP, so it has no port numbers.
The outer source and destination are the two tunnel endpoints, which are normally public addresses. The inner source and destination are the addresses the tunnel is meant to connect, often private ones. Routers between the endpoints look only at the outer header, so they never need a route to the inner addresses.
The GRE header
The mandatory part of the header is 4 bytes. It holds a set of flag bits, a version field (0 for standard GRE), and a 16-bit protocol type. The protocol type uses EtherType values and tells the receiver what the payload is. For example, 0x0800 means IPv4, 0x86DD means IPv6, and 0x6558 means an Ethernet frame. Because the payload type is a field, GRE can carry more than IPv4. This is why it is described as generic.
Three flag bits switch on optional 4-byte fields:
| Flag | Optional field | Purpose |
|---|---|---|
| C | Checksum (plus 2 reserved bytes) | Detects corruption of the GRE header and payload. It is not a security feature. |
| K | Key | A 32-bit value that lets several tunnels share the same pair of endpoint addresses. It is an identifier, not a secret. |
| S | Sequence number | Lets the receiver detect reordered or lost packets. |
The fixed cost of GRE over IPv4 is therefore 20 bytes of outer IP header plus 4 bytes of GRE header, or 24 bytes. With the checksum, key and sequence fields all enabled, the cost rises to 36 bytes.
Configuring a tunnel on Linux
On Linux, a GRE tunnel is an ordinary network interface. Anything routed to that interface gets wrapped. The example below uses the classic ip tunnel command.
# 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 does one job:
ip tunnel addcreates the interface and records the two outer endpoints.ttl 255fixes the outer TTL. Without it, the kernel may copy the inner TTL, which makes path behavior harder to predict.ip addr addgives the tunnel interface an inner address. A /30 holds exactly two usable addresses, which suits a point-to-point link.ip link set upactivates the interface.ip route addsends traffic for the remote subnet to the far end of the tunnel.
Host B needs the mirror image. Assume host A has a LAN of 10.1.0.0/16 behind it:
sudo ip tunnel add gre1 mode gre local 198.51.100.1 remote 192.0.2.1 ttl 255
sudo ip addr add 10.10.10.2/30 dev gre1
sudo ip link set gre1 up
sudo ip route add 10.1.0.0/16 via 10.10.10.1 dev gre1If either machine forwards packets for hosts behind it, enable forwarding with sudo sysctl -w net.ipv4.ip_forward=1. Newer scripts often use the equivalent ip link add gre1 type gre local 192.0.2.1 remote 198.51.100.1 ttl 255. It creates the same kind of interface.
To verify, ping the far tunnel address with ping 10.10.10.2. To watch the encapsulation, capture on the physical interface with sudo tcpdump -ni eth0 proto gre (substitute your interface name). You will see outer packets of type GREv0 with the inner IP packet visible inside them, in plain text.
Mistakes and edge cases
Recursive routing
The outer packets must reach the remote endpoint through the real network. Suppose a route sends 198.51.100.1 itself into gre1, for example after you add a default route through the tunnel. The kernel then tries to wrap the outer packet in another GRE packet, which needs the same route again. The tunnel loops and stops working. Keep a specific route for the remote endpoint through the physical interface:
sudo ip route add 198.51.100.1/32 via 192.0.2.254 dev eth0Replace 192.0.2.254 with your real next hop. This pattern returns in the routing-policy chapter, where a full tunnel needs the same exception for the VPN server.
No link state
A GRE interface stays UP whether or not the far end exists. Nothing in plain GRE detects a dead peer. Routes through gre1 remain active, and packets are silently lost. Production setups add a check above GRE, such as a routing protocol's hello packets, a monitoring ping across the tunnel, or a script that removes routes when the check fails.
Firewalls and NAT
GRE is its own IP protocol, not a TCP or UDP port. A firewall rule must allow protocol 47 in both directions. A rule for "port 47" does nothing. A NAT device can rewrite the outer addresses of a GRE packet but has no ports to tell flows apart. Two hosts behind one NAT address usually cannot each hold their own tunnel to the same remote endpoint. Some NAT devices drop GRE entirely. This is a main reason GRE is rarely used across home or carrier-grade NAT, and it is the problem that NAT traversal in IPsec and UDP-based VPNs addresses.
Several tunnels between the same two hosts
A receiver picks the tunnel for an incoming packet by the outer address pair. A second tunnel between the same two addresses is ambiguous unless the two tunnels use different keys. Add key 100 and key 200 to the respective ip tunnel add commands, with the same value on both ends.
Overhead and MTU
The extra 24 bytes shrink the inner packet size that fits in a 1500-byte outer packet. The usable inner MTU is 1500 − 24 = 1476, and Linux sets this value automatically on the tunnel interface. Inner packets larger than 1476 bytes are fragmented, or dropped if they have the Don't Fragment bit set. A dropped packet needs a working ICMP "fragmentation needed" message to get back to the sender. If a firewall blocks that ICMP, large transfers hang while small packets pass. This is an MTU black hole. Fix it by lowering the tunnel MTU (sudo ip link set gre1 mtu 1400) or clamping TCP's maximum segment size on the router. The next chapters treat this failure in depth.
Other behaviors
- GRE can carry multicast and routing-protocol traffic, which is why it pairs with OSPF or BGP between sites. IPsec in its original form carried only unicast.
- Using
mode gretap(orip link add ... type gretap) carries whole Ethernet frames, which bridges two layer-2 segments across a routed network. - IPv6 has its own variant,
ip6gre, for tunnels whose outer packets are IPv6.
What GRE does not give you
GRE provides no confidentiality, no integrity protection, and no authentication of the peer. The checksum catches random corruption, but anyone can recompute it. Anyone on the path can read the inner packets in tcpdump. Anyone who can inject a packet with the right outer addresses can place traffic into your tunnel, because the receiver accepts any packet whose outer addresses match, whatever its origin. The key field is sent in plain text and is not a password.
A GRE tunnel is therefore a transport mechanism, not a VPN in the security sense. Operators who need protection usually run GRE inside IPsec, with IPsec in transport mode protecting the outer GRE packets. This combines GRE's support for multicast and routing protocols with IPsec's encryption.
Exercise
A GRE tunnel runs over an Ethernet path with a 1500-byte MTU. The tunnel uses the checksum, key and sequence fields over IPv4. What is the largest inner packet that fits without fragmentation?
Show answer
Outer IPv4 header (20) + base GRE header (4) + checksum field (4) + key field (4) + sequence field (4) = 36 bytes of overhead. The largest inner packet is 1500 − 36 = 1464 bytes.
Exercise
You add a default route that sends all traffic into gre1, and the tunnel immediately stops passing traffic. The remote endpoint is 198.51.100.1 and the physical next hop is 192.0.2.254 on eth0. What went wrong, and what command fixes it?
Show answer
The outer GRE packets addressed to 198.51.100.1 now match the default route and are sent back into gre1, which is recursive routing. Add a more specific route for the endpoint through the physical interface: sudo ip route add 198.51.100.1/32 via 192.0.2.254 dev eth0.
Exercise
A colleague says the GRE key field makes the tunnel secure because only peers who know the key can use it. Why is this wrong?
Show answer
The key is sent in plain text in every packet and is only an identifier for telling tunnels apart. Anyone who sees one packet learns it, and the receiver does no cryptographic check. GRE provides no confidentiality, integrity or peer authentication.