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
|
package bgp
import (
"encoding/binary"
"github.com/stretchr/testify/assert"
"net"
"testing"
)
func bgpupdate() *BGPMessage {
aspath := []AsPathParamInterface{
NewAsPathParam(2, []uint16{65001}),
}
p := []PathAttributeInterface{
NewPathAttributeOrigin(1),
NewPathAttributeAsPath(aspath),
NewPathAttributeNextHop("192.168.1.1"),
}
n := []*IPAddrPrefix{NewIPAddrPrefix(24, "10.10.10.0")}
return NewBGPUpdateMessage(nil, p, n)
}
func bgpupdateV6() *BGPMessage {
aspath := []AsPathParamInterface{
NewAsPathParam(2, []uint16{65001}),
}
mp_nlri := []AddrPrefixInterface{NewIPv6AddrPrefix(100,
"fe80:1234:1234:5667:8967:af12:8912:1023")}
p := []PathAttributeInterface{
NewPathAttributeOrigin(1),
NewPathAttributeAsPath(aspath),
NewPathAttributeMpReachNLRI("1023::", mp_nlri),
}
return NewBGPUpdateMessage(nil, p, nil)
}
func Test_Validate_CapV4(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv6_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(true, res)
}
func Test_Validate_CapV6(t *testing.T) {
assert := assert.New(t)
message := bgpupdateV6().Body.(*BGPUpdate)
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv6_UC: true}, false)
assert.Equal(true, res)
assert.NoError(err)
res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
}
func Test_Validate_OK(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(true, res)
assert.NoError(err)
}
// func Test_Validate_wellknown_but_nontransitive(t *testing.T) {
// assert := assert.New(t)
// message := bgpupdate().Body.(*BGPUpdate)
// originBytes := []byte{0, 1, 1, 1} // 0 means Flags
// origin := &PathAttributeOrigin{}
// origin.DecodeFromBytes(originBytes)
// message.PathAttributes[0] = origin
// res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC,})
// assert.Equal(false, res)
// assert.Error(err)
// e := err.(*MessageError)
// assert.Equal(BGP_ERROR_UPDATE_MESSAGE_ERROR, e.TypeCode)
// assert.Equal(BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, e.SubTypeCode)
// assert.Equal(originBytes, e.Data)
// }
// func Test_Validate_wellknown_but_partial(t *testing.T) {
// assert := assert.New(t)
// message := bgpupdate().Body.(*BGPUpdate)
// originBytes := []byte{BGP_ATTR_FLAG_PARTIAL, 1, 1, 1}
// origin := &PathAttributeOrigin{}
// origin.DecodeFromBytes(originBytes)
// message.PathAttributes[0] = origin
// res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC,})
// assert.Equal(false, res)
// assert.Error(err)
// e := err.(*MessageError)
// assert.Equal(BGP_ERROR_UPDATE_MESSAGE_ERROR, e.TypeCode)
// assert.Equal(BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, e.SubTypeCode)
// assert.Equal(originBytes, e.Data)
// }
// func Test_Validate_optional_nontransitive_but_partial(t *testing.T) {
// assert := assert.New(t)
// message := bgpupdate().Body.(*BGPUpdate)
// f := BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_PARTIAL
// originBytes := []byte{byte(f), 1, 1, 1}
// origin := &PathAttributeOrigin{}
// origin.DecodeFromBytes(originBytes)
// message.PathAttributes[0] = origin
// res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC,})
// assert.Equal(false, res)
// assert.Error(err)
// e := err.(*MessageError)
// assert.Equal(BGP_ERROR_UPDATE_MESSAGE_ERROR, e.TypeCode)
// assert.Equal(BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, e.SubTypeCode)
// assert.Equal(originBytes, e.Data)
// }
// func Test_Validate_flag_mismatch(t *testing.T) {
// assert := assert.New(t)
// message := bgpupdate().Body.(*BGPUpdate)
// f := BGP_ATTR_FLAG_OPTIONAL
// // origin needs to be well-known
// originBytes := []byte{byte(f), 1, 1, 1}
// origin := &PathAttributeOrigin{}
// origin.DecodeFromBytes(originBytes)
// message.PathAttributes[0] = origin
// res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC,})
// assert.Equal(false, res)
// assert.Error(err)
// e := err.(*MessageError)
// assert.Equal(BGP_ERROR_UPDATE_MESSAGE_ERROR, e.TypeCode)
// assert.Equal(BGP_ERROR_SUB_ATTRIBUTE_FLAGS_ERROR, e.SubTypeCode)
// assert.Equal(originBytes, e.Data)
// }
func Test_Validate_duplicate_attribute(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// duplicate origin path attribute
originBytes := []byte{byte(PathAttrFlags[BGP_ATTR_TYPE_ORIGIN]), 1, 1, 1}
origin := &PathAttributeOrigin{}
origin.DecodeFromBytes(originBytes)
message.PathAttributes = append(message.PathAttributes, origin)
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST), e.SubTypeCode)
assert.Nil(e.Data)
}
func Test_Validate_mandatory_missing(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
message.PathAttributes = message.PathAttributes[1:]
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_MISSING_WELL_KNOWN_ATTRIBUTE), e.SubTypeCode)
missing, _ := binary.Uvarint(e.Data)
assert.Equal(uint64(1), missing)
}
func Test_Validate_mandatory_missing_nocheck(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
message.PathAttributes = message.PathAttributes[1:]
message.NLRI = nil
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(true, res)
assert.NoError(err)
}
func Test_Validate_invalid_origin(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// origin needs to be well-known
originBytes := []byte{byte(PathAttrFlags[BGP_ATTR_TYPE_ORIGIN]), 1, 1, 5}
origin := &PathAttributeOrigin{}
origin.DecodeFromBytes(originBytes)
message.PathAttributes[0] = origin
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_INVALID_ORIGIN_ATTRIBUTE), e.SubTypeCode)
assert.Equal(originBytes, e.Data)
}
func Test_Validate_invalid_nexthop_zero(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// invalid nexthop
addr := net.ParseIP("0.0.0.1").To4()
nexthopBytes := []byte{byte(PathAttrFlags[BGP_ATTR_TYPE_NEXT_HOP]), 3, 4}
nexthopBytes = append(nexthopBytes, addr...)
nexthop := &PathAttributeNextHop{}
nexthop.DecodeFromBytes(nexthopBytes)
message.PathAttributes[2] = nexthop
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE), e.SubTypeCode)
assert.Equal(nexthopBytes, e.Data)
}
func Test_Validate_invalid_nexthop_lo(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// invalid nexthop
addr := net.ParseIP("127.0.0.1").To4()
nexthopBytes := []byte{byte(PathAttrFlags[BGP_ATTR_TYPE_NEXT_HOP]), 3, 4}
nexthopBytes = append(nexthopBytes, addr...)
nexthop := &PathAttributeNextHop{}
nexthop.DecodeFromBytes(nexthopBytes)
message.PathAttributes[2] = nexthop
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE), e.SubTypeCode)
assert.Equal(nexthopBytes, e.Data)
}
func Test_Validate_invalid_nexthop_de(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// invalid nexthop
addr := net.ParseIP("224.0.0.1").To4()
nexthopBytes := []byte{byte(PathAttrFlags[BGP_ATTR_TYPE_NEXT_HOP]), 3, 4}
nexthopBytes = append(nexthopBytes, addr...)
nexthop := &PathAttributeNextHop{}
nexthop.DecodeFromBytes(nexthopBytes)
message.PathAttributes[2] = nexthop
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_INVALID_NEXT_HOP_ATTRIBUTE), e.SubTypeCode)
assert.Equal(nexthopBytes, e.Data)
}
func Test_Validate_unrecognized_well_known(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
f := BGP_ATTR_FLAG_TRANSITIVE
unknownBytes := []byte{byte(f), 30, 1, 1}
unknown := &PathAttributeUnknown{}
unknown.DecodeFromBytes(unknownBytes)
message.PathAttributes = append(message.PathAttributes, unknown)
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, false)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_UNRECOGNIZED_WELL_KNOWN_ATTRIBUTE), e.SubTypeCode)
assert.Equal(unknownBytes, e.Data)
}
func Test_Validate_aspath(t *testing.T) {
assert := assert.New(t)
message := bgpupdate().Body.(*BGPUpdate)
// VALID AS_PATH
res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, true)
assert.Equal(true, res)
// CONFED_SET
newAttrs := make([]PathAttributeInterface, 0)
attrs := message.PathAttributes
for _, attr := range attrs {
if _, y := attr.(*PathAttributeAsPath); y {
aspath := []AsPathParamInterface{
NewAsPathParam(BGP_ASPATH_ATTR_TYPE_CONFED_SET, []uint16{65001}),
}
newAttrs = append(newAttrs, NewPathAttributeAsPath(aspath))
} else {
newAttrs = append(newAttrs, attr)
}
}
message.PathAttributes = newAttrs
res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, true)
assert.Equal(false, res)
assert.Error(err)
e := err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_MALFORMED_AS_PATH), e.SubTypeCode)
assert.Nil(e.Data)
// CONFED_SEQ
newAttrs = make([]PathAttributeInterface, 0)
attrs = message.PathAttributes
for _, attr := range attrs {
if _, y := attr.(*PathAttributeAsPath); y {
aspath := []AsPathParamInterface{
NewAsPathParam(BGP_ASPATH_ATTR_TYPE_CONFED_SEQ, []uint16{65001}),
}
newAttrs = append(newAttrs, NewPathAttributeAsPath(aspath))
} else {
newAttrs = append(newAttrs, attr)
}
}
message.PathAttributes = newAttrs
res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}, true)
assert.Equal(false, res)
assert.Error(err)
e = err.(*MessageError)
assert.Equal(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), e.TypeCode)
assert.Equal(uint8(BGP_ERROR_SUB_MALFORMED_AS_PATH), e.SubTypeCode)
assert.Nil(e.Data)
}
func Test_Validate_flowspec(t *testing.T) {
assert := assert.New(t)
cmp := make([]FlowSpecComponentInterface, 0)
cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0")))
cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0")))
eq := 0x1
gt := 0x2
lt := 0x4
and := 0x40
not := 0x2
item1 := NewFlowSpecComponentItem(eq, TCP)
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_IP_PROTO, []*FlowSpecComponentItem{item1}))
item2 := NewFlowSpecComponentItem(gt|eq, 20)
item3 := NewFlowSpecComponentItem(and|lt|eq, 30)
item4 := NewFlowSpecComponentItem(eq, 10)
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DST_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_SRC_PORT, []*FlowSpecComponentItem{item2, item3, item4}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_TYPE, []*FlowSpecComponentItem{item2, item3, item4}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_ICMP_CODE, []*FlowSpecComponentItem{item2, item3, item4}))
item5 := NewFlowSpecComponentItem(0, TCP_FLAG_ACK)
item6 := NewFlowSpecComponentItem(and|not, TCP_FLAG_URGENT)
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_TCP_FLAG, []*FlowSpecComponentItem{item5, item6}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_PKT_LEN, []*FlowSpecComponentItem{item2, item3, item4}))
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_DSCP, []*FlowSpecComponentItem{item2, item3, item4}))
isFlagment := 0x02
item7 := NewFlowSpecComponentItem(isFlagment, 0)
cmp = append(cmp, NewFlowSpecComponent(FLOW_SPEC_TYPE_FRAGMENT, []*FlowSpecComponentItem{item7}))
n1 := NewFlowSpecIPv4Unicast(cmp)
a := NewPathAttributeMpReachNLRI("", []AddrPrefixInterface{n1})
m := map[RouteFamily]BGPAddPathMode{RF_FS_IPv4_UC: BGP_ADD_PATH_NONE}
_, err := ValidateAttribute(a, m, false)
assert.Nil(err)
cmp = make([]FlowSpecComponentInterface, 0)
cmp = append(cmp, NewFlowSpecSourcePrefix(NewIPAddrPrefix(24, "10.0.0.0")))
cmp = append(cmp, NewFlowSpecDestinationPrefix(NewIPAddrPrefix(24, "10.0.0.0")))
n1 = NewFlowSpecIPv4Unicast(cmp)
a = NewPathAttributeMpReachNLRI("", []AddrPrefixInterface{n1})
_, err = ValidateAttribute(a, m, false)
assert.NotNil(err)
}
func TestValidateLargeCommunities(t *testing.T) {
assert := assert.New(t)
c1, err := ParseLargeCommunity("10:10:10")
assert.Nil(err)
c2, err := ParseLargeCommunity("10:10:10")
assert.Nil(err)
c3, err := ParseLargeCommunity("10:10:20")
assert.Nil(err)
a := NewPathAttributeLargeCommunities([]*LargeCommunity{c1, c2, c3})
assert.True(len(a.Values) == 3)
_, err = ValidateAttribute(a, nil, false)
assert.Nil(err)
assert.True(len(a.Values) == 2)
}
|