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
|
// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package bgp
import (
"encoding/binary"
"fmt"
"net"
)
const (
RTR_SERIAL_NOTIFY = iota
RTR_SERIAL_QUERY
RTR_RESET_QUERY
RTR_CACHE_RESPONSE
RTR_IPV4_PREFIX
_
RTR_IPV6_PREFIX
RTR_END_OF_DATA
RTR_CACHE_RESET
_
RTR_ERROR_REPORT
)
const (
RTR_SERIAL_NOTIFY_LEN = 12
RTR_SERIAL_QUERY_LEN = 12
RTR_RESET_QUERY_LEN = 8
RTR_CACHE_RESPONSE_LEN = 8
RTR_IPV4_PREFIX_LEN = 20
RTR_IPV6_PREFIX_LEN = 32
RTR_END_OF_DATA_LEN = 12
RTR_CACHE_RESET_LEN = 8
RTR_MIN_LEN = 8
)
type RTRMessage interface {
DecodeFromBytes([]byte) error
Serialize() ([]byte, error)
}
type RTRCommon struct {
Version uint8
Type uint8
SessionID uint16
Len uint32
SerialNumber uint32
}
func (m *RTRCommon) DecodeFromBytes(data []byte) error {
m.Version = data[0]
m.Type = data[1]
m.SessionID = binary.BigEndian.Uint16(data[2:4])
m.Len = binary.BigEndian.Uint32(data[4:8])
m.SerialNumber = binary.BigEndian.Uint32(data[8:12])
return nil
}
func (m *RTRCommon) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Version
data[1] = m.Type
binary.BigEndian.PutUint16(data[2:4], m.SessionID)
binary.BigEndian.PutUint32(data[4:8], m.Len)
binary.BigEndian.PutUint32(data[8:12], m.SerialNumber)
return data, nil
}
type RTRSerialNotify struct {
RTRCommon
}
type RTRSerialQuery struct {
RTRCommon
}
type RTRReset struct {
Version uint8
Type uint8
Len uint32
}
func (m *RTRReset) DecodeFromBytes(data []byte) error {
m.Version = data[0]
m.Type = data[1]
m.Len = binary.BigEndian.Uint32(data[4:8])
return nil
}
func (m *RTRReset) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Version
data[1] = m.Type
binary.BigEndian.PutUint32(data[4:8], m.Len)
return data, nil
}
type RTRResetQuery struct {
RTRReset
}
func (m *RTRResetQuery) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Version
data[1] = m.Type
binary.BigEndian.PutUint32(data[4:8], m.Len)
return data, nil
}
func NewRTRResetQuery() *RTRResetQuery {
return &RTRResetQuery{
RTRReset{
Type: RTR_RESET_QUERY,
Len: RTR_RESET_QUERY_LEN,
},
}
}
type RTRCacheResponse struct {
Version uint8
Type uint8
SessionID uint16
Len uint32
}
func (m *RTRCacheResponse) DecodeFromBytes(data []byte) error {
m.Version = data[0]
m.Type = data[1]
m.SessionID = binary.BigEndian.Uint16(data[2:4])
m.Len = binary.BigEndian.Uint32(data[4:8])
return nil
}
func (m *RTRCacheResponse) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Version
data[1] = m.Type
binary.BigEndian.PutUint16(data[2:4], m.SessionID)
binary.BigEndian.PutUint32(data[4:8], m.Len)
return data, nil
}
type RTRIPPrefix struct {
Version uint8
Type uint8
SessionID uint16
Len uint32
Flags uint8
PrefixLen uint8
MaxLen uint8
Prefix net.IP
AS uint32
}
func (m *RTRIPPrefix) DecodeFromBytes(data []byte) error {
m.Version = data[0]
m.Type = data[1]
m.SessionID = binary.BigEndian.Uint16(data[2:4])
m.Len = binary.BigEndian.Uint32(data[4:8])
m.Flags = data[8]
m.PrefixLen = data[9]
m.MaxLen = data[10]
if m.Type == RTR_IPV4_PREFIX {
m.Prefix = net.IP(data[12:16]).To4()
m.AS = binary.BigEndian.Uint32(data[16:20])
} else {
m.Prefix = net.IP(data[12:28]).To16()
m.AS = binary.BigEndian.Uint32(data[28:32])
}
return nil
}
func (m *RTRIPPrefix) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Type
data[1] = m.Version
binary.BigEndian.PutUint16(data[2:4], m.SessionID)
binary.BigEndian.PutUint32(data[4:8], m.Len)
data[8] = m.Flags
data[9] = m.PrefixLen
data[10] = m.MaxLen
if m.Type == RTR_IPV4_PREFIX {
copy(data[12:16], m.Prefix.To4())
binary.BigEndian.PutUint32(data[16:20], m.AS)
} else {
copy(data[12:28], m.Prefix.To16())
binary.BigEndian.PutUint32(data[28:32], m.AS)
}
return data, nil
}
type RTREndOfData struct {
RTRCommon
}
type RTRCacheReset struct {
RTRReset
}
type RTRErrorReport struct {
Version uint8
Type uint8
SessionID uint16
Len uint32
PDULen uint32
PDU []byte
TextLen uint32
Text []byte
}
func (m *RTRErrorReport) DecodeFromBytes(data []byte) error {
m.Version = data[0]
m.Type = data[1]
m.SessionID = binary.BigEndian.Uint16(data[2:4])
m.Len = binary.BigEndian.Uint32(data[4:8])
m.PDULen = binary.BigEndian.Uint32(data[8:12])
m.PDU = make([]byte, m.PDULen)
copy(m.PDU, data[12:12+m.PDULen])
m.TextLen = binary.BigEndian.Uint32(data[12+m.PDULen : 16+m.PDULen])
m.PDU = make([]byte, m.TextLen)
copy(m.Text, data[16+m.PDULen:])
return nil
}
func (m *RTRErrorReport) Serialize() ([]byte, error) {
data := make([]byte, m.Len)
data[0] = m.Version
data[1] = m.Type
binary.BigEndian.PutUint16(data[2:4], m.SessionID)
binary.BigEndian.PutUint32(data[4:8], m.Len)
binary.BigEndian.PutUint32(data[8:12], m.PDULen)
copy(data[12:], m.PDU)
binary.BigEndian.PutUint32(data[12+m.PDULen:16+m.PDULen], m.TextLen)
copy(data[16+m.PDULen:], m.Text)
return data, nil
}
func SplitRTR(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 || len(data) < RTR_MIN_LEN {
return 0, nil, nil
}
totalLen := binary.BigEndian.Uint32(data[4:8])
if totalLen < RTR_MIN_LEN {
return 0, nil, fmt.Errorf("Invalid length: %d", totalLen)
}
if uint32(len(data)) < totalLen {
return 0, nil, nil
}
return int(totalLen), data[0:totalLen], nil
}
func ParseRTR(data []byte) (RTRMessage, error) {
var msg RTRMessage
switch data[1] {
case RTR_SERIAL_NOTIFY:
msg = &RTRSerialNotify{}
case RTR_SERIAL_QUERY:
msg = &RTRSerialQuery{}
case RTR_RESET_QUERY:
msg = &RTRResetQuery{}
case RTR_CACHE_RESPONSE:
msg = &RTRCacheResponse{}
case RTR_IPV4_PREFIX:
msg = &RTRIPPrefix{}
case RTR_IPV6_PREFIX:
msg = &RTRIPPrefix{}
case RTR_END_OF_DATA:
msg = &RTREndOfData{}
case RTR_CACHE_RESET:
msg = &RTRCacheReset{}
case RTR_ERROR_REPORT:
msg = &RTRErrorReport{}
default:
return nil, fmt.Errorf("unknown RTR message type %d:", data[1])
}
err := msg.DecodeFromBytes(data)
return msg, err
}
|