Concepts / Tunnels and overlays

Tunnels and overlays

GRE tunnel

Also called generic routing encapsulation, IP protocol 47, tunnel, encapsulation, tunnel interface, GRE over IPsec

A virtual link between two routers: each packet is wrapped in a new IP header between their public addresses.

GRE (RFC 2784) puts a 4-byte header and a new IP header (protocol 47) in front of the packet, so a network that cannot route the inner addresses, such as the internet with private ones, carries it between the two tunnel endpoints. It costs 24 bytes, so the tunnel's MTU is 1476. It carries multicast and routing protocols, but it neither encrypts nor authenticates, and it keeps no state: without keepalives or a routing protocol over it, a tunnel to a dead peer stays up. Across the internet it is run inside IPsec. Linux: ip tunnel add … mode gre; Cisco: interface Tunnel0.

A packet inside a packet
A packet inside a packet

The tunnel wraps the whole packet in a new IP header; routers on the way route by the outer header, and the far end unwraps it. From the GRE tunnels: send a private network across someone else's lab.

How a GRE tunnel works, step by step

Two sites, 10.40.1.0/24 behind harbourRouter and 10.40.7.0/24 behind islandRouter. The routers' outside addresses are 198.51.100.2 and 192.0.2.2, and the carrier between them routes only public addresses. Each router gets a tunnel card, and the two cards share a small link network, 10.40.99.0/30. Follow ticketDesk (10.40.1.10) pinging bookingServer (10.40.7.20):

  1. harbourRouter looks up the destination. Its route 10.40.7.0/24 via 10.40.99.2 points out of the tunnel card, toIsland. The tunnel is an ordinary link to the routing table.
  2. The tunnel card encapsulates. In front of the desk's 84-byte packet it puts a GRE header (RFC 2784: 4 bytes, flags and version 0, then the protocol type of the payload, 0x0800 for IPv4, the same EtherType numbers Ethernet uses) and a new outer IP header: source 198.51.100.2, destination 192.0.2.2, protocol 47. The packet is now 108 bytes.
  3. The carrier routes the outer header only. It never reads the inner addresses and never lowers the inner TTL, so the whole crossing counts as one hop.
  4. islandRouter decapsulates. A packet with protocol 47 from 198.51.100.2 matches its tunnel toHarbour (remote 198.51.100.2). It strips both headers and routes the desk's packet onto its LAN as if it had arrived on a cable.
  5. The reply takes the mirror path, wrapped by islandRouter with the addresses swapped.

Optional fields (RFC 2890) add a checksum, a 4-byte key to tell several tunnels between the same two addresses apart, and a sequence number. Because the payload type is an EtherType, GRE carries more than IPv4: IPv6, MPLS, or whole Ethernet frames (gretap). It sets up no session and sends no hellos of its own.

What it looks like on a Linux machine

The GRE tunnel lab builds both ends with iproute2. On harbourRouter:

ip link add toIsland up type gre local 198.51.100.2 remote 192.0.2.2 ttl 64
ip addr add 10.40.99.1/30 dev toIsland
ip route add 10.40.7.0/24 via 10.40.99.2

islandRouter runs the mirror, local 192.0.2.2 remote 198.51.100.2. The card reports its kind and size:

7: toIsland@NONE: <POINTOPOINT,NOARP,UP,LOWER_UP> mtu 1476 qdisc noqueue state UNKNOWN group default
    link/gre 198.51.100.2 peer 192.0.2.2

On a carrier's router, tcpdump -nn 'proto 47' shows a packet inside a packet:

IP 198.51.100.2 > 192.0.2.2: GREv0, length 64: IP 10.40.1.10.58872 > 10.40.7.20.80: Flags [S], seq 398203690, ...

The desk's traceroute lists 10.40.1.2, then 10.40.99.2, then 10.40.7.20: the carrier's routers never appear.

What goes wrong, and how you see it

One end points at the wrong address. GRE has no handshake, so both cards say UP even when one end's remote is a typo. Packets arrive at the far router, its answers go nowhere, and the desk sees 100% packet loss. ip tunnel show prints the addresses each card uses, and ip tunnel change fixes them. tcpdump 'proto 47' in the middle shows traffic in one direction only.

The path dies, the tunnel stays up. Cut the far router's line and the near one still shows its card up with 10.40.7.0/24 via 10.40.99.2 dev toIsland, so nothing fails over. Run OSPF or BGP over the tunnel, or keepalives, so the route goes away when the far end does.

Big packets vanish. The tunnel's MTU is 1476. A ping with -M do -s 1472 gets Frag needed and DF set (mtu = 1476) from 10.40.1.2; if those ICMP messages are filtered, big downloads hang while small ones work. See MTU and MSS clamping.

A firewall or NAT in the path. GRE is protocol 47, not TCP or UDP. A filter that allows only ports drops it, and many NAT devices cannot translate it because there is no port to tell flows apart.

Anyone on the path reads it. The lab's capture on the carrier prints the whole passenger list in plain text.

Questions people ask

Is GRE a VPN? It is a tunnel without security: no encryption, no authentication. Across the internet it runs inside IPsec (GRE over IPsec), where GRE carries multicast and routing protocols and IPsec protects them. The IPsec tunnel lab shows the encrypted kind.

GRE vs IPIP vs VXLAN? IPIP (protocol 4) carries only IPv4 and costs 20 bytes. GRE costs 24 and carries any protocol. VXLAN carries Ethernet frames in UDP port 4789 for 50 bytes, which load-balances better in data centre fabrics.

Does GRE work over NAT? Usually not well. Use GRE inside IPsec with NAT traversal, or a UDP-based tunnel.

See it on real machines

Read next

Learn it on real machines.

Every lab is a small network of real Linux machines in your browser. Sign in with Google or email to start.

Start this lab