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