Fix unit tests
[govpp.git] / vendor / github.com / google / gopacket / layers / geneve.go
1 // Copyright 2016 Google, Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source
5 // tree.
6
7 package layers
8
9 import (
10         "encoding/binary"
11
12         "github.com/google/gopacket"
13 )
14
15 // Geneve is specifed here https://tools.ietf.org/html/draft-ietf-nvo3-geneve-03
16 // Geneve Header:
17 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
18 // |Ver|  Opt Len  |O|C|    Rsvd.  |          Protocol Type        |
19 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
20 // |        Virtual Network Identifier (VNI)       |    Reserved   |
21 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
22 // |                    Variable Length Options                    |
23 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
24 type Geneve struct {
25         BaseLayer
26         Version        uint8        // 2 bits
27         OptionsLength  uint8        // 6 bits
28         OAMPacket      bool         // 1 bits
29         CriticalOption bool         // 1 bits
30         Protocol       EthernetType // 16 bits
31         VNI            uint32       // 24bits
32         Options        []*GeneveOption
33 }
34
35 // Geneve Tunnel Options
36 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
37 // |          Option Class         |      Type     |R|R|R| Length  |
38 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39 // |                      Variable Option Data                     |
40 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
41 type GeneveOption struct {
42         Class  uint16 // 16 bits
43         Type   uint8  // 8 bits
44         Flags  uint8  // 3 bits
45         Length uint8  // 5 bits
46         Data   []byte
47 }
48
49 // LayerType returns LayerTypeGeneve
50 func (gn *Geneve) LayerType() gopacket.LayerType { return LayerTypeGeneve }
51
52 func decodeGeneveOption(data []byte, gn *Geneve) (*GeneveOption, uint8) {
53         opt := &GeneveOption{}
54
55         opt.Class = binary.BigEndian.Uint16(data[0:1])
56         opt.Type = data[2]
57         opt.Flags = data[3] >> 4
58         opt.Length = data[3] & 0xf
59
60         copy(opt.Data, data[4:opt.Length])
61
62         return opt, 4 + opt.Length
63 }
64
65 func (gn *Geneve) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
66         gn.Version = data[0] >> 7
67         gn.OptionsLength = data[0] & 0x3f
68
69         gn.OAMPacket = data[1]&0x80 > 0
70         gn.CriticalOption = data[1]&0x40 > 0
71         gn.Protocol = EthernetType(binary.BigEndian.Uint16(data[2:4]))
72
73         var buf [4]byte
74         copy(buf[1:], data[4:7])
75         gn.VNI = binary.BigEndian.Uint32(buf[:])
76
77         offset, length := uint8(8), gn.OptionsLength
78         for length > 0 {
79                 opt, len := decodeGeneveOption(data[offset:], gn)
80                 gn.Options = append(gn.Options, opt)
81
82                 length -= len
83                 offset += len
84         }
85
86         gn.BaseLayer = BaseLayer{data[:offset], data[offset:]}
87
88         return nil
89 }
90
91 func (gn *Geneve) NextLayerType() gopacket.LayerType {
92         return gn.Protocol.LayerType()
93 }
94
95 func decodeGeneve(data []byte, p gopacket.PacketBuilder) error {
96         gn := &Geneve{}
97         return decodingLayerDecoder(gn, data, p)
98 }