Skip to content

Commit

Permalink
Fix geneve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gpotter2 committed Nov 17, 2023
1 parent 28e9557 commit 6294c6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
12 changes: 6 additions & 6 deletions scapy/contrib/geneve.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"""
Geneve: Generic Network Virtualization Encapsulation
draft-ietf-nvo3-geneve-16
https://datatracker.ietf.org/doc/html/rfc8926
"""

import struct
Expand All @@ -19,7 +19,6 @@
from scapy.layers.inet import IP, UDP
from scapy.layers.inet6 import IPv6
from scapy.layers.l2 import Ether, ETHER_TYPES
from scapy.compat import chb, orb

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

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


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

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

def answers(self, other):
Expand Down
8 changes: 5 additions & 3 deletions test/contrib/geneve.uts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ assert a.mysummary() in ['GENEVE (vni=0x0,optionlen=0,proto=0x800)', 'GENEVE (vn

= GENEVE - Optionlen

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

0 comments on commit 6294c6e

Please # to comment.