1
2
3 """Generic Routing Encapsulation."""
4
5 import struct
6 import dpkt
7
8 GRE_CP = 0x8000
9 GRE_RP = 0x4000
10 GRE_KP = 0x2000
11 GRE_SP = 0x1000
12 GRE_SS = 0x0800
13 GRE_AP = 0x0080
14
15 GRE_opt_fields = (
16 (GRE_CP|GRE_RP, 'sum', 'H'), (GRE_CP|GRE_RP, 'off', 'H'),
17 (GRE_KP, 'key', 'I'), (GRE_SP, 'seq', 'I'), (GRE_AP, 'ack', 'I')
18 )
19 -class GRE(dpkt.Packet):
20 __hdr__ = (
21 ('flags', 'H', 0),
22 ('p', 'H', 0x0800),
23 )
24 _protosw = {}
25 sre = ()
27 return self.flags & 0x7
30 v = property(get_v, set_v)
31
33 return (self.flags >> 5) & 0x7
35 self.flags = (self.flags & ~0xe0) | ((v & 0x7) << 5)
36 recur = property(get_recur, set_recur)
37
38 - class SRE(dpkt.Packet):
39 __hdr__ = [
40 ('family', 'H', 0),
41 ('off', 'B', 0),
42 ('len', 'B', 0)
43 ]
47
49 if self.v == 0:
50 fields, fmts = [], []
51 opt_fields = GRE_opt_fields
52 else:
53 fields, fmts = [ 'len', 'callid' ], [ 'H', 'H' ]
54 opt_fields = GRE_opt_fields[-2:]
55 for flags, field, fmt in opt_fields:
56 if self.flags & flags:
57 fields.append(field)
58 fmts.append(fmt)
59 return fields, fmts
60
82
87
88
89
91 fields, fmts = self.opt_fields_fmts()
92 if fields:
93 vals = []
94 for f in fields:
95 vals.append(getattr(self, f))
96 opt_s = struct.pack(''.join(fmts), *vals)
97 else:
98 opt_s = ''
99 return self.pack_hdr() + opt_s + ''.join(map(str, self.sre)) + \
100 str(self.data)
101
102
103 import ethernet
104 GRE._protosw.update(ethernet.Ethernet._typesw)
105