Skip to content
Open
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
4 changes: 4 additions & 0 deletions route/bgp_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,10 @@ func (b *BGPPath) Prepend(asn uint32, times uint16) {
return
}

if b.ASPath == nil {
b.ASPath = types.NewASPath(nil)
}

if len(*b.ASPath) == 0 {
b.insertNewASSequence()
}
Expand Down
173 changes: 173 additions & 0 deletions route/mixed_path_type_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package route

import (
"testing"

bnet "github.com/bio-routing/bio-rd/net"
"github.com/bio-routing/bio-rd/protocols/bgp/types"
)

// TestECMP_MixedPathTypes_BGPvsStatic verifies that ECMP does not panic
// when comparing a BGPPathType path against a StaticPathType path.
// This is the primary crash: bio-rd calls p.BGPPath.ECMP(q.BGPPath)
// without checking that q is also BGPPathType. When q is StaticPathType,
// q.BGPPath is nil → nil pointer dereference.
func TestECMP_MixedPathTypes_BGPvsStatic(t *testing.T) {
bgpPath := &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
BGPPathA: &BGPPathA{
NextHop: bnet.IPv4(1).Ptr(),
LocalPref: 100,
Origin: 0,
Source: bnet.IPv4(1).Ptr(),
},
ASPath: types.NewASPath([]uint32{64512}),
ASPathLen: 1,
},
}

staticPath := &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
NextHop: bnet.IPv4(2).Ptr(),
},
}

// Must not panic — should return false (different types are never ECMP-equal)
result := bgpPath.ECMP(staticPath)
if result {
t.Error("ECMP should return false for different path types")
}
}

// TestECMP_MixedPathTypes_StaticvsBGP is the reverse direction.
func TestECMP_MixedPathTypes_StaticvsBGP(t *testing.T) {
staticPath := &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
NextHop: bnet.IPv4(1).Ptr(),
},
}

bgpPath := &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
BGPPathA: &BGPPathA{
NextHop: bnet.IPv4(2).Ptr(),
LocalPref: 100,
Origin: 0,
Source: bnet.IPv4(2).Ptr(),
},
ASPath: types.NewASPath([]uint32{64513}),
ASPathLen: 1,
},
}

result := staticPath.ECMP(bgpPath)
if result {
t.Error("ECMP should return false for different path types")
}
}

// TestECMP_MixedPathTypes_FIBvsBGP covers the FIBPathType case.
func TestECMP_MixedPathTypes_FIBvsBGP(t *testing.T) {
fibPath := &Path{
Type: FIBPathType,
FIBPath: &FIBPath{
NextHop: bnet.IPv4(1).Ptr(),
},
}

bgpPath := &Path{
Type: BGPPathType,
BGPPath: &BGPPath{
BGPPathA: &BGPPathA{
NextHop: bnet.IPv4(2).Ptr(),
LocalPref: 100,
Origin: 0,
Source: bnet.IPv4(2).Ptr(),
},
ASPath: types.NewASPath([]uint32{64512}),
ASPathLen: 1,
},
}

result := fibPath.ECMP(bgpPath)
if result {
t.Error("ECMP should return false for different path types")
}
}

// TestRouteUpdateEqualPathCount_MixedTypes verifies that
// Route.updateEqualPathCount does not panic when the route has paths
// of different types (the real-world scenario: locally-originated
// StaticPath + BGP-learned BGPPath for the same anycast prefix).
func TestRouteUpdateEqualPathCount_MixedTypes(t *testing.T) {
pfx := bnet.NewPfx(bnet.IPv4FromOctets(10, 0, 99, 0), 24)

r := NewRoute(pfx.Ptr(), &Path{
Type: StaticPathType,
StaticPath: &StaticPath{
NextHop: bnet.IPv4(1).Ptr(),
},
})

r.AddPath(&Path{
Type: BGPPathType,
BGPPath: &BGPPath{
BGPPathA: &BGPPathA{
NextHop: bnet.IPv4(2).Ptr(),
LocalPref: 100,
Origin: 0,
Source: bnet.IPv4(2).Ptr(),
},
ASPath: types.NewASPath([]uint32{64513}),
ASPathLen: 1,
},
})

// Trigger path selection, which in turn calls updateEqualPathCount.
r.PathSelection()

// Must not panic and should result in a single ECMP path (static preferred).
count := r.ECMPPathCount()
if count != 1 {
t.Errorf("unexpected ECMP count: got %d, want 1", count)
}
}

// TestBGPPathPrepend_NilASPath verifies that Prepend does not panic
// when ASPath is nil. This happens when a route is originated locally
// as BGPPathType without initializing the ASPath field.
func TestBGPPathPrepend_NilASPath(t *testing.T) {
p := &BGPPath{
BGPPathA: &BGPPathA{
NextHop: bnet.IPv4(1).Ptr(),
LocalPref: 200,
Origin: 0,
Source: bnet.IPv4(1).Ptr(),
},
ASPath: nil, // not initialized
}

// Must not panic — should handle nil ASPath gracefully
p.Prepend(64512, 1)

if p.ASPath == nil {
t.Error("Prepend should initialize ASPath if nil")
}
if p.ASPathLen == 0 {
t.Error("Prepend should increase ASPathLen")
}
if len(*p.ASPath) == 0 {
t.Error("Prepend should add at least one AS_PATH segment")
return
}
firstSeg := (*p.ASPath)[0]
if len(firstSeg.ASNs) == 0 {
t.Error("Prepend should add at least one ASN to the first AS_PATH segment")
} else if firstSeg.ASNs[0] != 64512 {
t.Errorf("Prepend should add prepended ASN 64512, got %d", firstSeg.ASNs[0])
}
Comment on lines +163 to +172

Copilot AI Mar 29, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TestBGPPathPrepend_NilASPath asserts len(*p.ASPath) != 0, but that only checks that at least one AS_PATH segment exists, not that Prepend actually added an ASN. To validate behavior, assert on p.ASPathLen and/or that the first segment contains the prepended ASN (e.g., len((*p.ASPath)[0].ASNs) > 0 and value matches).

Suggested change
if len(*p.ASPath) == 0 {
t.Error("Prepend should have added an AS segment")
}
if p.ASPathLen == 0 {
t.Error("Prepend should increase ASPathLen")
}
if len(*p.ASPath) == 0 {
t.Error("Prepend should add at least one AS_PATH segment")
return
}
firstSeg := (*p.ASPath)[0]
if len(firstSeg.ASNs) == 0 {
t.Error("Prepend should add at least one ASN to the first AS_PATH segment")
} else if firstSeg.ASNs[0] != 64512 {
t.Errorf("Prepend should add prepended ASN 64512, got %d", firstSeg.ASNs[0])
}

Copilot uses AI. Check for mistakes.
}
4 changes: 4 additions & 0 deletions route/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ func (p *Path) Select(q *Path) int8 {

// ECMP checks if path p and q are equal enough to be considered for ECMP usage
func (p *Path) ECMP(q *Path) bool {
if p.Type != q.Type {
return false
}

switch p.Type {
case BGPPathType:
return p.BGPPath.ECMP(q.BGPPath)
Expand Down
Loading