-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathextn.go
631 lines (547 loc) · 16.2 KB
/
extn.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
// Copyright 2020 Anapaya Systems
//
// 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 slayers
import (
"encoding/binary"
"fmt"
"github.com/google/gopacket"
"github.com/scionproto/scion/pkg/private/serrors"
)
var (
ErrOptionNotFound = serrors.New("Option not found")
)
// OptionType indicates the type of a TLV Option that is part of an extension header.
type OptionType uint8
// Definition of option type constants.
const (
OptTypePad1 OptionType = iota
OptTypePadN
OptTypeAuthenticator
)
type tlvOption struct {
OptType OptionType
OptDataLen uint8
ActualLength int
OptData []byte
OptAlign [2]uint8 // Xn+Y = [2]uint8{X, Y}
}
func (o *tlvOption) length(fixLengths bool) int {
if o.OptType == OptTypePad1 {
return 1
}
if fixLengths {
return len(o.OptData) + 2
}
return int(o.OptDataLen) + 2
}
func (o *tlvOption) serializeTo(data []byte, fixLengths bool) {
dryrun := data == nil
if o.OptType == OptTypePad1 {
if !dryrun {
data[0] = 0x0
}
return
}
if fixLengths {
o.OptDataLen = uint8(len(o.OptData))
}
if !dryrun {
data[0] = uint8(o.OptType)
data[1] = o.OptDataLen
copy(data[2:], o.OptData)
}
}
func decodeTLVOption(data []byte) (*tlvOption, error) {
o := &tlvOption{OptType: OptionType(data[0])}
if OptionType(data[0]) == OptTypePad1 {
o.ActualLength = 1
return o, nil
}
if len(data) < 2 {
return nil, serrors.New("buffer too short", "expected", 2, "actual", len(data))
}
o.OptDataLen = data[1]
o.ActualLength = int(o.OptDataLen) + 2
if len(data) < o.ActualLength {
return nil, serrors.New("buffer too short", "expected", o.ActualLength, "actual", len(data))
}
o.OptData = data[2:o.ActualLength]
return o, nil
}
// serializeTLVOptionPadding adds an appropriate PadN extension.
func serializeTLVOptionPadding(data []byte, padLength int) {
if padLength <= 0 {
return
}
if padLength == 1 {
data[0] = 0x0
return
}
dataLen := uint8(padLength) - 2
padN := tlvOption{
OptType: OptTypePadN,
OptDataLen: dataLen,
OptData: make([]byte, dataLen),
}
padN.serializeTo(data, false)
}
// serializeTLVOptions serializes options to buf and returns the length of the serialized options.
// Passing in a nil-buffer will treat the serialization as a dryrun that can be used to calculate
// the length needed for the buffer.
func serializeTLVOptions(buf []byte, options []*tlvOption, fixLengths bool) int {
dryrun := buf == nil
// length start at 2 since the padding needs to be calculated taking the first 2 bytes of the
// extension header (NextHdr and ExtLen fields) into account.
length := 2
for _, opt := range options {
if fixLengths {
x := int(opt.OptAlign[0])
y := int(opt.OptAlign[1])
if x != 0 {
n := length / x
offset := x*n + y
if offset < length {
offset += x
}
if length != offset {
pad := offset - length
if !dryrun {
serializeTLVOptionPadding(buf[length-2:], pad)
}
length += pad
}
}
}
if !dryrun {
opt.serializeTo(buf[length-2:], fixLengths)
}
length += opt.length(fixLengths)
}
if fixLengths {
p := length % LineLen
if p != 0 {
pad := LineLen - p
if !dryrun {
serializeTLVOptionPadding(buf[length-2:], pad)
}
length += pad
}
}
return length - 2
}
type extnBase struct {
BaseLayer
NextHdr L4ProtocolType
// ExtLen is the length of the extension header in multiple of 4-bytes NOT including the
// first 4 bytes.
ExtLen uint8
ActualLen int
}
func (e *extnBase) serializeToWithTLVOptions(b gopacket.SerializeBuffer,
opts gopacket.SerializeOptions, tlvOptions []*tlvOption) error {
l := serializeTLVOptions(nil, tlvOptions, opts.FixLengths)
bytes, err := b.PrependBytes(l)
if err != nil {
return err
}
serializeTLVOptions(bytes, tlvOptions, opts.FixLengths)
length := len(bytes) + 2
if length%LineLen != 0 {
return serrors.New("SCION extension actual length must be multiple of 4")
}
bytes, err = b.PrependBytes(2)
if err != nil {
return err
}
bytes[0] = uint8(e.NextHdr)
if opts.FixLengths {
e.ExtLen = uint8((length / LineLen) - 1)
}
bytes[1] = e.ExtLen
return nil
}
func decodeExtnBase(data []byte, df gopacket.DecodeFeedback) (extnBase, error) {
e := extnBase{}
if len(data) < 2 {
df.SetTruncated()
return e, serrors.New(fmt.Sprintf("invalid extension header. Length %d less than 2",
len(data)))
}
e.NextHdr = L4ProtocolType(data[0])
e.ExtLen = data[1]
e.ActualLen = (int(e.ExtLen) + 1) * LineLen
if len(data) < e.ActualLen {
return extnBase{}, serrors.New(fmt.Sprintf("invalid extension header. "+
"Length %d less than specified length %d", len(data), e.ActualLen))
}
e.Contents = data[:e.ActualLen]
e.Payload = data[e.ActualLen:]
return e, nil
}
// HopByHopOption is a TLV option present in a SCION hop-by-hop extension.
type HopByHopOption tlvOption
// HopByHopExtn is the SCION hop-by-hop options extension.
type HopByHopExtn struct {
extnBase
Options []*HopByHopOption
}
func (h *HopByHopExtn) LayerType() gopacket.LayerType {
return LayerTypeHopByHopExtn
}
func (h *HopByHopExtn) CanDecode() gopacket.LayerClass {
return LayerClassHopByHopExtn
}
func (h *HopByHopExtn) NextLayerType() gopacket.LayerType {
return scionNextLayerTypeAfterHBH(h.NextHdr)
}
func (h *HopByHopExtn) LayerPayload() []byte {
return h.Payload
}
// SerializeTo implementation according to gopacket.SerializableLayer.
func (h *HopByHopExtn) SerializeTo(b gopacket.SerializeBuffer,
opts gopacket.SerializeOptions) error {
if err := checkHopByHopExtnNextHdr(h.NextHdr); err != nil {
return err
}
o := make([]*tlvOption, 0, len(h.Options))
for _, v := range h.Options {
o = append(o, (*tlvOption)(v))
}
return h.extnBase.serializeToWithTLVOptions(b, opts, o)
}
// DecodeFromBytes implementation according to gopacket.DecodingLayer.
func (h *HopByHopExtn) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
var err error
h.extnBase, err = decodeExtnBase(data, df)
if err != nil {
return err
}
if err := checkHopByHopExtnNextHdr(h.NextHdr); err != nil {
return err
}
offset := 2
for offset < h.ActualLen {
opt, err := decodeTLVOption(data[offset:h.ActualLen])
if err != nil {
return err
}
h.Options = append(h.Options, (*HopByHopOption)(opt))
offset += opt.ActualLength
}
return nil
}
func decodeHopByHopExtn(data []byte, p gopacket.PacketBuilder) error {
h := &HopByHopExtn{}
err := h.DecodeFromBytes(data, p)
p.AddLayer(h)
if err != nil {
return err
}
return p.NextDecoder(scionNextLayerTypeAfterHBH(h.NextHdr))
}
func checkHopByHopExtnNextHdr(t L4ProtocolType) error {
if t == HopByHopClass {
return serrors.New("hbh extension must not be repeated")
}
return nil
}
// EndToEndOption is a TLV option present in a SCION end-to-end extension.
type EndToEndOption tlvOption
// EndToEndExtn is the SCION end-to-end options extension.
type EndToEndExtn struct {
extnBase
Options []*EndToEndOption
}
func (e *EndToEndExtn) LayerType() gopacket.LayerType {
return LayerTypeEndToEndExtn
}
func (e *EndToEndExtn) CanDecode() gopacket.LayerClass {
return LayerClassEndToEndExtn
}
func (e *EndToEndExtn) NextLayerType() gopacket.LayerType {
return scionNextLayerTypeAfterE2E(e.NextHdr)
}
func (e *EndToEndExtn) LayerPayload() []byte {
return e.Payload
}
// DecodeFromBytes implementation according to gopacket.DecodingLayer.
func (e *EndToEndExtn) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
var err error
e.extnBase, err = decodeExtnBase(data, df)
if err != nil {
return err
}
if err := checkEndToEndExtnNextHdr(e.NextHdr); err != nil {
return err
}
offset := 2
for offset < e.ActualLen {
opt, err := decodeTLVOption(data[offset:e.ActualLen])
if err != nil {
return err
}
e.Options = append(e.Options, (*EndToEndOption)(opt))
offset += opt.ActualLength
}
return nil
}
func decodeEndToEndExtn(data []byte, p gopacket.PacketBuilder) error {
e := &EndToEndExtn{}
err := e.DecodeFromBytes(data, p)
p.AddLayer(e)
if err != nil {
return err
}
return p.NextDecoder(scionNextLayerTypeAfterE2E(e.NextHdr))
}
func checkEndToEndExtnNextHdr(t L4ProtocolType) error {
if t == HopByHopClass {
return serrors.New("e2e extension must not come before the HBH extension")
} else if t == End2EndClass {
return serrors.New("e2e extension must not be repeated")
}
return nil
}
// SerializeTo implementation according to gopacket.SerializableLayer
func (e *EndToEndExtn) SerializeTo(b gopacket.SerializeBuffer,
opts gopacket.SerializeOptions) error {
if err := checkEndToEndExtnNextHdr(e.NextHdr); err != nil {
return err
}
o := make([]*tlvOption, 0, len(e.Options))
for _, v := range e.Options {
o = append(o, (*tlvOption)(v))
}
return e.extnBase.serializeToWithTLVOptions(b, opts, o)
}
// FindOption returns the first option entry of the given type if any exists,
// or ErrOptionNotFound otherwise.
func (e *EndToEndExtn) FindOption(typ OptionType) (*EndToEndOption, error) {
for _, o := range e.Options {
if o.OptType == typ {
return o, nil
}
}
return nil, ErrOptionNotFound
}
// HopByHopExtnSkipper is a DecodingLayer which decodes a HopByHop extension
// without parsing its content.
// This can be used with a DecodingLayerParser to handle SCION packets which
// may or may not have a HopByHop extension.
type HopByHopExtnSkipper struct {
extnBase
}
// DecodeFromBytes implementation according to gopacket.DecodingLayer
func (s *HopByHopExtnSkipper) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
var err error
s.extnBase, err = decodeExtnBase(data, df)
if err != nil {
return err
}
if err := checkHopByHopExtnNextHdr(s.NextHdr); err != nil {
return err
}
return nil
}
func (e *HopByHopExtnSkipper) LayerType() gopacket.LayerType {
return LayerTypeHopByHopExtn
}
func (s *HopByHopExtnSkipper) CanDecode() gopacket.LayerClass {
return LayerClassHopByHopExtn
}
func (h *HopByHopExtnSkipper) NextLayerType() gopacket.LayerType {
return scionNextLayerTypeAfterHBH(h.NextHdr)
}
// EndToEndExtnSkipper is a DecodingLayer which decodes a HopByHop extensions
// without parsing its content.
// This can be used with a DecodingLayerParser to handle SCION packets which
// may or may not have an EndToEnd extension.
type EndToEndExtnSkipper struct {
extnBase
}
// DecodeFromBytes implementation according to gopacket.DecodingLayer
func (s *EndToEndExtnSkipper) DecodeFromBytes(data []byte, df gopacket.DecodeFeedback) error {
var err error
s.extnBase, err = decodeExtnBase(data, df)
if err != nil {
return err
}
if err := checkEndToEndExtnNextHdr(s.NextHdr); err != nil {
return err
}
return nil
}
func (e *EndToEndExtnSkipper) LayerType() gopacket.LayerType {
return LayerTypeEndToEndExtn
}
func (s *EndToEndExtnSkipper) CanDecode() gopacket.LayerClass {
return LayerClassEndToEndExtn
}
func (e *EndToEndExtnSkipper) NextLayerType() gopacket.LayerType {
return scionNextLayerTypeAfterE2E(e.NextHdr)
}
// PacketAuthSPI is the identifier for the key used for the
// packet authentication option. DRKey values are in the
// range [1, 2^21-1].
type PacketAuthSPI uint32
type DRKeyType uint8
const (
ASHost DRKeyType = iota
HostHost
)
type DRKeyDirection uint8
const (
SenderSide DRKeyDirection = iota
ReceiverSide
)
type DRKeyEpochType uint8
const (
Later DRKeyEpochType = iota
Earlier
)
func (p PacketAuthSPI) Type() DRKeyType {
if p^(1<<13) == 0 {
return ASHost
}
return HostHost
}
func (p PacketAuthSPI) Direction() DRKeyDirection {
if p^(1<<14) == 0 {
return SenderSide
}
return ReceiverSide
}
func (p PacketAuthSPI) Epoch() DRKeyEpochType {
if p^(1<<14) == 0 {
return Later
}
return Earlier
}
func (p PacketAuthSPI) DRKeyProto() uint16 {
return uint16(p >> 16)
}
func (p PacketAuthSPI) IsDRKey() bool {
return p > 0 && p < (1<<21)
}
func MakePacketAuthSPIDrkey(proto uint16, drkeyType DRKeyType,
dir DRKeyDirection, epoch DRKeyEpochType) (PacketAuthSPI, error) {
if proto < 1 {
return 0, serrors.New("Invalid proto identifier value")
}
if drkeyType > 1 {
return 0, serrors.New("Invalid DRKeyType value")
}
if dir > 1 {
return 0, serrors.New("Invalid DRKeyDirection value")
}
if epoch > 1 {
return 0, serrors.New("Invalid DRKeyEpochType value")
}
spi := uint32((drkeyType & 0x1)) << 13
spi |= uint32((dir & 0x1)) << 14
spi |= uint32((epoch & 0x1)) << 15
spi |= uint32(proto) << 16
return PacketAuthSPI(spi), nil
}
// PacketAuthAlg is the enumerator for authenticator algorithm types in the
// packet authenticator option.
type PacketAuthAlg uint8
const (
PacketAuthCMAC PacketAuthAlg = iota
PacketAuthSHA1_AES_CBC
)
// PacketAuthenticatorOption wraps an EndToEndOption of OptTypeAuthenticator.
// This can be used to serialize and parse the internal structure of the packet authenticator
// option.
type PacketAuthenticatorOption struct {
*EndToEndOption
}
// NewPacketAuthenticatorOption creates a new EndToEndOption of
// OptTypeAuthenticator, initialized with the given SPAO data.
func NewPacketAuthenticatorOption(spi PacketAuthSPI, alg PacketAuthAlg, ts uint32,
sn uint32, auth []byte) PacketAuthenticatorOption {
o := PacketAuthenticatorOption{EndToEndOption: new(EndToEndOption)}
o.Reset(spi, alg, ts, sn, auth)
return o
}
// ParsePacketAuthenticatorOption parses o as a packet authenticator option.
// Performs minimal checks to ensure that SPI, algorithm, timestamp, RSV, and
// sequence number are set.
// Checking the size and content of the Authenticator data must be done by the
// caller.
func ParsePacketAuthenticatorOption(o *EndToEndOption) (PacketAuthenticatorOption, error) {
if o.OptType != OptTypeAuthenticator {
return PacketAuthenticatorOption{},
serrors.New("wrong option type", "expected", OptTypeAuthenticator, "actual", o.OptType)
}
if len(o.OptData) < 12 {
return PacketAuthenticatorOption{},
serrors.New("buffer too short", "expected", 12, "actual", len(o.OptData))
}
return PacketAuthenticatorOption{o}, nil
}
// Reset reinitializes the underlying EndToEndOption with the SPAO data.
// Reuses the OptData buffer if it is of sufficient capacity.
func (o PacketAuthenticatorOption) Reset(spi PacketAuthSPI, alg PacketAuthAlg,
ts uint32, sn uint32, auth []byte) {
if ts >= (1 << 24) {
panic("Timestamp value should be smaller than 2^24")
}
if sn >= (1 << 24) {
panic("Sequence number should be smaller than 2^24")
}
o.OptType = OptTypeAuthenticator
n := 12 + len(auth) // 4 + 1 + 3 + 1 + 3 + len(data)
if n <= cap(o.OptData) {
o.OptData = o.OptData[:n]
} else {
o.OptData = make([]byte, n)
}
binary.BigEndian.PutUint32(o.OptData[:4], uint32(spi))
o.OptData[4] = byte(alg)
o.OptData[5] = byte(ts >> 16)
o.OptData[6] = byte(ts >> 8)
o.OptData[7] = byte(ts)
o.OptData[8] = byte(0)
o.OptData[9] = byte(sn >> 16)
o.OptData[10] = byte(sn >> 8)
o.OptData[11] = byte(sn)
copy(o.OptData[12:], auth)
o.OptAlign = [2]uint8{4, 2}
// reset unused/implicit fields
o.OptDataLen = 0
o.ActualLength = 0
}
// SPI returns returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) SPI() PacketAuthSPI {
return PacketAuthSPI(binary.BigEndian.Uint32(o.OptData[:4]))
}
// Algorithm returns the algorithm type stored in the data buffer.
func (o PacketAuthenticatorOption) Algorithm() PacketAuthAlg {
return PacketAuthAlg(o.OptData[4])
}
// Timestamp returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) Timestamp() uint32 {
return uint32(o.OptData[5])<<16 + uint32(o.OptData[6])<<8 + uint32(o.OptData[7])
}
// SequenceNumber returns the value set in the homonym field in the extension.
func (o PacketAuthenticatorOption) SequenceNumber() uint32 {
return uint32(o.OptData[9])<<16 + uint32(o.OptData[10])<<8 + uint32(o.OptData[11])
}
// Authenticator returns slice of the underlying auth buffer.
// Changes to this slice will be reflected on the wire when
// the extension is serialized.
func (o PacketAuthenticatorOption) Authenticator() []byte {
return o.OptData[12:]
}