mpls.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. // MPLS is the MPLS packet header.
  13. type MPLS struct {
  14. BaseLayer
  15. Label uint32
  16. TrafficClass uint8
  17. StackBottom bool
  18. TTL uint8
  19. }
  20. // LayerType returns gopacket.LayerTypeMPLS.
  21. func (m *MPLS) LayerType() gopacket.LayerType { return LayerTypeMPLS }
  22. // ProtocolGuessingDecoder attempts to guess the protocol of the bytes it's
  23. // given, then decode the packet accordingly. Its algorithm for guessing is:
  24. // If the packet starts with byte 0x45-0x4F: IPv4
  25. // If the packet starts with byte 0x60-0x6F: IPv6
  26. // Otherwise: Error
  27. // See draft-hsmit-isis-aal5mux-00.txt for more detail on this approach.
  28. type ProtocolGuessingDecoder struct{}
  29. func (ProtocolGuessingDecoder) Decode(data []byte, p gopacket.PacketBuilder) error {
  30. switch data[0] {
  31. // 0x40 | header_len, where header_len is at least 5.
  32. case 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f:
  33. return decodeIPv4(data, p)
  34. // IPv6 can start with any byte whose first 4 bits are 0x6.
  35. case 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f:
  36. return decodeIPv6(data, p)
  37. }
  38. return errors.New("Unable to guess protocol of packet data")
  39. }
  40. // MPLSPayloadDecoder is the decoder used to data encapsulated by each MPLS
  41. // layer. MPLS contains no type information, so we have to explicitly decide
  42. // which decoder to use. This is initially set to ProtocolGuessingDecoder, our
  43. // simple attempt at guessing protocols based on the first few bytes of data
  44. // available to us. However, if you know that in your environment MPLS always
  45. // encapsulates a specific protocol, you may reset this.
  46. var MPLSPayloadDecoder gopacket.Decoder = ProtocolGuessingDecoder{}
  47. func decodeMPLS(data []byte, p gopacket.PacketBuilder) error {
  48. decoded := binary.BigEndian.Uint32(data[:4])
  49. mpls := &MPLS{
  50. Label: decoded >> 12,
  51. TrafficClass: uint8(decoded>>9) & 0x7,
  52. StackBottom: decoded&0x100 != 0,
  53. TTL: uint8(decoded),
  54. BaseLayer: BaseLayer{data[:4], data[4:]},
  55. }
  56. p.AddLayer(mpls)
  57. if mpls.StackBottom {
  58. return p.NextDecoder(MPLSPayloadDecoder)
  59. }
  60. return p.NextDecoder(gopacket.DecodeFunc(decodeMPLS))
  61. }
  62. // SerializeTo writes the serialized form of this layer into the
  63. // SerializationBuffer, implementing gopacket.SerializableLayer.
  64. // See the docs for gopacket.SerializableLayer for more info.
  65. func (m *MPLS) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
  66. bytes, err := b.PrependBytes(4)
  67. if err != nil {
  68. return err
  69. }
  70. encoded := m.Label << 12
  71. encoded |= uint32(m.TrafficClass) << 9
  72. encoded |= uint32(m.TTL)
  73. if m.StackBottom {
  74. encoded |= 0x100
  75. }
  76. binary.BigEndian.PutUint32(bytes, encoded)
  77. return nil
  78. }