Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 79 ยท The Network Layer: IP

GRE Tunnels and Tunneling Protocols

Learn how tunneling wraps one packet inside another, how GRE headers and overhead work, how routing and MTU behave over a tunnel, and how PPTP, L2TP and IPsec compare.

Normally an IP packet travels from its source to its destination, and the routers in between only read its outer header. Sometimes that is not enough. You may want to connect two private networks across the public Internet, carry IPv6 across an IPv4-only provider, run a routing protocol between two offices, or send traffic through a path that normal routing would not choose. For all of these you use a tunnel.

A tunnel hides one packet inside another. The original packet becomes the payload of a new packet that is addressed between the two tunnel endpoints. The routers in the middle only see the outer addresses; the inner packet is carried along unchanged until the far endpoint takes the wrapper off. This lesson explains encapsulation, takes apart the simplest and most common tunnel header (GRE), shows how routing and MTU behave over a tunnel, and compares the main tunneling protocols. The earlier lesson on firewalls and VPNs covers encrypted tunnels; here we focus on how tunnels work mechanically.

Encapsulation

Encapsulation means putting a packet inside another packet. You have already seen it at every layer: a TCP segment inside an IP packet, an IP packet inside an Ethernet frame. Tunneling is the same idea applied across a layer boundary of the same kind, for example an IP packet inside an IP packet.

flowchart LR
  subgraph Outer["Outer packet (public addresses)"]
    direction LR
    oip["Outer IP header<br/>203.0.113.1 to 198.51.100.7"]
    tun["Tunnel header<br/>(GRE)"]
    subgraph Inner["Inner packet (private addresses)"]
      iip["Inner IP header<br/>10.1.0.5 to 10.2.0.9"]
      data["Payload"]
    end
  end
  oip --- tun --- iip
  iip --- data

The routers between the endpoints read only the outer IP header.

The ends of the tunnel are two routers (or hosts). The sending endpoint encapsulates: it adds the new headers. The receiving endpoint decapsulates: it removes them and forwards the inner packet as usual. The inner packet may use private addresses (see NAT) that are not routable on the Internet, because the Internet only ever sees the outer ones.

What tunnels are used for

Site to site Join private networks over the Internet
IPv6 over IPv4 Cross networks that lack IPv6
Routing protocols Carry OSPF or multicast between sites
Security Add encryption around the inner packet

The inner packet travels unchanged

1 / 2

GRE: Generic Routing Encapsulation

GRE (RFC 2784, with extensions in RFC 2890) is a simple tunnel protocol invented by Cisco and now supported by nearly every router, firewall and operating system. It can carry almost any network-layer protocol inside IP, which is the "generic" in its name. GRE packets are IP packets with protocol number 47 (compare TCP = 6, UDP = 17). There are no ports: GRE is not TCP or UDP.

A basic GRE header is only 4 bytes:

Field Size Meaning
Flags and version 2 bytes Bits that say whether the optional fields are present; the version is 0
Protocol type 2 bytes An EtherType naming the payload: 0x0800 IPv4, 0x86DD IPv6, 0x6558 a whole Ethernet frame

Three optional fields can follow, each switched on by a flag bit: a checksum (the C flag, 4 bytes including reserved space), a key (the K flag, 4 bytes, used to tell tunnels between the same endpoints apart) and a sequence number (the S flag, 4 bytes, to detect reordering).

The overhead of a plain GRE tunnel over IPv4 is therefore 20 bytes (outer IP) plus 4 (GRE) = 24 bytes per packet. The program below builds a real encapsulated packet byte by byte: an inner IPv4 packet with a UDP payload placed inside a GRE header and an outer IPv4 header. Then it parses the result back.

Python Build and parse a GRE packet

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the inner TTL (64) is not reduced while the packet crosses the Internet inside the tunnel. The tunnel counts as a single hop for the inner packet, which can hide a long physical path from tools such as traceroute. It also means the inner header's checksum and the outer header's checksum are independent.

GRE is not encrypted

GRE adds no confidentiality and no strong authentication. Anyone on the path can read the inner packet. It is common to run GRE inside IPsec (GRE over IPsec). Why not just use IPsec tunnel mode? Classic IPsec tunnels carry only unicast IP traffic and cannot easily carry multicast, so routing protocols like OSPF would not work over them. GRE carries multicast and routing protocols happily, and IPsec then encrypts the whole GRE packet. This combination is a long-time favorite for connecting branch offices.

Routing over a tunnel

A tunnel appears to the router as a normal virtual interface (named something like tunnel0 or gre1). You give it an IP address and add routes that point at it:

TEXT
Site A router
  eth0     203.0.113.1/30      (public side)
  tunnel0  172.16.0.1/30       (tunnel endpoint address, private)
  route 10.2.0.0/16  via tunnel0
  route 0.0.0.0/0    via eth0

When a packet for 10.2.1.5 arrives, the router matches 10.2.0.0/16, hands the packet to tunnel0, which encapsulates it with a new destination (the far endpoint, 198.51.100.7). That outer packet is then routed again, this time using the default route out of eth0. So every tunneled packet passes through the routing table twice.

