Skip to content

Commit 219c612

Browse files
committed
redirect: Fix UDP rules
1 parent a8ce383 commit 219c612

File tree

2 files changed

+74
-7
lines changed

2 files changed

+74
-7
lines changed

redirect_nftables.go

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,6 @@ func (r *autoRedirect) setupNFTables() error {
113113
Priority: nftables.ChainPriorityRef(*nftables.ChainPriorityNATDest + 2),
114114
Type: nftables.ChainTypeFilter,
115115
})
116-
if r.enableIPv4 {
117-
nftablesCreateExcludeDestinationIPSet(nft, table, chainPreRoutingUDP, 5, "inet4_local_address_set", nftables.TableFamilyIPv4, false)
118-
}
119-
if r.enableIPv6 {
120-
nftablesCreateExcludeDestinationIPSet(nft, table, chainPreRoutingUDP, 6, "inet6_local_address_set", nftables.TableFamilyIPv6, false)
121-
}
122116
nft.AddRule(&nftables.Rule{
123117
Table: table,
124118
Chain: chainPreRoutingUDP,
@@ -128,10 +122,28 @@ func (r *autoRedirect) setupNFTables() error {
128122
Register: 1,
129123
},
130124
&expr.Cmp{
131-
Op: expr.CmpOpEq,
125+
Op: expr.CmpOpNeq,
132126
Register: 1,
133127
Data: []byte{unix.IPPROTO_UDP},
134128
},
129+
&expr.Verdict{
130+
Kind: expr.VerdictReturn,
131+
},
132+
},
133+
})
134+
nft.AddRule(&nftables.Rule{
135+
Table: table,
136+
Chain: chainPreRoutingUDP,
137+
Exprs: []expr.Any{
138+
&expr.Meta{
139+
Key: expr.MetaKeyIIFNAME,
140+
Register: 1,
141+
},
142+
&expr.Cmp{
143+
Op: expr.CmpOpNeq,
144+
Register: 1,
145+
Data: nftablesIfname(r.tunOptions.Name),
146+
},
135147
&expr.Ct{
136148
Key: expr.CtKeyMARK,
137149
Register: 1,
@@ -149,6 +161,40 @@ func (r *autoRedirect) setupNFTables() error {
149161
&expr.Counter{},
150162
},
151163
})
164+
nft.AddRule(&nftables.Rule{
165+
Table: table,
166+
Chain: chainPreRoutingUDP,
167+
Exprs: []expr.Any{
168+
&expr.Ct{
169+
Key: expr.CtKeyMARK,
170+
Register: 1,
171+
},
172+
&expr.Cmp{
173+
Op: expr.CmpOpNeq,
174+
Register: 1,
175+
Data: binaryutil.NativeEndian.PutUint32(r.tunOptions.AutoRedirectInputMark),
176+
},
177+
&expr.Immediate{
178+
Register: 1,
179+
Data: binaryutil.NativeEndian.PutUint32(r.tunOptions.AutoRedirectOutputMark),
180+
},
181+
&expr.Meta{
182+
Key: expr.MetaKeyMARK,
183+
Register: 1,
184+
SourceRegister: true,
185+
},
186+
&expr.Meta{
187+
Key: expr.MetaKeyMARK,
188+
Register: 1,
189+
},
190+
&expr.Ct{
191+
Key: expr.CtKeyMARK,
192+
Register: 1,
193+
SourceRegister: true,
194+
},
195+
&expr.Counter{},
196+
},
197+
})
152198
}
153199

154200
err = r.configureOpenWRTFirewall4(nft, false)

redirect_nftables_rules.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,27 @@ func (r *autoRedirect) nftablesCreateExcludeRules(nft *nftables.Conn, table *nft
138138
},
139139
},
140140
})
141+
if chain.Type == nftables.ChainTypeRoute {
142+
nft.AddRule(&nftables.Rule{
143+
Table: table,
144+
Chain: chain,
145+
Exprs: []expr.Any{
146+
&expr.Ct{
147+
Key: expr.CtKeyMARK,
148+
Register: 1,
149+
},
150+
&expr.Cmp{
151+
Op: expr.CmpOpEq,
152+
Register: 1,
153+
Data: binaryutil.NativeEndian.PutUint32(r.tunOptions.AutoRedirectOutputMark),
154+
},
155+
&expr.Counter{},
156+
&expr.Verdict{
157+
Kind: expr.VerdictReturn,
158+
},
159+
},
160+
})
161+
}
141162
}
142163
if chain.Hooknum == nftables.ChainHookPrerouting {
143164
if len(r.tunOptions.IncludeInterface) > 0 {

0 commit comments

Comments
 (0)