Skip to content

Commit 6294c6e

Browse files
committed
Fix geneve tests
1 parent 28e9557 commit 6294c6e

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

scapy/contrib/geneve.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010
Geneve: Generic Network Virtualization Encapsulation
1111
12-
draft-ietf-nvo3-geneve-16
12+
https://datatracker.ietf.org/doc/html/rfc8926
1313
"""
1414

1515
import struct
@@ -19,7 +19,6 @@
1919
from scapy.layers.inet import IP, UDP
2020
from scapy.layers.inet6 import IPv6
2121
from scapy.layers.l2 import Ether, ETHER_TYPES
22-
from scapy.compat import chb, orb
2322

2423
CLASS_IDS = {0x0100: "Linux",
2524
0x0101: "Open vSwitch",
@@ -42,12 +41,12 @@ class GeneveOptions(Packet):
4241
XByteField("type", 0x00),
4342
BitField("reserved", 0, 3),
4443
BitField("length", None, 5),
45-
StrLenField('data', '', length_from=lambda x:x.length * 4)]
44+
StrLenField('data', '', length_from=lambda x: x.length * 4)]
4645

4746
def post_build(self, p, pay):
4847
if self.length is None:
4948
tmp_len = len(self.data) // 4
50-
p = p[:3] + struct.pack("!B", tmp_len) + p[4:]
49+
p = p[:3] + struct.pack("!B", (p[3] & 0x3) | (tmp_len & 0x1f)) + p[4:]
5150
return p + pay
5251

5352

@@ -61,12 +60,13 @@ class GENEVE(Packet):
6160
XShortEnumField("proto", 0x0000, ETHER_TYPES),
6261
X3BytesField("vni", 0),
6362
XByteField("reserved2", 0x00),
64-
PacketListField("options", [], GeneveOptions, length_from=lambda pkt:pkt.optionlen * 4)]
63+
PacketListField("options", [], GeneveOptions,
64+
length_from=lambda pkt: pkt.optionlen * 4)]
6565

6666
def post_build(self, p, pay):
6767
if self.optionlen is None:
6868
tmp_len = (len(p) - 8) // 4
69-
p = chb(tmp_len & 0x3f | orb(p[0]) & 0xc0) + p[1:]
69+
p = struct.pack("!B", (p[0] & 0xc0) | (tmp_len & 0x3f)) + p[1:]
7070
return p + pay
7171

7272
def answers(self, other):

test/contrib/geneve.uts

+5-3
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ assert a.mysummary() in ['GENEVE (vni=0x0,optionlen=0,proto=0x800)', 'GENEVE (vn
6969

7070
= GENEVE - Optionlen
7171

72-
data = raw(RandString(size=random.randint(0, pow(2, 6)) * 4))
73-
p = GENEVE(raw(GENEVE(options=GeneveOptions(data=data))))
74-
assert p[GENEVE].optionlen == (len(data) // 4 + 1)
72+
for size in range(0, 0x1f, 4):
73+
p = GENEVE(bytes(GENEVE(options=GeneveOptions(data=RandString(size)))))
74+
assert p[GENEVE].optionlen == (size // 4 + 1)
75+
assert len(p[GENEVE].options[0].data) == size
76+

0 commit comments

Comments
 (0)