-
Notifications
You must be signed in to change notification settings - Fork 160
/
path.go
488 lines (469 loc) · 15.1 KB
/
path.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
// Copyright 2016 ETH Zurich
// Copyright 2018 ETH Zurich, 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.
// This file handles the path header (parsing/validating/updating/etc).
package rpkt
import (
"hash"
"time"
"github.com/scionproto/scion/go/border/ifstate"
"github.com/scionproto/scion/go/border/rcmn"
"github.com/scionproto/scion/go/lib/assert"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/scmp"
"github.com/scionproto/scion/go/lib/spath"
"github.com/scionproto/scion/go/proto"
)
// validatePath validates the path header.
func (rp *RtrPkt) validatePath(dirFrom rcmn.Dir) error {
// First check if there is a path
if rp.infoF == nil || rp.hopF == nil {
return common.NewBasicError("Path required",
scmp.NewError(scmp.C_Path, scmp.T_P_PathRequired, nil, nil))
}
// There is a path, so ifCurr will be set
if err := rp.validateLocalIF(rp.ifCurr); err != nil {
return err
}
// Check for shorcuts in packets from core links
if rp.infoF.Shortcut {
currentLinkType := rp.Ctx.Conf.Net.IFs[*rp.ifCurr].Type
if currentLinkType == proto.LinkType_core {
return common.NewBasicError("Shortcut not allowed on core segment", nil)
}
}
// A verify-only Hop Field cannot be used for routing.
if rp.hopF.VerifyOnly {
return common.NewBasicError("Hop field is VERIFY_ONLY",
scmp.NewError(scmp.C_Path, scmp.T_P_NonRoutingHopF, rp.mkInfoPathOffsets(), nil))
}
// Check if Hop Field has expired.
hopfExpiry := rp.infoF.Timestamp().Add(rp.hopF.ExpTime.ToDuration())
if time.Now().After(hopfExpiry) {
return common.NewBasicError(
"Hop field expired",
scmp.NewError(scmp.C_Path, scmp.T_P_ExpiredHopF, rp.mkInfoPathOffsets(), nil),
"expiry", hopfExpiry,
)
}
// Verify the Hop Field MAC.
hfmac := rp.Ctx.Conf.HFMacPool.Get().(hash.Hash)
err := rp.hopF.Verify(hfmac, rp.infoF.TsInt, rp.getHopFVer(dirFrom))
rp.Ctx.Conf.HFMacPool.Put(hfmac)
if err != nil && common.GetErrorMsg(err) == spath.ErrorHopFBadMac {
err = scmp.NewError(scmp.C_Path, scmp.T_P_BadMac, rp.mkInfoPathOffsets(), err)
}
return err
}
// validateLocalIF makes sure a given interface ID exists in the local AS, and
// that it isn't revoked. Note that revocations are ignored if the packet's
// destination is this router.
func (rp *RtrPkt) validateLocalIF(ifid *common.IFIDType) error {
if ifid == nil {
return common.NewBasicError("validateLocalIF: Interface is nil", nil)
}
if _, ok := rp.Ctx.Conf.Topo.IFInfoMap[*ifid]; !ok {
// No such interface.
return common.NewBasicError(
"Unknown IF",
scmp.NewError(scmp.C_Path, scmp.T_P_BadIF, rp.mkInfoPathOffsets(), nil),
"ifid", *ifid,
)
}
for _, e := range rp.HBHExt {
if e.Type() == common.ExtnOneHopPathType {
// Ignore revocations if OneHopExtension is present
return nil
}
}
state, ok := ifstate.LoadState(*ifid)
if !ok || state.Active {
// Interface is not revoked
return nil
}
// Interface is revoked.
sRevInfo := state.SRevInfo
if sRevInfo == nil {
rp.Warn("No SRevInfo for revoked interface", "ifid", *ifid)
return nil
}
revInfo, err := sRevInfo.RevInfo()
if err != nil {
rp.Warn("Could not parse RevInfo for interface", "ifid", *ifid, "err", err)
return nil
}
err = revInfo.Active()
if err != nil {
if !common.IsTimeoutErr(err) {
rp.Error("Error checking revocation", "err", err)
return nil
}
// If the BR does not have a revocation for the current epoch, it considers
// the interface as active until it receives a new revocation.
newState := ifstate.NewInfo(*ifid, true, nil, nil)
ifstate.UpdateIfNew(*ifid, state, newState)
return nil
}
sinfo := scmp.NewInfoRevocation(
uint16(rp.CmnHdr.CurrInfoF), uint16(rp.CmnHdr.CurrHopF), uint16(*ifid),
rp.DirFrom == rcmn.DirExternal, state.RawSRev)
return common.NewBasicError(
errIntfRevoked,
scmp.NewError(scmp.C_Path, scmp.T_P_RevokedIF, sinfo, nil),
"ifid", ifid,
)
}
// mkInfoPathOffsets is a helper function to create an scmp.InfoPathOffsets
// instance from the current packet.
func (rp *RtrPkt) mkInfoPathOffsets() scmp.Info {
return &scmp.InfoPathOffsets{
InfoF: uint16(rp.CmnHdr.CurrInfoF), HopF: uint16(rp.CmnHdr.CurrHopF),
IfID: uint16(*rp.ifCurr), Ingress: rp.DirFrom == rcmn.DirExternal,
}
}
// InfoF retrieves the current path Info Field if it isn't already known.
func (rp *RtrPkt) InfoF() (*spath.InfoField, error) {
if rp.infoF == nil {
for _, f := range rp.hooks.Infof {
ret, infof, err := f()
switch {
case err != nil:
return nil, err
case ret == HookContinue:
continue
case ret == HookFinish:
rp.infoF = infof
return rp.infoF, nil
}
}
// Check the common header path metadata validity, and if so extract
// the current Info Field.
hOff := rp.CmnHdr.InfoFOffBytes()
switch {
case rp.CmnHdr.CurrHopF == rp.CmnHdr.CurrInfoF:
// There is no path, so do nothing.
case hOff < rp.idxs.path: // Error
return nil, common.NewBasicError(
"Info field index too small",
scmp.NewError(scmp.C_CmnHdr, scmp.T_C_BadInfoFOffset, nil, nil),
"min", rp.idxs.path/common.LineLen, "actual", rp.CmnHdr.CurrInfoF,
)
case rp.CmnHdr.CurrInfoF > rp.CmnHdr.HdrLen: // Error
return nil, common.NewBasicError(
"Info field index too large",
scmp.NewError(scmp.C_CmnHdr, scmp.T_C_BadInfoFOffset, nil, nil),
"max", rp.CmnHdr.HdrLen, "actual", rp.CmnHdr.CurrInfoF,
)
case rp.CmnHdr.CurrInfoF < rp.CmnHdr.HdrLen: // Parse
var err error
if rp.infoF, err = spath.InfoFFromRaw(rp.Raw[hOff:]); err != nil {
return nil, err
}
}
}
return rp.infoF, nil
}
// HopF retrieves the current path Hop Field if it isn't already known.
func (rp *RtrPkt) HopF() (*spath.HopField, error) {
if rp.hopF == nil {
for _, f := range rp.hooks.HopF {
ret, hopf, err := f()
switch {
case err != nil:
return nil, err
case ret == HookContinue:
continue
case ret == HookFinish:
rp.hopF = hopf
return rp.hopF, nil
}
}
hOff := rp.CmnHdr.HopFOffBytes()
iOff := rp.CmnHdr.InfoFOffBytes()
// Check if the common header path metadata is valid, and if so
// extracts the current Hop Field.
switch {
case rp.CmnHdr.CurrHopF == rp.CmnHdr.CurrInfoF:
// There is no path, so do nothing.
case hOff < iOff+spath.InfoFieldLength: // Error
return nil, common.NewBasicError(
"Hop field index too small",
scmp.NewError(scmp.C_CmnHdr, scmp.T_C_BadHopFOffset, nil, nil),
"min", (iOff+spath.InfoFieldLength)/common.LineLen, "actual", rp.CmnHdr.CurrHopF,
)
case rp.CmnHdr.CurrHopF >= rp.CmnHdr.HdrLen: // Error
return nil, common.NewBasicError(
"Hop field index too large",
scmp.NewError(scmp.C_CmnHdr, scmp.T_C_BadHopFOffset, nil, nil),
"max", (rp.CmnHdr.HdrLenBytes()-spath.HopFieldLength)/common.LineLen,
"actual", rp.CmnHdr.CurrHopF,
)
default: // Parse
var err error
if rp.hopF, err = spath.HopFFromRaw(rp.Raw[hOff:]); err != nil {
return nil, err
}
}
}
return rp.hopF, nil
}
// getHopFVer retrieves the Hop Field (if any) required for verifying the MAC
// of the current Hop Field.
func (rp *RtrPkt) getHopFVer(dirFrom rcmn.Dir) common.RawBytes {
ingress := dirFrom == rcmn.DirExternal
var offset int
if !rp.hopF.Xover || (rp.infoF.Shortcut && !rp.infoF.Peer) {
offset = rp.getHopFVerNormalOffset()
} else {
switch rp.infoF.Peer {
case true:
// Peer shortcut paths have two extra HOFs; 1 for the peering
// interface, and another from the upstream interface, used for
// verification only.
switch {
case ingress && rp.infoF.ConsDir:
offset = +1
case ingress && !rp.infoF.ConsDir:
offset = +2
case !ingress && rp.infoF.ConsDir:
offset = -2
case !ingress && !rp.infoF.ConsDir:
offset = -1
}
case false:
// Non-peer shortcut paths have an extra HOF above the last hop,
// used for verification of the last hop in that segment.
switch {
case ingress && rp.infoF.ConsDir:
offset = -1
case !ingress && !rp.infoF.ConsDir:
offset = +1
}
}
}
return rp.hopFVerFromRaw(offset)
}
// getHopFVerNormalOffset is a helper function for getHopFVer, to handle cases
// where the verification Hop Field (if any) is directly before or after
// (depending on the ConsDir flag) the current Hop Field.
func (rp *RtrPkt) getHopFVerNormalOffset() int {
iOff := rp.CmnHdr.InfoFOffBytes()
hOff := rp.CmnHdr.HopFOffBytes()
// If this is the last hop of an Up path, or the first hop of a Down path, there's no previous
// HOF to verify against.
if (!rp.infoF.ConsDir &&
hOff == (iOff+spath.InfoFieldLength+int(rp.infoF.Hops-1)*spath.HopFieldLength)) ||
(rp.infoF.ConsDir && hOff == (iOff+spath.InfoFieldLength)) {
return 0
}
// Otherwise use the next/prev HOF based on the consDir flag.
if !rp.infoF.ConsDir {
return 1
}
return -1
}
// hopFVerFromRaw is a helper function for getHopFVer. It extracts the raw
// bytes of the specified Hop Field, excluding the leading flag byte.
func (rp *RtrPkt) hopFVerFromRaw(offset int) common.RawBytes {
ans := make(common.RawBytes, common.LineLen-1)
// If the offset is 0, a zero'd slice is returned.
if offset != 0 {
b := rp.Raw[(int(rp.CmnHdr.CurrHopF)+offset)*common.LineLen:]
copy(ans, b[1:common.LineLen])
}
return ans
}
// IncPath increments the packet's path, if any. The bool return value is set
// to true if the segment changes (specifically if the packet's metadata is
// updated to the new segment).
func (rp *RtrPkt) IncPath() (bool, error) {
if rp.infoF == nil {
// Path is empty, nothing to increment.
return false, nil
}
if assert.On {
assert.Mustf(rp.consDirFlag != nil, rp.ErrStr, "rp.consDirFlag must not be nil")
}
var err error
var hopF *spath.HopField
// Initialize to the current InfoF and offset values.
infoF := rp.infoF
iOff := rp.CmnHdr.InfoFOffBytes()
hOff := rp.CmnHdr.HopFOffBytes()
hdrLen := rp.CmnHdr.HdrLenBytes()
vOnly := 0
origConsDir := *rp.consDirFlag
for {
hOff += spath.HopFieldLength
if hOff-iOff > int(infoF.Hops*spath.HopFieldLength) {
// Passed end of current segment, switch to next segment, and read
// the new Info Field.
iOff = hOff
if infoF, err = spath.InfoFFromRaw(rp.Raw[iOff:]); err != nil {
// Still return false as the metadata hasn't been updated to the new segment.
return false, err
}
continue
}
// Read new Hop Field
if hopF, err = spath.HopFFromRaw(rp.Raw[hOff:]); err != nil {
return false, err
}
// Find first non-verify-only Hop Field.
if !hopF.VerifyOnly {
break
}
vOnly++
}
if hOff > hdrLen {
return false, common.NewBasicError("New HopF offset > header length", nil,
"max", hdrLen, "actual", hOff)
}
segChgd := iOff != rp.CmnHdr.InfoFOffBytes()
// Check that there's no VERIFY_ONLY fields in the middle of a segment.
if vOnly > 0 && !segChgd {
return segChgd, common.NewBasicError("VERIFY_ONLY in middle of segment",
scmp.NewError(scmp.C_Path, scmp.T_P_BadHopField, rp.mkInfoPathOffsets(), nil))
}
// Check that the segment didn't change from a down-segment to an up-segment.
if origConsDir && !infoF.ConsDir {
return segChgd, common.NewBasicError("Switched from down-segment to up-segment",
scmp.NewError(scmp.C_Path, scmp.T_P_BadSegment, rp.mkInfoPathOffsets(), nil))
}
// Update common header, and packet's InfoF/HopF fields.
rp.CmnHdr.UpdatePathOffsets(rp.Raw, uint8(iOff/common.LineLen), uint8(hOff/common.LineLen))
rp.infoF = infoF
rp.hopF = hopF
rp.IncrementedPath = true
if segChgd {
// Extract new ConsDir flag.
rp.consDirFlag = nil
if _, err = rp.ConsDirFlag(); err != nil {
return segChgd, err
}
}
// Extract the next interface ID.
rp.ifNext = nil
if _, err = rp.IFNext(); err != nil {
return segChgd, err
}
return segChgd, nil
}
// ConsDirFlag retrieves the current path segment's ConsDir flag if not already known.
// (Consdir is defined as being the direction in which the segment was created.)
func (rp *RtrPkt) ConsDirFlag() (*bool, error) {
if rp.consDirFlag != nil {
return rp.consDirFlag, nil
}
// Try to get ConsDir flag from extensions
for _, f := range rp.hooks.ConsDirFlag {
ret, consDir, err := f()
switch {
case err != nil:
return nil, err
case ret == HookContinue:
continue
case ret == HookFinish:
rp.consDirFlag = &consDir
return rp.consDirFlag, nil
}
}
// Try to get ConsDir flag from InfoField
if rp.infoF == nil {
return nil, nil
}
rp.consDirFlag = &rp.infoF.ConsDir
return rp.consDirFlag, nil
}
// IFCurr retrieves the current interface ID from the packet headers/extensions,
// if not already known.
func (rp *RtrPkt) IFCurr() (*common.IFIDType, error) {
if rp.ifCurr != nil {
return rp.ifCurr, nil
}
if rp.consDirFlag != nil {
// Try to get IFID from registered hooks.
if ifid, err := rp.hookIF(*rp.consDirFlag, rp.hooks.IFCurr); err != nil {
return nil, err
} else if ifid != nil {
return rp.checkSetCurrIF(ifid)
}
// Try to get IFID from HopField
if rp.hopF != nil {
var ingress bool
switch rp.DirFrom {
case rcmn.DirLocal:
ingress = !*rp.consDirFlag
case rcmn.DirExternal:
ingress = *rp.consDirFlag
default:
return nil, common.NewBasicError("DirFrom value unsupported", nil,
"val", rp.DirFrom)
}
if ingress {
return rp.checkSetCurrIF(&rp.hopF.ConsIngress)
}
return rp.checkSetCurrIF(&rp.hopF.ConsEgress)
}
}
return nil, nil
}
// checkSetCurrIF is a helper function that ensures the given interface ID is
// valid before setting the ifCurr field and returning the value.
func (rp *RtrPkt) checkSetCurrIF(ifid *common.IFIDType) (*common.IFIDType, error) {
if ifid == nil {
return nil, common.NewBasicError("No interface found", nil)
}
if _, ok := rp.Ctx.Conf.Net.IFs[*ifid]; !ok {
return nil, common.NewBasicError("Unknown interface", nil, "ifid", *ifid)
}
rp.ifCurr = ifid
return rp.ifCurr, nil
}
// IFNext retrieves the next interface ID if not already known. As this may be
// an interface in an external ISD-AS, this is not sanity-checked.
func (rp *RtrPkt) IFNext() (*common.IFIDType, error) {
if rp.ifNext == nil && rp.consDirFlag != nil {
var err error
// Try to get IFID from registered hooks.
if rp.ifNext, err = rp.hookIF(*rp.consDirFlag, rp.hooks.IFNext); err != nil {
return nil, err
} else if rp.ifNext != nil {
return rp.ifNext, nil
}
// Get IFID from HopField
if *rp.consDirFlag {
rp.ifNext = &rp.hopF.ConsEgress
} else {
rp.ifNext = &rp.hopF.ConsIngress
}
}
return rp.ifNext, nil
}
// hookIF is a helper function used by IFCurr/IFNext to run interface ID
// retrival hooks.
func (rp *RtrPkt) hookIF(consDir bool, hooks []hookIntf) (*common.IFIDType, error) {
for _, f := range hooks {
ret, intf, err := f(consDir, rp.DirFrom)
switch {
case err != nil:
return nil, err
case ret == HookContinue:
continue
case ret == HookFinish:
return &intf, nil
}
}
return nil, nil
}