Permalink
Browse files

Fix geneve options parsing

  • Loading branch information...
safchain authored and gconnell committed Sep 26, 2018
1 parent 5644b95 commit 9b9af424be8c676a5dd011b22824bc8e259809b7
Showing with 54 additions and 4 deletions.
  1. +5 −4 layers/geneve.go
  2. +49 −0 layers/geneve_test.go
@@ -53,14 +53,15 @@ func (gn *Geneve) LayerType() gopacket.LayerType { return LayerTypeGeneve }
func decodeGeneveOption(data []byte, gn *Geneve) (*GeneveOption, uint8) {
opt := &GeneveOption{}
opt.Class = binary.BigEndian.Uint16(data[0:1])
opt.Class = binary.BigEndian.Uint16(data[0:2])
opt.Type = data[2]
opt.Flags = data[3] >> 4
opt.Length = data[3] & 0xf
opt.Length = (data[3]&0xf)*4 + 4
opt.Data = make([]byte, opt.Length-4)
copy(opt.Data, data[4:opt.Length])
return opt, 4 + opt.Length
return opt, opt.Length
}
func (gn *Geneve) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
@@ -70,7 +71,7 @@ func (gn *Geneve) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error
}
gn.Version = data[0] >> 7
gn.OptionsLength = data[0] & 0x3f
gn.OptionsLength = (data[0] & 0x3f) * 4
gn.OAMPacket = data[1]&0x80 > 0
gn.CriticalOption = data[1]&0x40 > 0
@@ -45,6 +45,19 @@ var testPacketGeneve2 = []byte{
0x34, 0x35, 0x36, 0x37,
}
var testPacketGeneve3 = []byte{
0x00, 0x1b, 0x21, 0x3c, 0xac, 0x30, 0x00, 0x1b, 0x21, 0x3c, 0xab, 0x64, 0x08, 0x00, 0x45, 0x00,
0x00, 0x8e, 0xdf, 0xad, 0x40, 0x00, 0x40, 0x11, 0x32, 0xaf, 0x14, 0x00, 0x00, 0x01, 0x14, 0x00,
0x00, 0x02, 0x31, 0x4a, 0x17, 0xc1, 0x00, 0x7a, 0x00, 0x00, 0x02, 0x40, 0x65, 0x58, 0x00, 0x00,
0x0a, 0x00, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0xfe, 0x71, 0xd8, 0x83, 0x72, 0x4f,
0xb6, 0x9e, 0xd2, 0x49, 0x51, 0x48, 0x08, 0x00, 0x45, 0x00, 0x00, 0x54, 0xbd, 0xa2, 0x40, 0x00,
0x40, 0x01, 0x41, 0x04, 0x1e, 0x00, 0x00, 0x01, 0x1e, 0x00, 0x00, 0x02, 0x08, 0x00, 0x2c, 0x54,
0x29, 0x52, 0x00, 0x17, 0xf1, 0xa2, 0xce, 0x54, 0x00, 0x00, 0x00, 0x00, 0x17, 0x78, 0x0c, 0x00,
0x00, 0x00, 0x00, 0x00, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
}
func TestDecodeGeneve1(t *testing.T) {
p := gopacket.NewPacket(testPacketGeneve1, LinkTypeLinuxSLL, gopacket.Default)
if p.ErrorLayer() != nil {
@@ -101,6 +114,42 @@ func TestDecodeGeneve2(t *testing.T) {
}
}
func TestDecodeGeneve3(t *testing.T) {
p := gopacket.NewPacket(testPacketGeneve3, LinkTypeEthernet, gopacket.Default)
if p.ErrorLayer() != nil {
t.Error("Failed to decode packet:", p.ErrorLayer().Error())
}
checkLayers(p, []gopacket.LayerType{
LayerTypeEthernet, LayerTypeIPv4, LayerTypeUDP, LayerTypeGeneve,
LayerTypeEthernet, LayerTypeIPv4, LayerTypeICMPv4, gopacket.LayerTypePayload,
}, t)
if got, ok := p.Layer(LayerTypeGeneve).(*Geneve); ok {
want := &Geneve{
BaseLayer: BaseLayer{
Contents: testPacketGeneve3[42:58],
Payload: testPacketGeneve3[58:156],
},
Version: 0x0,
OptionsLength: 0x8,
OAMPacket: false,
CriticalOption: true,
Protocol: EthernetTypeTransparentEthernetBridging,
VNI: 0xa,
Options: []*GeneveOption{
&GeneveOption{
Class: 0x0,
Type: 0x80,
Length: 8,
Data: []byte{0, 0, 0, 0xc},
},
},
}
if !reflect.DeepEqual(want, got) {
t.Errorf("Geneve layer mismatch, \nwant %#v\ngot %#v\n", want, got)
}
}
}
func BenchmarkDecodeGeneve1(b *testing.B) {
for i := 0; i < b.N; i++ {
gopacket.NewPacket(testPacketGeneve1, LinkTypeEthernet, gopacket.NoCopy)

0 comments on commit 9b9af42

Please sign in to comment.