This two-step lookup creates a classic mistake called recursive routing: if the route to the tunnel's own far endpoint points into the tunnel, the router needs the tunnel to send the packet that goes into the tunnel. The tunnel flaps up and down and traffic loops. It typically happens when a routing protocol learns a better route to the far endpoint through the tunnel itself. Routers report it with messages such as "temporarily disabled due to recursive routing". The fix is to make sure the outer addresses are reached over the physical network, using more specific static routes or filters.

Python Two routing lookups, and a recursive-routing loop

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

MTU and fragmentation over tunnels

Every layer of encapsulation costs bytes. A standard Ethernet link carries at most 1500 bytes of IP packet (the MTU, see the IPv4 packet). A GRE tunnel over IPv4 adds 24 bytes, so the largest inner packet that fits is 1500 - 24 = 1476 bytes. If the inner packet is larger:

  • If its DF (don't fragment) bit is set, the tunnel router has to drop it and send an ICMP "fragmentation needed" message back so the sender can reduce its packet size (this is path MTU discovery, see ICMP).
  • If DF is not set, the router may fragment the packet, which costs CPU and hurts performance.

A frequent real-world symptom: small pings and DNS work through the tunnel, but large file transfers or HTTPS pages stall. The cause is often a firewall that blocks the ICMP messages, so path MTU discovery fails silently. Common fixes are setting the tunnel interface MTU (for example 1476 or 1400), and clamping the TCP MSS (the maximum segment size in the TCP handshake) so TCP never builds oversized packets. Add 20 more bytes when GRE has a key or sequence number, and more again when IPsec is wrapped around it.

Other tunneling protocols

Protocol How it encapsulates Notes
IP-in-IP (protocol 4) Inner IP packet directly after the outer IP header Smallest overhead (20 bytes), IPv4 only, no extra fields
6in4 / 6to4 (protocol 41) IPv6 packet inside IPv4 IPv6 transition; see IPv6
GRE (protocol 47) Outer IP + GRE header Carries many protocols, multicast, and routing protocols
PPTP PPP inside enhanced GRE, control on TCP 1723 Old Microsoft VPN, now insecure (weak authentication and encryption); avoid
L2TP PPP frames inside UDP 1701 No encryption of its own; normally paired with IPsec as L2TP/IPsec
IPsec ESP (protocol 50) Encrypts and authenticates, in tunnel or transport mode The standard secure tunnel; see Firewalls and VPNs
VXLAN, Geneve Ethernet frame inside UDP Data center overlays, see SDN and network virtualization
WireGuard, OpenVPN Encrypted tunnels over UDP (or TCP) Modern VPNs
MPLS Label stack between layer 2 and 3 Provider tunnels, see WAN technologies

When choosing, ask three questions. Do you need confidentiality? (Add IPsec or use WireGuard; do not use plain GRE across the Internet.) Do you need multicast or routing protocols over the tunnel? (GRE or an overlay.) How much MTU can you afford to lose? (IP-in-IP costs the least.)

Limits and troubleshooting

  • Firewalls and NAT: GRE is not TCP or UDP, so port-based NAT cannot translate it easily. Allow protocol 47 explicitly, and avoid NAT between endpoints when you can. Tunnels built on UDP (VXLAN, WireGuard, L2TP) traverse NAT more simply.
  • Performance: encapsulation costs bytes and CPU, and hardware offload is not always available.
  • Visibility: tunneled traffic hides the inner packet from monitoring and firewalls on the path. Inspect it at the endpoints.
  • Stability: a tunnel stays "up" even if the far end is dead unless you configure keepalives, so use them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is an IP tunnel?

    Encapsulation puts the original packet inside a new one. The routers between the tunnel ends only look at the outer header.
  2. 2. A GRE tunnel over IPv4 on a 1500-byte MTU link adds a 20-byte outer IP header and a 4-byte GRE header. What is the largest inner packet that fits without fragmentation?

    1500 - 20 - 4 = 1476 bytes. With optional GRE fields or IPsec the number gets smaller.
  3. 3. Why is GRE often placed inside IPsec?

    GRE supplies the flexibility (any protocol, multicast, routing protocols); IPsec encrypts the resulting GRE packets.
  4. 4. What is recursive routing in a tunnel setup?

    Sending the outer packet requires the tunnel, which requires sending the outer packet. The tunnel goes down and flaps.
  5. 5. Small pings work through a tunnel but large downloads stall. What is the most likely cause?

    Large packets exceed the tunnel's smaller MTU. If the ICMP messages that report this are blocked, the sender never learns to shrink its packets. Lower the tunnel MTU or clamp the TCP MSS.

Summary

  • A tunnel encapsulates a packet inside another packet addressed between two endpoints; the network in between sees only the outer header.
  • GRE (IP protocol 47) is a simple, generic tunnel with a 4-byte header and optional checksum, key and sequence number; it carries multicast and routing protocols but is not encrypted.
  • Tunnel packets are routed twice, and a route to the far endpoint that points into the tunnel causes recursive routing.
  • Each tunnel layer shrinks the usable MTU (24 bytes for plain GRE); fix problems with a lower tunnel MTU and TCP MSS clamping.
  • Choose a protocol by need: IPsec or WireGuard for security, GRE for flexibility, IP-in-IP for the least overhead, VXLAN for data center overlays; avoid PPTP.

Next, see how name services work on top of the network in DNS, or revisit encrypted tunnels in Firewalls and VPNs.