From 6742058800376a47c0494eb360e266dd13ac4073 Mon Sep 17 00:00:00 2001 From: AdaAibaby Date: Tue, 10 Nov 2026 16:24:04 +0800 Subject: [PATCH] fix(orchestrator): match the default route in both of netlink's Dst forms getDefaultGateway identified the IPv4 default route with route.Dst.String() == "0.0.0.0/0". The kernel reports that route 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. The string check only matched the second form and silently missed the first. Match on the zero-length prefix via a new isDefaultRoute helper so both forms work, keeping the IPv4-only scope via route.Family (which is populated even when Dst is nil). Note: route.Dst.String() is nil-safe (net.IPNet.String() returns "" for a nil receiver), so the old code never panicked on the nil form; it just failed to find the gateway, which at package init (var defaultGateway = utils.Must(getDefaultGateway(ctx))) would panic the orchestrator on startup. Checking only Dst == nil would move that failure to hosts whose kernel sends 0.0.0.0/0 instead; the zero-prefix check covers both. Add a table-driven test over both default-route forms, a non-default prefix, and the IPv6 default; no root required. --- .../orchestrator/pkg/sandbox/network/host.go | 38 +++++++-- .../network/host_default_route_test.go | 80 +++++++++++++++++++ 2 files changed, 110 insertions(+), 8 deletions(-) create mode 100644 packages/orchestrator/pkg/sandbox/network/host_default_route_test.go diff --git a/packages/orchestrator/pkg/sandbox/network/host.go b/packages/orchestrator/pkg/sandbox/network/host.go index e85d2e6e054..5f610e19ba5 100644 --- a/packages/orchestrator/pkg/sandbox/network/host.go +++ b/packages/orchestrator/pkg/sandbox/network/host.go @@ -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 "", 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 +} diff --git a/packages/orchestrator/pkg/sandbox/network/host_default_route_test.go b/packages/orchestrator/pkg/sandbox/network/host_default_route_test.go new file mode 100644 index 00000000000..2d5510726c1 --- /dev/null +++ b/packages/orchestrator/pkg/sandbox/network/host_default_route_test.go @@ -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 ""), +// 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, "", dst.String()) +}