-
Notifications
You must be signed in to change notification settings - Fork 163
/
Copy pathhandlers.go
360 lines (333 loc) · 12.2 KB
/
handlers.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
// 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.
package servers
import (
"context"
"fmt"
"net"
"time"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/ctrl/path_mgmt"
"github.com/scionproto/scion/go/lib/infra"
"github.com/scionproto/scion/go/lib/infra/modules/itopo"
"github.com/scionproto/scion/go/lib/infra/modules/segverifier"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/revcache"
"github.com/scionproto/scion/go/lib/sciond"
"github.com/scionproto/scion/go/lib/topology"
"github.com/scionproto/scion/go/proto"
"github.com/scionproto/scion/go/sciond/internal/fetcher"
)
const (
// DefaultReplyTimeout is allocated to SCIOND handlers to reply back to the client.
DefaultReplyTimeout = 2 * time.Second
// DefaultWorkTimeout is allocated to SCIOND handlers work (e.g., network
// traffic and crypto operations)
DefaultWorkTimeout = 10 * time.Second
DefaultEarlyReply = 200 * time.Millisecond
// DefaultServiceTTL is the TTL value for ServiceInfoReply objects,
// expressed in seconds.
DefaultServiceTTL uint32 = 300
)
type Handler interface {
Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld, logger log.Logger)
}
// PathRequestHandler represents the shared global state for the handling of all
// PathRequest queries. The SCIOND API spawns a goroutine with method Handle
// for each PathRequest it receives.
type PathRequestHandler struct {
Fetcher *fetcher.Fetcher
}
func (h *PathRequestHandler) Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld,
logger log.Logger) {
logger.Debug("[SCIOND:PathRequestHandler] Received request",
"request", fmt.Sprintf("%v->%v", pld.PathReq.Src, pld.PathReq.Dst),
"count", pld.PathReq.MaxPaths, "flags", pld.PathReq.Flags)
workCtx, workCancelF := context.WithTimeout(context.Background(), DefaultWorkTimeout)
defer workCancelF()
getPathsReply, err := h.Fetcher.GetPaths(workCtx, &pld.PathReq, DefaultEarlyReply, logger)
if err != nil {
logger.Warn("[SCIOND:PathRequestHandler] Unable to get paths", "err", err)
}
// Always reply, as the Fetcher will fill in the relevant error bits of the reply
reply := &sciond.Pld{
Id: pld.Id,
Which: proto.SCIONDMsg_Which_pathReply,
PathReply: *getPathsReply,
}
b, err := proto.PackRoot(reply)
if err != nil {
// This is constructed locally, so it should always succeed. Otherwise,
// it is a bug.
panic(err)
}
ctx, cancelF := context.WithTimeout(context.Background(), DefaultReplyTimeout)
defer cancelF()
if err := transport.SendMsgTo(ctx, b, src); err != nil {
logger.Warn("[SCIOND:PathRequestHandler] Unable to reply to client",
"client", src, "err", err)
return
}
logger.Debug("[SCIOND:PathRequestHandler] Replied with paths", "num_paths",
len(getPathsReply.Entries))
logger.Trace("[SCIOND:PathRequestHandler] Full reply", "paths", getPathsReply)
}
// ASInfoRequestHandler represents the shared global state for the handling of all
// ASInfoRequest queries. The SCIOND API spawns a goroutine with method Handle
// for each ASInfoRequest it receives.
type ASInfoRequestHandler struct {
TrustStore infra.TrustStore
}
func (h *ASInfoRequestHandler) Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld,
logger log.Logger) {
logger.Debug("[SCIOND:ASInfoRequestHandler] Received request", "request", &pld.AsInfoReq)
workCtx, workCancelF := context.WithTimeout(context.Background(), DefaultWorkTimeout)
defer workCancelF()
// NOTE(scrye): Only support single-homed SCIONDs for now (returned slice
// will at most contain one element).
reqIA := pld.AsInfoReq.Isdas.IA()
topo := itopo.GetCurrentTopology()
if reqIA.IsZero() {
reqIA = topo.ISD_AS
}
asInfoReply := sciond.ASInfoReply{}
trcObj, err := h.TrustStore.GetValidTRC(workCtx, reqIA.I, nil)
if err != nil {
// FIXME(scrye): return a zero AS because the protocol doesn't
// support errors, but we probably want to return an error here in
// the future.
asInfoReply.Entries = []sciond.ASInfoReplyEntry{}
}
if reqIA.IsZero() || reqIA.Eq(topo.ISD_AS) {
// Requested AS is us
asInfoReply.Entries = []sciond.ASInfoReplyEntry{
{
RawIsdas: topo.ISD_AS.IAInt(),
Mtu: uint16(topo.MTU),
IsCore: trcObj.CoreASes.Contains(topo.ISD_AS),
},
}
} else {
// Requested AS is not us
asInfoReply.Entries = []sciond.ASInfoReplyEntry{
{
RawIsdas: reqIA.IAInt(),
Mtu: 0,
IsCore: trcObj.CoreASes.Contains(reqIA),
},
}
}
reply := &sciond.Pld{
Id: pld.Id,
Which: proto.SCIONDMsg_Which_asInfoReply,
AsInfoReply: asInfoReply,
}
b, err := proto.PackRoot(reply)
if err != nil {
panic(err)
}
ctx, cancelF := context.WithTimeout(context.Background(), DefaultReplyTimeout)
defer cancelF()
if err := transport.SendMsgTo(ctx, b, src); err != nil {
logger.Warn("Unable to reply to client", "client", src, "err", err)
return
}
logger.Trace("[SCIOND:ASInfoRequestHandler] Sent reply", "asInfo", asInfoReply)
}
// IFInfoRequestHandler represents the shared global state for the handling of all
// IFInfoRequest queries. The SCIOND API spawns a goroutine with method Handle
// for each IFInfoRequest it receives.
type IFInfoRequestHandler struct{}
func (h *IFInfoRequestHandler) Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld,
logger log.Logger) {
logger.Debug("[SCIOND:IFInfoRequestHandler] Received request", "request", &pld.IfInfoRequest)
ifInfoRequest := pld.IfInfoRequest
ifInfoReply := sciond.IFInfoReply{}
topo := itopo.GetCurrentTopology()
if len(ifInfoRequest.IfIDs) == 0 {
// Reply with all the IFIDs we know
for ifid, ifInfo := range topo.IFInfoMap {
ifInfoReply.RawEntries = append(ifInfoReply.RawEntries, sciond.IFInfoReplyEntry{
IfID: ifid,
HostInfo: sciond.HostInfoFromTopoBRAddr(*ifInfo.InternalAddrs),
})
}
} else {
// Reply with only the IFIDs the client requested
for _, ifid := range ifInfoRequest.IfIDs {
ifInfo, ok := topo.IFInfoMap[ifid]
if !ok {
logger.Info("Received IF Info Request, but IFID not found", "ifid", ifid)
continue
}
ifInfoReply.RawEntries = append(ifInfoReply.RawEntries, sciond.IFInfoReplyEntry{
IfID: ifid,
HostInfo: sciond.HostInfoFromTopoBRAddr(*ifInfo.InternalAddrs),
})
}
}
reply := &sciond.Pld{
Id: pld.Id,
Which: proto.SCIONDMsg_Which_ifInfoReply,
IfInfoReply: ifInfoReply,
}
b, err := proto.PackRoot(reply)
if err != nil {
panic(err)
}
ctx, cancelF := context.WithTimeout(context.Background(), DefaultReplyTimeout)
defer cancelF()
if err := transport.SendMsgTo(ctx, b, src); err != nil {
logger.Warn("Unable to reply to client", "client", src, "err", err)
return
}
logger.Trace("[SCIOND:IFInfoRequestHandler] Sent reply", "ifInfo", ifInfoReply)
}
// SVCInfoRequestHandler represents the shared global state for the handling of all
// SVCInfoRequest queries. The SCIOND API spawns a goroutine with method Handle
// for each SVCInfoRequest it receives.
type SVCInfoRequestHandler struct{}
func (h *SVCInfoRequestHandler) Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld,
logger log.Logger) {
logger.Debug("[SCIOND:SVCInfoRequestHandler] Received request",
"request", &pld.ServiceInfoRequest)
svcInfoRequest := pld.ServiceInfoRequest
svcInfoReply := sciond.ServiceInfoReply{}
topo := itopo.GetCurrentTopology()
for _, t := range svcInfoRequest.ServiceTypes {
var hostInfos []sciond.HostInfo
hostInfos = makeHostInfos(topo, t)
replyEntry := sciond.ServiceInfoReplyEntry{
ServiceType: t,
Ttl: DefaultServiceTTL,
HostInfos: hostInfos,
}
svcInfoReply.Entries = append(svcInfoReply.Entries, replyEntry)
}
reply := &sciond.Pld{
Id: pld.Id,
Which: proto.SCIONDMsg_Which_serviceInfoReply,
ServiceInfoReply: svcInfoReply,
}
b, err := proto.PackRoot(reply)
if err != nil {
panic(err)
}
ctx, cancelF := context.WithTimeout(context.Background(), DefaultReplyTimeout)
defer cancelF()
if err := transport.SendMsgTo(ctx, b, src); err != nil {
logger.Warn("Unable to reply to client", "client", src, "err", err)
return
}
logger.Trace("[SCIOND:SVCInfoRequestHandler] Sent reply", "svcInfo", svcInfoReply)
}
func makeHostInfos(topo *topology.Topo, t proto.ServiceType) []sciond.HostInfo {
var hostInfos []sciond.HostInfo
addresses, err := topo.GetAllTopoAddrs(t)
if err != nil {
// FIXME(lukedirtwalker): inform client about this:
// see https://github.com/scionproto/scion/issues/1673
return hostInfos
}
for _, a := range addresses {
hostInfos = append(hostInfos, sciond.HostInfoFromTopoAddr(a))
}
return hostInfos
}
// RevNotificationHandler represents the shared global state for the handling of all
// RevNotification announcements. The SCIOND API spawns a goroutine with method Handle
// for each RevNotification it receives.
type RevNotificationHandler struct {
RevCache revcache.RevCache
TrustStore infra.TrustStore
}
func (h *RevNotificationHandler) Handle(transport infra.Transport, src net.Addr, pld *sciond.Pld,
logger log.Logger) {
logger.Debug("[SCIOND:RevNotificationHandler] Received revocation",
"notification", &pld.RevNotification)
workCtx, workCancelF := context.WithTimeout(context.Background(), DefaultWorkTimeout)
defer workCancelF()
revNotification := pld.RevNotification
revReply := sciond.RevReply{}
revInfo, err := h.verifySRevInfo(workCtx, revNotification.SRevInfo)
if err == nil {
_, err = h.RevCache.Insert(workCtx, revNotification.SRevInfo)
if err != nil {
logger.Error("[SCIOND:RevNotificationHandler] Failed to insert revocations", "err", err)
}
}
switch {
case isValid(err):
revReply.Result = sciond.RevValid
case isStale(err):
revReply.Result = sciond.RevStale
case isInvalid(err):
revReply.Result = sciond.RevInvalid
case isUnknown(err):
revReply.Result = sciond.RevUnknown
default:
panic(fmt.Sprintf("unknown error type, err = %v", err))
}
reply := &sciond.Pld{
Id: pld.Id,
Which: proto.SCIONDMsg_Which_revReply,
RevReply: revReply,
}
b, err := proto.PackRoot(reply)
if err != nil {
panic(err)
}
ctx, cancelF := context.WithTimeout(context.Background(), DefaultReplyTimeout)
defer cancelF()
if err := transport.SendMsgTo(ctx, b, src); err != nil {
logger.Warn("Unable to reply to client", "client", src, "err", err)
return
}
logger.Trace("[SCIOND:RevNotificationHandler] Sent reply", "revInfo", revInfo)
}
// verifySRevInfo first checks if the RevInfo can be extracted from sRevInfo,
// and immediately returns with an error if it cannot. Then, revocation
// verification is performed and the result is returned.
func (h *RevNotificationHandler) verifySRevInfo(ctx context.Context,
sRevInfo *path_mgmt.SignedRevInfo) (*path_mgmt.RevInfo, error) {
// Error out immediately if RevInfo is bad
info, err := sRevInfo.RevInfo()
if err != nil {
return nil, common.NewBasicError("Unable to extract RevInfo", nil)
}
err = segverifier.VerifyRevInfo(ctx, h.TrustStore, nil, sRevInfo)
return info, err
}
// isValid is a placeholder. It should return true if and only if revocation
// verification ended with an outcome of valid.
func isValid(err error) bool {
return err == nil
}
// isStale is a placeholder. It should return true if and only if revocation
// verification ended with an outcome of stale.
func isStale(err error) bool {
// FIXME(scrye): implement this once we have verification
return false
}
// isInvalid is a placeholder. It should return true if and only if revocation
// verification ended with an outcome of invalid.
func isInvalid(err error) bool {
// FIXME(scrye): implement this once we have verification
return false
}
// isUnknown is a placeholder. It should return true if and only if revocation
// verification ended with an outcome of unknown.
func isUnknown(err error) bool {
return err != nil
}