/
e2e.go
409 lines (394 loc) · 11.4 KB
/
e2e.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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package main
import (
"fmt"
"os"
"runtime"
"runtime/pprof"
"golang.org/x/sys/unix"
"github.com/google/nftables"
"github.com/sbezverk/nftableslib"
"github.com/sbezverk/nftableslib/pkg/e2e/setenv"
"github.com/sbezverk/nftableslib/pkg/e2e/validations"
)
var accept = nftableslib.ChainPolicyAccept
func init() {
runtime.LockOSThread()
}
func main() {
tests := []setenv.NFTablesTest{
{
Name: "IPV4 ICMP Drop",
Version: nftables.TableFamilyIPv4,
DstNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeFilter,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookInput,
Policy: &accept,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_ICMP),
Dst: &nftableslib.IPAddrSpec{
List: []*nftableslib.IPAddr{setIPAddr("1.1.1.2")},
},
},
Action: setActionVerdict(nftableslib.NFT_DROP),
},
},
},
},
Saddr: "1.1.1.1/24",
Daddr: "1.1.1.2/24",
Validation: validations.ICMPDropTestValidation,
},
{
Name: "IPV4 Redirecting TCP port 8888 to 9999",
Version: nftables.TableFamilyIPv4,
DstNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: nil,
Rules: []nftableslib.Rule{
{
// This rule will block ALL TCP traffic with the exception of traffic destined to port 8888
L4: &nftableslib.L4Rule{
L4Proto: unix.IPPROTO_TCP,
Dst: &nftableslib.Port{
List: nftableslib.SetPortList([]int{8888}),
RelOp: nftableslib.NEQ,
},
},
Action: setActionVerdict(nftableslib.NFT_DROP),
},
{
// Allowed TCP traffic to port 8888 will be redirected to port 9999
L4: &nftableslib.L4Rule{
L4Proto: unix.IPPROTO_TCP,
Dst: &nftableslib.Port{
List: nftableslib.SetPortList([]int{8888}),
},
},
Action: setActionRedirect(9999, false),
},
},
},
{
Name: "chain-2",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPrerouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_TCP),
},
Action: setActionVerdict(unix.NFT_JUMP, "chain-1"),
},
},
},
},
Saddr: "1.1.1.1/24",
Daddr: "1.1.1.2/24",
Validation: validations.TCPPortRedirectValidation,
},
{
Name: "IPV6 Redirecting TCP port 8888 to 9999",
Version: nftables.TableFamilyIPv6,
DstNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: nil,
Rules: []nftableslib.Rule{
{
// This rule will block ALL TCP traffic with the exception of traffic destined to port 8888
L4: &nftableslib.L4Rule{
L4Proto: unix.IPPROTO_TCP,
Dst: &nftableslib.Port{
List: nftableslib.SetPortList([]int{8888}),
RelOp: nftableslib.NEQ,
},
},
Action: setActionVerdict(nftableslib.NFT_DROP),
},
{
// Allowed TCP traffic to port 8888 will be redirected to port 9999
L4: &nftableslib.L4Rule{
L4Proto: unix.IPPROTO_TCP,
Dst: &nftableslib.Port{
List: nftableslib.SetPortList([]int{8888}),
},
},
Action: setActionRedirect(9999, false),
},
},
},
{
Name: "chain-2",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPrerouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_TCP),
},
Action: setActionVerdict(unix.NFT_JUMP, "chain-1"),
},
},
},
},
Saddr: "2001:1::1/64",
Daddr: "2001:1::2/64",
Validation: validations.TCPPortRedirectValidation,
},
{
Name: "IPV4 TCP SNAT",
Version: nftables.TableFamilyIPv4,
SrcNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPostrouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_TCP),
},
Action: setSNAT(&nftableslib.NATAttributes{
L3Addr: [2]*nftableslib.IPAddr{setIPAddr("5.5.5.5")},
Port: [2]uint16{7777},
}),
},
},
},
},
Saddr: "1.1.1.1/24",
Daddr: "1.1.1.2/24",
Validation: validations.IPv4TCPSNATValidation,
DebugNFRules: false,
},
{
Name: "IPV6 TCP SNAT",
Version: nftables.TableFamilyIPv6,
SrcNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPostrouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_TCP),
},
Action: setSNAT(&nftableslib.NATAttributes{
L3Addr: [2]*nftableslib.IPAddr{setIPAddr("2001:1234::1")},
Port: [2]uint16{7777},
})},
},
},
},
Saddr: "2001:1::1/64",
Daddr: "2001:1::2/64",
Validation: validations.IPv6TCPSNATValidation,
},
{
Name: "IPV4 UDP SNAT",
Version: nftables.TableFamilyIPv4,
SrcNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPostrouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_UDP),
},
Action: setSNAT(&nftableslib.NATAttributes{
L3Addr: [2]*nftableslib.IPAddr{setIPAddr("5.5.5.5")},
Port: [2]uint16{7777},
}),
},
},
},
},
Saddr: "1.1.1.1/24",
Daddr: "1.1.1.2/24",
Validation: validations.IPv4UDPSNATValidation,
DebugNFRules: false,
},
{
Name: "IPV6 UDP SNAT",
Version: nftables.TableFamilyIPv6,
SrcNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeNAT,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookPostrouting,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_UDP),
},
Action: setSNAT(&nftableslib.NATAttributes{
L3Addr: [2]*nftableslib.IPAddr{setIPAddr("2001:1234::1")},
Port: [2]uint16{7777},
})},
},
},
},
Saddr: "2001:1::1/64",
Daddr: "2001:1::2/64",
Validation: validations.IPv6UDPSNATValidation,
},
{
Name: "IPV6 ICMP Drop",
Version: nftables.TableFamilyIPv6,
DstNFRules: []setenv.TestChain{
{
Name: "chain-1",
Attr: &nftableslib.ChainAttributes{
Type: nftables.ChainTypeFilter,
Priority: nftables.ChainPriorityFirst,
Hook: nftables.ChainHookInput,
Policy: &accept,
},
Rules: []nftableslib.Rule{
{
L3: &nftableslib.L3Rule{
Protocol: nftableslib.L3Protocol(unix.IPPROTO_ICMPV6),
Dst: &nftableslib.IPAddrSpec{
List: []*nftableslib.IPAddr{setIPAddr("2001:1::2")},
},
},
Action: setActionVerdict(nftableslib.NFT_DROP),
},
},
},
},
Saddr: "2001:1::1/64",
Daddr: "2001:1::2/64",
Validation: validations.ICMPDropTestValidation,
},
}
memProf, err := os.Create("/tmp/heap.out")
if err != nil {
os.Exit(1)
}
defer memProf.Close()
// if err := pprof.WriteHeapProfile(memProf); err != nil {
// fmt.Printf("Error writing memory profile with error: %+v\n", err)
// }
for _, tt := range tests {
fmt.Printf("+++ Starting test: \"%s\" \n", tt.Name)
t, err := setenv.NewP2PTestEnv(tt.Version, tt.Saddr, tt.Daddr)
if err != nil {
fmt.Printf("--- Test: \"%s\" failed with error: %+v\n", tt.Name, err)
os.Exit(1)
}
defer t.Cleanup()
// Get allocated namesapces and prepared ip addresses
ns := t.GetNamespace()
ip := t.GetIPs()
// Initial connectivity test before applying any nftables rules
if err := setenv.TestICMP(ns[0], tt.Version, ip[0], ip[1]); err != nil {
fmt.Printf("--- Test: \"%s\" failed during initial connectivity test with error: %+v\n", tt.Name, err)
os.Exit(1)
}
if tt.SrcNFRules != nil {
if _, err := setenv.NFTablesSet(setenv.MakeTablesInterface(ns[0]), tt.Version, tt.SrcNFRules, tt.DebugNFRules); err != nil {
fmt.Printf("--- Test: \"%s\" failed to setup nftables table/chain/rule in a source namespace with error: %+v\n", tt.Name, err)
os.Exit(1)
}
}
if tt.DstNFRules != nil {
if _, err := setenv.NFTablesSet(setenv.MakeTablesInterface(ns[1]), tt.Version, tt.DstNFRules, tt.DebugNFRules); err != nil {
fmt.Printf("--- Test: \"%s\" failed to setup nftables table/chain/rule in a destination namespace with error: %+v\n", tt.Name, err)
os.Exit(1)
}
}
// Check if test's validation is set and execute validation.
if tt.Validation != nil {
if err := tt.Validation(tt.Version, ns, ip); err != nil {
fmt.Printf("--- Test: \"%s\" failed validation error: %+v\n", tt.Name, err)
os.Exit(1)
}
} else {
fmt.Printf("--- Test: \"%s\" has no validation, test without validation is not allowed\n", tt.Name)
os.Exit(1)
}
fmt.Printf("+++ Finished test: \"%s\" successfully.\n", tt.Name)
}
fmt.Printf("+++ Starting test: Sync() \n")
// Testing Sync feature, in a namespace a set of rules will be created and programmed, then tables/chains/rules in
// memory removed, Sync is supposed to learn and rebuild in-memory data structures based on discovered in the namesapce
// nftables information.
if err := testSync(); err != nil {
fmt.Printf("--- Test: Sync failed with error: %+v\n", err)
os.Exit(1)
}
fmt.Printf("+++ Finished test: Sync() successfully.\n")
if err := pprof.WriteHeapProfile(memProf); err != nil {
fmt.Printf("Error writing memory profile with error: %+v\n", err)
}
}
func setActionVerdict(key int, chain ...string) *nftableslib.RuleAction {
ra, err := nftableslib.SetVerdict(key, chain...)
if err != nil {
fmt.Printf("failed to SetVerdict with error: %+v\n", err)
return nil
}
return ra
}
func setActionRedirect(port int, tproxy bool) *nftableslib.RuleAction {
ra, err := nftableslib.SetRedirect(port, tproxy)
if err != nil {
fmt.Printf("failed to SetRedirect with error: %+v", err)
return nil
}
return ra
}
func setIPAddr(addr string) *nftableslib.IPAddr {
a, err := nftableslib.NewIPAddr(addr)
if err != nil {
fmt.Printf("error %+v return from NewIPAddr for address: %s\n", err, addr)
return nil
}
return a
}
func setSNAT(attrs *nftableslib.NATAttributes) *nftableslib.RuleAction {
ra, err := nftableslib.SetSNAT(attrs)
if err != nil {
fmt.Printf("error %+v return from SetSNAT call\n", err)
return nil
}
return ra
}
func setDNAT(attrs *nftableslib.NATAttributes) *nftableslib.RuleAction {
ra, err := nftableslib.SetDNAT(attrs)
if err != nil {
fmt.Printf("error %+v return from SetSNAT call\n", err)
return nil
}
return ra
}