forked from scionproto/scion
/
extensions_layer.go
104 lines (92 loc) · 3.15 KB
/
extensions_layer.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright 2019 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package layers
import (
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/util"
)
var (
LayerTypeHopByHopExtension = gopacket.RegisterLayerType(1101,
gopacket.LayerTypeMetadata{Name: "HopByHopExtension", Decoder: nil})
LayerTypeEndToEndExtension = gopacket.RegisterLayerType(1102,
gopacket.LayerTypeMetadata{Name: "EndToEndExtension", Decoder: nil})
LayerTypeSCIONUDP = gopacket.RegisterLayerType(1103,
gopacket.LayerTypeMetadata{Name: "SCIONUDP", Decoder: nil})
LayerTypeSCMP = gopacket.RegisterLayerType(1104,
gopacket.LayerTypeMetadata{Name: "SCMP", Decoder: nil})
)
var (
LayerToHeaderMap = map[gopacket.LayerType]common.L4ProtocolType{
LayerTypeHopByHopExtension: common.HopByHopClass,
LayerTypeEndToEndExtension: common.End2EndClass,
LayerTypeSCIONUDP: common.L4UDP,
LayerTypeSCMP: common.L4SCMP,
}
)
var (
zeroes = make([]byte, common.MaxMTU)
)
type Extension struct {
layers.BaseLayer
NextHeader common.L4ProtocolType
NumLines uint8
Type uint8
Data []byte
}
func (e *Extension) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
if len(data) < common.ExtnSubHdrLen {
df.SetTruncated()
return common.NewBasicError("Invalid SCION Extension header, small raw length",
nil, "actual", len(data), "wanted", common.ExtnSubHdrLen)
}
expectedLength := int(data[1]) * common.LineLen
if expectedLength <= 0 {
df.SetTruncated()
return serrors.New("Invalid SCION Extension header, length is zero")
}
if len(data) < expectedLength {
df.SetTruncated()
return common.NewBasicError("Invalid SCION Extension body, actual length too short", nil,
"actual", len(data), "wanted", expectedLength)
}
e.NextHeader = common.L4ProtocolType(data[0])
e.NumLines = data[1]
e.Type = data[2]
e.Data = data[3:expectedLength]
e.BaseLayer.Contents = data[:expectedLength]
e.BaseLayer.Payload = data[expectedLength:]
return nil
}
func (e *Extension) SerializeTo(b gopacket.SerializeBuffer, opts gopacket.SerializeOptions) error {
totalLength := common.ExtnSubHdrLen + len(e.Data)
paddingSize := 0
if opts.FixLengths {
paddingSize = util.CalcPadding(totalLength, common.LineLen)
totalLength += paddingSize
e.NumLines = uint8(totalLength / common.LineLen)
}
bytes, err := b.PrependBytes(totalLength)
if err != nil {
return err
}
bytes[0] = uint8(e.NextHeader)
bytes[1] = e.NumLines
bytes[2] = e.Type
copy(bytes[3:], e.Data)
copy(bytes[3+len(e.Data):], zeroes[:paddingSize])
return nil
}