-
Notifications
You must be signed in to change notification settings - Fork 162
/
traceroute.go
311 lines (289 loc) · 8.51 KB
/
traceroute.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
// 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 traceroute implements tracerouting based on SCMP traceroute messages.
package traceroute
import (
"context"
"math/rand"
"net"
"time"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/log"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/lib/slayers/path"
"github.com/scionproto/scion/go/lib/slayers/path/scion"
"github.com/scionproto/scion/go/lib/snet"
"github.com/scionproto/scion/go/lib/sock/reliable"
"github.com/scionproto/scion/go/lib/spath"
)
// Update contains the information for a single hop.
type Update struct {
// Index indicates the hop index in the path.
Index int
// Remote is the remote router.
Remote *snet.UDPAddr
// Interface is the interface ID of the remote router.
Interface uint64
// RTTs are the RTTs for this hop. To detect whether there was a timeout the
// value of the RTT can be compared against the timeout value from the
// configuration.
RTTs []time.Duration
}
func (u Update) empty() bool {
return u.Index == 0 && u.Remote == nil && u.Interface == 0 && len(u.RTTs) == 0
}
// Stats contains the amount of sent and received packets.
type Stats struct {
Sent, Recv uint
}
// Config configures the traceroute run.
type Config struct {
Dispatcher reliable.Dispatcher
Local *snet.UDPAddr
MTU uint16
PathEntry snet.Path
PayloadSize uint
Remote *snet.UDPAddr
Timeout time.Duration
// ProbesPerHop indicates how many probes should be done per hop.
ProbesPerHop int
// ErrHandler is invoked for every error that does not cause tracerouting to
// abort. Execution time must be small, as it is run synchronously.
ErrHandler func(error)
// Update handler is invoked for every hop. Execution time must be
// small, as it is run synchronously.
UpdateHandler func(Update)
}
type tracerouter struct {
probesPerHop int
timeout time.Duration
conn snet.PacketConn
local *snet.UDPAddr
remote *snet.UDPAddr
errHandler func(error)
updateHandler func(Update)
replies <-chan reply
path snet.Path
id uint16
index int
stats Stats
}
// Run runs the traceroute.
func Run(ctx context.Context, cfg Config) (Stats, error) {
if cfg.PathEntry.Path().IsEmpty() {
return Stats{}, serrors.New("empty path is not allowed for traceroute")
}
id := rand.Uint64()
replies := make(chan reply, 10)
dispatcher := snet.DefaultPacketDispatcherService{
Dispatcher: cfg.Dispatcher,
SCMPHandler: scmpHandler{replies: replies},
}
conn, port, err := dispatcher.Register(ctx, cfg.Local.IA, cfg.Local.Host, addr.SvcNone)
if err != nil {
return Stats{}, err
}
local := cfg.Local.Copy()
local.Host.Port = int(port)
t := tracerouter{
probesPerHop: cfg.ProbesPerHop,
timeout: cfg.Timeout,
conn: conn,
local: local,
remote: cfg.Remote,
replies: replies,
errHandler: cfg.ErrHandler,
updateHandler: cfg.UpdateHandler,
id: uint16(id),
path: cfg.PathEntry,
}
return t.Traceroute(ctx)
}
func (t *tracerouter) Traceroute(ctx context.Context) (Stats, error) {
pktPath := scion.Decoded{}
if err := pktPath.DecodeFromBytes(t.path.Path().Raw); err != nil {
return t.stats, serrors.WrapStr("decoding path", err)
}
idxPath := scion.Decoded{}
if err := idxPath.DecodeFromBytes(t.path.Path().Raw); err != nil {
return t.stats, serrors.WrapStr("decoding path", err)
}
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
defer log.HandlePanic()
t.drain(ctx)
}()
prevXover := false
for i := 0; i < len(idxPath.HopFields); i++ {
hf := pktPath.HopFields[idxPath.PathMeta.CurrHF]
info := pktPath.InfoFields[idxPath.PathMeta.CurrINF]
// First hop of the path isn't probed, since only the egress hop is
// relevant.
// After a crossover (segment change) only the egress interface is
// relevant, since the ingress interface is in previous hop.
if i != 0 && !prevXover {
u, err := t.probeHop(ctx, &pktPath, hf, !info.ConsDir)
if err != nil {
return t.stats, serrors.WrapStr("probing hop", err, "hop_index", i)
}
if t.updateHandler != nil && !u.empty() {
t.updateHandler(u)
}
}
xover := idxPath.IsXover()
// The last hop of the path isn't probed, only the ingress interface is
// relevant.
// At a crossover (segment change) only the ingress interface is
// relevant, since the egress interface is in the next hop.
if i < len(idxPath.HopFields)-1 && !xover {
u, err := t.probeHop(ctx, &pktPath, hf, info.ConsDir)
if err != nil {
return t.stats, serrors.WrapStr("probing hop", err, "hop_index", i)
}
if t.updateHandler != nil && !u.empty() {
t.updateHandler(u)
}
}
if i < len(idxPath.HopFields)-1 {
if err := idxPath.IncPath(); err != nil {
return t.stats, serrors.WrapStr("incrementing path", err)
}
}
prevXover = xover
}
return t.stats, nil
}
func (t *tracerouter) probeHop(ctx context.Context, dp *scion.Decoded,
hf *path.HopField, egress bool) (Update, error) {
// set alert flag and reset once we are done.
if egress {
hf.EgressRouterAlert = true
defer func() { hf.EgressRouterAlert = false }()
} else {
hf.IngressRouterAlert = true
defer func() { hf.IngressRouterAlert = false }()
}
p := t.path.Path()
if err := dp.SerializeTo(p.Raw); err != nil {
return Update{}, serrors.WrapStr("serializing path", err)
}
u := Update{
Index: t.index,
RTTs: make([]time.Duration, 0, t.probesPerHop),
}
t.index++
pkt := &snet.Packet{
PacketInfo: snet.PacketInfo{
Destination: snet.SCIONAddress{
IA: t.remote.IA,
Host: addr.HostFromIP(t.remote.Host.IP),
},
Source: snet.SCIONAddress{
IA: t.local.IA,
Host: addr.HostFromIP(t.local.Host.IP),
},
Path: p,
Payload: snet.SCMPTracerouteRequest{Identifier: t.id},
},
}
for i := 0; i < t.probesPerHop; i++ {
sendTs := time.Now()
t.stats.Sent++
if err := t.conn.WriteTo(pkt, t.remote.NextHop); err != nil {
return u, serrors.WrapStr("writing", err)
}
select {
case <-time.After(t.timeout):
u.RTTs = append(u.RTTs, t.timeout+1)
continue
case reply := <-t.replies:
if reply.Error != nil {
if t.errHandler != nil {
t.errHandler(reply.Error)
}
continue
}
if t.id != reply.Reply.Identifier {
if t.errHandler != nil {
t.errHandler(serrors.New("wrong SCMP ID",
"expected", t.id, "actual", reply.Reply.Identifier))
}
continue
}
t.stats.Recv++
rtt := reply.Received.Sub(sendTs).Round(time.Microsecond)
u.RTTs = append(u.RTTs, rtt)
u.Interface = reply.Reply.Interface
u.Remote = reply.Remote
case <-ctx.Done():
return u, nil
}
}
return u, nil
}
func (t tracerouter) drain(ctx context.Context) {
var last time.Time
for {
select {
case <-ctx.Done():
return
default:
var pkt snet.Packet
var ov net.UDPAddr
if err := t.conn.ReadFrom(&pkt, &ov); err != nil && t.errHandler != nil {
// Rate limit the error reports.
if now := time.Now(); now.Sub(last) > 500*time.Millisecond {
t.errHandler(serrors.WrapStr("reading packet", err))
last = now
}
}
}
}
}
type reply struct {
Received time.Time
Reply snet.SCMPTracerouteReply
Remote *snet.UDPAddr
Error error
}
type scmpHandler struct {
replies chan<- reply
}
func (h scmpHandler) Handle(pkt *snet.Packet) error {
r, err := h.handle(pkt)
h.replies <- reply{
Received: time.Now(),
Reply: r,
Remote: &snet.UDPAddr{
IA: pkt.Source.IA,
Host: &net.UDPAddr{IP: pkt.Destination.Host.IP()},
Path: spath.Path{Raw: append(pkt.Path.Raw[:0:0], pkt.Path.Raw...), Type: pkt.Path.Type},
},
Error: err,
}
return nil
}
func (h scmpHandler) handle(pkt *snet.Packet) (snet.SCMPTracerouteReply, error) {
if pkt.Payload == nil {
return snet.SCMPTracerouteReply{}, serrors.New("no payload found")
}
r, ok := pkt.Payload.(snet.SCMPTracerouteReply)
if !ok {
return snet.SCMPTracerouteReply{}, serrors.New("not SCMPTracerouteReply",
"type", common.TypeOf(pkt.Payload))
}
return r, nil
}