1
2
3 """Generic Routing Encapsulation."""
4 from __future__ import absolute_import
5
6 import struct
7 import codecs
8
9 from . import dpkt
10 from . import ethernet
11 from .decorators import deprecated
12 from .compat import compat_izip
13
14 GRE_CP = 0x8000
15 GRE_RP = 0x4000
16 GRE_KP = 0x2000
17 GRE_SP = 0x1000
18 GRE_SS = 0x0800
19 GRE_AP = 0x0080
20
21 GRE_opt_fields = (
22 (GRE_CP | GRE_RP, 'sum', 'H'), (GRE_CP | GRE_RP, 'off', 'H'),
23 (GRE_KP, 'key', 'I'), (GRE_SP, 'seq', 'I'), (GRE_AP, 'ack', 'I')
24 )
25
26
27 -class GRE(dpkt.Packet):
28 """Generic Routing Encapsulation.
29
30 TODO: Longer class information....
31
32 Attributes:
33 __hdr__: Header fields of GRE.
34 TODO.
35 """
36
37 __hdr__ = (
38 ('flags', 'H', 0),
39 ('p', 'H', 0x0800),
40 )
41 sre = ()
42
43 @property
45 return self.flags & 0x7
46
47 @v.setter
50
51 @property
53 return (self.flags >> 5) & 0x7
54
55 @recur.setter
57 self.flags = (self.flags & ~0xe0) | ((v & 0x7) << 5)
58
59 - class SRE(dpkt.Packet):
60 __hdr__ = [
61 ('family', 'H', 0),
62 ('off', 'B', 0),
63 ('len', 'B', 0)
64 ]
65
69
71 if self.v == 0:
72 fields, fmts = [], []
73 opt_fields = GRE_opt_fields
74 else:
75 fields, fmts = ['len', 'callid'], ['H', 'H']
76 opt_fields = GRE_opt_fields[-2:]
77 for flags, field, fmt in opt_fields:
78 if self.flags & flags:
79 fields.append(field)
80 fmts.append(fmt)
81 return fields, fmts
82
107
111
113 fields, fmts = self.opt_fields_fmts()
114 if fields:
115 vals = []
116 for f in fields:
117 vals.append(getattr(self, f))
118 opt_s = struct.pack(b''.join(fmts), *vals)
119 else:
120 opt_s = b''
121 return self.pack_hdr() + opt_s + b''.join(map(bytes, self.sre)) + bytes(self.data)
122
125
126 s = codecs.decode("3081880a0067178000068fb100083a76", 'hex') + b"A" * 103
127 g = GRE(s)
128
129 assert g.v == 1
130 assert g.p == 0x880a
131 assert g.seq == 430001
132 assert g.ack == 539254
133 assert g.callid == 6016
134 assert g.len == 103
135 assert g.data == b"A" * 103
136
137 s = codecs.decode("3001880a00b2001100083ab8", 'hex') + b"A" * 178
138 g = GRE(s)
139
140 assert g.v == 1
141 assert g.p == 0x880a
142 assert g.seq == 539320
143 assert g.callid == 17
144 assert g.len == 178
145 assert g.data == b"A" * 178
146
147
148 if __name__ == '__main__':
149 test_gre_v1()
150