diff options
Diffstat (limited to 'packet')
-rw-r--r-- | packet/bgp.go | 4 | ||||
-rw-r--r-- | packet/validate.go | 21 | ||||
-rw-r--r-- | packet/validate_test.go | 26 |
3 files changed, 23 insertions, 28 deletions
diff --git a/packet/bgp.go b/packet/bgp.go index 3a27bce0..1b80e543 100644 --- a/packet/bgp.go +++ b/packet/bgp.go @@ -959,6 +959,10 @@ func AfiSafiToRouteFamily(afi uint16, safi uint8) RouteFamily { return RouteFamily(int(afi)<<16 | int(safi)) } +func RouteFamilyToAfiSafi(rf RouteFamily) (uint16, uint8) { + return uint16(int(rf) >> 16), uint8(int(rf) & 0xff) +} + type RouteFamily int const ( diff --git a/packet/validate.go b/packet/validate.go index 80d51a9e..b05574b2 100644 --- a/packet/validate.go +++ b/packet/validate.go @@ -7,23 +7,14 @@ import ( "strconv" ) -func isRfSupported(rf RouteFamily, rfs []RouteFamily) bool { - for _, r := range rfs { - if rf == r { - return true - } - } - return false -} - // Validator for BGPUpdate -func ValidateUpdateMsg(m *BGPUpdate, rfs []RouteFamily) (bool, error) { +func ValidateUpdateMsg(m *BGPUpdate, rfs map[RouteFamily]bool) (bool, error) { eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) eSubCodeAttrList := uint8(BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST) eSubCodeMissing := uint8(BGP_ERROR_SUB_MISSING_WELL_KNOWN_ATTRIBUTE) if len(m.NLRI) > 0 || len(m.WithdrawnRoutes) > 0 { - if isRfSupported(RF_IPv4_UC, rfs) == false { + if _, ok := rfs[RF_IPv4_UC]; !ok { return false, NewMessageError(0, 0, nil, fmt.Sprintf("Address-family rf %d not avalible for session", RF_IPv4_UC)) } } @@ -67,7 +58,7 @@ func ValidateUpdateMsg(m *BGPUpdate, rfs []RouteFamily) (bool, error) { return true, nil } -func ValidateAttribute(a PathAttributeInterface, rfs []RouteFamily) (bool, error) { +func ValidateAttribute(a PathAttributeInterface, rfs map[RouteFamily]bool) (bool, error) { eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) eSubCodeBadOrigin := uint8(BGP_ERROR_SUB_INVALID_ORIGIN_ATTRIBUTE) @@ -77,7 +68,7 @@ func ValidateAttribute(a PathAttributeInterface, rfs []RouteFamily) (bool, error checkPrefix := func(l []AddrPrefixInterface) bool { for _, prefix := range l { rf := AfiSafiToRouteFamily(prefix.AFI(), prefix.SAFI()) - if isRfSupported(rf, rfs) == false { + if _, ok := rfs[rf]; !ok { return false } } @@ -87,7 +78,7 @@ func ValidateAttribute(a PathAttributeInterface, rfs []RouteFamily) (bool, error switch p := a.(type) { case *PathAttributeMpUnreachNLRI: rf := AfiSafiToRouteFamily(p.AFI, p.SAFI) - if isRfSupported(rf, rfs) == false { + if _, ok := rfs[rf]; !ok { return false, NewMessageError(0, 0, nil, fmt.Sprintf("Address-family rf %d not avalible for session", rf)) } if checkPrefix(p.Value) == false { @@ -95,7 +86,7 @@ func ValidateAttribute(a PathAttributeInterface, rfs []RouteFamily) (bool, error } case *PathAttributeMpReachNLRI: rf := AfiSafiToRouteFamily(p.AFI, p.SAFI) - if isRfSupported(rf, rfs) == false { + if _, ok := rfs[rf]; !ok { return false, NewMessageError(0, 0, nil, fmt.Sprintf("Address-family rf %d not avalible for session", rf)) } if checkPrefix(p.Value) == false { diff --git a/packet/validate_test.go b/packet/validate_test.go index effd9d89..46ee3798 100644 --- a/packet/validate_test.go +++ b/packet/validate_test.go @@ -41,29 +41,29 @@ func bgpupdateV6() *BGPMessage { func Test_Validate_CapV4(t *testing.T) { assert := assert.New(t) message := bgpupdate().Body.(*BGPUpdate) - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv6_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv6_UC: true}) assert.Equal(false, res) assert.Error(err) - res, err = ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(true, res) } func Test_Validate_CapV6(t *testing.T) { assert := assert.New(t) message := bgpupdateV6().Body.(*BGPUpdate) - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv6_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv6_UC: true}) assert.Equal(true, res) assert.NoError(err) - res, err = ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err = ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) } func Test_Validate_OK(t *testing.T) { assert := assert.New(t) message := bgpupdate().Body.(*BGPUpdate) - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(true, res) assert.NoError(err) @@ -151,7 +151,7 @@ func Test_Validate_duplicate_attribute(t *testing.T) { origin.DecodeFromBytes(originBytes) message.PathAttributes = append(message.PathAttributes, origin) - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -164,7 +164,7 @@ 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, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -180,7 +180,7 @@ func Test_Validate_mandatory_missing_nocheck(t *testing.T) { message.PathAttributes = message.PathAttributes[1:] message.NLRI = nil - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(true, res) assert.NoError(err) } @@ -194,7 +194,7 @@ func Test_Validate_invalid_origin(t *testing.T) { origin.DecodeFromBytes(originBytes) message.PathAttributes[0] = origin - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -215,7 +215,7 @@ func Test_Validate_invalid_nexthop_zero(t *testing.T) { nexthop.DecodeFromBytes(nexthopBytes) message.PathAttributes[2] = nexthop - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -236,7 +236,7 @@ func Test_Validate_invalid_nexthop_lo(t *testing.T) { nexthop.DecodeFromBytes(nexthopBytes) message.PathAttributes[2] = nexthop - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -257,7 +257,7 @@ func Test_Validate_invalid_nexthop_de(t *testing.T) { nexthop.DecodeFromBytes(nexthopBytes) message.PathAttributes[2] = nexthop - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) @@ -277,7 +277,7 @@ func Test_Validate_unrecognized_well_known(t *testing.T) { unknown.DecodeFromBytes(unknownBytes) message.PathAttributes = append(message.PathAttributes, unknown) - res, err := ValidateUpdateMsg(message, []RouteFamily{RF_IPv4_UC}) + res, err := ValidateUpdateMsg(message, map[RouteFamily]bool{RF_IPv4_UC: true}) assert.Equal(false, res) assert.Error(err) e := err.(*MessageError) |