Skip to content
Snippets Groups Projects
Commit 1eff34e0 authored by Daniel Czerwonk's avatar Daniel Czerwonk
Browse files

added asn4 cap to open message

parent 433f77dc
No related branches found
No related tags found
No related merge requests found
......@@ -219,13 +219,7 @@ func (fsm *FSM) resetConnectRetryCounter() {
}
func (fsm *FSM) sendOpen() error {
msg := packet.SerializeOpenMsg(&packet.BGPOpen{
Version: BGPVersion,
ASN: uint16(fsm.peer.localASN),
HoldTime: uint16(fsm.peer.holdTime / time.Second),
BGPIdentifier: fsm.peer.server.routerID,
OptParams: fsm.peer.optOpenParams,
})
msg := packet.SerializeOpenMsg(fsm.openMessage())
_, err := fsm.con.Write(msg)
if err != nil {
......@@ -235,6 +229,24 @@ func (fsm *FSM) sendOpen() error {
return nil
}
func (fsm *FSM) openMessage() *packet.BGPOpen {
return &packet.BGPOpen{
Version: BGPVersion,
ASN: fsm.local16BitASN(),
HoldTime: uint16(fsm.peer.holdTime / time.Second),
BGPIdentifier: fsm.peer.routerID,
OptParams: fsm.peer.optOpenParams,
}
}
func (fsm *FSM) local16BitASN() uint16 {
if fsm.peer.localASN > uint32(^uint16(0)) {
return packet.ASTransASN
}
return uint16(fsm.peer.localASN)
}
func (fsm *FSM) sendNotification(errorCode uint8, errorSubCode uint8) error {
msg := packet.SerializeNotificationMsg(&packet.BGPNotification{})
......
package server
import (
"testing"
"time"
"github.com/bio-routing/bio-rd/protocols/bgp/packet"
"github.com/stretchr/testify/assert"
)
func TestOpenMessage(t *testing.T) {
tests := []struct {
name string
localASN uint32
holdTime time.Duration
routerID uint32
expected packet.BGPOpen
}{
{
name: "16bit ASN",
localASN: 12345,
holdTime: time.Duration(30 * time.Second),
routerID: 1,
expected: packet.BGPOpen{
ASN: 12345,
BGPIdentifier: 1,
HoldTime: 30,
OptParams: []packet.OptParam{
packet.OptParam{
Type: packet.CapabilitiesParamType,
Value: packet.Capabilities{
packet.Capability{
Code: 65,
Value: packet.ASN4Capability{
ASN4: 12345,
},
},
},
},
},
Version: 4,
},
},
{
name: "32bit ASN",
localASN: 202739,
holdTime: time.Duration(30 * time.Second),
routerID: 1,
expected: packet.BGPOpen{
ASN: 23456,
BGPIdentifier: 1,
HoldTime: 30,
OptParams: []packet.OptParam{
packet.OptParam{
Type: packet.CapabilitiesParamType,
Value: packet.Capabilities{
packet.Capability{
Code: 65,
Value: packet.ASN4Capability{
ASN4: 202739,
},
},
},
},
},
Version: 4,
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
p := Peer{
localASN: test.localASN,
holdTime: test.holdTime,
routerID: test.routerID,
optOpenParams: []packet.OptParam{
packet.OptParam{
Type: packet.CapabilitiesParamType,
Value: packet.Capabilities{
packet.Capability{
Code: 65,
Value: packet.ASN4Capability{
ASN4: test.localASN,
},
},
},
},
},
}
fsm := newFSM2(&p)
msg := fsm.openMessage()
assert.Equal(t, &test.expected, msg)
})
}
}
......@@ -87,7 +87,6 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer
server: server,
addr: c.PeerAddress,
peerASN: c.PeerAS,
localASN: c.LocalAS,
fsms: make([]*FSM, 0),
rib: rib,
addPathSend: c.AddPathSend,
......@@ -103,24 +102,12 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer
caps := make([]packet.Capability, 0)
addPath := uint8(0)
if c.AddPathRecv {
addPath += packet.AddPathReceive
}
if !c.AddPathSend.BestOnly {
addPath += packet.AddPathSend
addPathEnabled, addPathCap := handleAddPathCapability(c)
if addPathEnabled {
caps = append(caps, addPathCap)
}
if addPath > 0 {
caps = append(caps, packet.Capability{
Code: packet.AddPathCapabilityCode,
Value: packet.AddPathCapability{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
SendReceive: addPath,
},
})
}
caps = append(caps, asn4Capability(c))
for _, cap := range caps {
p.optOpenParams = append(p.optOpenParams, packet.OptParam{
......@@ -132,6 +119,38 @@ func NewPeer(c config.Peer, rib routingtable.RouteTableClient, server *BGPServer
return p, nil
}
func asn4Capability(c config.Peer) packet.Capability {
return packet.Capability{
Code: packet.ASN4CapabilityCode,
Value: packet.ASN4Capability{
ASN4: c.LocalAS,
},
}
}
func handleAddPathCapability(c config.Peer) (bool, packet.Capability) {
addPath := uint8(0)
if c.AddPathRecv {
addPath += packet.AddPathReceive
}
if !c.AddPathSend.BestOnly {
addPath += packet.AddPathSend
}
if addPath == 0 {
return false, packet.Capability{}
}
return true, packet.Capability{
Code: packet.AddPathCapabilityCode,
Value: packet.AddPathCapability{
AFI: packet.IPv4AFI,
SAFI: packet.UnicastSAFI,
SendReceive: addPath,
},
}
}
func filterOrDefault(f *filter.Filter) *filter.Filter {
if f != nil {
return f
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment