Skip to content

Commit

Permalink
Fix RouteListFiltered when using FAMILY_ALL
Browse files Browse the repository at this point in the history
  • Loading branch information
foyerunix authored and aboch committed Apr 3, 2024
1 parent 6765a44 commit 19057e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
2 changes: 1 addition & 1 deletion route_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
var res []Route
for _, m := range msgs {
msg := nl.DeserializeRtMsg(m)
if msg.Family != uint8(family) {
if family != FAMILY_ALL && msg.Family != uint8(family) {
// Ignore routes not matching requested family
continue
}
Expand Down
73 changes: 73 additions & 0 deletions route_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,6 +876,79 @@ func TestRouteFilterAllTables(t *testing.T) {
}
}

func TestRouteFilterByFamily(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()

const table int = 999

// get loopback interface
link, err := LinkByName("lo")
if err != nil {
t.Fatal(err)
}
// bring the interface up
if err = LinkSetUp(link); err != nil {
t.Fatal(err)
}

// add a IPv4 gateway route
dst4 := &net.IPNet{
IP: net.IPv4(2, 2, 0, 0),
Mask: net.CIDRMask(24, 32),
}
route4 := Route{LinkIndex: link.Attrs().Index, Dst: dst4, Table: table}
if err := RouteAdd(&route4); err != nil {
t.Fatal(err)
}

// add a IPv6 gateway route
dst6 := &net.IPNet{
IP: net.ParseIP("2001:db9::0"),
Mask: net.CIDRMask(64, 128),
}
route6 := Route{LinkIndex: link.Attrs().Index, Dst: dst6, Table: table}
if err := RouteAdd(&route6); err != nil {
t.Fatal(err)
}

// Get routes for both families
routes_all, err := RouteListFiltered(FAMILY_ALL, &Route{Table: table}, RT_FILTER_TABLE)
if err != nil {
t.Fatal(err)
}
if len(routes_all) != 2 {
t.Fatal("Filtering by FAMILY_ALL doesn't find two routes")
}

// Get IPv4 route
routes_v4, err := RouteListFiltered(FAMILY_V4, &Route{Table: table}, RT_FILTER_TABLE)
if err != nil {
t.Fatal(err)
}
if len(routes_v4) != 1 {
t.Fatal("Filtering by FAMILY_V4 doesn't find one route")
}

// Get IPv6 route
routes_v6, err := RouteListFiltered(FAMILY_V6, &Route{Table: table}, RT_FILTER_TABLE)
if err != nil {
t.Fatal(err)
}
if len(routes_v6) != 1 {
t.Fatal("Filtering by FAMILY_V6 doesn't find one route")
}

// Get non-existent routes
routes_non_existent, err := RouteListFiltered(99, &Route{Table: table}, RT_FILTER_TABLE)
if err != nil {
t.Fatal(err)
}
if len(routes_non_existent) != 0 {
t.Fatal("Filtering by non-existent family find some route")
}
}

func tableIDIn(ids []int, id int) bool {
for _, v := range ids {
if v == id {
Expand Down

0 comments on commit 19057e8

Please sign in to comment.