github.com/gopacket/gopacket@v1.1.0/layers/rmcp.go (about)

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