diff options
Diffstat (limited to 'pkg/packet/bgp')
-rw-r--r-- | pkg/packet/bgp/bgp.go | 107 | ||||
-rw-r--r-- | pkg/packet/bgp/prefix_sid.go | 143 | ||||
-rw-r--r-- | pkg/packet/bgp/prefix_sid_test.go | 34 |
3 files changed, 274 insertions, 10 deletions
diff --git a/pkg/packet/bgp/bgp.go b/pkg/packet/bgp/bgp.go index 84604312..f72a3706 100644 --- a/pkg/packet/bgp/bgp.go +++ b/pkg/packet/bgp/bgp.go @@ -31,7 +31,8 @@ import ( ) type MarshallingOption struct { - AddPath map[RouteFamily]BGPAddPathMode + AddPath map[RouteFamily]BGPAddPathMode + Attributes map[BGPAttrType]bool } func IsAddPathEnabled(decode bool, f RouteFamily, options []*MarshallingOption) bool { @@ -49,6 +50,18 @@ func IsAddPathEnabled(decode bool, f RouteFamily, options []*MarshallingOption) } return false } +func IsAttributePresent(attr BGPAttrType, options []*MarshallingOption) bool { + for _, opt := range options { + if opt == nil { + continue + } + if o := opt.Attributes; o != nil { + _, ok := o[attr] + return ok + } + } + return false +} const ( AFI_IP = 1 @@ -1588,9 +1601,17 @@ type MPLSLabelStack struct { Labels []uint32 } -func (l *MPLSLabelStack) DecodeFromBytes(data []byte) error { +func (l *MPLSLabelStack) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { labels := []uint32{} foundBottom := false + bottomExpected := true + if IsAttributePresent(BGP_ATTR_TYPE_PREFIX_SID, options) { + // If Update carries Prefix SID attribute then there is no a label stack, + // but just 3 bytes which are used to carry the lower portion of Prefix SID. + // There is no bottom stack indication in this case. Once 3 bytes are stored + // breaking out of the loop. + bottomExpected = false + } for len(data) >= 3 { label := uint32(data[0])<<16 | uint32(data[1])<<8 | uint32(data[2]) if label == WITHDRAW_LABEL || label == ZERO_LABEL { @@ -1599,6 +1620,11 @@ func (l *MPLSLabelStack) DecodeFromBytes(data []byte) error { } data = data[3:] labels = append(labels, label>>4) + if !bottomExpected { + // Faking found bottom. + foundBottom = true + break + } if label&1 == 1 { foundBottom = true break @@ -1613,7 +1639,7 @@ func (l *MPLSLabelStack) DecodeFromBytes(data []byte) error { return nil } -func (l *MPLSLabelStack) Serialize() ([]byte, error) { +func (l *MPLSLabelStack) Serialize(options ...*MarshallingOption) ([]byte, error) { buf := make([]byte, len(l.Labels)*3) for i, label := range l.Labels { if label == WITHDRAW_LABEL { @@ -1624,6 +1650,13 @@ func (l *MPLSLabelStack) Serialize() ([]byte, error) { buf[i*3+1] = byte((label >> 8) & 0xff) buf[i*3+2] = byte(label & 0xff) } + if IsAttributePresent(BGP_ATTR_TYPE_PREFIX_SID, options) { + // If Update carries Prefix SID attribute then there is no a label stack, + // but just 3 bytes which are used to carry the lower portion of Prefix SID. + // No need BoS bit set + return buf, nil + } + buf[len(buf)-1] |= 1 return buf, nil } @@ -1721,7 +1754,7 @@ func (l *LabeledVPNIPAddrPrefix) DecodeFromBytes(data []byte, options ...*Marsha } l.Length = uint8(data[0]) data = data[1:] - l.Labels.DecodeFromBytes(data) + l.Labels.DecodeFromBytes(data, options...) if int(l.Length)-8*(l.Labels.Len()) < 0 { l.Labels.Labels = []uint32{} } @@ -1746,7 +1779,7 @@ func (l *LabeledVPNIPAddrPrefix) Serialize(options ...*MarshallingOption) ([]byt } } buf = append(buf, l.Length) - lbuf, err := l.Labels.Serialize() + lbuf, err := l.Labels.Serialize(options...) if err != nil { return nil, err } @@ -8480,6 +8513,7 @@ var PathAttrFlags map[BGPAttrType]BGPAttrFlag = map[BGPAttrType]BGPAttrFlag{ BGP_ATTR_TYPE_AIGP: BGP_ATTR_FLAG_OPTIONAL, BGP_ATTR_TYPE_LARGE_COMMUNITY: BGP_ATTR_FLAG_TRANSITIVE | BGP_ATTR_FLAG_OPTIONAL, BGP_ATTR_TYPE_LS: BGP_ATTR_FLAG_OPTIONAL, + BGP_ATTR_TYPE_PREFIX_SID: BGP_ATTR_FLAG_TRANSITIVE | BGP_ATTR_FLAG_OPTIONAL, } // getPathAttrFlags returns BGP Path Attribute flags value from its type and @@ -9515,6 +9549,7 @@ type PathAttributeMpReachNLRI struct { } func (p *PathAttributeMpReachNLRI) DecodeFromBytes(data []byte, options ...*MarshallingOption) error { + value, err := p.PathAttribute.DecodeFromBytes(data, options...) if err != nil { return err @@ -12325,6 +12360,56 @@ func NewPathAttributeUnknown(flags BGPAttrFlag, typ BGPAttrType, value []byte) * } } +// BGPUpdateAttributes defines a map with a key as bgp attribute type +// and value as bool. Value set to true indicates that the attribute specified by the key +// exists in the bgp update. +type BGPUpdateAttributes struct { + Attribute map[BGPAttrType]bool +} + +func GetBGPUpdateAttributes(data []byte) map[BGPAttrType]bool { + m := make(map[BGPAttrType]bool) + for p := 0; p < len(data); { + flag := data[p] + p++ + if p < len(data) { + t := data[p] + m[BGPAttrType(t)] = true + } else { + break + } + p++ + var l uint16 + // Checking for Extened + if flag&0x10 == 0x10 { + if p+2 <= len(data) { + l = binary.BigEndian.Uint16(data[p : p+2]) + } else { + break + } + p += 2 + } else { + if p < len(data) { + l = uint16(data[p]) + p++ + } else { + break + } + } + p += int(l) + } + return m +} + +func GetBGPUpdateAttributesFromMsg(msg *BGPUpdate) map[BGPAttrType]bool { + m := make(map[BGPAttrType]bool) + for _, p := range msg.PathAttributes { + m[p.GetType()] = true + } + + return m +} + func GetPathAttribute(data []byte) (PathAttributeInterface, error) { if len(data) < 2 { eCode := uint8(BGP_ERROR_UPDATE_MESSAGE_ERROR) @@ -12440,6 +12525,11 @@ func (msg *BGPUpdate) DecodeFromBytes(data []byte, options ...*MarshallingOption if len(data) < int(msg.TotalPathAttributeLen) { return NewMessageError(eCode, eSubCode, nil, "path total attribute length exceeds message length") } + attributes := GetBGPUpdateAttributes(data) + o := MarshallingOption{ + Attributes: attributes, + } + options = append(options, &o) msg.PathAttributes = []PathAttributeInterface{} for pathlen := msg.TotalPathAttributeLen; pathlen > 0; { @@ -12457,7 +12547,6 @@ func (msg *BGPUpdate) DecodeFromBytes(data []byte, options ...*MarshallingOption if err != nil { return err } - err = p.DecodeFromBytes(data, options...) if err != nil { e = err.(*MessageError) @@ -12519,6 +12608,11 @@ func (msg *BGPUpdate) Serialize(options ...*MarshallingOption) ([]byte, error) { msg.WithdrawnRoutesLen = uint16(len(wbuf) - 2) binary.BigEndian.PutUint16(wbuf, msg.WithdrawnRoutesLen) + attributes := GetBGPUpdateAttributesFromMsg(msg) + o := MarshallingOption{ + Attributes: attributes, + } + options = append(options, &o) pbuf := make([]byte, 2) for _, p := range msg.PathAttributes { onepbuf, err := p.Serialize(options...) @@ -12538,6 +12632,7 @@ func (msg *BGPUpdate) Serialize(options ...*MarshallingOption) ([]byte, error) { } buf = append(buf, nbuf...) } + return buf, nil } diff --git a/pkg/packet/bgp/prefix_sid.go b/pkg/packet/bgp/prefix_sid.go index 7dd90082..a7983bcd 100644 --- a/pkg/packet/bgp/prefix_sid.go +++ b/pkg/packet/bgp/prefix_sid.go @@ -5,6 +5,10 @@ import ( "encoding/binary" "encoding/json" "fmt" + "net" + + "github.com/golang/protobuf/ptypes" + api "github.com/osrg/gobgp/api" ) const ( @@ -14,9 +18,8 @@ const ( type TLVType uint8 type TLV struct { - Type TLVType - Length uint16 - Reserved uint8 + Type TLVType + Length uint16 } func (s *TLV) Len() int { @@ -391,7 +394,7 @@ func (s *SRv6InformationSubTLV) MarshalJSON() ([]byte, error) { func (s *SRv6InformationSubTLV) String() string { var buf bytes.Buffer - buf.WriteString(fmt.Sprintf("SID: %s ", string(s.SID))) + buf.WriteString(fmt.Sprintf("SID: %s ", net.IP(s.SID).To16().String())) buf.WriteString(fmt.Sprintf("Flag: %d ", s.Flags)) buf.WriteString(fmt.Sprintf("Endpoint Behavior: %d ", s.EndpointBehavior)) for _, tlv := range s.SubSubTLVs { @@ -540,3 +543,135 @@ func (s *SRv6SIDStructureSubSubTLV) String() string { s.TranspositionOffset, ) } + +func NewPathAttributePrefixSID(psid *api.PrefixSID) (*PathAttributePrefixSID, error) { + t := BGP_ATTR_TYPE_PREFIX_SID + s := &PathAttributePrefixSID{ + PathAttribute: PathAttribute{ + Flags: PathAttrFlags[t], + Type: t, + }, + TLVs: make([]PrefixSIDTLVInterface, 0), + } + for _, raw := range psid.Tlvs { + var tlv ptypes.DynamicAny + if err := ptypes.UnmarshalAny(raw, &tlv); err != nil { + return nil, err + } + switch v := tlv.Message.(type) { + case *api.SRv6L3ServiceTLV: + tlvLength, tlvs, err := UnmarshalSubTLVs(v.SubTlvs) + if err != nil { + return nil, err + } + o := &SRv6L3ServiceAttribute{ + TLV: TLV{ + Type: TLVType(5), + Length: tlvLength, + }, + } + s.PathAttribute.Length += tlvLength + // Storing Sub TLVs in a Service TLV + o.SubTLVs = append(o.SubTLVs, tlvs...) + // Adding Service TLV to Path Attribute TLV slice. + s.TLVs = append(s.TLVs, o) + default: + return nil, fmt.Errorf("unknown or not implemented Prefix SID type: %+v", v) + } + } + // Final Path Attribute Length is 3 bytes of the header and 1 byte Reserved1 + s.PathAttribute.Length += (3 + 1) + return s, nil +} + +func UnmarshalSubTLVs(stlvs map[uint32]*api.SRv6TLV) (uint16, []PrefixSIDTLVInterface, error) { + p := make([]PrefixSIDTLVInterface, 0, len(stlvs)) + l := uint16(0) + // v.SubTlvs is a map by sub tlv type and the value is a slice of sub tlvs of the specific type + for t, tlv := range stlvs { + switch t { + case 1: + // Sub TLV Type 1 is SRv6 Informational Sub TLV + for _, stlvRaw := range tlv.Tlv { + // Instantiating Information Sub TLV + info := &SRv6InformationSubTLV{ + SubTLV: SubTLV{ + Type: SubTLVType(1), + }, + SubSubTLVs: make([]PrefixSIDTLVInterface, 0), + } + var raw ptypes.DynamicAny + if err := ptypes.UnmarshalAny(stlvRaw, &raw); err != nil { + return 0, nil, err + } + infoProto := raw.Message.(*api.SRv6InformationSubTLV) + info.SID = make([]byte, len(infoProto.Sid)) + copy(info.SID, infoProto.Sid) + // TODO Once RFC is published add processing of flags + info.Flags = 0 + info.EndpointBehavior = uint16(infoProto.EndpointBehavior) + var sstlvslength uint16 + var sstlvs []PrefixSIDTLVInterface + if len(infoProto.SubSubTlvs) != 0 { + // Processing Sub Sub TLVs + var err error + sstlvslength, sstlvs, err = UnmarshalSubSubTLVs(infoProto.SubSubTlvs) + if err != nil { + return 0, nil, err + } + info.SubSubTLVs = append(info.SubSubTLVs, sstlvs...) + } + // SRv6 Information Sub TLV length consists 1 byte Resrved2, 16 bytes SID, 1 byte flags, 2 bytes Endpoint Behavior + // 1 byte Reserved3 and length of Sub Sub TLVs + info.SubTLV.Length = 1 + 16 + 1 + 2 + 1 + sstlvslength + // For total Srv6 Information Sub TLV length, adding 3 bytes of the Sub TLV header + l += info.SubTLV.Length + 4 + p = append(p, info) + } + default: + return 0, nil, fmt.Errorf("unknown or not implemented Prefix SID Sub TLV type: %d", t) + } + } + + return l, p, nil +} + +func UnmarshalSubSubTLVs(stlvs map[uint32]*api.SRv6TLV) (uint16, []PrefixSIDTLVInterface, error) { + p := make([]PrefixSIDTLVInterface, 0) + l := uint16(0) + // v.SubTlvs is a map by sub tlv type and the value is a slice of sub tlvs of the specific type + for t, tlv := range stlvs { + switch t { + case 1: + // Sub Sub TLV Type 1 is SRv6 Structure Sub Sub TLV + for _, stlvRaw := range tlv.Tlv { + // Instantiating Information Sub TLV + structure := &SRv6SIDStructureSubSubTLV{ + SubSubTLV: SubSubTLV{ + Type: SubSubTLVType(1), + Length: 6, + }, + } + var raw ptypes.DynamicAny + if err := ptypes.UnmarshalAny(stlvRaw, &raw); err != nil { + return 0, nil, err + } + structureProto := raw.Message.(*api.SRv6StructureSubSubTLV) + structure.LocalBlockLength = uint8(structureProto.LocalBlockLength) + structure.LocatorNodeLength = uint8(structureProto.LocalNodeLength) + structure.FunctionLength = uint8(structureProto.FunctionLength) + structure.ArgumentLength = uint8(structureProto.ArgumentLength) + structure.TranspositionLength = uint8(structureProto.TranspositionLength) + structure.TranspositionOffset = uint8(structureProto.TranspositionOffset) + + // SRv6 Structure Sub Sub TLV length consists of header 3 bytes, 6 bytes of value + l += (3 + 6) + p = append(p, structure) + } + default: + return 0, nil, fmt.Errorf("unknown or not implemented Prefix SID Sub TLV type: %d", t) + } + } + + return l, p, nil +} diff --git a/pkg/packet/bgp/prefix_sid_test.go b/pkg/packet/bgp/prefix_sid_test.go index 2df8a3a3..a286228c 100644 --- a/pkg/packet/bgp/prefix_sid_test.go +++ b/pkg/packet/bgp/prefix_sid_test.go @@ -3,6 +3,9 @@ package bgp import ( "bytes" "testing" + + "github.com/golang/protobuf/ptypes/any" + api "github.com/osrg/gobgp/api" ) func TestRoundTripSubSubTLV(t *testing.T) { @@ -88,3 +91,34 @@ func TestRoundTripPrefixSID(t *testing.T) { }) } } + +func TestNewPathAttributePrefixSID(t *testing.T) { + tests := []struct { + name string + input *api.PrefixSID + }{ + { + name: "path attribute srv6 prefix sid", + input: &api.PrefixSID{ + Tlvs: []*any.Any{ + { + TypeUrl: "type.googleapis.com/gobgpapi.SRv6L3ServiceTLV", + Value: []byte{10, 157, 1, 8, 1, 18, 152, 1, 10, 149, 1, 10, 50, 116, 121, 112, 101, 46, 103, 111, 111, 103, 108, 101, 97, 112, 105, 115, 46, 99, 111, 109, 47, 103, 111, 98, 103, 112, 97, 112, 105, 46, 83, 82, 118, 54, 73, 110, 102, 111, 114, 109, 97, 116, 105, 111, 110, 83, 117, 98, 84, 76, 86, 18, 95, 10, 16, 32, 1, 0, 0, 0, 5, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 18, 0, 24, 17, 34, 71, 8, 1, 18, 67, 10, 65, 10, 51, 116, 121, 112, 101, 46, 103, 111, 111, 103, 108, 101, 97, 112, 105, 115, 46, 99, 111, 109, 47, 103, 111, 98, 103, 112, 97, 112, 105, 46, 83, 82, 118, 54, 83, 116, 114, 117, 99, 116, 117, 114, 101, 83, 117, 98, 83, 117, 98, 84, 76, 86, 18, 10, 8, 40, 16, 24, 24, 16, 40, 16, 48, 64}, + }, + }, + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + p, err := NewPathAttributePrefixSID(tt.input) + if err != nil { + t.Fatalf("failed with error: %+v", err) + } + t.Logf("resulting prefix sid: %s", p.String()) + b, _ := p.Serialize() + t.Logf("serialized prefix sid: %s", string(b)) + + }) + } +} |