vxlan.go 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. if vx.ValidIDFlag {
  63. bytes[0] |= 0x08
  64. }
  65. if vx.GBPExtension {
  66. bytes[0] |= 0x80
  67. }
  68. if vx.GBPDontLearn {
  69. bytes[1] |= 0x40
  70. }
  71. if vx.GBPApplied {
  72. bytes[1] |= 0x80
  73. }
  74. binary.BigEndian.PutUint16(bytes[2:4], vx.GBPGroupPolicyID)
  75. if vx.VNI >= 1<<24 {
  76. return fmt.Errorf("Virtual Network Identifier = %x exceeds max for 24-bit uint", vx.VNI)
  77. }
  78. binary.BigEndian.PutUint32(bytes[4:8], vx.VNI<<8)
  79. return nil
  80. }