# Generic Routing Encapsulation

Generic Routing Encapsulation (GRE) é um protocolo de encapsulamento desenvolvido pela Cisco Systems que pode encapsular uma ampla variedade de protocolos de Camada de rede dentro de conexões virtuais ponto-a-ponto através de uma rede IP.

## Exemplos de uso

* Em conjunto com o PPTP para criar VPNs.
* Em conjunto com IPsec VPN para permitir a passagem de informações de roteamento entre redes conectadas.
* Em protocolos de mobilidade.
* Linux e BSD podem estabelecer conexão IP direta através de tunel GRE para interoperações com equipamentos Cisco.
* Em appliances de proteção contraAtaque de negação de serviço (DDoS) para terminais desprotegidos.

### Exemplo do empilhamento do protocolo

| Camada no Modelo OSI | Protocolo |
| 5. Sessão | X.225 |
| 4. Transporte | UDP |
| 3. Rede (GRE-encapsulated) | IPv6 |
| Encapsulamento | GRE |
| 3. Rede | IPv4 |
| 2. Enlace | Ethernet |
| 1. Físical | 100BASE-TX |
Com base nos princípios de camadas de protocolo do Modelo OSI, o encapsulamento de protocolo, não especificamente GRE, quebra a ordem de estratificação. Pode ser visto como um separador entre duas pilhas de protocolos diferentes, um atuando como portadora pra outra.

## IP como protocolo de entrega

Pacotes GRE que são encapsulados dentro de IP usam o protocolo IP do tipo 47.

## Cabeçalho do pacote
### Cabeçalho do pacote GRE padrão

A estrutura do cabeçalho GRE padrão, como definido em RFC 2784 e RFC 2890, é representada no diagrama abaixo.
| Bits 0–3 | Bits 0–3 | Bits 0–3 | Bits 0–3 | 4–12 | 4–12 | 4–12 | 4–12 | 4–12 | 4–12 | 4–12 | 4–12 | 4–12 | 13–15 | 13–15 | 13–15 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 |
| C | | K | S | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Reserved0 | Version | Version | Version | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type |
| Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Checksum (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) | Reserved1 (optional) |
| Key (optional) ||||||||||||||||||||||||||||||||
| Sequence Number (optional) ||||||||||||||||||||||||||||||||

#### C

    Checksum bit. Set to 1 if a checksum is present.

#### K

    Key bit. Set to 1 if a key is present.

#### S

    Sequence number bit. Set to 1 if a sequence number is present.

#### Reserved0

    Reserved bits; set to 0.

#### Version

    GRE Version number; set to 0.

#### Protocol Type

    Indicates the ether protocol type of the encapsulated payload. (For IPv4, this would be hex 0800.)

#### Checksum

    Present if the C bit is set; contains the checksum for the GRE header and payload.

#### Reserved1

    Present if the C bit is set; is set to 0.

#### Key

    Present if the K bit is set; contains an application-specific key value.

#### Sequence Number

    Present if the S bit is set; contains a sequence number for the GRE packet.

### Cabeçalho de pacote PPTP GRE

O Point-to-Point Tunneling Protocol (PPTP), definido na RFC 2637, usa uma variante da estrutura GRE como cabeçalho, representada abaixo. PPTP cria um túnel GRE através do qual os pacotes PPTP GRE são enviadas.
| Bits 0–4 | Bits 0–4 | Bits 0–4 | Bits 0–4 | Bits 0–4 | 5–7 | 5–7 | 5–7 | 8 | 9-12 | 9-12 | 9-12 | 9-12 | 13–15 | 13–15 | 13–15 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 | 16–31 |
| C | R | K | S | s | Recur | Recur | Recur | A | Flags | Flags | Flags | Flags | Version | Version | Version | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type | Protocol Type |
| Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Payload Length | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID | Key Call ID |
| Sequence Number (optional) ||||||||||||||||||||||||||||||||
| Acknowledgement Number (optional) ||||||||||||||||||||||||||||||||

#### C

    Checksum bit. For PPTP GRE packets, this is set to 0.

#### R

    Routing bit. For PPTP GRE packets, this is set to 0.

#### K

    Key bit. For PPTP GRE packets, this is set to 1. (All PPTP GRE packets carry a key.)

#### S

    Sequence number bit. Set to 1 if a sequence number is supplied, indicating a PPTP GRE data packet.

#### s

    Strict source route bit. For PPTP GRE packets, this is set to 0.

#### Recur

    Recursion control bits. For PPTP GRE packets, these are set to 0.

#### A

    Acknowledgement number present. Set to 1 if an acknowledgement number is supplied, indicating a PPTP GRE acknowledgement packet.

#### Flags

    Flag bits. For PPTP GRE packets, these are set to 0.

#### Version

    GRE Version number. For PPTP GRE packets, this is set to 1.

#### Protocol Type

    For PPTP GRE packets, this is set to hex 880B.

#### Key Payload Length

    Contains the size of the payload, not including the GRE header.

#### Key Call ID

    Contains the Peer's Call ID for the session to which the packet belongs.

#### Sequence Number

    Present if the S bit is set; contains the GRE payload sequence number.

#### Acknowledgement Number

    Present if the A bit is set; contains the sequence number of the highest GRE payload packet received by the sender.