vxlan.go 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. package layers
  7. import (
  8. "encoding/binary"
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. // VXLAN is specifed in RFC 7348 https://tools.ietf.org/html/rfc7348
  13. // G, D, A, Group Policy ID from https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
  14. // 0 1 2 3
  15. // 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
  16. // 0 8 16 24 32
  17. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  18. // |G|R|R|R|I|R|R|R|R|D|R|R|A|R|R|R| Group Policy ID |
  19. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  20. // | 24 bit VXLAN Network Identifier | Reserved |
  21. // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  22. // VXLAN is a VXLAN packet header
  23. type VXLAN struct {
  24. BaseLayer
  25. ValidIDFlag bool // 'I' bit per RFC 7348
  26. VNI uint32 // 'VXLAN Network Identifier' 24 bits per RFC 7348
  27. GBPExtension bool // 'G' bit per Group Policy https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
  28. GBPDontLearn bool // 'D' bit per Group Policy
  29. GBPApplied bool // 'A' bit per Group Policy
  30. GBPGroupPolicyID uint16 // 'Group Policy ID' 16 bits per Group Policy
  31. }
  32. // LayerType returns LayerTypeVXLAN
  33. func (vx *VXLAN) LayerType() gopacket.LayerType { return LayerTypeVXLAN }
  34. func decodeVXLAN(data []byte, p gopacket.PacketBuilder) error {
  35. vx := &VXLAN{}
  36. // VNI is a 24bit number, Uint32 requires 32 bits
  37. var buf [4]byte
  38. copy(buf[1:], data[4:7])
  39. // RFC 7348 https://tools.ietf.org/html/rfc7348
  40. vx.ValidIDFlag = data[0]&0x08 > 0 // 'I' bit per RFC7348
  41. vx.VNI = binary.BigEndian.Uint32(buf[:]) // VXLAN Network Identifier per RFC7348
  42. // Group Based Policy https://tools.ietf.org/html/draft-smith-vxlan-group-policy-00
  43. vx.GBPExtension = data[0]&0x80 > 0 // 'G' bit per the group policy draft
  44. vx.GBPDontLearn = data[1]&0x40 > 0 // 'D' bit - the egress VTEP MUST NOT learn the source address of the encapsulated frame.
  45. vx.GBPApplied = data[1]&0x80 > 0 // 'A' bit - indicates that the group policy has already been applied to this packet.
  46. vx.GBPGroupPolicyID = binary.BigEndian.Uint16(data[2:4]) // Policy ID as per the group policy draft
  47. // Layer information
  48. const vxlanLength = 8
  49. vx.Contents = data[:vxlanLength]
  50. vx.Payload = data[vxlanLength:]
  51. p.AddLayer(vx)
  52. return p.NextDecoder(LinkTypeEthernet)
  53. }
  54. // SerializeTo writes the serialized form of this layer into the
  55. // SerializationBuffer, implementing gopacket.SerializableLayer.
  56. // See the docs for gopacket.SerializableLayer for more info.
  57. func (vx *VXLAN) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  58. bytes, err := b.PrependBytes(8)
  59. if err != nil {
  60. return err
  61. }
  62. // PrependBytes does not guarantee that bytes are zeroed. Setting flags via OR requires that they start off at zero
  63. bytes[0] = 0
  64. bytes[1] = 0
  65. if vx.ValidIDFlag {
  66. bytes[0] |= 0x08
  67. }
  68. if vx.GBPExtension {
  69. bytes[0] |= 0x80
  70. }
  71. if vx.GBPDontLearn {
  72. bytes[1] |= 0x40
  73. }
  74. if vx.GBPApplied {
  75. bytes[1] |= 0x80
  76. }
  77. binary.BigEndian.PutUint16(bytes[2:4], vx.GBPGroupPolicyID)
  78. if vx.VNI >= 1<<24 {
  79. return fmt.Errorf("Virtual Network Identifier = %x exceeds max for 24-bit uint", vx.VNI)
  80. }
  81. binary.BigEndian.PutUint32(bytes[4:8], vx.VNI<<8)
  82. return nil
  83. }