Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

packet/bgp,mup: fix prefix format in Type 1 ST route #2625

Merged
merged 1 commit into from
Feb 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions pkg/packet/bgp/mup.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,30 +477,30 @@ func (r *MUPType1SessionTransformedRoute) DecodeFromBytes(data []byte, afi uint1
}
prefixLength := int(data[p])
p += 1
addrLen := 0
switch afi {
case AFI_IP:
if prefixLength > 32 {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid Prefix length: %d", prefixLength))
}
addr, ok := netip.AddrFromSlice(data[p : p+4])
if !ok {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid Prefix: %x", data[p:p+4]))
}
r.Prefix = netip.PrefixFrom(addr, prefixLength)
p += 4
addrLen = 4
case AFI_IP6:
if prefixLength > 128 {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid Prefix length: %d", prefixLength))
}
addr, ok := netip.AddrFromSlice(data[p : p+16])
if !ok {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid Prefix: %x", data[p:p+16]))
}
r.Prefix = netip.PrefixFrom(addr, prefixLength)
p += 16
addrLen = 16
default:
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid AFI: %d", afi))
}
byteLen := (prefixLength + 7) / 8
b := make([]byte, addrLen)
copy(b[0:byteLen], data[p:p+byteLen])
addr, ok := netip.AddrFromSlice(b)
if !ok {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid Prefix: %x", b))
}
r.Prefix = netip.PrefixFrom(addr, prefixLength)
p += byteLen
r.TEID = binary.BigEndian.Uint32(data[p : p+4])
if r.TEID == 0 {
return NewMessageError(BGP_ERROR_UPDATE_MESSAGE_ERROR, BGP_ERROR_SUB_MALFORMED_ATTRIBUTE_LIST, nil, fmt.Sprintf("Invalid TEID: %d", r.TEID))
Expand Down Expand Up @@ -534,7 +534,8 @@ func (r *MUPType1SessionTransformedRoute) Serialize() ([]byte, error) {
buf = make([]byte, 8)
}
buf = append(buf, byte(r.Prefix.Bits()))
buf = append(buf, r.Prefix.Addr().AsSlice()...)
byteLen := (r.Prefix.Bits() + 7) / 8
buf = append(buf, r.Prefix.Addr().AsSlice()[:byteLen]...)
t := make([]byte, 4)
binary.BigEndian.PutUint32(t, r.TEID)
buf = append(buf, t...)
Expand All @@ -552,9 +553,9 @@ func (r *MUPType1SessionTransformedRoute) AFI() uint16 {
}

func (r *MUPType1SessionTransformedRoute) Len() int {
// RD(8) + PrefixLength(1) + Prefix(4 or 16)
// RD(8) + PrefixLength(1) + Prefix(variable)
// + TEID(4) + QFI(1) + EndpointAddressLength(1) + EndpointAddress(4 or 16)
return 15 + r.Prefix.Addr().BitLen()/8 + int(r.EndpointAddressLength/8)
return 15 + (r.Prefix.Bits()+7)/8 + int(r.EndpointAddressLength/8)
}

func (r *MUPType1SessionTransformedRoute) String() string {
Expand Down
4 changes: 2 additions & 2 deletions pkg/packet/bgp/mup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func Test_MUPType1SessionTransformedRouteIPv4(t *testing.T) {
rd, _ := ParseRouteDistinguisher("100:100")
r := &MUPType1SessionTransformedRoute{
RD: rd,
Prefix: netip.MustParsePrefix("192.100.0.1/32"),
Prefix: netip.MustParsePrefix("192.100.0.0/24"),
TEID: 100,
QFI: 9,
EndpointAddressLength: 32,
Expand All @@ -140,7 +140,7 @@ func Test_MUPType1SessionTransformedRouteIPv6(t *testing.T) {
rd, _ := ParseRouteDistinguisher("100:100")
r := &MUPType1SessionTransformedRoute{
RD: rd,
Prefix: netip.MustParsePrefix("2001:db8:1:1::1/128"),
Prefix: netip.MustParsePrefix("2001:db8:1::/48"),
TEID: 100,
QFI: 9,
EndpointAddressLength: 128,
Expand Down