-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.go
638 lines (552 loc) · 15.3 KB
/
format.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
632
633
634
635
636
637
638
// Defines all formatters available to gobgpdump, and convenience
// functions for formatters.
// Current formatters:
// -TextFormatter
// -JSONFormatter
// -IdentityFormatter - Rename to MRTFormatter?
// These both need new configuration names
// -UniquePrefixList
// -UniquePrefixSeries
package gobgpdump
import (
"encoding/gob"
"encoding/json"
"fmt"
"io"
"strings"
"sync"
"time"
mrt "github.com/CSUNetSec/protoparse/protocol/mrt"
util "github.com/CSUNetSec/protoparse/util"
radix "github.com/armon/go-radix"
)
// A Formatter takes the bufferstack and the underlying buffer
// and returns a representation of the data to be written to the
// dump file.
// The underlying buffer is necessary for the ID formatter
type Formatter interface {
format(*mrt.MrtBufferStack, MBSInfo) (string, error)
summarize()
}
// This contains miscellaneous info for the MBS structure
type MBSInfo struct {
file string
msgNum int
}
func NewMBSInfo(file string, msg int) MBSInfo {
return MBSInfo{file, msg}
}
// A simple text representation for the dump.
// The only formatter that needs the msgnum
type TextFormatter struct {
msgNum int
}
func NewTextFormatter() *TextFormatter {
return &TextFormatter{0}
}
func (t *TextFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
ret := fmt.Sprintf("[%d] MRT Header: %s\n", t.msgNum, mbs.MrthBuf)
if mbs.IsRibStack() {
ret += fmt.Sprintf("RIB Header: %s\n", mbs.Ribbuf)
} else {
ret += fmt.Sprintf("BGP4MP Header: %s\n", mbs.Bgp4mpbuf)
ret += fmt.Sprintf("BGP Header: %s\n", mbs.Bgphbuf)
ret += fmt.Sprintf("BGP Update: %s\n\n", mbs.Bgpupbuf)
}
t.msgNum++
return ret, nil
}
// The text formatter doesn't need to summarize
func (t *TextFormatter) summarize() {}
// PrefixLock formatter keeps track of a prefix and the AS that advertized it.
// It then "locks" that relation. In case the same prefix (exactly the same for now)
// is advertized by some other AS, it outputs that event.
type PrefixLockFormatter struct {
plmap map[string]*asLock
m *sync.Mutex
}
type asEvent struct {
as uint32
t time.Time
}
type asLock struct {
owner asEvent
others []asEvent
}
func newAsLock(as uint32, t time.Time) *asLock {
return &asLock{
owner: asEvent{
as: as,
t: t,
},
}
}
func NewPrefixLockFormatter() *PrefixLockFormatter {
return &PrefixLockFormatter{
plmap: make(map[string]*asLock),
m: &sync.Mutex{},
}
}
// this function will output a string of an event if the prefix is already registered to another AS.
func (p *PrefixLockFormatter) registerPrefixAS(pref string, as uint32, t time.Time) (string, error) {
p.m.Lock()
defer p.m.Unlock()
al, ok := p.plmap[pref]
if ok { // it exists already. check if we the event is from an "owner" or a "hijacker"
if al.owner.as == as { // owner.
return "", nil
}
for _, has := range al.others {
if has.as == as { // we have seen this "hijacker"
return "", nil
}
}
// we have a new "hijacker" register him and output it.
al.others = append(al.others, asEvent{as, t})
return fmt.Sprintf("Prefix:%s\t\tOwner:%d\t\tHijacker:%d\t\tTime:%s", pref, al.owner.as, as, t), nil
}
// register it and don't output anything
p.plmap[pref] = newAsLock(as, t)
return "", nil
}
func (p *PrefixLockFormatter) summarize() {}
func (p *PrefixLockFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
eventstrs := []string(nil)
advRoutes, merr := mrt.GetAdvertisedPrefixes(mbs)
asp, errasp := mrt.GetASPath(mbs)
if errasp != nil || len(asp) == 0 || merr != nil {
//maybe just a withdrawn message? don't output anything
return "", nil
}
ts := mrt.GetTimestamp(mbs)
for _, route := range advRoutes {
rets, err := p.registerPrefixAS(route.String(), asp[len(asp)-1], ts)
if rets != "" {
eventstrs = append(eventstrs, rets)
}
if err != nil {
return "", err
}
}
if len(eventstrs) != 0 {
return fmt.Sprintf("%s\n", strings.Join(eventstrs, "\n")), nil
}
return "", nil
}
// Formats each update as a JSON message
type JSONFormatter struct{}
func NewJSONFormatter() JSONFormatter {
return JSONFormatter{}
}
func (j JSONFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
mbsj, err := json.Marshal(mbs)
return string(mbsj) + "\n", err
}
// The JSON formatter doesn't need to summarize
func (j JSONFormatter) summarize() {}
func NewMlFormatter() mlFormatter {
return mlFormatter{}
}
type mltext struct {
Mrt_header struct {
Timestamp string
}
Bgp4mp_header struct {
Local_AS int
Peer_AS int
Local_IP string
Peer_IP string
}
Bgp_update struct {
Advertized_routes []struct {
Prefix string
Mask int
}
Attrs struct {
AS_path []struct {
AS_seq []int
AS_set []int
}
Next_hop string
}
Withdrawn_routes []struct {
Prefix string
Mask int
}
}
}
type mlFormatter struct{}
func (m mlFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
mbsj, err := json.Marshal(mbs)
if err != nil {
return "", err
}
mtext := &mltext{}
err = json.Unmarshal(mbsj, mtext)
if err != nil {
return "", err
}
tparts := strings.Split(mtext.Mrt_header.Timestamp, "T")
if len(tparts) != 2 {
return "", err
}
t1parts := strings.Split(tparts[1], "Z")
retstr := ""
for _, ar := range mtext.Bgp_update.Advertized_routes {
aspstr := ""
for _, asp := range mtext.Bgp_update.Attrs.AS_path {
for _, setelem := range asp.AS_set {
if aspstr == "" {
aspstr += fmt.Sprintf("%d", setelem)
} else {
aspstr += fmt.Sprintf("-%d", setelem)
}
}
for _, seqelem := range asp.AS_seq {
if aspstr == "" {
aspstr += fmt.Sprintf("%d", seqelem)
} else {
aspstr += fmt.Sprintf("-%d", seqelem)
}
}
}
retstr += fmt.Sprintf("%s,%s,%d,%d,%s,%s,%s,%s,%d,%s,%s\n", tparts[0], t1parts[0], mtext.Bgp4mp_header.Local_AS,
mtext.Bgp4mp_header.Peer_AS, mtext.Bgp4mp_header.Local_IP, mtext.Bgp4mp_header.Peer_IP,
"advertized", ar.Prefix, ar.Mask, aspstr,
mtext.Bgp_update.Attrs.Next_hop)
}
for _, wr := range mtext.Bgp_update.Withdrawn_routes {
retstr += fmt.Sprintf("%s,%s,%d,%d,%s,%s,%s,%s,%d,%s,%s\n", tparts[0], t1parts[0], mtext.Bgp4mp_header.Local_AS,
mtext.Bgp4mp_header.Peer_AS, mtext.Bgp4mp_header.Local_IP, mtext.Bgp4mp_header.Peer_IP,
"withdrawn", wr.Prefix, wr.Mask, "",
"")
}
return retstr, nil
}
func (m mlFormatter) summarize() {}
// Applies no formatting to the data
// But data is decompressed, may need to fix that
// However, golang bz2 doesn't have compression features
type IdentityFormatter struct{}
func NewIdentityFormatter() IdentityFormatter {
return IdentityFormatter{}
}
func (id IdentityFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
return string(mbs.GetRawMessage()), nil
}
// No summarization needed
func (id IdentityFormatter) summarize() {}
type PrefixHistory struct {
Pref string
info MBSInfo
Events []PrefixEvent
}
func NewPrefixHistory(pref string, info MBSInfo, firstTime time.Time, advert bool, asp []uint32) *PrefixHistory {
pe := PrefixEvent{firstTime, advert, asp}
return &PrefixHistory{pref, info, []PrefixEvent{pe}}
}
func (ph *PrefixHistory) addEvent(timestamp time.Time, advert bool, asp []uint32) {
ph.Events = append(ph.Events, PrefixEvent{timestamp, advert, asp})
}
func (ph *PrefixHistory) String() string {
str := ph.Pref
if len(ph.Events) > 0 {
str += fmt.Sprintf(" %d", ph.Events[0].Timestamp.Unix())
}
str += debugSprintf(" %s[%d]", ph.info.file, ph.info.msgNum)
return str
}
type PrefixEvent struct {
Timestamp time.Time
Advertized bool
ASPath []uint32
}
// In original gobgpdump, the List and Series are the same struct.
// Consider two separate structs
// UniquePrefixList will look at all incoming messages, and output
// only the top level prefixes seen.
type UniquePrefixList struct {
output io.Writer // This should only be used in summarize
mux *sync.Mutex
prefixes map[string]interface{}
}
func NewUniquePrefixList(fd io.Writer) *UniquePrefixList {
upl := UniquePrefixList{}
upl.output = fd
upl.mux = &sync.Mutex{}
upl.prefixes = make(map[string]interface{})
return &upl
}
func (upl *UniquePrefixList) format(mbs *mrt.MrtBufferStack, inf MBSInfo) (string, error) {
timestamp := mrt.GetTimestamp(mbs)
advRoutes, err := mrt.GetAdvertisedPrefixes(mbs)
asp, errasp := mrt.GetASPath(mbs)
if errasp != nil || len(asp) == 0 {
//maybe just a withdrawn message? make it empty.
asp = []uint32{}
}
// Do something with routes only if there is no error.
// Otherwise, move on to withdrawn routes
if err == nil {
upl.addRoutes(advRoutes, inf, timestamp, true, asp)
}
wdnRoutes, err := mrt.GetWithdrawnPrefixes(mbs)
if err == nil {
upl.addRoutes(wdnRoutes, inf, timestamp, false, asp)
}
return "", nil
}
// If this finds a Route that is not present in the prefixes map,
// adds it in. If it finds one, but these Routes have an earlier
// timestamp, it replaces the old one.
func (upl *UniquePrefixList) addRoutes(rts []mrt.Route, info MBSInfo, timestamp time.Time, advert bool, asp []uint32) {
for _, route := range rts {
// Ignore this prefix, because it causes a lot of problems
if route.Mask == 1 {
continue
}
key := util.IPToRadixkey(route.IP, route.Mask)
if key == "" {
continue
}
upl.mux.Lock()
if upl.prefixes[key] == nil {
upl.prefixes[key] = NewPrefixHistory(route.String(), info, timestamp, advert, asp)
} else {
oldT := upl.prefixes[key].(*PrefixHistory).Events[0].Timestamp
if oldT.After(timestamp) {
upl.prefixes[key] = NewPrefixHistory(route.String(), info, timestamp, advert, asp)
}
}
upl.mux.Unlock()
}
}
// All output is done in this function
func (upl *UniquePrefixList) summarize() {
deleteChildPrefixes(upl.prefixes)
// Whatever is left should be output
for _, value := range upl.prefixes {
ph := value.(*PrefixHistory)
str := ph.String() + "\n"
upl.output.Write([]byte(str))
}
}
// UniquePrefixSeries does the same thing as UniquePrefixList, but
// rather than just a list, it will output a gob file containing each
// prefix and every event seen associated with that prefix
type UniquePrefixSeries struct {
output io.Writer
mux *sync.Mutex
prefixes map[string]interface{}
}
func NewUniquePrefixSeries(fd io.Writer) *UniquePrefixSeries {
ups := UniquePrefixSeries{}
ups.output = fd
ups.mux = &sync.Mutex{}
ups.prefixes = make(map[string]interface{})
return &ups
}
func (ups *UniquePrefixSeries) format(mbs *mrt.MrtBufferStack, inf MBSInfo) (string, error) {
timestamp := mrt.GetTimestamp(mbs)
advRoutes, err := mrt.GetAdvertisedPrefixes(mbs)
asp, errasp := mrt.GetASPath(mbs)
if errasp != nil || len(asp) == 0 {
//maybe just a withdrawn message? make it empty.
asp = []uint32{}
}
if err == nil {
ups.addRoutes(advRoutes, inf, timestamp, true, asp)
}
wdnRoutes, err := mrt.GetWithdrawnPrefixes(mbs)
if err == nil {
ups.addRoutes(wdnRoutes, inf, timestamp, false, asp)
}
return "", nil
}
func (ups *UniquePrefixSeries) addRoutes(rts []mrt.Route, info MBSInfo, timestamp time.Time, advert bool, asp []uint32) {
for _, route := range rts {
//This route causes a lot of trouble
if route.Mask == 1 {
continue
}
key := util.IPToRadixkey(route.IP, route.Mask)
if key == "" {
continue
}
ups.mux.Lock()
if ups.prefixes[key] == nil {
ups.prefixes[key] = NewPrefixHistory(route.String(), info, timestamp, advert, asp)
} else {
ups.prefixes[key].(*PrefixHistory).addEvent(timestamp, advert, asp)
}
ups.mux.Unlock()
}
}
// All output is done here
func (ups *UniquePrefixSeries) summarize() {
g := gob.NewEncoder(ups.output)
var err error
deleteChildPrefixes(ups.prefixes)
// Whatever is left are top-level prefixes and should be
// encoded
for _, value := range ups.prefixes {
ph := value.(*PrefixHistory)
err = g.Encode(ph)
if err != nil {
fmt.Printf("Error marshalling gob:%s\n", err)
}
}
}
type PrefixWalker struct {
top bool
prefixes map[string]interface{}
}
func (p *PrefixWalker) subWalk(s string, v interface{}) bool {
if p.top {
p.top = false
} else {
delete(p.prefixes, s)
}
return false
}
// This function will delete subprefixes from the provided map
func deleteChildPrefixes(pm map[string]interface{}) {
pw := &PrefixWalker{false, pm}
rTree := radix.New()
for key, value := range pm {
rTree.Insert(key, value)
}
rTree.Walk(func(s string, v interface{}) bool {
pw.top = true
rTree.WalkPrefix(s, pw.subWalk)
return false
})
}
type DayFormatter struct {
output io.Writer
hourCt []int
}
func NewDayFormatter(fd io.Writer) *DayFormatter {
return &DayFormatter{fd, make([]int, 24)}
}
func (d *DayFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
timestamp := mrt.GetTimestamp(mbs)
d.hourCt[timestamp.Hour()]++
return "", nil
}
func (d *DayFormatter) summarize() {
for i := 0; i < len(d.hourCt); i++ {
d.output.Write([]byte(fmt.Sprintf("%d %d\n", i, d.hourCt[i])))
}
}
type ASNode struct {
as uint32
ct int
next []uint32
isOrigin bool
}
func (asn *ASNode) HasNext(as uint32) bool {
for _, next := range asn.next {
if next == as {
return true
}
}
return false
}
func (asn *ASNode) AddNext(as uint32) {
if asn.HasNext(as) {
return
}
asn.next = append(asn.next, as)
}
func (asn *ASNode) GetDotAttributres() string {
attrTmpl := "[style=\"filled\",fillcolor=\"%s\"]"
color := ""
// These colors were chosen to be lightly colored but still
// noticeable
if asn.isOrigin && asn.ct == 1 {
color = "darkorchid1"
} else if asn.isOrigin {
color = "cornflowerblue"
} else if asn.ct == 1 {
color = "firebrick1"
} else {
return ""
}
return fmt.Sprintf(attrTmpl, color)
}
type ASMap struct {
nodes map[uint32]*ASNode
}
func NewASMap() *ASMap {
return &ASMap{nodes: make(map[uint32]*ASNode)}
}
func (asm *ASMap) AddPath(aspath []uint32) {
// Add all the links starting from the origin
for i := len(aspath) - 1; i >= 0; i-- {
node, ok := asm.nodes[aspath[i]]
if !ok {
node = &ASNode{as: aspath[i], ct: 0, next: []uint32{}, isOrigin: false}
}
node.ct++
if i == len(aspath)-1 {
node.isOrigin = true
}
if i != 0 {
node.AddNext(aspath[i-1])
}
asm.nodes[aspath[i]] = node
}
}
func (asm *ASMap) ToDotFile(w io.Writer) error {
graphTmpl := "digraph ASMap {\n %s \n\n %s \n}"
nodeTmpl := "%d %s; // Appeared: %d\n"
nodes := ""
edges := ""
for k, v := range asm.nodes {
nodes += fmt.Sprintf(nodeTmpl, k, v.GetDotAttributres(), v.ct)
asList := "{"
for _, as := range v.next {
asList = fmt.Sprintf("%s %d", asList, as)
}
asList += "}"
edges += fmt.Sprintf("%d -> %s;\n", k, asList)
}
graph := fmt.Sprintf(graphTmpl, nodes, edges)
_, err := w.Write([]byte(graph))
return err
}
type ASMapFormatter struct {
output io.Writer
asMap *ASMap
pathC chan []uint32
wg *sync.WaitGroup
}
func NewASMapFormatter(fd io.Writer) *ASMapFormatter {
asmf := &ASMapFormatter{output: fd}
asmf.asMap = NewASMap()
asmf.pathC = make(chan []uint32, 32)
asmf.wg = &sync.WaitGroup{}
asmf.wg.Add(1)
go asmf.processPaths()
return asmf
}
func (asmf *ASMapFormatter) format(mbs *mrt.MrtBufferStack, _ MBSInfo) (string, error) {
asp, err := mrt.GetASPath(mbs)
if err != nil || len(asp) == 0 {
return "", nil
}
asmf.pathC <- asp
return "", nil
}
func (asmf *ASMapFormatter) processPaths() {
defer asmf.wg.Done()
for path := range asmf.pathC {
asmf.asMap.AddPath(path)
}
}
func (asmf *ASMapFormatter) summarize() {
close(asmf.pathC)
asmf.wg.Wait()
asmf.asMap.ToDotFile(asmf.output)
}