llc.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2012 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. "github.com/google/gopacket"
  11. )
  12. // LLC is the layer used for 802.2 Logical Link Control headers.
  13. // See http://standards.ieee.org/getieee802/download/802.2-1998.pdf
  14. type LLC struct {
  15. BaseLayer
  16. DSAP uint8
  17. IG bool // true means group, false means individual
  18. SSAP uint8
  19. CR bool // true means response, false means command
  20. Control uint16
  21. }
  22. // LayerType returns gopacket.LayerTypeLLC.
  23. func (l *LLC) LayerType() gopacket.LayerType { return LayerTypeLLC }
  24. // SNAP is used inside LLC. See
  25. // http://standards.ieee.org/getieee802/download/802-2001.pdf.
  26. // From http://en.wikipedia.org/wiki/Subnetwork_Access_Protocol:
  27. // "[T]he Subnetwork Access Protocol (SNAP) is a mechanism for multiplexing,
  28. // on networks using IEEE 802.2 LLC, more protocols than can be distinguished
  29. // by the 8-bit 802.2 Service Access Point (SAP) fields."
  30. type SNAP struct {
  31. BaseLayer
  32. OrganizationalCode []byte
  33. Type EthernetType
  34. }
  35. // LayerType returns gopacket.LayerTypeSNAP.
  36. func (s *SNAP) LayerType() gopacket.LayerType { return LayerTypeSNAP }
  37. func decodeLLC(data []byte, p gopacket.PacketBuilder) error {
  38. l := &LLC{
  39. DSAP: data[0] & 0xFE,
  40. IG: data[0]&0x1 != 0,
  41. SSAP: data[1] & 0xFE,
  42. CR: data[1]&0x1 != 0,
  43. Control: uint16(data[2]),
  44. }
  45. if l.Control&0x1 == 0 || l.Control&0x3 == 0x1 {
  46. l.Control = l.Control<<8 | uint16(data[3])
  47. l.Contents = data[:4]
  48. l.Payload = data[4:]
  49. } else {
  50. l.Contents = data[:3]
  51. l.Payload = data[3:]
  52. }
  53. p.AddLayer(l)
  54. switch {
  55. case l.DSAP == 0xAA && l.SSAP == 0xAA:
  56. return p.NextDecoder(LayerTypeSNAP)
  57. case l.DSAP == 0x42 && l.SSAP == 0x42:
  58. return p.NextDecoder(LayerTypeSTP)
  59. }
  60. return p.NextDecoder(gopacket.DecodeUnknown)
  61. }
  62. func decodeSNAP(data []byte, p gopacket.PacketBuilder) error {
  63. s := &SNAP{
  64. OrganizationalCode: data[:3],
  65. Type: EthernetType(binary.BigEndian.Uint16(data[3:5])),
  66. BaseLayer: BaseLayer{data[:5], data[5:]},
  67. }
  68. p.AddLayer(s)
  69. // BUG(gconnell): When decoding SNAP, we treat the SNAP type as an Ethernet
  70. // type. This may not actually be an ethernet type in all cases,
  71. // depending on the organizational code. Right now, we don't check.
  72. return p.NextDecoder(s.Type)
  73. }
  74. // SerializeTo writes the serialized form of this layer into the
  75. // SerializationBuffer, implementing gopacket.SerializableLayer.
  76. // See the docs for gopacket.SerializableLayer for more info.
  77. func (l *LLC) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  78. var ig_flag, cr_flag byte
  79. var length int
  80. if l.Control&0xFF00 != 0 {
  81. length = 4
  82. } else {
  83. length = 3
  84. }
  85. if l.DSAP&0x1 != 0 {
  86. return errors.New("DSAP value invalid, should not include IG flag bit")
  87. }
  88. if l.SSAP&0x1 != 0 {
  89. return errors.New("SSAP value invalid, should not include CR flag bit")
  90. }
  91. if buf, err := b.PrependBytes(length); err != nil {
  92. return err
  93. } else {
  94. ig_flag = 0
  95. if l.IG {
  96. ig_flag = 0x1
  97. }
  98. cr_flag = 0
  99. if l.CR {
  100. cr_flag = 0x1
  101. }
  102. buf[0] = l.DSAP + ig_flag
  103. buf[1] = l.SSAP + cr_flag
  104. if length == 4 {
  105. buf[2] = uint8(l.Control >> 8)
  106. buf[3] = uint8(l.Control)
  107. } else {
  108. buf[2] = uint8(l.Control)
  109. }
  110. }
  111. return nil
  112. }
  113. // SerializeTo writes the serialized form of this layer into the
  114. // SerializationBuffer, implementing gopacket.SerializableLayer.
  115. // See the docs for gopacket.SerializableLayer for more info.
  116. func (s *SNAP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  117. if buf, err := b.PrependBytes(5); err != nil {
  118. return err
  119. } else {
  120. buf[0] = s.OrganizationalCode[0]
  121. buf[1] = s.OrganizationalCode[1]
  122. buf[2] = s.OrganizationalCode[2]
  123. binary.BigEndian.PutUint16(buf[3:5], uint16(s.Type))
  124. }
  125. return nil
  126. }