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
|
// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation.
//
// 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 bgp
import (
"encoding/binary"
"fmt"
"math"
"net"
)
type BMPHeader struct {
Version uint8
Length uint32
Type uint8
}
const (
BMP_VERSION = 3
BMP_HEADER_SIZE = 6
BMP_PEER_HEADER_SIZE = 42
)
const (
BMP_DEFAULT_PORT = 11019
)
const (
BMP_PEER_TYPE_GLOBAL = iota
BMP_PEER_TYPE_L3VPN
)
func (h *BMPHeader) DecodeFromBytes(data []byte) error {
h.Version = data[0]
if data[0] != 3 {
return fmt.Errorf("error version")
}
h.Length = binary.BigEndian.Uint32(data[1:5])
h.Type = data[5]
return nil
}
func (h *BMPHeader) Serialize() ([]byte, error) {
buf := make([]byte, BMP_HEADER_SIZE)
buf[0] = h.Version
binary.BigEndian.PutUint32(buf[1:], h.Length)
buf[5] = h.Type
return buf, nil
}
type BMPPeerHeader struct {
PeerType uint8
IsPostPolicy bool
PeerDistinguisher uint64
PeerAddress net.IP
PeerAS uint32
PeerBGPID net.IP
Timestamp float64
flags uint8
}
func NewBMPPeerHeader(t uint8, policy bool, dist uint64, address string, as uint32, id string, stamp float64) *BMPPeerHeader {
h := &BMPPeerHeader{
PeerType: t,
IsPostPolicy: policy,
PeerDistinguisher: dist,
PeerAS: as,
PeerBGPID: net.ParseIP(id).To4(),
Timestamp: stamp,
}
if policy == true {
h.flags |= (1 << 6)
}
if net.ParseIP(address).To4() != nil {
h.PeerAddress = net.ParseIP(address).To4()
} else {
h.PeerAddress = net.ParseIP(address).To16()
h.flags |= (1 << 7)
}
return h
}
func (h *BMPPeerHeader) DecodeFromBytes(data []byte) error {
h.PeerType = data[0]
h.flags = data[1]
if h.flags&(1<<6) != 0 {
h.IsPostPolicy = true
} else {
h.IsPostPolicy = false
}
h.PeerDistinguisher = binary.BigEndian.Uint64(data[2:10])
if h.flags&(1<<7) != 0 {
h.PeerAddress = net.IP(data[10:26]).To16()
} else {
h.PeerAddress = net.IP(data[10:14]).To4()
}
h.PeerAS = binary.BigEndian.Uint32(data[26:30])
h.PeerBGPID = data[30:34]
timestamp1 := binary.BigEndian.Uint32(data[34:38])
timestamp2 := binary.BigEndian.Uint32(data[38:42])
h.Timestamp = float64(timestamp1) + float64(timestamp2)*math.Pow10(-6)
return nil
}
func (h *BMPPeerHeader) Serialize() ([]byte, error) {
buf := make([]byte, BMP_PEER_HEADER_SIZE)
buf[0] = h.PeerType
buf[1] = h.flags
binary.BigEndian.PutUint64(buf[2:10], h.PeerDistinguisher)
if h.flags&(1<<7) != 0 {
copy(buf[10:26], h.PeerAddress)
} else {
copy(buf[10:14], h.PeerAddress.To4())
}
binary.BigEndian.PutUint32(buf[26:30], h.PeerAS)
copy(buf[30:34], h.PeerBGPID)
t1, t2 := math.Modf(h.Timestamp)
t2 = math.Ceil(t2 * math.Pow10(6))
binary.BigEndian.PutUint32(buf[34:38], uint32(t1))
binary.BigEndian.PutUint32(buf[38:42], uint32(t2))
return buf, nil
}
type BMPRouteMonitoring struct {
BGPUpdate *BGPMessage
}
func NewBMPRouteMonitoring(p BMPPeerHeader, update *BGPMessage) *BMPMessage {
return &BMPMessage{
Header: BMPHeader{
Version: BMP_VERSION,
Type: BMP_MSG_ROUTE_MONITORING,
},
PeerHeader: p,
Body: &BMPRouteMonitoring{
BGPUpdate: update,
},
}
}
func (body *BMPRouteMonitoring) ParseBody(msg *BMPMessage, data []byte) error {
update, err := ParseBGPMessage(data)
if err != nil {
return err
}
body.BGPUpdate = update
return nil
}
func (body *BMPRouteMonitoring) Serialize() ([]byte, error) {
return body.BGPUpdate.Serialize()
}
const (
BMP_STAT_TYPE_REJECTED = iota
BMP_STAT_TYPE_DUPLICATE_PREFIX
BMP_STAT_TYPE_DUPLICATE_WITHDRAW
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_CLUSTER_LIST_LOOP
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_PATH_LOOP
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_ORIGINATOR_ID
BMP_STAT_TYPE_INV_UPDATE_DUE_TO_AS_CONFED_LOOP
BMP_STAT_TYPE_ADJ_RIB_IN
BMP_STAT_TYPE_LOC_RIB
)
type BMPStatsTLV struct {
Type uint16
Length uint16
Value uint64
}
type BMPStatisticsReport struct {
Count uint32
Stats []BMPStatsTLV
}
const (
BMP_PEER_DOWN_REASON_UNKNOWN = iota
BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION
BMP_PEER_DOWN_REASON_LOCAL_NO_NOTIFICATION
BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION
BMP_PEER_DOWN_REASON_REMOTE_NO_NOTIFICATION
)
type BMPPeerDownNotification struct {
Reason uint8
BGPNotification *BGPMessage
Data []byte
}
func NewBMPPeerDownNotification(p BMPPeerHeader, reason uint8, notification *BGPMessage, data []byte) *BMPMessage {
b := &BMPPeerDownNotification{
Reason: reason,
}
switch reason {
case BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION, BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION:
b.BGPNotification = notification
default:
b.Data = data
}
return &BMPMessage{
Header: BMPHeader{
Version: BMP_VERSION,
Type: BMP_MSG_PEER_DOWN_NOTIFICATION,
},
PeerHeader: p,
Body: b,
}
}
func (body *BMPPeerDownNotification) ParseBody(msg *BMPMessage, data []byte) error {
body.Reason = data[0]
data = data[1:]
if body.Reason == BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION || body.Reason == BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION {
notification, err := ParseBGPMessage(data)
if err != nil {
return err
}
body.BGPNotification = notification
} else {
body.Data = data
}
return nil
}
func (body *BMPPeerDownNotification) Serialize() ([]byte, error) {
buf := make([]byte, 1)
buf[0] = body.Reason
switch body.Reason {
case BMP_PEER_DOWN_REASON_LOCAL_BGP_NOTIFICATION, BMP_PEER_DOWN_REASON_REMOTE_BGP_NOTIFICATION:
if body.BGPNotification != nil {
b, err := body.BGPNotification.Serialize()
if err != nil {
return nil, err
} else {
buf = append(buf, b...)
}
}
default:
if body.Data != nil {
buf = append(buf, body.Data...)
}
}
return buf, nil
}
type BMPPeerUpNotification struct {
LocalAddress net.IP
LocalPort uint16
RemotePort uint16
SentOpenMsg *BGPMessage
ReceivedOpenMsg *BGPMessage
}
func NewBMPPeerUpNotification(p BMPPeerHeader, lAddr string, lPort, rPort uint16, sent, recv *BGPMessage) *BMPMessage {
b := &BMPPeerUpNotification{
LocalPort: lPort,
RemotePort: rPort,
SentOpenMsg: sent,
ReceivedOpenMsg: recv,
}
addr := net.ParseIP(lAddr)
if addr.To4() != nil {
b.LocalAddress = addr.To4()
} else {
b.LocalAddress = addr.To16()
}
return &BMPMessage{
Header: BMPHeader{
Version: BMP_VERSION,
Type: BMP_MSG_PEER_UP_NOTIFICATION,
},
PeerHeader: p,
Body: b,
}
}
func (body *BMPPeerUpNotification) ParseBody(msg *BMPMessage, data []byte) error {
if msg.PeerHeader.flags&(1<<7) != 0 {
body.LocalAddress = net.IP(data[:16]).To16()
} else {
body.LocalAddress = net.IP(data[:4]).To4()
}
body.LocalPort = binary.BigEndian.Uint16(data[16:18])
body.RemotePort = binary.BigEndian.Uint16(data[18:20])
data = data[20:]
sentopen, err := ParseBGPMessage(data)
if err != nil {
return err
}
body.SentOpenMsg = sentopen
data = data[body.SentOpenMsg.Header.Len:]
body.ReceivedOpenMsg, err = ParseBGPMessage(data)
if err != nil {
return err
}
return nil
}
func (body *BMPPeerUpNotification) Serialize() ([]byte, error) {
buf := make([]byte, 20)
if body.LocalAddress.To4() != nil {
copy(buf[:4], body.LocalAddress.To4())
} else {
copy(buf[:16], body.LocalAddress.To16())
}
binary.BigEndian.PutUint16(buf[16:18], body.LocalPort)
binary.BigEndian.PutUint16(buf[18:20], body.RemotePort)
m, _ := body.SentOpenMsg.Serialize()
buf = append(buf, m...)
m, _ = body.ReceivedOpenMsg.Serialize()
buf = append(buf, m...)
return buf, nil
}
func (body *BMPStatisticsReport) ParseBody(msg *BMPMessage, data []byte) error {
body.Count = binary.BigEndian.Uint32(data[0:4])
data = data[4:]
for len(data) >= 4 {
s := BMPStatsTLV{}
s.Type = binary.BigEndian.Uint16(data[0:2])
s.Length = binary.BigEndian.Uint16(data[2:4])
data = data[4:]
if len(data) < int(s.Length) {
break
}
if s.Type == BMP_STAT_TYPE_ADJ_RIB_IN || s.Type == BMP_STAT_TYPE_LOC_RIB {
if s.Length < 8 {
break
}
s.Value = binary.BigEndian.Uint64(data[:8])
} else {
if s.Length < 4 {
break
}
s.Value = uint64(binary.BigEndian.Uint32(data[:4]))
}
body.Stats = append(body.Stats, s)
data = data[s.Length:]
}
return nil
}
func (body *BMPStatisticsReport) Serialize() ([]byte, error) {
// TODO
buf := make([]byte, 4)
body.Count = uint32(len(body.Stats))
binary.BigEndian.PutUint32(buf[0:4], body.Count)
return buf, nil
}
type BMPTLV struct {
Type uint16
Length uint16
Value []byte
}
func NewBMPTLV(t uint16, v []byte) *BMPTLV {
return &BMPTLV{
Type: t,
Length: uint16(len(v)),
Value: v,
}
}
func (tlv *BMPTLV) DecodeFromBytes(data []byte) error {
//TODO: check data length
tlv.Type = binary.BigEndian.Uint16(data[0:2])
tlv.Length = binary.BigEndian.Uint16(data[2:4])
tlv.Value = data[4 : 4+tlv.Length]
return nil
}
func (tlv *BMPTLV) Serialize() ([]byte, error) {
if tlv.Length == 0 {
tlv.Length = uint16(len(tlv.Value))
}
buf := make([]byte, 4+tlv.Length)
binary.BigEndian.PutUint16(buf[0:2], tlv.Type)
binary.BigEndian.PutUint16(buf[2:4], tlv.Length)
copy(buf[4:], tlv.Value)
return buf, nil
}
func (tlv *BMPTLV) Len() int {
return 4 + int(tlv.Length)
}
type BMPInitiation struct {
Info []BMPTLV
}
func NewBMPInitiation(info []BMPTLV) *BMPMessage {
return &BMPMessage{
Header: BMPHeader{
Version: BMP_VERSION,
Type: BMP_MSG_INITIATION,
},
Body: &BMPInitiation{
Info: info,
},
}
}
func (body *BMPInitiation) ParseBody(msg *BMPMessage, data []byte) error {
for len(data) > 0 {
tlv := BMPTLV{}
tlv.DecodeFromBytes(data)
body.Info = append(body.Info, tlv)
data = data[tlv.Len():]
}
return nil
}
func (body *BMPInitiation) Serialize() ([]byte, error) {
buf := make([]byte, 0)
for _, tlv := range body.Info {
b, err := tlv.Serialize()
if err != nil {
return buf, err
}
buf = append(buf, b...)
}
return buf, nil
}
type BMPTermination struct {
Info []BMPTLV
}
func NewBMPTermination(info []BMPTLV) *BMPMessage {
return &BMPMessage{
Header: BMPHeader{
Version: BMP_VERSION,
Type: BMP_MSG_TERMINATION,
},
Body: &BMPTermination{
Info: info,
},
}
}
func (body *BMPTermination) ParseBody(msg *BMPMessage, data []byte) error {
for len(data) > 0 {
tlv := BMPTLV{}
tlv.DecodeFromBytes(data)
body.Info = append(body.Info, tlv)
data = data[tlv.Len():]
}
return nil
}
func (body *BMPTermination) Serialize() ([]byte, error) {
buf := make([]byte, 0)
for _, tlv := range body.Info {
b, err := tlv.Serialize()
if err != nil {
return buf, err
}
buf = append(buf, b...)
}
return buf, nil
}
type BMPBody interface {
// Sigh, some body messages need a BMPHeader to parse the body
// data so we need to pass BMPHeader (avoid DecodeFromBytes
// function name).
ParseBody(*BMPMessage, []byte) error
Serialize() ([]byte, error)
}
type BMPMessage struct {
Header BMPHeader
PeerHeader BMPPeerHeader
Body BMPBody
}
func (msg *BMPMessage) Serialize() ([]byte, error) {
buf := make([]byte, 0)
if msg.Header.Type != BMP_MSG_INITIATION && msg.Header.Type != BMP_MSG_INITIATION {
p, err := msg.PeerHeader.Serialize()
if err != nil {
return nil, err
}
buf = append(buf, p...)
}
b, err := msg.Body.Serialize()
if err != nil {
return nil, err
}
buf = append(buf, b...)
if msg.Header.Length == 0 {
msg.Header.Length = uint32(BMP_HEADER_SIZE + len(buf))
}
h, err := msg.Header.Serialize()
if err != nil {
return nil, err
}
return append(h, buf...), nil
}
func (msg *BMPMessage) Len() int {
return int(msg.Header.Length)
}
const (
BMP_MSG_ROUTE_MONITORING = iota
BMP_MSG_STATISTICS_REPORT
BMP_MSG_PEER_DOWN_NOTIFICATION
BMP_MSG_PEER_UP_NOTIFICATION
BMP_MSG_INITIATION
BMP_MSG_TERMINATION
)
func ParseBMPMessage(data []byte) (*BMPMessage, error) {
msg := &BMPMessage{}
msg.Header.DecodeFromBytes(data)
data = data[BMP_HEADER_SIZE:msg.Header.Length]
switch msg.Header.Type {
case BMP_MSG_ROUTE_MONITORING:
msg.Body = &BMPRouteMonitoring{}
case BMP_MSG_STATISTICS_REPORT:
msg.Body = &BMPStatisticsReport{}
case BMP_MSG_PEER_DOWN_NOTIFICATION:
msg.Body = &BMPPeerDownNotification{}
case BMP_MSG_PEER_UP_NOTIFICATION:
msg.Body = &BMPPeerUpNotification{}
case BMP_MSG_INITIATION:
msg.Body = &BMPInitiation{}
case BMP_MSG_TERMINATION:
msg.Body = &BMPTermination{}
}
if msg.Header.Type != BMP_MSG_INITIATION && msg.Header.Type != BMP_MSG_INITIATION {
msg.PeerHeader.DecodeFromBytes(data)
data = data[BMP_PEER_HEADER_SIZE:]
}
err := msg.Body.ParseBody(msg, data)
if err != nil {
return nil, err
}
return msg, nil
}
type MessageError struct {
TypeCode uint8
SubTypeCode uint8
Data []byte
Message string
}
func NewMessageError(typeCode, subTypeCode uint8, data []byte, msg string) error {
return &MessageError{
TypeCode: typeCode,
SubTypeCode: subTypeCode,
Data: data,
Message: msg,
}
}
func (e *MessageError) Error() string {
return e.Message
}
func SplitBMP(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 || len(data) < BMP_HEADER_SIZE {
return 0, nil, nil
}
msg := &BMPMessage{}
msg.Header.DecodeFromBytes(data)
if uint32(len(data)) < msg.Header.Length {
return 0, nil, nil
}
return int(msg.Header.Length), data[0:msg.Header.Length], nil
}
|