rmcp.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2019 The GoPacket Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be found
  4. // in the LICENSE file in the root of the source tree.
  5. package layers
  6. // This file implements the ASF-RMCP header specified in section 3.2.2.2 of
  7. // https://www.dmtf.org/sites/default/files/standards/documents/DSP0136.pdf
  8. import (
  9. "fmt"
  10. "github.com/google/gopacket"
  11. )
  12. // RMCPClass is the class of a RMCP layer's payload, e.g. ASF or IPMI. This is a
  13. // 4-bit unsigned int on the wire; all but 6 (ASF), 7 (IPMI) and 8 (OEM-defined)
  14. // are currently reserved.
  15. type RMCPClass uint8
  16. // LayerType returns the payload layer type corresponding to a RMCP class.
  17. func (c RMCPClass) LayerType() gopacket.LayerType {
  18. if lt := rmcpClassLayerTypes[uint8(c)]; lt != 0 {
  19. return lt
  20. }
  21. return gopacket.LayerTypePayload
  22. }
  23. func (c RMCPClass) String() string {
  24. return fmt.Sprintf("%v(%v)", uint8(c), c.LayerType())
  25. }
  26. const (
  27. // RMCPVersion1 identifies RMCP v1.0 in the Version header field. Lower
  28. // values are considered legacy, while higher values are reserved by the
  29. // specification.
  30. RMCPVersion1 uint8 = 0x06
  31. // RMCPNormal indicates a "normal" message, i.e. not an acknowledgement.
  32. RMCPNormal uint8 = 0
  33. // RMCPAck indicates a message is acknowledging a received normal message.
  34. RMCPAck uint8 = 1 << 7
  35. // RMCPClassASF identifies an RMCP message as containing an ASF-RMCP
  36. // payload.
  37. RMCPClassASF RMCPClass = 0x06
  38. // RMCPClassIPMI identifies an RMCP message as containing an IPMI payload.
  39. RMCPClassIPMI RMCPClass = 0x07
  40. // RMCPClassOEM identifies an RMCP message as containing an OEM-defined
  41. // payload.
  42. RMCPClassOEM RMCPClass = 0x08
  43. )
  44. var (
  45. rmcpClassLayerTypes = [16]gopacket.LayerType{
  46. RMCPClassASF: LayerTypeASF,
  47. // RMCPClassIPMI is to implement; RMCPClassOEM is deliberately not
  48. // implemented, so we return LayerTypePayload
  49. }
  50. )
  51. // RegisterRMCPLayerType allows specifying that the payload of a RMCP packet of
  52. // a certain class should processed by the provided layer type. This overrides
  53. // any existing registrations, including defaults.
  54. func RegisterRMCPLayerType(c RMCPClass, l gopacket.LayerType) {
  55. rmcpClassLayerTypes[c] = l
  56. }
  57. // RMCP describes the format of an RMCP header, which forms a UDP payload. See
  58. // section 3.2.2.2.
  59. type RMCP struct {
  60. BaseLayer
  61. // Version identifies the version of the RMCP header. 0x06 indicates RMCP
  62. // v1.0; lower values are legacy, higher values are reserved.
  63. Version uint8
  64. // Sequence is the sequence number assicated with the message. Note that
  65. // this rolls over to 0 after 254, not 255. Seq num 255 indicates the
  66. // receiver must not send an ACK.
  67. Sequence uint8
  68. // Ack indicates whether this packet is an acknowledgement. If it is, the
  69. // payload will be empty.
  70. Ack bool
  71. // Class idicates the structure of the payload. There are only 2^4 valid
  72. // values, however there is no uint4 data type. N.B. the Ack bit has been
  73. // split off into another field. The most significant 4 bits of this field
  74. // will always be 0.
  75. Class RMCPClass
  76. }
  77. // LayerType returns LayerTypeRMCP. It partially satisfies Layer and
  78. // SerializableLayer.
  79. func (*RMCP) LayerType() gopacket.LayerType {
  80. return LayerTypeRMCP
  81. }
  82. // CanDecode returns LayerTypeRMCP. It partially satisfies DecodingLayer.
  83. func (r *RMCP) CanDecode() gopacket.LayerClass {
  84. return r.LayerType()
  85. }
  86. // DecodeFromBytes makes the layer represent the provided bytes. It partially
  87. // satisfies DecodingLayer.
  88. func (r *RMCP) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
  89. if len(data) < 4 {
  90. df.SetTruncated()
  91. return fmt.Errorf("invalid RMCP header, length %v less than 4",
  92. len(data))
  93. }
  94. r.BaseLayer.Contents = data[:4]
  95. r.BaseLayer.Payload = data[4:]
  96. r.Version = uint8(data[0])
  97. // 1 byte reserved
  98. r.Sequence = uint8(data[2])
  99. r.Ack = data[3]&RMCPAck != 0
  100. r.Class = RMCPClass(data[3] & 0xF)
  101. return nil
  102. }
  103. // NextLayerType returns the data layer of this RMCP layer. This partially
  104. // satisfies DecodingLayer.
  105. func (r *RMCP) NextLayerType() gopacket.LayerType {
  106. return r.Class.LayerType()
  107. }
  108. // Payload returns the data layer. It partially satisfies ApplicationLayer.
  109. func (r *RMCP) Payload() []byte {
  110. return r.BaseLayer.Payload
  111. }
  112. // SerializeTo writes the serialized fom of this layer into the SerializeBuffer,
  113. // partially satisfying SerializableLayer.
  114. func (r *RMCP) SerializeTo(b gopacket.SerializeBuffer, _ gopacket.SerializeOptions) error {
  115. // The IPMI v1.5 spec contains a pad byte for frame sizes of certain lengths
  116. // to work around issues in LAN chips. This is no longer necessary as of
  117. // IPMI v2.0 (renamed to "legacy pad") so we do not attempt to add it. The
  118. // same approach is taken by FreeIPMI:
  119. // http://git.savannah.gnu.org/cgit/freeipmi.git/tree/libfreeipmi/interface/ipmi-lan-interface.c?id=b5ffcd38317daf42074458879f4c55ba6804a595#n836
  120. bytes, err := b.PrependBytes(4)
  121. if err != nil {
  122. return err
  123. }
  124. bytes[0] = r.Version
  125. bytes[1] = 0x00
  126. bytes[2] = r.Sequence
  127. bytes[3] = bool2uint8(r.Ack)<<7 | uint8(r.Class) // thanks, BFD layer
  128. return nil
  129. }
  130. // decodeRMCP decodes the byte slice into an RMCP type, and sets the application
  131. // layer to it.
  132. func decodeRMCP(data []byte, p gopacket.PacketBuilder) error {
  133. rmcp := &RMCP{}
  134. err := rmcp.DecodeFromBytes(data, p)
  135. p.AddLayer(rmcp)
  136. p.SetApplicationLayer(rmcp)
  137. if err != nil {
  138. return err
  139. }
  140. return p.NextDecoder(rmcp.NextLayerType())
  141. }