From b5f3fecc25df3b25e34658f6b08abb51b303b02e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Tue, 26 Aug 2025 14:29:48 +0800 Subject: [PATCH] ping: Fix linux route rules --- redirect_nftables.go | 25 ++++++++++++++++++++----- tun_linux.go | 18 ------------------ 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/redirect_nftables.go b/redirect_nftables.go index ec4bf5d..0123953 100644 --- a/redirect_nftables.go +++ b/redirect_nftables.go @@ -143,12 +143,26 @@ func (r *autoRedirect) setupNFTables() error { } } chainPreRoutingUDP := nft.AddChain(&nftables.Chain{ - Name: "prerouting_udp", + Name: "prerouting_udp_icmp", Table: table, Hooknum: nftables.ChainHookPrerouting, Priority: nftables.ChainPriorityRef(*nftables.ChainPriorityNATDest + 2), Type: nftables.ChainTypeFilter, }) + ipProto := &nftables.Set{ + Table: table, + Anonymous: true, + Constant: true, + KeyType: nftables.TypeInetProto, + } + err = nft.AddSet(ipProto, []nftables.SetElement{ + {Key: []byte{unix.IPPROTO_UDP}}, + {Key: []byte{unix.IPPROTO_ICMP}}, + {Key: []byte{unix.IPPROTO_ICMPV6}}, + }) + if err != nil { + return err + } nft.AddRule(&nftables.Rule{ Table: table, Chain: chainPreRoutingUDP, @@ -157,10 +171,11 @@ func (r *autoRedirect) setupNFTables() error { Key: expr.MetaKeyL4PROTO, Register: 1, }, - &expr.Cmp{ - Op: expr.CmpOpNeq, - Register: 1, - Data: []byte{unix.IPPROTO_UDP}, + &expr.Lookup{ + SourceRegister: 1, + SetID: ipProto.ID, + SetName: ipProto.Name, + Invert: true, }, &expr.Verdict{ Kind: expr.VerdictReturn, diff --git a/tun_linux.go b/tun_linux.go index 6d7dfed..b19d5da 100644 --- a/tun_linux.go +++ b/tun_linux.go @@ -816,14 +816,6 @@ func (t *NativeTun) rules() []*netlink.Rule { it.Family = unix.AF_INET rules = append(rules, it) } - if p4 && !t.options.StrictRoute { - it = netlink.NewRule() - it.Priority = priority - it.IPProto = syscall.IPPROTO_ICMP - it.Goto = nopPriority - it.Family = unix.AF_INET - rules = append(rules, it) - } if p6 { it = netlink.NewRule() it.Priority = priority6 @@ -834,16 +826,6 @@ func (t *NativeTun) rules() []*netlink.Rule { it.Family = unix.AF_INET6 rules = append(rules, it) } - - if p6 && !t.options.StrictRoute { - it = netlink.NewRule() - it.Priority = priority6 - it.IPProto = syscall.IPPROTO_ICMPV6 - it.Goto = nopPriority - it.Family = unix.AF_INET6 - rules = append(rules, it) - priority6++ - } } if p4 { it = netlink.NewRule()