forked from kaorimatz/go-mrt
/
mrt.go
327 lines (278 loc) · 6.5 KB
/
mrt.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
package mrt
import (
"bytes"
"io"
"net"
"time"
)
type header struct {
timestamp time.Time
type_ RecordType
subtype uint16
}
func (h *header) Timestamp() time.Time {
return h.timestamp
}
func (h *header) Type() RecordType {
return h.type_
}
func (h *header) Subtype() uint16 {
return h.subtype
}
func (h *header) decodeHeader(d *decoder) error {
h.timestamp = d.unixTime()
h.type_ = RecordType(d.uint16())
h.subtype = d.uint16()
d.skip(4) // Length (4 octets)
if h.type_.HasExtendedTimestamp() {
h.timestamp.Add(time.Duration(d.uint32()) * time.Microsecond)
}
return nil
}
const (
TABLE_DUMP_SUBTYPE_AFI_IPv4 = 1
TABLE_DUMP_SUBTYPE_AFI_IPv6 = 2
)
type TableDump struct {
header
ViewNumber uint16
SequenceNumber uint16
Prefix *net.IPNet
OriginatedTime time.Time
PeerIPAddress net.IP
PeerAS AS
BGPAttributes []*BGPPathAttribute
}
func (r *TableDump) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
r.ViewNumber = d.uint16()
r.SequenceNumber = d.uint16()
if r.subtype == TABLE_DUMP_SUBTYPE_AFI_IPv4 {
ip := d.ipv4()
mask := net.CIDRMask(int(d.uint8()), net.IPv4len*8)
r.Prefix = &net.IPNet{IP: ip, Mask: mask}
} else {
ip := d.ipv6()
mask := net.CIDRMask(int(d.uint8()), net.IPv6len*8)
r.Prefix = &net.IPNet{IP: ip, Mask: mask}
}
d.skip(1) // Status (1 octet)
r.OriginatedTime = d.unixTime()
if r.subtype == TABLE_DUMP_SUBTYPE_AFI_IPv4 {
r.PeerIPAddress = d.ipv4()
} else {
r.PeerIPAddress = d.ipv6()
}
r.PeerAS = d.as2()
attrBytes := d.skip(d.size())
reader := bgpPathAttributeReader{reader: bytes.NewReader(attrBytes), as4: false}
for {
attr, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
r.BGPAttributes = append(r.BGPAttributes, attr)
}
return nil
}
type TableDumpV2PeerIndexTable struct {
header
CollectorBGPID net.IP
ViewName string
PeerEntries []*TableDumpV2PeerEntry
}
func (r *TableDumpV2PeerIndexTable) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
r.CollectorBGPID = d.ipv4()
r.ViewName = d.string(int(d.uint16()))
n := int(d.uint16())
r.PeerEntries = make([]*TableDumpV2PeerEntry, n)
for i := 0; i < n; i++ {
entry := &TableDumpV2PeerEntry{}
entry.PeerType = d.uint8()
entry.PeerBGPID = d.ipv4()
if entry.PeerType&0x1 == 0 {
entry.PeerIPAddress = d.ipv4()
} else {
entry.PeerIPAddress = d.ipv6()
}
if entry.PeerType&0x2 == 0 {
entry.PeerAS = d.as2()
} else {
entry.PeerAS = d.as4()
}
r.PeerEntries[i] = entry
}
return nil
}
type TableDumpV2PeerEntry struct {
PeerType uint8
PeerBGPID net.IP
PeerIPAddress net.IP
PeerAS AS
}
type TableDumpV2RIB struct {
header
SequenceNumber uint32
Prefix *net.IPNet
RIBEntries []*TableDumpV2RIBEntry
}
func (r *TableDumpV2RIB) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
r.SequenceNumber = d.uint32()
if r.subtype == TABLE_DUMP_V2_SUBTYPE_RIB_IPv4_UNICAST ||
r.subtype == TABLE_DUMP_V2_SUBTYPE_RIB_IPv4_MULTICAST ||
r.subtype == TABLE_DUMP_V2_SUBTYPE_RIB_IPv4_UNICAST_ADDPATH ||
r.subtype == TABLE_DUMP_V2_SUBTYPE_RIB_IPv4_MULTICAST_ADDPATH {
r.Prefix = d.nlriIPv4()
} else {
r.Prefix = d.nlriIPv6()
}
n := int(d.uint16())
r.RIBEntries = make([]*TableDumpV2RIBEntry, n)
for i := 0; i < n; i++ {
entry := &TableDumpV2RIBEntry{}
entry.PeerIndex = d.uint16()
entry.OriginatedTime = d.unixTime()
if r.subtype >= TABLE_DUMP_V2_SUBTYPE_RIB_IPv4_UNICAST_ADDPATH && r.subtype <= TABLE_DUMP_V2_SUBTYPE_RIB_GENERIC_ADDPATH {
entry.PathIdentifier = d.uint32()
}
attrBytes := d.skip(int(d.uint16()))
reader := bgpPathAttributeReader{reader: bytes.NewReader(attrBytes), as4: true}
for {
attr, err := reader.Next()
if err == io.EOF {
break
}
if err != nil {
return err
}
entry.BGPAttributes = append(entry.BGPAttributes, attr)
}
r.RIBEntries[i] = entry
}
return nil
}
type AFI uint16
const (
AFIIPv4 AFI = 1
AFIIPv6 = 2
)
type SAFI uint8
const (
SAFIUnicast SAFI = 1
SAFIMulticast = 2
)
type TableDumpV2RIBGeneric struct {
header
SequenceNumber uint32
AFI AFI
SAFI SAFI
NLRI []byte
RIBEntries []*TableDumpV2RIBEntry
}
func (r *TableDumpV2RIBGeneric) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
r.SequenceNumber = d.uint32()
r.AFI = AFI(d.uint16())
r.SAFI = SAFI(d.uint8())
// r.NLRI
// r.RIBEntries
return nil
}
type TableDumpV2RIBEntry struct {
PeerIndex uint16
OriginatedTime time.Time
PathIdentifier uint32
BGPAttributes []*BGPPathAttribute
}
const (
BGP4MP_SUBTYPE_BGP4MP_STATE_CHANGE = 0
BGP4MP_SUBTYPE_BGP4MP_MESSAGE = 1
BGP4MP_SUBTYPE_BGP4MP_MESSAGE_AS4 = 4
BGP4MP_SUBTYPE_BGP4MP_STATE_CHANGE_AS4 = 5
BGP4MP_SUBTYPE_BGP4MP_MESSAGE_LOCAL = 6
BGP4MP_SUBTYPE_BGP4MP_MESSAGE_AS4_LOCAL = 7
)
type BGP4MPStateChange struct {
header
PeerAS AS
LocalAS AS
InterfaceIndex uint16
AFI AFI
PeerIPAddress net.IP
LocalIPAddress net.IP
OldState uint16
NewState uint16
}
func (r *BGP4MPStateChange) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
if r.subtype == BGP4MP_SUBTYPE_BGP4MP_STATE_CHANGE {
r.PeerAS = d.as2()
r.LocalAS = d.as2()
} else {
r.PeerAS = d.as4()
r.LocalAS = d.as4()
}
r.InterfaceIndex = d.uint16()
r.AFI = AFI(d.uint16())
if r.AFI == AFIIPv4 {
r.PeerIPAddress = d.ipv4()
r.LocalIPAddress = d.ipv4()
} else {
r.PeerIPAddress = d.ipv6()
r.LocalIPAddress = d.ipv6()
}
r.OldState = d.uint16()
r.NewState = d.uint16()
return nil
}
type BGP4MPMessage struct {
header
PeerAS AS
LocalAS AS
InterfaceIndex uint16
AFI AFI
PeerIPAddress net.IP
LocalIPAddress net.IP
BGPMessage *BGPMessage
}
func (r *BGP4MPMessage) DecodeBytes(data []byte) error {
d := &decoder{data}
r.decodeHeader(d)
as4 := r.subtype == BGP4MP_SUBTYPE_BGP4MP_MESSAGE_AS4 ||
r.subtype == BGP4MP_SUBTYPE_BGP4MP_MESSAGE_AS4_LOCAL
if as4 {
r.PeerAS = d.as4()
r.LocalAS = d.as4()
} else {
r.PeerAS = d.as2()
r.LocalAS = d.as2()
}
r.InterfaceIndex = d.uint16()
r.AFI = AFI(d.uint16())
if r.AFI == AFIIPv4 {
r.PeerIPAddress = d.ipv4()
r.LocalIPAddress = d.ipv4()
} else {
r.PeerIPAddress = d.ipv6()
r.LocalIPAddress = d.ipv6()
}
var err error
r.BGPMessage, err = decodeBGPMessage(d.skip(d.size()), as4, r.AFI)
return err
}
type Record interface {
Timestamp() time.Time
Type() RecordType
Subtype() uint16
DecodeBytes([]byte) error
}