1 // Copyright 2012 Google, Inc. All rights reserved.
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
13 "github.com/google/gopacket"
16 // LLC is the layer used for 802.2 Logical Link Control headers.
17 // See http://standards.ieee.org/getieee802/download/802.2-1998.pdf
21 IG bool // true means group, false means individual
23 CR bool // true means response, false means command
27 // LayerType returns gopacket.LayerTypeLLC.
28 func (l *LLC) LayerType() gopacket.LayerType { return LayerTypeLLC }
30 // SNAP is used inside LLC. See
31 // http://standards.ieee.org/getieee802/download/802-2001.pdf.
32 // From http://en.wikipedia.org/wiki/Subnetwork_Access_Protocol:
33 // "[T]he Subnetwork Access Protocol (SNAP) is a mechanism for multiplexing,
34 // on networks using IEEE 802.2 LLC, more protocols than can be distinguished
35 // by the 8-bit 802.2 Service Access Point (SAP) fields."
38 OrganizationalCode []byte
42 // LayerType returns gopacket.LayerTypeSNAP.
43 func (s *SNAP) LayerType() gopacket.LayerType { return LayerTypeSNAP }
45 func decodeLLC(data []byte, p gopacket.PacketBuilder) error {
51 Control: uint16(data[2]),
53 if l.Control&0x1 == 0 || l.Control&0x3 == 0x1 {
54 l.Control = l.Control<<8 | uint16(data[3])
63 case l.DSAP == 0xAA && l.SSAP == 0xAA:
64 return p.NextDecoder(LayerTypeSNAP)
65 case l.DSAP == 0x42 && l.SSAP == 0x42:
66 return p.NextDecoder(LayerTypeSTP)
68 return p.NextDecoder(gopacket.DecodeUnknown)
71 func decodeSNAP(data []byte, p gopacket.PacketBuilder) error {
73 OrganizationalCode: data[:3],
74 Type: EthernetType(binary.BigEndian.Uint16(data[3:5])),
75 BaseLayer: BaseLayer{data[:5], data[5:]},
78 // BUG(gconnell): When decoding SNAP, we treat the SNAP type as an Ethernet
79 // type. This may not actually be an ethernet type in all cases,
80 // depending on the organizational code. Right now, we don't check.
81 return p.NextDecoder(s.Type)
84 // SerializeTo writes the serialized form of this layer into the
85 // SerializationBuffer, implementing gopacket.SerializableLayer.
86 // See the docs for gopacket.SerializableLayer for more info.
87 func (l *LLC) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
88 var ig_flag, cr_flag byte
91 if l.Control&0xFF00 != 0 {
98 return errors.New("DSAP value invalid, should not include IG flag bit")
102 return errors.New("SSAP value invalid, should not include CR flag bit")
105 if buf, err := b.PrependBytes(length); err != nil {
118 buf[0] = l.DSAP + ig_flag
119 buf[1] = l.SSAP + cr_flag
122 buf[2] = uint8(l.Control >> 8)
123 buf[3] = uint8(l.Control)
125 buf[2] = uint8(l.Control)
132 // SerializeTo writes the serialized form of this layer into the
133 // SerializationBuffer, implementing gopacket.SerializableLayer.
134 // See the docs for gopacket.SerializableLayer for more info.
135 func (s *SNAP) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
136 if buf, err := b.PrependBytes(5); err != nil {
139 buf[0] = s.OrganizationalCode[0]
140 buf[1] = s.OrganizationalCode[1]
141 buf[2] = s.OrganizationalCode[2]
142 binary.BigEndian.PutUint16(buf[3:5], uint16(s.Type))