-
Notifications
You must be signed in to change notification settings - Fork 11
/
packet.go
558 lines (515 loc) · 13.3 KB
/
packet.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
package corebgp
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"math"
"net/netip"
"time"
)
const (
openMessageType = 1
updateMessageType = 2
notificationMessageType = 3
keepAliveMessageType = 4
)
type message interface {
messageType() uint8
}
const (
headerLength = 19
)
func messageFromBytes(b []byte, messageType uint8) (message, error) {
switch messageType {
case openMessageType:
o := &openMessage{}
err := o.decode(b)
if err != nil {
return nil, err
}
return o, nil
case updateMessageType:
u := make([]byte, len(b))
copy(u, b)
return updateMessage(u), nil
case notificationMessageType:
n := &Notification{}
err := n.decode(b)
if err != nil {
return nil, err
}
return n, nil
case keepAliveMessageType:
k := &keepAliveMessage{}
return k, nil
default:
badType := make([]byte, 1)
badType[0] = messageType
n := newNotification(NOTIF_CODE_MESSAGE_HEADER_ERR,
NOTIF_SUBCODE_BAD_MESSAGE_TYPE, badType)
return nil, newNotificationError(n, true)
}
}
func prependHeader(m []byte, t uint8) []byte {
b := make([]byte, headerLength)
for i := 0; i < 16; i++ {
b[i] = 0xFF
}
msgLen := uint16(len(m) + headerLength)
binary.BigEndian.PutUint16(b[16:], msgLen)
b[18] = t
b = append(b, m...)
return b
}
type AddPathTuple struct {
AFI uint16
SAFI uint8
Tx bool
Rx bool
}
func DecodeAddPathTuples(b []byte) ([]AddPathTuple, error) {
if len(b) == 0 || len(b)%4 != 0 {
return nil, &Notification{
Code: NOTIF_CODE_OPEN_MESSAGE_ERR,
}
}
tuples := make([]AddPathTuple, 0, len(b)/4)
for len(b) > 0 {
var a AddPathTuple
err := a.Decode(b)
if err != nil {
return nil, err
}
tuples = append(tuples, a)
b = b[4:]
}
return tuples, nil
}
func (a *AddPathTuple) Decode(b []byte) error {
if len(b) < 4 {
return &Notification{
Code: NOTIF_CODE_OPEN_MESSAGE_ERR,
}
}
a.AFI = binary.BigEndian.Uint16(b)
a.SAFI = b[2]
switch b[3] {
case 3:
a.Tx = true
a.Rx = true
case 2:
a.Tx = true
case 1:
a.Rx = true
default:
return &Notification{
Code: NOTIF_CODE_OPEN_MESSAGE_ERR,
}
}
return nil
}
func (a *AddPathTuple) Encode() []byte {
b := make([]byte, 4)
binary.BigEndian.PutUint16(b, a.AFI)
b[2] = a.SAFI
switch {
// https://www.rfc-editor.org/rfc/rfc7911#page-4
// Send/Receive:
// This field indicates whether the sender is (a) able to receive
// multiple paths from its peer (value 1), (b) able to send
// multiple paths to its peer (value 2), or (c) both (value 3) for
// the <AFI, SAFI>.
case a.Tx && a.Rx:
b[3] = 3
case a.Tx:
b[3] = 2
case a.Rx:
b[3] = 1
}
return b
}
// NewAddPathCapability returns an add-path Capability for the provided
// AddPathTuples.
func NewAddPathCapability(tuples []AddPathTuple) Capability {
value := make([]byte, 0, 4*len(tuples))
for _, tuple := range tuples {
value = append(value, tuple.Encode()...)
}
return Capability{
Code: CAP_ADD_PATH,
Value: value,
}
}
// NewMPExtensionsCapability returns a Multiprotocol Extensions Capability for
// the provided AFI and SAFI.
func NewMPExtensionsCapability(afi uint16, safi uint8) Capability {
mpData := make([]byte, 4)
binary.BigEndian.PutUint16(mpData, afi)
mpData[3] = safi
return Capability{
Code: CAP_MP_EXTENSIONS,
Value: mpData,
}
}
// Notification is a Notification message.
type Notification struct {
Code uint8
Subcode uint8
Data []byte
}
func newNotification(code, subcode uint8, data []byte) *Notification {
return &Notification{
Code: code,
Subcode: subcode,
Data: data,
}
}
func (n *Notification) messageType() uint8 {
return notificationMessageType
}
func (n *Notification) decode(b []byte) error {
/*
If a peer sends a NOTIFICATION message, and the receiver of the
message detects an error in that message, the receiver cannot use a
NOTIFICATION message to report this error back to the peer. Any such
error (e.g., an unrecognized Error Code or Error Subcode) SHOULD be
noticed, logged locally, and brought to the attention of the
administration of the peer. The means to do this, however, lies
outside the scope of this document.
*/
if len(b) < 2 {
return errors.New("notification message too short")
}
n.Code = b[0]
n.Subcode = b[1]
if len(b) > 2 {
n.Data = make([]byte, len(b)-2)
copy(n.Data, b[2:])
}
return nil
}
func (n *Notification) encode() ([]byte, error) {
b := make([]byte, 2)
b[0] = n.Code
b[1] = n.Subcode
if len(n.Data) > 1 {
b = append(b, n.Data...)
}
return prependHeader(b, notificationMessageType), nil
}
func (n *Notification) Error() string {
var codeDesc, subcodeDesc string
d, ok := notifCodesMap[n.Code]
if ok {
codeDesc = d.desc
s, ok := d.subcodes[n.Subcode]
if ok {
subcodeDesc = s
}
}
return fmt.Sprintf("notification code:%d (%s) subcode:%d (%s)",
n.Code, codeDesc, n.Subcode, subcodeDesc)
}
func (n *Notification) AsSessionReset() *Notification {
return n
}
type openMessage struct {
version uint8
asn uint16
holdTime uint16
bgpID uint32
optionalParams []optionalParam
}
func (o *openMessage) messageType() uint8 {
return openMessageType
}
// https://tools.ietf.org/html/rfc4271#section-6.2
func (o *openMessage) validate(localID, localAS, remoteAS uint32) error {
if o.version != 4 {
version := make([]byte, 2)
binary.BigEndian.PutUint16(version, uint16(4))
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_UNSUPPORTED_VERSION_NUM, version)
return newNotificationError(n, true)
}
var fourOctetAS, fourOctetASFound bool
if o.asn == asTrans {
fourOctetAS = true
} else if uint32(o.asn) != remoteAS {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_BAD_PEER_AS, nil)
return newNotificationError(n, true)
}
if o.holdTime < 3 && o.holdTime != 0 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_UNACCEPTABLE_HOLD_TIME, nil)
return newNotificationError(n, true)
}
var id [4]byte
binary.BigEndian.PutUint32(id[:], o.bgpID)
addr := netip.AddrFrom4(id)
if addr.IsMulticast() {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_BAD_BGP_ID, nil)
return newNotificationError(n, true)
}
// https://tools.ietf.org/html/rfc6286#section-2.2
if localAS == remoteAS && localID == o.bgpID {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_BAD_BGP_ID, nil)
return newNotificationError(n, true)
}
caps := o.getCapabilities()
for _, c := range caps {
if c.Code == CAP_FOUR_OCTET_AS {
fourOctetASFound = true
if len(c.Value) != 4 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return newNotificationError(n, true)
}
if binary.BigEndian.Uint32(c.Value) != remoteAS {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_BAD_PEER_AS, nil)
return newNotificationError(n, true)
}
}
}
if fourOctetAS && !fourOctetASFound {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_BAD_PEER_AS, nil)
return newNotificationError(n, true)
} else if !fourOctetASFound {
// corebgp requires four-octet ASN space support
//
// https://www.rfc-editor.org/rfc/rfc5492.html#section-5
// This document defines a new Error Subcode, Unsupported Capability.
// The value of this Subcode is 7. The Data field in the NOTIFICATION
// message MUST list the set of capabilities that causes the speaker to
// send the message. Each such capability is encoded in the same way as
// it would be encoded in the OPEN message.
//
// As explained in the "Overview of Operations" section, the Unsupported
// Capability NOTIFICATION is a way for a BGP speaker to complain that
// its peer does not support a required capability without which the
// peering cannot proceed. It MUST NOT be used when a BGP speaker
// receives a capability that it does not understand; such capabilities
// MUST be ignored.
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_UNSUPPORTED_CAPABILITY, newFourOctetASCap(remoteAS).encode())
return newNotificationError(n, true)
}
return nil
}
func (o *openMessage) getCapabilities() []Capability {
caps := make([]Capability, 0)
for _, param := range o.optionalParams {
p, isCap := param.(*capabilityOptionalParam)
if isCap {
caps = append(caps, p.capabilities...)
}
}
return caps
}
func (o *openMessage) decode(b []byte) error {
if len(b) < 10 {
n := newNotification(NOTIF_CODE_MESSAGE_HEADER_ERR,
NOTIF_SUBCODE_BAD_MESSAGE_LEN, b)
return newNotificationError(n, true)
}
o.version = b[0]
o.asn = binary.BigEndian.Uint16(b[1:3])
o.holdTime = binary.BigEndian.Uint16(b[3:5])
o.bgpID = binary.BigEndian.Uint32(b[5:9])
optionalParamsLen := int(b[9])
if optionalParamsLen != len(b)-10 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return newNotificationError(n, true)
}
optionalParams, err := decodeOptionalParams(b[10:])
if err != nil {
return err
}
o.optionalParams = optionalParams
return nil
}
func decodeOptionalParams(b []byte) ([]optionalParam, error) {
params := make([]optionalParam, 0)
for {
if len(b) < 2 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return nil, newNotificationError(n, true)
}
paramCode := b[0]
paramLen := b[1]
if len(b) < int(paramLen)+2 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return nil, newNotificationError(n, true)
}
paramToDecode := make([]byte, 0)
if paramLen > 0 {
paramToDecode = b[2 : paramLen+2]
}
nextParam := 2 + int(paramLen)
b = b[nextParam:]
switch paramCode {
case capabilityOptionalParamType:
c := &capabilityOptionalParam{}
err := c.decode(paramToDecode)
if err != nil {
return nil, err
}
params = append(params, c)
default:
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR,
NOTIF_SUBCODE_UNSUPPORTED_OPTIONAL_PARAM, nil)
return nil, newNotificationError(n, true)
}
if len(b) == 0 {
break
}
}
return params, nil
}
func (o *openMessage) encode() ([]byte, error) {
b := make([]byte, 9)
b[0] = o.version
binary.BigEndian.PutUint16(b[1:3], o.asn)
binary.BigEndian.PutUint16(b[3:5], o.holdTime)
binary.BigEndian.PutUint32(b[5:9], o.bgpID)
params := make([]byte, 0)
for _, param := range o.optionalParams {
p, err := param.encode()
if err != nil {
return nil, err
}
params = append(params, p...)
}
b = append(b, uint8(len(params)))
b = append(b, params...)
return prependHeader(b, openMessageType), nil
}
const (
asTrans uint16 = 23456
)
func newFourOctetASCap(asn uint32) Capability {
c := Capability{
Code: CAP_FOUR_OCTET_AS,
Value: make([]byte, 4),
}
binary.BigEndian.PutUint32(c.Value, asn)
return c
}
func newOpenMessage(asn uint32, holdTime time.Duration, bgpID uint32,
caps []Capability) (*openMessage, error) {
allCaps := make([]Capability, 0, len(caps)+1)
allCaps = append(allCaps, newFourOctetASCap(asn))
for _, c := range caps {
// ignore four octet as capability as we include this implicitly above
if c.Code != CAP_FOUR_OCTET_AS {
allCaps = append(allCaps, c)
}
}
o := &openMessage{
version: 4,
holdTime: uint16(holdTime.Truncate(time.Second).Seconds()),
bgpID: bgpID,
optionalParams: []optionalParam{
&capabilityOptionalParam{
capabilities: allCaps,
},
},
}
if asn > math.MaxUint16 {
o.asn = asTrans
} else {
o.asn = uint16(asn)
}
return o, nil
}
const (
capabilityOptionalParamType uint8 = 2
)
type optionalParam interface {
paramType() uint8
encode() ([]byte, error)
decode(b []byte) error
}
type capabilityOptionalParam struct {
capabilities []Capability
}
func (c *capabilityOptionalParam) paramType() uint8 {
return capabilityOptionalParamType
}
func (c *capabilityOptionalParam) decode(b []byte) error {
for {
if len(b) < 2 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return newNotificationError(n, true)
}
capCode := b[0]
capLen := b[1]
if len(b) < int(capLen)+2 {
n := newNotification(NOTIF_CODE_OPEN_MESSAGE_ERR, 0, nil)
return newNotificationError(n, true)
}
capValue := make([]byte, 0)
if capLen > 0 {
capValue = b[2 : capLen+2]
}
capability := Capability{
Code: capCode,
Value: capValue,
}
c.capabilities = append(c.capabilities, capability)
nextCap := 2 + int(capLen)
b = b[nextCap:]
if len(b) == 0 {
return nil
}
}
}
func (c *capabilityOptionalParam) encode() ([]byte, error) {
b := make([]byte, 0)
caps := make([]byte, 0)
if len(c.capabilities) > 0 {
for _, capability := range c.capabilities {
caps = append(caps, capability.encode()...)
}
} else {
return nil, errors.New("empty capabilities in capability optional param")
}
b = append(b, capabilityOptionalParamType)
b = append(b, uint8(len(caps)))
b = append(b, caps...)
return b, nil
}
// Capability is a BGP capability as defined by RFC5492.
type Capability struct {
Code uint8
Value []byte
}
func (c Capability) Equal(d Capability) bool {
if c.Code != d.Code {
return false
}
return bytes.Equal(c.Value, d.Value)
}
func (c Capability) encode() []byte {
b := make([]byte, 2+len(c.Value))
b[0] = c.Code
b[1] = uint8(len(c.Value))
copy(b[2:], c.Value)
return b
}
type updateMessage []byte
func (u updateMessage) messageType() uint8 {
return updateMessageType
}
type keepAliveMessage struct{}
func (k keepAliveMessage) messageType() uint8 {
return keepAliveMessageType
}
func (k keepAliveMessage) encode() ([]byte, error) {
return prependHeader(nil, keepAliveMessageType), nil
}