vrrp.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. "errors"
  10. "net"
  11. "github.com/google/gopacket"
  12. )
  13. /*
  14. This layer provides decoding for Virtual Router Redundancy Protocol (VRRP) v2.
  15. https://tools.ietf.org/html/rfc3768#section-5
  16. 0 1 2 3
  17. 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
  18. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  19. |Version| Type | Virtual Rtr ID| Priority | Count IP Addrs|
  20. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  21. | Auth Type | Adver Int | Checksum |
  22. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  23. | IP Address (1) |
  24. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  25. | . |
  26. | . |
  27. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  28. | IP Address (n) |
  29. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  30. | Authentication Data (1) |
  31. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  32. | Authentication Data (2) |
  33. +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  34. */
  35. type VRRPv2Type uint8
  36. type VRRPv2AuthType uint8
  37. const (
  38. VRRPv2Advertisement VRRPv2Type = 0x01 // router advertisement
  39. )
  40. // String conversions for VRRP message types
  41. func (v VRRPv2Type) String() string {
  42. switch v {
  43. case VRRPv2Advertisement:
  44. return "VRRPv2 Advertisement"
  45. default:
  46. return ""
  47. }
  48. }
  49. const (
  50. VRRPv2AuthNoAuth VRRPv2AuthType = 0x00 // No Authentication
  51. VRRPv2AuthReserved1 VRRPv2AuthType = 0x01 // Reserved field 1
  52. VRRPv2AuthReserved2 VRRPv2AuthType = 0x02 // Reserved field 2
  53. )
  54. func (v VRRPv2AuthType) String() string {
  55. switch v {
  56. case VRRPv2AuthNoAuth:
  57. return "No Authentication"
  58. case VRRPv2AuthReserved1:
  59. return "Reserved"
  60. case VRRPv2AuthReserved2:
  61. return "Reserved"
  62. default:
  63. return ""
  64. }
  65. }
  66. // VRRPv2 represents an VRRP v2 message.
  67. type VRRPv2 struct {
  68. BaseLayer
  69. Version uint8 // The version field specifies the VRRP protocol version of this packet (v2)
  70. Type VRRPv2Type // The type field specifies the type of this VRRP packet. The only type defined in v2 is ADVERTISEMENT
  71. VirtualRtrID uint8 // identifies the virtual router this packet is reporting status for
  72. Priority uint8 // specifies the sending VRRP router's priority for the virtual router (100 = default)
  73. CountIPAddr uint8 // The number of IP addresses contained in this VRRP advertisement.
  74. AuthType VRRPv2AuthType // identifies the authentication method being utilized
  75. AdverInt uint8 // The Advertisement interval indicates the time interval (in seconds) between ADVERTISEMENTS. The default is 1 second
  76. Checksum uint16 // used to detect data corruption in the VRRP message.
  77. IPAddress []net.IP // one or more IP addresses associated with the virtual router. Specified in the CountIPAddr field.
  78. }
  79. // LayerType returns LayerTypeVRRP for VRRP v2 message.
  80. func (v *VRRPv2) LayerType() gopacket.LayerType { return LayerTypeVRRP }
  81. func (v *VRRPv2) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  82. v.BaseLayer = BaseLayer{Contents: data[:len(data)]}
  83. v.Version = data[0] >> 4 // high nibble == VRRP version. We're expecting v2
  84. v.Type = VRRPv2Type(data[0] & 0x0F) // low nibble == VRRP type. Expecting 1 (advertisement)
  85. if v.Type != 1 {
  86. // rfc3768: A packet with unknown type MUST be discarded.
  87. return errors.New("Unrecognized VRRPv2 type field.")
  88. }
  89. v.VirtualRtrID = data[1]
  90. v.Priority = data[2]
  91. v.CountIPAddr = data[3]
  92. if v.CountIPAddr < 1 {
  93. return errors.New("VRRPv2 number of IP addresses is not valid.")
  94. }
  95. v.AuthType = VRRPv2AuthType(data[4])
  96. v.AdverInt = uint8(data[5])
  97. v.Checksum = binary.BigEndian.Uint16(data[6:8])
  98. // populate the IPAddress field. The number of addresses is specified in the v.CountIPAddr field
  99. // offset references the starting byte containing the list of ip addresses
  100. offset := 8
  101. for i := uint8(0); i < v.CountIPAddr; i++ {
  102. v.IPAddress = append(v.IPAddress, data[offset:offset+4])
  103. offset += 4
  104. }
  105. // any trailing packets here may be authentication data and *should* be ignored in v2 as per RFC
  106. //
  107. // 5.3.10. Authentication Data
  108. //
  109. // The authentication string is currently only used to maintain
  110. // backwards compatibility with RFC 2338. It SHOULD be set to zero on
  111. // transmission and ignored on reception.
  112. return nil
  113. }
  114. // CanDecode specifies the layer type in which we are attempting to unwrap.
  115. func (v *VRRPv2) CanDecode() gopacket.LayerClass {
  116. return LayerTypeVRRP
  117. }
  118. // NextLayerType specifies the next layer that should be decoded. VRRP does not contain any further payload, so we set to 0
  119. func (v *VRRPv2) NextLayerType() gopacket.LayerType {
  120. return gopacket.LayerTypeZero
  121. }
  122. // The VRRP packet does not include payload data. Setting byte slice to nil
  123. func (v *VRRPv2) Payload() []byte {
  124. return nil
  125. }
  126. // decodeVRRP will parse VRRP v2
  127. func decodeVRRP(data []byte, p gopacket.PacketBuilder) error {
  128. if len(data) < 8 {
  129. return errors.New("Not a valid VRRP packet. Packet length is too small.")
  130. }
  131. v := &VRRPv2{}
  132. return decodingLayerDecoder(v, data, p)
  133. }