From 51ac6b34f1923491ead2fb3a6ded8cd081132108 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Sat, 12 Apr 2025 12:07:56 +0800 Subject: [PATCH] redirect: Fix handling of local pings --- redirect_nftables.go | 2 +- redirect_nftables_rules.go | 23 +++++++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/redirect_nftables.go b/redirect_nftables.go index e89a246..3545617 100644 --- a/redirect_nftables.go +++ b/redirect_nftables.go @@ -64,7 +64,7 @@ func (r *autoRedirect) setupNFTables() error { r.nftablesCreateRedirect(nft, table, chainOutput) chainOutputUDP := nft.AddChain(&nftables.Chain{ - Name: "output_udp", + Name: "output_udp_icmp", Table: table, Hooknum: nftables.ChainHookOutput, Priority: nftables.ChainPriorityMangle, diff --git a/redirect_nftables_rules.go b/redirect_nftables_rules.go index 8361917..b12f731 100644 --- a/redirect_nftables_rules.go +++ b/redirect_nftables_rules.go @@ -439,6 +439,20 @@ func (r *autoRedirect) nftablesCreateExcludeRules(nft *nftables.Conn, table *nft if r.tunOptions.AutoRedirectMarkMode && ((chain.Hooknum == nftables.ChainHookOutput && chain.Type == nftables.ChainTypeRoute) || (chain.Hooknum == nftables.ChainHookPrerouting && chain.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: chain, @@ -447,10 +461,11 @@ func (r *autoRedirect) nftablesCreateExcludeRules(nft *nftables.Conn, table *nft 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,