Skip to content

Commit 0689718

Browse files
committed
fix: restore nftables helper functions removed with WireGuard cleanup
The WireGuard removal (931b95b) deleted firewall.go which also contained shared nftables helpers still needed by firewall_proxy.go. Extract them into firewall_helpers.go to fix the build. Made-with: Cursor
1 parent 55ac6ef commit 0689718

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
//go:build linux
2+
3+
package network
4+
5+
import (
6+
"github.com/google/nftables"
7+
"github.com/google/nftables/binaryutil"
8+
"github.com/google/nftables/expr"
9+
)
10+
11+
const ipprotoTCP = 6
12+
13+
func applyIPv6Rules(conn *nftables.Conn, loIfIndex int) {
14+
policyDrop := nftables.ChainPolicyDrop
15+
16+
table6 := conn.AddTable(&nftables.Table{
17+
Family: nftables.TableFamilyIPv6,
18+
Name: "filter6",
19+
})
20+
21+
output6 := conn.AddChain(&nftables.Chain{
22+
Name: "output6",
23+
Table: table6,
24+
Type: nftables.ChainTypeFilter,
25+
Hooknum: nftables.ChainHookOutput,
26+
Priority: nftables.ChainPriorityFilter,
27+
Policy: &policyDrop,
28+
})
29+
30+
addOifAcceptRule(conn, table6, output6, loIfIndex)
31+
}
32+
33+
func addOifAcceptRule(conn *nftables.Conn, table *nftables.Table, chain *nftables.Chain, ifIndex int) {
34+
conn.AddRule(&nftables.Rule{
35+
Table: table,
36+
Chain: chain,
37+
Exprs: []expr.Any{
38+
&expr.Meta{Key: expr.MetaKeyOIF, Register: 1},
39+
&expr.Cmp{
40+
Op: expr.CmpOpEq,
41+
Register: 1,
42+
Data: binaryutil.NativeEndian.PutUint32(uint32(ifIndex)),
43+
},
44+
&expr.Verdict{Kind: expr.VerdictAccept},
45+
},
46+
})
47+
}
48+
49+
func addIifAcceptRule(conn *nftables.Conn, table *nftables.Table, chain *nftables.Chain, ifIndex int) {
50+
conn.AddRule(&nftables.Rule{
51+
Table: table,
52+
Chain: chain,
53+
Exprs: []expr.Any{
54+
&expr.Meta{Key: expr.MetaKeyIIF, Register: 1},
55+
&expr.Cmp{
56+
Op: expr.CmpOpEq,
57+
Register: 1,
58+
Data: binaryutil.NativeEndian.PutUint32(uint32(ifIndex)),
59+
},
60+
&expr.Verdict{Kind: expr.VerdictAccept},
61+
},
62+
})
63+
}
64+
65+
func addOifCtEstablishedRule(conn *nftables.Conn, table *nftables.Table, chain *nftables.Chain, ifIndex int) {
66+
conn.AddRule(&nftables.Rule{
67+
Table: table,
68+
Chain: chain,
69+
Exprs: []expr.Any{
70+
&expr.Meta{Key: expr.MetaKeyOIF, Register: 1},
71+
&expr.Cmp{
72+
Op: expr.CmpOpEq,
73+
Register: 1,
74+
Data: binaryutil.NativeEndian.PutUint32(uint32(ifIndex)),
75+
},
76+
&expr.Ct{Register: 1, SourceRegister: false, Key: expr.CtKeySTATE},
77+
&expr.Bitwise{
78+
SourceRegister: 1,
79+
DestRegister: 1,
80+
Len: 4,
81+
Mask: binaryutil.NativeEndian.PutUint32(expr.CtStateBitESTABLISHED | expr.CtStateBitRELATED),
82+
Xor: binaryutil.NativeEndian.PutUint32(0),
83+
},
84+
&expr.Cmp{
85+
Op: expr.CmpOpNeq,
86+
Register: 1,
87+
Data: []byte{0, 0, 0, 0},
88+
},
89+
&expr.Verdict{Kind: expr.VerdictAccept},
90+
},
91+
})
92+
}
93+
94+
func addIifCtEstablishedRule(conn *nftables.Conn, table *nftables.Table, chain *nftables.Chain, ifIndex int) {
95+
conn.AddRule(&nftables.Rule{
96+
Table: table,
97+
Chain: chain,
98+
Exprs: []expr.Any{
99+
&expr.Meta{Key: expr.MetaKeyIIF, Register: 1},
100+
&expr.Cmp{
101+
Op: expr.CmpOpEq,
102+
Register: 1,
103+
Data: binaryutil.NativeEndian.PutUint32(uint32(ifIndex)),
104+
},
105+
&expr.Ct{Register: 1, SourceRegister: false, Key: expr.CtKeySTATE},
106+
&expr.Bitwise{
107+
SourceRegister: 1,
108+
DestRegister: 1,
109+
Len: 4,
110+
Mask: binaryutil.NativeEndian.PutUint32(expr.CtStateBitESTABLISHED | expr.CtStateBitRELATED),
111+
Xor: binaryutil.NativeEndian.PutUint32(0),
112+
},
113+
&expr.Cmp{
114+
Op: expr.CmpOpNeq,
115+
Register: 1,
116+
Data: []byte{0, 0, 0, 0},
117+
},
118+
&expr.Verdict{Kind: expr.VerdictAccept},
119+
},
120+
})
121+
}
122+
123+
func addIifTCPDportAcceptRule(conn *nftables.Conn, table *nftables.Table, chain *nftables.Chain, ifIndex int, port uint16) {
124+
conn.AddRule(&nftables.Rule{
125+
Table: table,
126+
Chain: chain,
127+
Exprs: []expr.Any{
128+
&expr.Meta{Key: expr.MetaKeyIIF, Register: 1},
129+
&expr.Cmp{
130+
Op: expr.CmpOpEq,
131+
Register: 1,
132+
Data: binaryutil.NativeEndian.PutUint32(uint32(ifIndex)),
133+
},
134+
&expr.Meta{Key: expr.MetaKeyL4PROTO, Register: 1},
135+
&expr.Cmp{
136+
Op: expr.CmpOpEq,
137+
Register: 1,
138+
Data: []byte{ipprotoTCP},
139+
},
140+
&expr.Payload{
141+
DestRegister: 1,
142+
Base: expr.PayloadBaseTransportHeader,
143+
Offset: 2,
144+
Len: 2,
145+
},
146+
&expr.Cmp{
147+
Op: expr.CmpOpEq,
148+
Register: 1,
149+
Data: binaryutil.BigEndian.PutUint16(port),
150+
},
151+
&expr.Verdict{Kind: expr.VerdictAccept},
152+
},
153+
})
154+
}

0 commit comments

Comments
 (0)