/
rib.go
327 lines (280 loc) · 7.62 KB
/
rib.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 rib
import (
"encoding/binary"
"encoding/json"
"fmt"
pbcom "github.com/CSUNetSec/netsec-protobufs/common"
pbbgp "github.com/CSUNetSec/netsec-protobufs/protocol/bgp"
pp "github.com/CSUNetSec/protoparse"
bgp "github.com/CSUNetSec/protoparse/protocol/bgp"
util "github.com/CSUNetSec/protoparse/util"
"net"
"time"
)
var (
ERR_NOT_IMPLEMENTED = fmt.Errorf("Feature not yet implemented")
)
type ribBuf struct {
dest *pbbgp.RIB
buf []byte
isv6 bool
isIndex bool
index pp.PbVal
}
func NewRibIndexBuf(buf []byte) *ribBuf {
return &ribBuf{
dest: new(pbbgp.RIB),
buf: buf,
isIndex: true,
}
}
func NewRibEntryBuf(buf []byte, subType int, index pp.PbVal) *ribBuf {
return &ribBuf{
dest: new(pbbgp.RIB),
buf: buf,
isIndex: false,
index: index,
isv6: subType == 4 || subType == 5,
}
}
func (r *ribBuf) Parse() (pp.PbVal, error) {
if r.isIndex {
return r.parseIndexTable()
} else {
return r.parseRIB()
}
return nil, nil
}
// This function only parses AFI/SAFI-Specific RIB subtypes
func (r *ribBuf) parseRIB() (pp.PbVal, error) {
if len(r.buf) < 5 {
return nil, fmt.Errorf("rib: Buffer too small to read bitlen")
}
r.buf = r.buf[4:]
bitlen := uint8(r.buf[0])
r.buf = r.buf[1:]
bytelen := int(bitlen+7) / 8
if int(bytelen) > len(r.buf) {
return nil, fmt.Errorf("Buffer too small to parse prefix. Buffer size:%d Prefix Size: %d\n", len(r.buf), bytelen)
}
pbuf := make([]byte, bytelen)
prefWrapper := new(pbcom.PrefixWrapper)
prefWrapper.Prefix = new(pbcom.IPAddressWrapper)
if bytelen != 0 {
copy(pbuf, r.buf[:bytelen])
if bitlen%8 != 0 {
mask := 0xff00 >> (bitlen % 8)
last_byte_value := pbuf[bytelen-1] & byte(mask)
pbuf[bytelen-1] = last_byte_value
}
var IPbuf []byte
if r.isv6 {
IPbuf = make([]byte, 16)
copy(IPbuf, pbuf)
prefWrapper.Prefix.IPv6 = IPbuf
} else {
IPbuf = make([]byte, 4)
copy(IPbuf, pbuf)
prefWrapper.Prefix.IPv4 = IPbuf
}
prefWrapper.Mask = uint32(bitlen)
r.buf = r.buf[bytelen:]
} else {
if r.isv6 {
prefWrapper.Prefix.IPv6 = make([]byte, 16)
prefWrapper.Mask = 0
} else {
prefWrapper.Prefix.IPv4 = make([]byte, 4)
prefWrapper.Mask = 0
}
}
if len(r.buf) < 2 {
return nil, fmt.Errorf("rib: Buffer too small to read entry count")
}
entryCount := int(binary.BigEndian.Uint16(r.buf[:2]))
r.buf = r.buf[2:]
routes := make([]*pbbgp.RIBEntry, entryCount)
for i := 0; i < entryCount; i++ {
re, err := r.parseRIBEntry(prefWrapper)
routes[i] = re
if err != nil {
return nil, fmt.Errorf("Error parsing RIB entries: %s", err)
}
}
r.dest.RouteEntry = routes
return nil, nil
}
func (r *ribBuf) parseRIBEntry(pref *pbcom.PrefixWrapper) (*pbbgp.RIBEntry, error) {
re := new(pbbgp.RIBEntry)
re.Prefix = pref
if len(r.buf) < 8 {
return nil, fmt.Errorf("rib: Buffer too small to parse RIB entry header")
}
re.PeerIndex = uint32(binary.BigEndian.Uint16(r.buf[:2]))
r.buf = r.buf[2:]
re.Timestamp = binary.BigEndian.Uint32(r.buf[:4])
r.buf = r.buf[4:]
attrLen := int(binary.BigEndian.Uint16(r.buf[:2]))
r.buf = r.buf[2:]
if len(r.buf) < attrLen {
return nil, fmt.Errorf("rib: Buffer too small to parse BGP attributes")
}
attrs, err, _, _ := bgp.ParseAttrs(r.buf[:attrLen], true, r.isv6)
r.buf = r.buf[attrLen:]
re.Attrs = attrs
if err != nil {
return nil, err
}
return re, nil
}
func (r *ribBuf) parseIndexTable() (pp.PbVal, error) {
// buf[0:4] is Collector BGP ID
if len(r.buf) < 6 {
return nil, fmt.Errorf("rib: Buffer too small to read view length")
}
vLength := int(binary.BigEndian.Uint16(r.buf[4:6]))
r.buf = r.buf[6:]
if len(r.buf) < vLength {
return nil, fmt.Errorf("rib: Buffer too small to read view name")
}
r.buf = r.buf[vLength:]
if len(r.buf) < 2 {
return nil, fmt.Errorf("rib: Buffer too small to read peer count")
}
peerCount := int(binary.BigEndian.Uint16(r.buf[:2]))
r.buf = r.buf[2:]
peers := make([]*pbbgp.PeerEntry, peerCount)
for i := 0; i < peerCount; i++ {
p, err := r.parsePeerEntry()
if err != nil {
return nil, err
}
peers[i] = p
}
r.dest.PeerEntry = peers
return r, nil
}
func (r *ribBuf) parsePeerEntry() (*pbbgp.PeerEntry, error) {
if len(r.buf) < 1 {
return nil, fmt.Errorf("rib: Buffer too small to read peer type")
}
peerType := uint8(r.buf[0])
r.buf = r.buf[1:]
AS4 := (peerType&0x2 != 0)
IPv6 := (peerType&0x1 != 0)
if len(r.buf) < 4 {
return nil, fmt.Errorf("rib: Buffer too small to read BGP id")
}
id := binary.BigEndian.Uint32(r.buf[:4])
r.buf = r.buf[4:]
peerIP := new(pbcom.IPAddressWrapper)
if IPv6 {
IPbuf := make([]byte, 16)
if len(r.buf) < 16 {
return nil, fmt.Errorf("rib: Buffer too small to read peer IPv6")
}
copy(IPbuf, r.buf[:16])
r.buf = r.buf[16:]
peerIP.IPv6 = IPbuf
} else {
IPbuf := make([]byte, 4)
if len(r.buf) < 4 {
return nil, fmt.Errorf("rib: Buffer too small to read peer IPv4")
}
copy(IPbuf, r.buf[:4])
r.buf = r.buf[4:]
peerIP.IPv4 = IPbuf
}
var ASNum uint32
if AS4 {
if len(r.buf) < 4 {
return nil, fmt.Errorf("rib: Buffer too small to read AS number")
}
ASNum = binary.BigEndian.Uint32(r.buf[:4])
r.buf = r.buf[4:]
} else {
if len(r.buf) < 2 {
return nil, fmt.Errorf("rib: Buffer too small to read AS number")
}
ASNum = uint32(binary.BigEndian.Uint16(r.buf[:2]))
r.buf = r.buf[2:]
}
pe := new(pbbgp.PeerEntry)
pe.PeerId = id
pe.Peer_AS = ASNum
pe.Peer_IP = peerIP
return pe, nil
}
func (r *ribBuf) GetHeader() *pbbgp.RIB {
return r.dest
}
func (r *ribBuf) String() string {
str := ""
if r.isIndex {
str += fmt.Sprintf("Peer Count: %d\n", len(r.dest.PeerEntry))
str += "Peers:\n"
for i := 0; i < len(r.dest.PeerEntry); i++ {
str += peerToString(r.dest.PeerEntry[i]) + "\n"
}
} else {
if len(r.dest.RouteEntry) > 0 {
str += fmt.Sprintf("ENTRIES: %d\n", len(r.dest.RouteEntry))
for i := 0; i < len(r.dest.RouteEntry); i++ {
str += ribEntryToString(r.dest.RouteEntry[i], r.index) + "\n"
}
}
}
return str
}
func peerToString(p *pbbgp.PeerEntry) string {
return fmt.Sprintf("%s AS%d", net.IP(util.GetIP(p.Peer_IP)), p.Peer_AS)
}
func ribEntryToString(r *pbbgp.RIBEntry, index pp.PbVal) string {
ind := index.(*ribBuf)
peer := ind.dest.PeerEntry[r.PeerIndex]
pref := r.Prefix
prefString := fmt.Sprintf("%s/%d", net.IP(util.GetIP(pref.GetPrefix())), pref.Mask)
str := fmt.Sprintf("PREFIX: %s\n", prefString)
str += fmt.Sprintf("FROM: %s\n", peerToString(peer))
str += fmt.Sprintf("ORIGINATED: %s\n", time.Unix(int64(r.Timestamp), 0))
str += bgp.AttrToString(r.Attrs)
return str
}
func (r *ribBuf) MarshalJSON() ([]byte, error) {
return json.Marshal(newribHeaderWrapper(r))
}
type ribHeaderWrapper struct {
Prefix *bgp.PrefixWrapper
Events []*ribEventWrapper
}
func newribHeaderWrapper(r *ribBuf) *ribHeaderWrapper {
rh := ribHeaderWrapper{}
rh.Prefix = bgp.NewPrefixWrapper(r.dest.RouteEntry[0].Prefix)
rh.Events = make([]*ribEventWrapper, len(r.dest.RouteEntry))
for i := 0; i < len(r.dest.RouteEntry); i++ {
rh.Events[i] = newribEventWrapper(r.dest.RouteEntry[i], r.index.(*ribBuf))
}
return &rh
}
type ribEventWrapper struct {
Peer *ribPeerWrapper
Originated time.Time
Attrs *bgp.AttrsWrapper
}
func newribEventWrapper(rib *pbbgp.RIBEntry, ind *ribBuf) *ribEventWrapper {
rew := ribEventWrapper{}
rew.Peer = newribPeerWrapper(ind.dest.PeerEntry[rib.PeerIndex])
rew.Originated = time.Unix(int64(rib.Timestamp), 0)
rew.Attrs = bgp.NewAttrsWrapper(rib.Attrs)
return &rew
}
type ribPeerWrapper struct {
*pbbgp.PeerEntry
Peer_IP net.IP `json:"peer_IP"`
}
func newribPeerWrapper(p *pbbgp.PeerEntry) *ribPeerWrapper {
rpw := ribPeerWrapper{}
rpw.PeerEntry = p
rpw.Peer_IP = net.IP(util.GetIP(p.Peer_IP))
return &rpw
}