GRE - Generic Routing Encapsulation¶
pcapkit.protocols.link.gre contains
GRE
only, which implements the extractor for the Generic Routing Encapsulation
base header [*] plus the optional Key and Sequence Number extensions [†], as
reached through IP protocol number 47 behind any layer sharing
Internet.__proto__,
IPv4 and IPv6 among them, or as seen on a
LINKTYPE_LINUX_SLL/LINKTYPE_LINUX_SLL2 capture whose
arphrd_type is ARPHRD_IPGRE or ARPHRD_IP6GRE – ARPHRD_IPGRE
and ARPHRD_IP6GRE both land on this one class, the “IP”/”IP6” distinction
being only which outer IP version the tunnel carries. Its structure, with
every optional field present, is described as below:
Octets |
Bits |
Name |
Description |
|---|---|---|---|
0 |
0 |
|
Checksum Present ( |
0 |
2 |
|
Key Present ( |
0 |
3 |
|
Sequence Number Present ( |
0 |
13 |
|
Version Number ( |
2 |
16 |
|
Protocol Type (EtherType) |
4 |
32 |
|
Checksum, present only if |
6 |
48 |
|
Reserved1, present only if |
8/4 |
64 |
|
Key, present only if |
12/8/4 |
96 |
|
Sequence Number, present only if |
C, K and S each gate their own field independently, so the header
is 4, 8, 12 or 16 octets depending on which are set. The Protocol Type field
is an EtherType, dispatched onward through
Link.__proto__, the same
family registry Ethernet, VLAN and LinuxSLL already share – unlike
FRAD, this class keeps no registry of
its own. A nonzero Version Number (RFC 2637’s “Enhanced GRE”/PPTP, version 1)
is a different, incompatible protocol and is refused rather than misread; see
the module docstring’s Scope section.
Flow tracing and TCP reassembly follow the tunnel’s inner flow, keyed by the IP header nearest the TCP segment as for any tunnel (#1581), and IPv4 reassembly of an outer datagram fragmented in transit yields a parsed GRE layer.
- class pcapkit.protocols.link.gre.GRE(file=None, length=None, **kwargs)[source]¶
-
This class implements Generic Routing Encapsulation (GRE).
The Protocol Type field is dispatched as an EtherType through
Link.__proto__, exactly asVLANresolves its owntypefield – so, unlikeFRAD, this class declares no registry of its own.The checksum, present when
Cis set, is verified on read underCHECKSUM_VERIFY, and computed bymake()whenchecksum_presentasks for one without giving it. Neither this header nor the Linux cooked-capture header before it states a length, so the message runs to the end of its frame. A frame the capture cut short, holding fewer octets than its original length, reads asTRUNCATED, asframe_lengthtells it (#2653), and so does a header the data ends inside (#2689). The header records such a cut, so the message rebuilt on its own reads as cut too (#2711).- read(length=None, **kwargs)[source]¶
Read Generic Routing Encapsulation (GRE) frame.
Structure of a GRE frame, with every optional field present; see the module docstring for the full diagram:
0 1 2 3 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |C| |K|S| Reserved0 | Ver | Protocol Type | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
- Parameters:
- Return type:
- Returns:
Parsed packet data.
- Raises:
ProtocolError – If the Version Number is not
0– see the module docstring’s Scope section.
- make(checksum=None, reserved1=None, checksum_present=None, key=None, sequence_number=None, reserved=0, version=0, protocol_type=EtherType.Internet_Protocol_version_4, payload=b'', **kwargs)[source]¶
Make (construct) packet data.
The presence of each optional field is inferred from whether its value is given, as
L2TPv2.makeinfersSfrom whetherns/nrare given: passingchecksumsetsC,keysetsK, andsequence_numbersetsS.Ccan be set without achecksumtoo, throughchecksum_present, as L2TPv2’slength_flagsetsLwithout a length.- Parameters:
checksum (
int|None) – Checksum; setting this also setsC. IfNonewithCset, computed over the header andpayload(RFC 2784 Section 2.5) whenCHECKSUM_COMPUTEis on, zero when it is off.reserved1 (
int|None) – Reserved1; only meaningful, and only accepted, whenCis set. Defaults to0whenCis set and this is not.checksum_present (
bool|None) – WhetherCis set, and the Checksum and Reserved1 fields present. WhenNone,Cis set ifchecksumis given.sequence_number (
int|None) – Sequence Number, RFC 2890; setting this also setsS.reserved (
int) – Reserved bits of the flags word, in their on-wire positions (bit 1 and bits 4-12, i.e. within0x4FF8).version (
Literal[0]) – Version Number; RFC 2784 fixes this at0.protocol_type (
EtherType|int) – Protocol Type: the EtherType of the encapsulated payload.payload (
bytes|ProtocolBase|Schema) – Payload data.**kwargs (
Any) – Arbitrary keyword arguments.
- Return type:
- Returns:
Constructed packet data.
- Raises:
ProtocolError – If
checksumorreserved1is given withCclear; ifreservedsets a bit outside0x4FF8; ifversiondoes not fit in 3 bits; ifprotocol_typeis a name, abool, or a member or code its field cannot carry; or ifchecksum,keyorsequence_numberis given but does not fit the width of its field (16 bits forchecksum, 32 bits forkeyandsequence_number).
- __length_hint__()[source]¶
Return an estimated length for the object.
The longest form – every optional field present, 16 octets – per the reason
L2TPv2.__length_hint__gives for a variable-length header: the shortest form would reject a fully captured longer header as too short.- Return type:
Literal[16]
- classmethod __index__()[source]¶
Numeral registry index of the protocol.
- Return type:
- Returns:
Numeral registry index of the protocol in IANA protocol numbers, which
Internet.__proto__is keyed by.LinuxSLL.__proto__names its own keys for this class,IPGREandIP6GRE(The Registry Index).
Header Schemas¶
- class pcapkit.protocols.schema.link.gre.GRE(flags=NO_VALUE, protocol_type=NO_VALUE, checksum=NO_VALUE, reserved1=NO_VALUE, key=NO_VALUE, sequence_number=NO_VALUE, payload=NO_VALUE, *, __packet__=None, **kwargs)[source]¶
Bases:
SchemaHeader schema for a Generic Routing Encapsulation (GRE) frame.
The base header [RFC 2784] plus the optional Key and Sequence Number extensions [RFC 2890], as carried under IP protocol number 47, or as presented to a
LINKTYPE_LINUX_SLL/LINKTYPE_LINUX_SLL2capture whosearphrd_typeisARPHRD_IPGREorARPHRD_IP6GRE.- flags: FlagsType = <BitField flags>¶
Flags and version.
- protocol_type: EtherType = <EnumField protocol_type>¶
Protocol Type – the EtherType of the encapsulated payload.
- checksum: int | None = <ConditionalField checksum>¶
Checksum, present only when
flags.checksum_presentis set.
- reserved1: int | None = <ConditionalField reserved1>¶
Reserved1, present only when
flags.checksum_presentis set.
- key: int | None = <ConditionalField key>¶
Key, present only when
flags.key_presentis set, RFC 2890.
Data Models¶
- class pcapkit.protocols.data.link.gre.GRE(checksum_present, key_present, sequence_present, reserved, version, protocol_type, checksum, reserved1, key, sequence_number, len)[source]¶
Bases:
ProtocolData model for a Generic Routing Encapsulation (GRE) frame.
- reserved: int¶
Reserved bits of the flags word outside
C/K/S/Ver, in their on-wire positions (i.e. masked by0x4FF8). RFC 2784 calls the whole 12-bit span (before RFC 2890 carvedK/Sout of it)Reserved0and requires it sent as zero; carried verbatim so a rebuild reproduces the word as it arrived.
- checksum: int | None¶
Checksum, present only when
checksum_presentis set.
- reserved1: int | None¶
Reserved1, present only when
checksum_presentis set; RFC 2784 requires it sent as zero, carried verbatim.
- key: int | None¶
Key, present only when
key_presentis set, RFC 2890.
- sequence_number: int | None¶
Sequence Number, present only when
sequence_presentis set, RFC 2890.
- len: int¶
Header length, in octets –
4, plus4more for each ofchecksum_present,key_presentandsequence_presentthat is set.
- checksum_status: ChecksumStatus¶
Outcome of verifying
checksum, present only whenCHECKSUM_VERIFYis on;NOT_COMPUTEDwhenchecksum_presentis clear.
Footnotes