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
38 changes: 30 additions & 8 deletions packages/orchestrator/pkg/sandbox/network/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,41 @@ func getDefaultGateway(ctx context.Context) (string, error) {
}

for _, route := range routes {
// 0.0.0.0/0
if route.Dst.String() == "0.0.0.0/0" && route.Gw != nil {
logger.L().Info(ctx, "default gateway", zap.String("gateway", route.Gw.String()))
if route.Gw == nil || !isDefaultRoute(route) {
continue
}

link, linkErr := netlink.LinkByIndex(route.LinkIndex)
logger.L().Info(ctx, "default gateway", zap.String("gateway", route.Gw.String()))

if linkErr != nil {
return "", fmt.Errorf("error fetching interface for default gateway: %w", linkErr)
}
link, linkErr := netlink.LinkByIndex(route.LinkIndex)

return link.Attrs().Name, nil
if linkErr != nil {
return "", fmt.Errorf("error fetching interface for default gateway: %w", linkErr)
}

return link.Attrs().Name, nil
}

return "", errors.New("cannot find default gateway")
}

// isDefaultRoute reports whether route is an IPv4 default route (0.0.0.0/0).
//
// The kernel represents it in one of two ways depending on kernel/netlink
// version: with Dst == nil (RTA_DST omitted) or with an explicit 0.0.0.0/0 Dst.
// Matching on the zero-length prefix accepts both. (net.IPNet.String() is
// nil-safe and returns "<nil>", so the previous route.Dst.String() check never
// panicked — it silently failed to match the nil form.)
//
// Restricted to IPv4 to preserve the original behaviour; route.Family is set
// even when Dst is nil, so it is the reliable family discriminator here.
func isDefaultRoute(route netlink.Route) bool {
if route.Family != netlink.FAMILY_V4 {
return false
}
if route.Dst == nil {
return true
}
ones, _ := route.Dst.Mask.Size()
return ones == 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//go:build linux

package network

import (
"net"
"testing"

"github.com/stretchr/testify/assert"
"github.com/vishvananda/netlink"
)

// TestIsDefaultRoute pins the default-route predicate against every way the
// kernel/netlink can represent (or fail to represent) a route. The IPv4 default
// route comes back either with Dst == nil (RTA_DST omitted) or with an explicit
// 0.0.0.0/0 Dst; both must match. A nil Gw, a non-default prefix, and the IPv6
// default must not.
func TestIsDefaultRoute(t *testing.T) {
t.Parallel()

gw := net.ParseIP("192.168.128.1")

tests := []struct {
name string
route netlink.Route
want bool
}{
{
name: "v4 default, Dst nil (RTA_DST omitted)",
route: netlink.Route{Family: netlink.FAMILY_V4, Dst: nil, Gw: gw},
want: true,
},
{
name: "v4 default, Dst 0.0.0.0/0 (RTA_DST sent)",
route: netlink.Route{Family: netlink.FAMILY_V4, Dst: &net.IPNet{IP: net.IPv4zero, Mask: net.CIDRMask(0, 32)}, Gw: gw},
want: true,
},
{
// isDefaultRoute classifies by prefix only; the Gw != nil gate
// lives in the caller (getDefaultGateway), so a gateway-less v4
// default is still a default route here.
name: "v4 default, no gateway (Gw gate is the caller's job)",
route: netlink.Route{Family: netlink.FAMILY_V4, Dst: nil, Gw: nil},
want: true,
},
{
name: "v4 non-default 10.0.0.0/8",
route: netlink.Route{Family: netlink.FAMILY_V4, Dst: &net.IPNet{IP: net.IPv4(10, 0, 0, 0), Mask: net.CIDRMask(8, 32)}, Gw: gw},
want: false,
},
{
name: "v6 default ::/0 is not an IPv4 default",
route: netlink.Route{Family: netlink.FAMILY_V6, Dst: &net.IPNet{IP: net.IPv6unspecified, Mask: net.CIDRMask(0, 128)}, Gw: gw},
want: false,
},
{
name: "v6 default with nil Dst is not an IPv4 default",
route: netlink.Route{Family: netlink.FAMILY_V6, Dst: nil, Gw: gw},
want: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
assert.Equal(t, tt.want, isDefaultRoute(tt.route))
})
}
}

// TestIsDefaultRoute_NilDstStringIsNilSafe documents the premise the fix rests
// on: calling String() on a nil *net.IPNet does not panic (it returns "<nil>"),
// so the original code never panicked on the nil default route — it just missed
// it. This guards against a regression back to a String()-equality check.
func TestIsDefaultRoute_NilDstStringIsNilSafe(t *testing.T) {
t.Parallel()

var dst *net.IPNet
assert.Equal(t, "<nil>", dst.String())
}