From e062d636ca402af73522a899d7aa8f401ca92be9 Mon Sep 17 00:00:00 2001 From: kunsonxs Date: Mon, 27 Nov 2023 15:06:15 +0800 Subject: [PATCH] fix : wireguard kernel mode max route table index --- proxy/wireguard/netlink/help_handle_linux.go | 7 ++++--- proxy/wireguard/tun_kernel_linux.go | 8 ++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/proxy/wireguard/netlink/help_handle_linux.go b/proxy/wireguard/netlink/help_handle_linux.go index 3df93a7cd2ba..a0960d7d32bc 100644 --- a/proxy/wireguard/netlink/help_handle_linux.go +++ b/proxy/wireguard/netlink/help_handle_linux.go @@ -6,8 +6,9 @@ import ( "github.com/vishvananda/netlink" ) -func (h *Handle) EmptyRouteTableIndex(family, from int) (out int, err error) { - r := &netlink.Route{Table: from} +func (h *Handle) EmptyRouteTableIndex(family int) (out int, err error) { + // maximum table index is 1023 + r := &netlink.Route{Table: 1023} for ; r.Table >= 0; r.Table-- { routeList, fErr := netlink.RouteListFiltered(family, r, netlink.RT_FILTER_TABLE) if len(routeList) == 0 || fErr != nil { @@ -15,7 +16,7 @@ func (h *Handle) EmptyRouteTableIndex(family, from int) (out int, err error) { } } if r.Table < 0 { - return 0, fmt.Errorf("failed to find available family[%d] from[%d] table index", family, from) + return 0, fmt.Errorf("failed to find available family[%d] table index", family) } return r.Table, nil } diff --git a/proxy/wireguard/tun_kernel_linux.go b/proxy/wireguard/tun_kernel_linux.go index 277bd0295365..dc7fbf02a4fb 100644 --- a/proxy/wireguard/tun_kernel_linux.go +++ b/proxy/wireguard/tun_kernel_linux.go @@ -218,17 +218,17 @@ func createKernelTun(localAddresses []netip.Addr, mtu int, handler promiscuousMo } }() - ipv4TableIndex := 3000 + ipv4TableIndex := -1 if v4Enable { - ipv4TableIndex, err = out.handle.EmptyRouteTableIndex(netlink.FAMILY_V4, ipv4TableIndex) + ipv4TableIndex, err = out.handle.EmptyRouteTableIndex(netlink.FAMILY_V4) if err != nil { return nil, err } } - ipv6TableIndex := 3000 + ipv6TableIndex := -1 if v6Enable { - ipv6TableIndex, err = out.handle.EmptyRouteTableIndex(netlink.FAMILY_V6, ipv6TableIndex) + ipv6TableIndex, err = out.handle.EmptyRouteTableIndex(netlink.FAMILY_V6) if err != nil { return nil, err }