1 // Copyright 2016 Google, Inc. All rights reserved.
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
12 "github.com/google/gopacket"
15 // VXLAN is specifed in RFC 7348 https://tools.ietf.org/html/rfc7348
16 // G, D, A, Group Policy ID from https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
18 // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
20 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
21 // |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
22 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
23 // | 24 bit VXLAN Network Identifier | Reserved |
24 // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
26 // VXLAN is a VXLAN packet header
29 ValidIDFlag bool // 'I' bit per RFC 7348
30 VNI uint32 // 'VXLAN Network Identifier' 24 bits per RFC 7348
31 GBPExtension bool // 'G' bit per Group Policy https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
32 GBPDontLearn bool // 'D' bit per Group Policy
33 GBPApplied bool // 'A' bit per Group Policy
34 GBPGroupPolicyID uint16 // 'Group Policy ID' 16 bits per Group Policy
37 // LayerType returns LayerTypeVXLAN
38 func (vx *VXLAN) LayerType() gopacket.LayerType { return LayerTypeVXLAN }
40 func decodeVXLAN(data []byte, p gopacket.PacketBuilder) error {
43 // VNI is a 24bit number, Uint32 requires 32 bits
45 copy(buf[1:], data[4:7])
47 // RFC 7348 https://tools.ietf.org/html/rfc7348
48 vx.ValidIDFlag = data[0]&0x08 > 0 // 'I' bit per RFC7348
49 vx.VNI = binary.BigEndian.Uint32(buf[:]) // VXLAN Network Identifier per RFC7348
51 // Group Based Policy https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
52 vx.GBPExtension = data[0]&0x80 > 0 // 'G' bit per the group policy draft
53 vx.GBPDontLearn = data[1]&0x40 > 0 // 'D' bit - the egress VTEP MUST NOT learn the source address of the encapsulated frame.
54 vx.GBPApplied = data[1]&0x80 > 0 // 'A' bit - indicates that the group policy has already been applied to this packet.
55 vx.GBPGroupPolicyID = binary.BigEndian.Uint16(data[2:4]) // Policy ID as per the group policy draft
59 vx.Contents = data[:vxlanLength]
60 vx.Payload = data[vxlanLength:]
63 return p.NextDecoder(LinkTypeEthernet)
66 // SerializeTo writes the serialized form of this layer into the
67 // SerializationBuffer, implementing gopacket.SerializableLayer.
68 // See the docs for gopacket.SerializableLayer for more info.
69 func (vx *VXLAN) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
70 bytes, err := b.PrependBytes(8)
88 binary.BigEndian.PutUint16(bytes[2:4], vx.GBPGroupPolicyID)
90 return fmt.Errorf("Virtual Network Identifier = %x exceeds max for 24-bit uint", vx.VNI)
92 binary.BigEndian.PutUint32(bytes[4:8], vx.VNI<<8)