Skip to content

Commit

Permalink
packet/bgp: Fix length calc when multiple RTM NLRIs
Browse files Browse the repository at this point in the history
When multiple RTM NLRIs are composed in a single MP_REACH_NLRI, the
current implementation will fail to decode those NLRIs because
RouteTargetMembershipNLRI.DecodeFromBytes() tests the length of the
given bytes even if including the subsequent bytes.

This patch fixes to cut the bytes by using the prefix length before
testing the byte length.

Signed-off-by: IWASE Yusuke <[email protected]>
  • Loading branch information
iwaseyusuke authored and fujita committed Jun 21, 2018
1 parent e2752ae commit 954562d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packet/bgp/bgp.go
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,7 @@ func (n *RouteTargetMembershipNLRI) DecodeFromBytes(data []byte, options ...*Mar
return NewMessageError(uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR), uint8(BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST), nil, "prefix misses length field")
}
n.Length = data[0]
data = data[1:]
data = data[1 : n.Length/8+1]
if len(data) == 0 {
return nil
} else if len(data) != 12 {
Expand Down Expand Up @@ -1984,7 +1984,7 @@ func (n *RouteTargetMembershipNLRI) Serialize(options ...*MarshallingOption) ([]
}
offset := len(buf)
buf = append(buf, make([]byte, 5)...)
buf[offset] = 12 * 8
buf[offset] = 96
binary.BigEndian.PutUint32(buf[offset+1:], n.AS)
ebuf, err := n.RouteTarget.Serialize()
if err != nil {
Expand Down
6 changes: 5 additions & 1 deletion packet/bgp/bgp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func Test_RouteTargetMembershipNLRIString(t *testing.T) {

// TwoOctetAsSpecificExtended
buf := make([]byte, 13)
buf[0] = 12
buf[0] = 96 // in bit length
binary.BigEndian.PutUint32(buf[1:5], 65546)
buf[5] = byte(EC_TYPE_TRANSITIVE_TWO_OCTET_AS_SPECIFIC) // typehigh
binary.BigEndian.PutUint16(buf[7:9], 65000)
Expand All @@ -108,6 +108,7 @@ func Test_RouteTargetMembershipNLRIString(t *testing.T) {

// IPv4AddressSpecificExtended
buf = make([]byte, 13)
buf[0] = 96 // in bit length
binary.BigEndian.PutUint32(buf[1:5], 65546)
buf[5] = byte(EC_TYPE_TRANSITIVE_IP4_SPECIFIC) // typehigh
ip := net.ParseIP("10.0.0.1").To4()
Expand All @@ -125,6 +126,7 @@ func Test_RouteTargetMembershipNLRIString(t *testing.T) {

// FourOctetAsSpecificExtended
buf = make([]byte, 13)
buf[0] = 96 // in bit length
binary.BigEndian.PutUint32(buf[1:5], 65546)
buf[5] = byte(EC_TYPE_TRANSITIVE_FOUR_OCTET_AS_SPECIFIC) // typehigh
buf[6] = byte(EC_SUBTYPE_ROUTE_TARGET) // subtype
Expand All @@ -142,6 +144,7 @@ func Test_RouteTargetMembershipNLRIString(t *testing.T) {

// OpaqueExtended
buf = make([]byte, 13)
buf[0] = 96 // in bit length
binary.BigEndian.PutUint32(buf[1:5], 65546)
buf[5] = byte(EC_TYPE_TRANSITIVE_OPAQUE) // typehigh
binary.BigEndian.PutUint32(buf[9:], 1000000)
Expand All @@ -157,6 +160,7 @@ func Test_RouteTargetMembershipNLRIString(t *testing.T) {

// Unknown
buf = make([]byte, 13)
buf[0] = 96 // in bit length
binary.BigEndian.PutUint32(buf[1:5], 65546)
buf[5] = 0x04 // typehigh
binary.BigEndian.PutUint32(buf[9:], 1000000)
Expand Down

0 comments on commit 954562d

Please sign in to comment.