/
forwarder.go
113 lines (104 loc) · 3.54 KB
/
forwarder.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
// Copyright 2020 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 segreq
import (
"context"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/ctrl/seg"
"github.com/scionproto/scion/go/lib/infra/modules/segfetcher"
"github.com/scionproto/scion/go/lib/serrors"
)
// ForwardingLookup handles path segment lookup requests in a non-core AS. If
// segments are missing, the request is forwarded to the respective core ASes.
// It should only be used in a non-core AS.
type ForwardingLookup struct {
LocalIA addr.IA
CoreChecker CoreChecker
Fetcher *segfetcher.Fetcher
Expander WildcardExpander
}
// LookupSegments looks up the segments for the given request
//
// - requests for up segment are answered directly, from the local DB
// - down and core segments are forwarded to the responsible core ASes,
// and results are cached
func (f ForwardingLookup) LookupSegments(ctx context.Context, src,
dst addr.IA) (segfetcher.Segments, error) {
segType, err := f.classify(ctx, src, dst)
if err != nil {
return nil, err
}
reqs, err := f.Expander.ExpandSrcWildcard(ctx,
segfetcher.Request{
Src: src,
Dst: dst,
SegType: segType,
},
)
if err != nil {
return nil, serrors.WrapStr("expanding wildcard request", err)
}
return f.Fetcher.Fetch(ctx, reqs, false)
}
// classify validates the request and determines the segment type for the request
func (f ForwardingLookup) classify(ctx context.Context,
src, dst addr.IA) (seg.Type, error) {
if src.ISD() == 0 || dst.ISD() == 0 {
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "zero ISD src or dst")
}
if dst == f.LocalIA {
// this could be an otherwise valid request, but probably the requester switched Src and Dst
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "dst is local AS, confusion?")
}
srcCore, err := f.CoreChecker.IsCore(ctx, src)
if err != nil {
return 0, err
}
dstCore, err := f.CoreChecker.IsCore(ctx, dst)
if err != nil {
return 0, err
}
switch {
case srcCore && dstCore:
// core
if src.ISD() != f.LocalIA.ISD() {
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "core segment request src ISD not local ISD")
}
return seg.TypeCore, nil
case srcCore:
// down
if src.ISD() != dst.ISD() {
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "down segment request src/dst in different ISD")
}
return seg.TypeDown, nil
case dstCore:
// up
if src != f.LocalIA {
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "up segment request src not local AS")
}
if dst.ISD() != f.LocalIA.ISD() {
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "up segment request dst in different ISD")
}
return seg.TypeUp, nil
default:
return 0, serrors.WithCtx(segfetcher.ErrInvalidRequest,
"src", src, "dst", dst, "reason", "non-core src & dst")
}
}