/
nftables.go
356 lines (297 loc) · 8.32 KB
/
nftables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package kubespan
import (
"fmt"
"github.com/google/nftables"
"github.com/google/nftables/binaryutil"
"github.com/google/nftables/expr"
"inet.af/netaddr"
)
// NfTablesManager manages nftables outside of controllers/resources scope.
type NfTablesManager interface {
Update(*netaddr.IPSet) error
Cleanup() error
}
// NewNfTablesManager initializes NfTablesManager.
func NewNfTablesManager(externalMark, internalMark, markMask uint32) NfTablesManager {
nfTable := &nftables.Table{
Family: nftables.TableFamilyINet,
Name: "talos_kubespan",
}
return &nfTablesManager{
ExternalMark: externalMark,
InternalMark: internalMark,
MarkMask: markMask,
nfTable: nfTable,
targetSet4: &nftables.Set{
Name: "kubespan_targets_ipv4",
Table: nfTable,
Interval: true,
KeyType: nftables.TypeIPAddr, // prefix
DataType: nftables.TypeInteger, // mask
},
targetSet6: &nftables.Set{
Name: "kubespan_targets_ipv6",
Table: nfTable,
Interval: true,
KeyType: nftables.TypeIP6Addr,
},
}
}
type nfTablesManager struct {
InternalMark uint32
ExternalMark uint32
MarkMask uint32
currentSet *netaddr.IPSet
// nfTable is a handle for the KubeSpan root table
nfTable *nftables.Table
// targetSet4 is a handle for the IPv4 target IP nftables set
targetSet4 *nftables.Set
// targetSet6 is a handle for the IPv6 target IP nftables set
targetSet6 *nftables.Set
}
// Update the nftables rules based on the IPSet.
func (m *nfTablesManager) Update(desired *netaddr.IPSet) error {
if m.currentSet != nil && m.currentSet.Equal(desired) {
return nil
}
if err := m.setNFTable(desired); err != nil {
return fmt.Errorf("failed to update IP sets: %w", err)
}
m.currentSet = desired
return nil
}
// Cleanup the nftables rules.
func (m *nfTablesManager) Cleanup() error {
foundExisting, err := m.tableExists()
if err != nil {
return err
}
if !foundExisting {
return nil
}
c := &nftables.Conn{}
c.FlushSet(m.targetSet4)
c.FlushSet(m.targetSet6)
c.FlushTable(m.nfTable)
c.DelSet(m.targetSet4)
c.DelSet(m.targetSet6)
c.DelTable(m.nfTable)
if err := c.Flush(); err != nil {
return fmt.Errorf("failed to execute nftable cleanup: %w", err)
}
return nil
}
func (m *nfTablesManager) tableExists() (bool, error) {
c := &nftables.Conn{}
tables, err := c.ListTables()
if err != nil {
return false, fmt.Errorf("error listing tables: %w", err)
}
foundExisting := false
for _, table := range tables {
if table.Name == m.nfTable.Name && table.Family == m.nfTable.Family {
foundExisting = true
break
}
}
return foundExisting, nil
}
func (m *nfTablesManager) setNFTable(ips *netaddr.IPSet) error {
c := &nftables.Conn{}
// NB: sets should be flushed before new members because nftables will fail
// if there are any conflicts between existing ranges and new ranges.
foundExisting, err := m.tableExists()
if err != nil {
return err
}
if foundExisting {
c.FlushSet(m.targetSet4)
c.FlushSet(m.targetSet6)
c.FlushTable(m.nfTable)
}
// Basic boilerplate; create a table & chain.
c.AddTable(m.nfTable)
preChain := c.AddChain(&nftables.Chain{
Name: "kubespan_prerouting",
Table: m.nfTable,
Type: nftables.ChainTypeFilter,
Hooknum: nftables.ChainHookPrerouting,
Priority: nftables.ChainPriorityFilter,
})
outChain := c.AddChain(&nftables.Chain{
Name: "kubespan_outgoing",
Table: m.nfTable,
Type: nftables.ChainTypeRoute,
Hooknum: nftables.ChainHookOutput,
Priority: nftables.ChainPriorityFilter,
})
setElements4, setElements6 := m.setElements(ips)
if err := c.AddSet(m.targetSet4, setElements4); err != nil {
return fmt.Errorf("failed to add IPv4 set: %w", err)
}
if err := c.AddSet(m.targetSet6, setElements6); err != nil {
return fmt.Errorf("failed to add IPv6 set: %w", err)
}
// meta mark & 0x00000060 == 0x00000020 accept
ruleExpr := []expr.Any{
// Load the firewall mark into register 1
&expr.Meta{
Key: expr.MetaKeyMARK,
Register: 1,
},
// Mask the mark with the configured mask:
// R1 = R1 & mask
&expr.Bitwise{
SourceRegister: 1,
DestRegister: 1,
Len: 4,
Xor: binaryutil.NativeEndian.PutUint32(0),
Mask: binaryutil.NativeEndian.PutUint32(m.MarkMask),
},
// Compare the masked firewall mark with expected value
&expr.Cmp{
Op: expr.CmpOpEq,
Register: 1,
Data: binaryutil.NativeEndian.PutUint32(m.ExternalMark),
},
// Accept the packet to stop the ruleset processing
&expr.Verdict{
Kind: expr.VerdictAccept,
},
}
// match fwmark of Wireguard interface (not kubespan mark)
// accept and return without modifying the table or mark
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: preChain,
Exprs: ruleExpr,
})
// match fwmark of Wireguard interface (not kubespan mark)
// accept and return without modifying the table or mark
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: outChain,
Exprs: ruleExpr,
})
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: preChain,
Exprs: matchIPv4Set(m.targetSet4, m.InternalMark, m.MarkMask),
})
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: preChain,
Exprs: matchIPv6Set(m.targetSet6, m.InternalMark, m.MarkMask),
})
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: outChain,
Exprs: matchIPv4Set(m.targetSet4, m.InternalMark, m.MarkMask),
})
c.AddRule(&nftables.Rule{
Table: m.nfTable,
Chain: outChain,
Exprs: matchIPv6Set(m.targetSet6, m.InternalMark, m.MarkMask),
})
if err := c.Flush(); err != nil {
return fmt.Errorf("failed to execute nftable creation: %w", err)
}
return nil
}
func matchIPv4Set(set *nftables.Set, mark, mask uint32) []expr.Any {
return matchIPSet(set, mark, mask, nftables.TableFamilyIPv4)
}
func matchIPv6Set(set *nftables.Set, mark, mask uint32) []expr.Any {
return matchIPSet(set, mark, mask, nftables.TableFamilyIPv6)
}
func matchIPSet(set *nftables.Set, mark, mask uint32, family nftables.TableFamily) []expr.Any {
var (
offset uint32 = 16
length uint32 = 4
)
if family == nftables.TableFamilyIPv6 {
offset = 24
length = 16
}
// ip daddr @kubespan_targets_ipv4 meta mark set meta mark & 0xffffffdf | 0x00000040 accept
return []expr.Any{
// Store protocol type to register 1
&expr.Meta{
Key: expr.MetaKeyNFPROTO,
Register: 1,
},
// Match IP Family
&expr.Cmp{
Op: expr.CmpOpEq,
Register: 1,
Data: []byte{byte(family)},
},
// Store the destination IP address to register 1
&expr.Payload{
DestRegister: 1,
Base: expr.PayloadBaseNetworkHeader,
Offset: offset,
Len: length,
},
// Match from target set
&expr.Lookup{
SourceRegister: 1,
SetName: set.Name,
SetID: set.ID,
},
// Load the current packet mark into register 1
&expr.Meta{
Key: expr.MetaKeyMARK,
Register: 1,
},
// This bitwise is equivalent to: R1 = R1 | (R1 & mask | mark)
//
// The NFTables backend bitwise operation is R3 = R2 & MASK ^ XOR,
// so we need to do a bit of a trick to do what we need: R1 = R1 & ^mask ^ mark
&expr.Bitwise{
SourceRegister: 1,
DestRegister: 1,
Len: 4,
Xor: binaryutil.NativeEndian.PutUint32(mark),
Mask: binaryutil.NativeEndian.PutUint32(^mask),
},
// Set firewall mark to the value computed in register 1
&expr.Meta{
Key: expr.MetaKeyMARK,
SourceRegister: true,
Register: 1,
},
// Accept the packet to stop the ruleset processing
&expr.Verdict{
Kind: expr.VerdictAccept,
},
}
}
func (m *nfTablesManager) setElements(ips *netaddr.IPSet) (setElements4, setElements6 []nftables.SetElement) {
if ips == nil {
return nil, nil
}
for _, r := range ips.Ranges() {
fromBin, _ := r.From().MarshalBinary() //nolint:errcheck // doesn't fail
toBin, _ := r.To().Next().MarshalBinary() //nolint:errcheck // doesn't fail
se := []nftables.SetElement{
{
Key: fromBin,
IntervalEnd: false,
},
{
Key: toBin,
IntervalEnd: true,
},
}
if r.From().Is6() {
setElements6 = append(setElements6, se...)
} else {
setElements4 = append(setElements4, se...)
}
}
return setElements4, setElements6
}