forked from metallb/metallb
/
messages.go
426 lines (384 loc) · 9.45 KB
/
messages.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
package bgp
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"net"
"time"
)
func sendOpen(w io.Writer, asn uint32, routerID net.IP, holdTime time.Duration) error {
if routerID.To4() == nil {
panic("non-ipv4 address used as RouterID")
}
msg := struct {
// Header
Marker1, Marker2 uint64
Len uint16
Type uint8
// OPEN
Version uint8
ASN16 uint16
HoldTime uint16
RouterID [4]byte
// Options (we only send one, capabilities)
OptsLen uint8
OptType uint8
OptLen uint8
// Capabilities: multiprotocol extension for IPv4+IPv6
// unicast, and 4-byte ASNs
MP4Type uint8
MP4Len uint8
AFI4 uint16
SAFI4 uint16
MP6Type uint8
MP6Len uint8
AFI6 uint16
SAFI6 uint16
CapType uint8
CapLen uint8
ASN32 uint32
}{
Marker1: 0xffffffffffffffff,
Marker2: 0xffffffffffffffff,
Len: 0, // Filled below
Type: 1, // OPEN
Version: 4,
ASN16: uint16(asn), // Possibly tweaked below
HoldTime: uint16(holdTime.Seconds()),
// RouterID filled below
OptsLen: 20,
OptType: 2, // Capabilities
OptLen: 18,
MP4Type: 1, // BGP Multi-protocol Extensions
MP4Len: 4,
AFI4: 1, // IPv4
SAFI4: 1, // Unicast
MP6Type: 1, // BGP Multi-protocol Extensions
MP6Len: 4,
AFI6: 2, // IPv6
SAFI6: 1, // Unicast
CapType: 65, // 4-byte ASN
CapLen: 4,
ASN32: asn,
}
msg.Len = uint16(binary.Size(msg))
if asn > 65535 {
msg.ASN16 = 23456
}
copy(msg.RouterID[:], routerID.To4())
return binary.Write(w, binary.BigEndian, msg)
}
type openResult struct {
asn uint32
holdTime time.Duration
mp4 bool
mp6 bool
}
var notificationCodes = map[uint16]string{
0x0100: "Message header error (unspecific)",
0x0101: "Connection not synchronized",
0x0102: "Bad message length",
0x0103: "Bad message type",
0x0200: "OPEN message error (unspecific)",
0x0201: "Unsupported version number",
0x0202: "Bad peer AS",
0x0203: "Bad BGP identifier",
0x0204: "Unsupported optional parameter",
0x0206: "Unacceptable hold time",
0x0207: "Unsupported capability",
0x0300: "UPDATE message error (unspecific)",
0x0301: "Malformed Attribute List",
0x0302: "Unrecognized Well-known Attribute",
0x0303: "Missing Well-known Attribute",
0x0304: "Attribute Flags Error",
0x0305: "Attribute Length Error",
0x0306: "Invalid ORIGIN Attribute",
0x0308: "Invalid NEXT_HOP Attribute",
0x0309: "Optional Attribute Error",
0x030a: "Invalid Network Field",
0x030b: "Malformed AS_PATH",
0x0500: "BGP FSM state error (unspecific)",
0x0501: "Receive Unexpected Message in OpenSent State",
0x0502: "Receive Unexpected Message in OpenConfirm State",
0x0503: "Receive Unexpected Message in Established State",
0x0601: "Maximum Number of Prefixes Reached",
0x0602: "Administrative Shutdown",
0x0603: "Peer De-configured",
0x0604: "Administrative Reset",
0x0605: "Connection Rejected",
0x0606: "Other Configuration Change",
0x0607: "Connection Collision Resolution",
0x0608: "Out of Resources",
}
// readNotification reads the body of a notification message (header
// has already been consumed). It must always return an error, because
// receiving a notification is an error.
func readNotification(r io.Reader) error {
var code uint16
if err := binary.Read(r, binary.BigEndian, &code); err != nil {
return err
}
v, ok := notificationCodes[code]
if !ok {
v = "unknown code"
}
return fmt.Errorf("got BGP notification code 0x%04x (%s)", code, v)
}
func readOpen(r io.Reader) (*openResult, error) {
hdr := struct {
// Header
Marker1, Marker2 uint64
Len uint16
Type uint8
}{}
if err := binary.Read(r, binary.BigEndian, &hdr); err != nil {
return nil, err
}
if hdr.Marker1 != 0xffffffffffffffff || hdr.Marker2 != 0xffffffffffffffff {
return nil, fmt.Errorf("synchronization error, incorrect header marker")
}
if hdr.Type == 3 {
return nil, readNotification(r)
}
if hdr.Type != 1 {
return nil, fmt.Errorf("message type is not OPEN, got %d, want 1", hdr.Type)
}
if hdr.Len < 37 {
return nil, fmt.Errorf("message length %d too small to be OPEN", hdr.Len)
}
lr := &io.LimitedReader{
R: r,
N: int64(hdr.Len) - 19,
}
open := struct {
Version uint8
ASN16 uint16
HoldTime uint16
RouterID uint32
OptsLen uint8
}{}
if err := binary.Read(lr, binary.BigEndian, &open); err != nil {
return nil, err
}
fmt.Printf("%#v\n", open)
if open.Version != 4 {
return nil, fmt.Errorf("wrong BGP version")
}
if open.HoldTime != 0 && open.HoldTime < 3 {
return nil, fmt.Errorf("invalid hold time %q, must be 0 or >=3s", open.HoldTime)
}
ret := &openResult{
asn: uint32(open.ASN16),
holdTime: time.Duration(open.HoldTime) * time.Second,
}
if err := readOptions(lr, ret); err != nil {
return nil, err
}
return ret, nil
}
func readOptions(r io.Reader, ret *openResult) error {
for {
hdr := struct {
Type uint8
Len uint8
}{}
if err := binary.Read(r, binary.BigEndian, &hdr); err != nil {
if err == io.EOF {
return nil
}
return err
}
if hdr.Type != 2 {
return fmt.Errorf("unknown BGP option type %d", hdr.Type)
}
lr := &io.LimitedReader{
R: r,
N: int64(hdr.Len),
}
if err := readCapabilities(lr, ret); err != nil {
return err
}
if lr.N != 0 {
return fmt.Errorf("%d trailing garbage bytes after capability option", lr.N)
}
}
}
func readCapabilities(r io.Reader, ret *openResult) error {
for {
cap := struct {
Code uint8
Len uint8
}{}
if err := binary.Read(r, binary.BigEndian, &cap); err != nil {
if err == io.EOF {
return nil
}
return err
}
lr := io.LimitedReader{
R: r,
N: int64(cap.Len),
}
switch cap.Code {
case 65:
if err := binary.Read(&lr, binary.BigEndian, &ret.asn); err != nil {
return err
}
case 1:
af := struct{ AFI, SAFI uint16 }{}
if err := binary.Read(&lr, binary.BigEndian, &af); err != nil {
return err
}
switch {
case af.AFI == 1 && af.SAFI == 1:
ret.mp4 = true
case af.AFI == 2 && af.SAFI == 1:
ret.mp6 = true
}
default:
// TODO: only ignore capabilities that we know are fine to
// ignore.
if _, err := io.Copy(ioutil.Discard, &lr); err != nil {
return err
}
}
if lr.N != 0 {
return fmt.Errorf("%d leftover bytes after decoding capability %d", lr.N, cap.Code)
}
}
}
func sendUpdate(w io.Writer, asn uint32, ibgp bool, defaultNextHop net.IP, adv *Advertisement) error {
var b bytes.Buffer
hdr := struct {
M1, M2 uint64
Len uint16
Type uint8
WdrLen uint16
AttrLen uint16
}{
M1: uint64(0xffffffffffffffff),
M2: uint64(0xffffffffffffffff),
Type: 2,
}
if err := binary.Write(&b, binary.BigEndian, hdr); err != nil {
return err
}
l := b.Len()
if err := encodePathAttrs(&b, asn, ibgp, defaultNextHop, adv); err != nil {
return err
}
binary.BigEndian.PutUint16(b.Bytes()[21:23], uint16(b.Len()-l))
encodePrefixes(&b, []*net.IPNet{adv.Prefix})
binary.BigEndian.PutUint16(b.Bytes()[16:18], uint16(b.Len()))
if _, err := io.Copy(w, &b); err != nil {
return err
}
return nil
}
func encodePrefixes(b *bytes.Buffer, pfxs []*net.IPNet) {
for _, pfx := range pfxs {
o, _ := pfx.Mask.Size()
b.WriteByte(byte(o))
b.Write(pfx.IP.To4()[:bytesForBits(o)])
}
}
func bytesForBits(n int) int {
// Evil bit hack that rounds n up to the next multiple of 8, then
// divides by 8. This returns the minimum number of whole bytes
// required to contain n bits.
return ((n + 7) &^ 7) / 8
}
func encodePathAttrs(b *bytes.Buffer, asn uint32, ibgp bool, defaultNextHop net.IP, adv *Advertisement) error {
b.Write([]byte{
0x40, 1, // mandatory, origin
1, // len
2, // incomplete
0x40, 2, // mandatory, as-path
})
if ibgp {
b.WriteByte(0) // empty AS path
} else {
b.Write([]byte{
6, // len
2, // AS_SEQUENCE
1, // len (in number of ASes)
})
if err := binary.Write(b, binary.BigEndian, asn); err != nil {
return err
}
}
b.Write([]byte{
0x40, 3, // mandatory, next-hop
4, // len
})
if adv.NextHop != nil {
b.Write(adv.NextHop.To4())
} else {
b.Write(defaultNextHop)
}
if ibgp {
b.Write([]byte{
0x40, 5, // well-known, localpref
4, // len
})
if err := binary.Write(b, binary.BigEndian, adv.LocalPref); err != nil {
return err
}
}
if len(adv.Communities) > 0 {
b.Write([]byte{
0xc0, 8, // optional transitive, communities
})
if err := binary.Write(b, binary.BigEndian, uint8(len(adv.Communities)*4)); err != nil {
return err
}
for _, c := range adv.Communities {
if err := binary.Write(b, binary.BigEndian, c); err != nil {
return err
}
}
}
return nil
}
func sendWithdraw(w io.Writer, prefixes []*net.IPNet) error {
var b bytes.Buffer
hdr := struct {
M1, M2 uint64
Len uint16
Type uint8
WdrLen uint16
}{
M1: uint64(0xffffffffffffffff),
M2: uint64(0xffffffffffffffff),
Type: 2,
}
if err := binary.Write(&b, binary.BigEndian, hdr); err != nil {
return err
}
l := b.Len()
encodePrefixes(&b, prefixes)
binary.BigEndian.PutUint16(b.Bytes()[19:21], uint16(b.Len()-l))
if err := binary.Write(&b, binary.BigEndian, uint16(0)); err != nil {
return err
}
binary.BigEndian.PutUint16(b.Bytes()[16:18], uint16(b.Len()))
if _, err := io.Copy(w, &b); err != nil {
return err
}
return nil
}
func sendKeepalive(w io.Writer) error {
msg := struct {
Marker1, Marker2 uint64
Len uint16
Type uint8
}{
Marker1: 0xffffffffffffffff,
Marker2: 0xffffffffffffffff,
Len: 19,
Type: 4,
}
return binary.Write(w, binary.BigEndian, msg)
}