Skip to content

Commit 19057e8

Browse files
foyerunixaboch
authored andcommitted
Fix RouteListFiltered when using FAMILY_ALL
1 parent 6765a44 commit 19057e8

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

route_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ func (h *Handle) RouteListFiltered(family int, filter *Route, filterMask uint64)
11481148
var res []Route
11491149
for _, m := range msgs {
11501150
msg := nl.DeserializeRtMsg(m)
1151-
if msg.Family != uint8(family) {
1151+
if family != FAMILY_ALL && msg.Family != uint8(family) {
11521152
// Ignore routes not matching requested family
11531153
continue
11541154
}

route_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,79 @@ func TestRouteFilterAllTables(t *testing.T) {
876876
}
877877
}
878878

879+
func TestRouteFilterByFamily(t *testing.T) {
880+
tearDown := setUpNetlinkTest(t)
881+
defer tearDown()
882+
883+
const table int = 999
884+
885+
// get loopback interface
886+
link, err := LinkByName("lo")
887+
if err != nil {
888+
t.Fatal(err)
889+
}
890+
// bring the interface up
891+
if err = LinkSetUp(link); err != nil {
892+
t.Fatal(err)
893+
}
894+
895+
// add a IPv4 gateway route
896+
dst4 := &net.IPNet{
897+
IP: net.IPv4(2, 2, 0, 0),
898+
Mask: net.CIDRMask(24, 32),
899+
}
900+
route4 := Route{LinkIndex: link.Attrs().Index, Dst: dst4, Table: table}
901+
if err := RouteAdd(&route4); err != nil {
902+
t.Fatal(err)
903+
}
904+
905+
// add a IPv6 gateway route
906+
dst6 := &net.IPNet{
907+
IP: net.ParseIP("2001:db9::0"),
908+
Mask: net.CIDRMask(64, 128),
909+
}
910+
route6 := Route{LinkIndex: link.Attrs().Index, Dst: dst6, Table: table}
911+
if err := RouteAdd(&route6); err != nil {
912+
t.Fatal(err)
913+
}
914+
915+
// Get routes for both families
916+
routes_all, err := RouteListFiltered(FAMILY_ALL, &Route{Table: table}, RT_FILTER_TABLE)
917+
if err != nil {
918+
t.Fatal(err)
919+
}
920+
if len(routes_all) != 2 {
921+
t.Fatal("Filtering by FAMILY_ALL doesn't find two routes")
922+
}
923+
924+
// Get IPv4 route
925+
routes_v4, err := RouteListFiltered(FAMILY_V4, &Route{Table: table}, RT_FILTER_TABLE)
926+
if err != nil {
927+
t.Fatal(err)
928+
}
929+
if len(routes_v4) != 1 {
930+
t.Fatal("Filtering by FAMILY_V4 doesn't find one route")
931+
}
932+
933+
// Get IPv6 route
934+
routes_v6, err := RouteListFiltered(FAMILY_V6, &Route{Table: table}, RT_FILTER_TABLE)
935+
if err != nil {
936+
t.Fatal(err)
937+
}
938+
if len(routes_v6) != 1 {
939+
t.Fatal("Filtering by FAMILY_V6 doesn't find one route")
940+
}
941+
942+
// Get non-existent routes
943+
routes_non_existent, err := RouteListFiltered(99, &Route{Table: table}, RT_FILTER_TABLE)
944+
if err != nil {
945+
t.Fatal(err)
946+
}
947+
if len(routes_non_existent) != 0 {
948+
t.Fatal("Filtering by non-existent family find some route")
949+
}
950+
}
951+
879952
func tableIDIn(ids []int, id int) bool {
880953
for _, v := range ids {
881954
if v == id {

0 commit comments

Comments
 (0)