Newer
Older
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
package packet
import (
"bytes"
"encoding/binary"
"fmt"
"net"
"github.com/taktv6/tflow2/convert"
)
// Decode decodes a BGP message
func Decode(buf *bytes.Buffer) (*BGPMessage, error) {
hdr, err := decodeHeader(buf)
if err != nil {
return nil, fmt.Errorf("Failed to decode header: %v", err)
}
body, err := decodeMsgBody(buf, hdr.Type, hdr.Length-MinLen)
if err != nil {
return nil, fmt.Errorf("Failed to decode message: %v", err)
}
return &BGPMessage{
Header: hdr,
Body: body,
}, nil
}
func decodeMsgBody(buf *bytes.Buffer, msgType uint8, l uint16) (interface{}, error) {
switch msgType {
case OpenMsg:
return decodeOpenMsg(buf)
case UpdateMsg:
return decodeUpdateMsg(buf, l)
case KeepaliveMsg:
return nil, nil // Nothing to decode in Keepalive message
case NotificationMsg:
return decodeNotificationMsg(buf)
}
return nil, fmt.Errorf("Unknown message type: %d", msgType)
}
func decodeUpdateMsg(buf *bytes.Buffer, l uint16) (*BGPUpdate, error) {
msg := &BGPUpdate{}
err := decode(buf, []interface{}{&msg.WithdrawnRoutesLen})
if err != nil {
return msg, err
}
msg.WithdrawnRoutes, err = decodeNLRIs(buf, uint16(msg.WithdrawnRoutesLen))
if err != nil {
return msg, err
}
err = decode(buf, []interface{}{&msg.TotalPathAttrLen})
if err != nil {
return msg, err
}
msg.PathAttributes, err = decodePathAttrs(buf, msg.TotalPathAttrLen)
if err != nil {
return msg, err
}
nlriLen := uint16(l) - 4 - uint16(msg.TotalPathAttrLen) - uint16(msg.WithdrawnRoutesLen)
if nlriLen > 0 {
msg.NLRI, err = decodeNLRIs(buf, nlriLen)
if err != nil {
return msg, err
}
}
return msg, nil
}
func decodeNotificationMsg(buf *bytes.Buffer) (*BGPNotification, error) {
msg := &BGPNotification{}
fields := []interface{}{
&msg.ErrorCode,
&msg.ErrorSubcode,
}
err := decode(buf, fields)
if err != nil {
return msg, err
}
if msg.ErrorCode > Cease {
return msg, fmt.Errorf("Invalid error code: %d", msg.ErrorSubcode)
}
switch msg.ErrorCode {
case MessageHeaderError:
if msg.ErrorSubcode > BadMessageType || msg.ErrorSubcode == 0 {
return invalidErrCode(msg)
}
case OpenMessageError:
if msg.ErrorSubcode > UnacceptableHoldTime || msg.ErrorSubcode == 0 || msg.ErrorSubcode == DeprecatedOpenMsgError5 {
return invalidErrCode(msg)
}
case UpdateMessageError:
if msg.ErrorSubcode > MalformedASPath || msg.ErrorSubcode == 0 || msg.ErrorSubcode == DeprecatedUpdateMsgError7 {
return invalidErrCode(msg)
}
case HoldTimeExpired:
if msg.ErrorSubcode != 0 {
return invalidErrCode(msg)
}
case FiniteStateMachineError:
if msg.ErrorSubcode != 0 {
return invalidErrCode(msg)
}
case Cease:
if msg.ErrorSubcode != 0 {
return invalidErrCode(msg)
}
default:
return invalidErrCode(msg)
}
return msg, nil
}
func invalidErrCode(n *BGPNotification) (*BGPNotification, error) {
return n, fmt.Errorf("Invalid error sub code: %d/%d", n.ErrorCode, n.ErrorSubcode)
}
func decodeOpenMsg(buf *bytes.Buffer) (*BGPOpen, error) {
msg, err := _decodeOpenMsg(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode OPEN message: %v", err)
}
return msg.(*BGPOpen), err
}
func _decodeOpenMsg(buf *bytes.Buffer) (interface{}, error) {
msg := &BGPOpen{}
fields := []interface{}{
&msg.Version,
&msg.AS,
&msg.HoldTime,
&msg.BGPIdentifier,
&msg.OptParmLen,
}
err := decode(buf, fields)
if err != nil {
return msg, err
}
err = validateOpen(msg)
if err != nil {
return nil, err
}
msg.OptParams, err = decodeOptParams(buf, msg.OptParmLen)
if err != nil {
return nil, fmt.Errorf("Unable to decode optional parameters: %v", err)
}
func decodeOptParams(buf *bytes.Buffer, optParmLen uint8) ([]OptParam, error) {
optParams := make([]OptParam, 0)
read := uint8(0)
for read < optParmLen {
o := OptParam{}
fields := []interface{}{
&o.Type,
&o.Length,
}
err := decode(buf, fields)
if err != nil {
return nil, err
}
read += 2
switch o.Type {
case CapabilitiesParamType:
if err != nil {
return nil, fmt.Errorf("Unable to decode capabilites: %v", err)
optParams = append(optParams, o)
for _, cap := range caps {
read += cap.Length + 2
}
default:
return nil, fmt.Errorf("Unrecognized option: %d", o.Type)
}
}
return optParams, nil
}
func decodeCapabilities(buf *bytes.Buffer, length uint8) (Capabilities, error) {
ret := make(Capabilities, 0)
read := uint8(0)
for read < length {
cap, err := decodeCapability(buf)
if err != nil {
return nil, fmt.Errorf("Unable to decode capability: %v", err)
}
ret = append(ret, cap)
read += cap.Length + 2
}
return ret, nil
}
func decodeCapability(buf *bytes.Buffer) (Capability, error) {
cap := Capability{}
fields := []interface{}{
&cap.Code,
&cap.Length,
}
err := decode(buf, fields)
if err != nil {
return cap, err
}
switch cap.Code {
case AddPathCapabilityCode:
addPathCap, err := decodeAddPathCapability(buf)
if err != nil {
return cap, fmt.Errorf("Unable to decode add path capability")
}
cap.Value = addPathCap
default:
for i := uint8(0); i < cap.Length; i++ {
_, err := buf.ReadByte()
if err != nil {
return cap, fmt.Errorf("Read failed: %v", err)
}
}
}
return cap, nil
}
func decodeAddPathCapability(buf *bytes.Buffer) (AddPathCapability, error) {
addPathCap := AddPathCapability{}
fields := []interface{}{
&addPathCap.AFI,
&addPathCap.SAFI,
&addPathCap.SendReceive,
}
err := decode(buf, fields)
if err != nil {
return addPathCap, err
}
return addPathCap, nil
}
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
func validateOpen(msg *BGPOpen) error {
if msg.Version != BGP4Version {
return BGPError{
ErrorCode: OpenMessageError,
ErrorSubCode: UnsupportedVersionNumber,
ErrorStr: fmt.Sprintf("Unsupported version number"),
}
}
if !isValidIdentifier(msg.BGPIdentifier) {
return BGPError{
ErrorCode: OpenMessageError,
ErrorSubCode: BadBGPIdentifier,
ErrorStr: fmt.Sprintf("Invalid BGP identifier"),
}
}
return nil
}
func isValidIdentifier(id uint32) bool {
addr := net.IP(convert.Uint32Byte(id))
if addr.IsLoopback() {
return false
}
if addr.IsMulticast() {
return false
}
if addr[0] == 0 {
return false
}
if addr[0] == 255 && addr[1] == 255 && addr[2] == 255 && addr[3] == 255 {
return false
}
return true
}
func decodeHeader(buf *bytes.Buffer) (*BGPHeader, error) {
hdr := &BGPHeader{}
marker := make([]byte, MarkerLen)
n, err := buf.Read(marker)
if err != nil {
return hdr, BGPError{
ErrorCode: Cease,
ErrorSubCode: 0,
ErrorStr: fmt.Sprintf("Failed to read from buffer: %v", err.Error()),
}
}
if n != MarkerLen {
return hdr, BGPError{
ErrorCode: Cease,
ErrorSubCode: 0,
ErrorStr: fmt.Sprintf("Unable to read marker"),
}
}
for i := range marker {
if marker[i] != 255 {
return nil, BGPError{
ErrorCode: MessageHeaderError,
ErrorSubCode: ConnectionNotSync,
ErrorStr: fmt.Sprintf("Invalid marker: %v", marker),
}
}
}
fields := []interface{}{
&hdr.Length,
&hdr.Type,
}
err = decode(buf, fields)
if err != nil {
return hdr, BGPError{
ErrorCode: Cease,
ErrorSubCode: 0,
ErrorStr: fmt.Sprintf("%v", err.Error()),
}
}
if hdr.Length < MinLen || hdr.Length > MaxLen {
return hdr, BGPError{
ErrorCode: MessageHeaderError,
ErrorSubCode: BadMessageLength,
ErrorStr: fmt.Sprintf("Invalid length in BGP header: %v", hdr.Length),
}
}
if hdr.Type > KeepaliveMsg || hdr.Type == 0 {
return hdr, BGPError{
ErrorCode: MessageHeaderError,
ErrorSubCode: BadMessageType,
ErrorStr: fmt.Sprintf("Invalid message type: %d", hdr.Type),
}
}
return hdr, nil
}
func decode(buf *bytes.Buffer, fields []interface{}) error {
var err error
for _, field := range fields {
err = binary.Read(buf, binary.BigEndian, field)
if err != nil {
return fmt.Errorf("Unable to read from buffer: %v", err)
}
}
return nil
}