forked from florianl/go-conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf_test.go
420 lines (410 loc) · 21.7 KB
/
bpf_test.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
410
411
412
413
414
415
416
417
418
419
420
package conntrack
import (
"encoding/binary"
"errors"
"testing"
"github.com/florianl/go-conntrack/internal/unix"
"golang.org/x/net/bpf"
)
func TestOldConstructFilter(t *testing.T) {
tests := []struct {
name string
table Table
filters []ConnAttr
rawInstr []bpf.RawInstruction
err error
}{
// Example from libnetfilter_conntrack/utils/conntrack_filter.c
{name: "conntrack_filter.c", table: Conntrack, filters: []ConnAttr{
{Type: AttrOrigL4Proto, Data: []byte{0x11}}, // TCP
{Type: AttrOrigL4Proto, Data: []byte{0x06}}, // UDP
{Type: AttrTCPState, Data: []byte{0x3}}, // TCP_CONNTRACK_ESTABLISHED
{Type: AttrOrigIPv4Src, Data: []byte{0x7F, 0x0, 0x0, 0x1}, Mask: []byte{0xff, 0xff, 0xff, 0xff}, Negate: true}, // SrcIP != 127.0.0.1
{Type: AttrOrigIPv6Src, Data: []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}, // SrcIP != ::1
Mask: []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, Negate: true},
},
rawInstr: []bpf.RawInstruction{
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0d, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x09, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x05, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0040, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0054, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x7f000001},
{Op: 0x0005, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x16, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x12, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000003},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0e, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0040, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0054, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0015, Jt: 0x00, Jf: 0x0a, K: 0x00000000},
{Op: 0x0040, Jt: 0x00, Jf: 0x00, K: 0x00000008},
{Op: 0x0054, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0015, Jt: 0x00, Jf: 0x07, K: 0x00000000},
{Op: 0x0040, Jt: 0x00, Jf: 0x00, K: 0x0000000c},
{Op: 0x0054, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0015, Jt: 0x00, Jf: 0x04, K: 0x00000000},
{Op: 0x0040, Jt: 0x00, Jf: 0x00, K: 0x00000010},
{Op: 0x0054, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: 0x0005, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0d, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000002},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x09, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x05, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0015, Jt: 0x02, Jf: 0x00, K: 0x00000011},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000006},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0c, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x08, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x04, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000003},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
},
},
{name: "tcp and port 22", table: Conntrack, filters: []ConnAttr{
{Type: AttrOrigL4Proto, Data: []byte{0x11}}, // TCP
{Type: AttrOrigPortDst, Data: []byte{0x00, 0x16}}, // 22
}, rawInstr: []bpf.RawInstruction{
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0c, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000002},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x08, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000003},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x04, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0048, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000016},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0000, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x0c, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x000000002},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x08, Jf: 0x00, K: 0x00000000},
{Op: 0x0004, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0001, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: 0x0030, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: 0x0015, Jt: 0x04, Jf: 0x00, K: 0x00000000},
{Op: 0x0007, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: 0x0015, Jt: 0x01, Jf: 0x00, K: 0x00000011},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: 0x0006, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
rawInstr, err := constructFilter(tc.table, tc.filters)
if err != tc.err {
t.Fatal(err)
}
if len(rawInstr) != len(tc.rawInstr) {
t.Fatalf("different length:\n- want: %#v\n- got: %#v", tc.rawInstr, rawInstr)
}
for i, v := range rawInstr {
if v != tc.rawInstr[i] {
t.Fatalf("unexpected reply:\n- want: %#v\n- got: %#v", tc.rawInstr, rawInstr)
}
}
})
}
}
func TestConstructFilter(t *testing.T) {
mark1ByteValue := make([]byte, 4)
binary.BigEndian.PutUint32(mark1ByteValue, 1)
mark10ByteValue := make([]byte, 4)
binary.BigEndian.PutUint32(mark10ByteValue, 10)
mark11ByteValue := make([]byte, 4)
binary.BigEndian.PutUint32(mark11ByteValue, 11)
mark50ByteValue := make([]byte, 4)
binary.BigEndian.PutUint32(mark50ByteValue, 50)
mark1000ByteValue := make([]byte, 4)
binary.BigEndian.PutUint32(mark1000ByteValue, 1000)
tests := map[string]struct {
table Table
filters []ConnAttr
rawInstr []bpf.RawInstruction
err error
}{
"mark positive filter: [1]": {
table: Conntrack,
filters: []ConnAttr{
{Type: AttrMark, Data: mark1ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: false},
},
rawInstr: []bpf.RawInstruction{
//--- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
//--- check mark ---
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000008},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
//---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
}},
"mark positive filter: [10,50,1000]": {
table: Conntrack,
filters: []ConnAttr{
{Type: AttrMark, Data: mark10ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: false},
{Type: AttrMark, Data: mark50ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: false},
{Type: AttrMark, Data: mark1000ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: false},
},
rawInstr: []bpf.RawInstruction{
//--- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
//--- check mark ---
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000008},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x08, Jf: 0x00, K: 0x0000000a},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x05, Jf: 0x00, K: 0x00000032},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x000003e8},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
//---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
}},
"mark negative filter: [10,11]": {
table: Conntrack, filters: []ConnAttr{
{Type: AttrMark, Data: mark10ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: true},
{Type: AttrMark, Data: mark11ByteValue, Mask: []byte{255, 255, 255, 255}, Negate: true},
},
rawInstr: []bpf.RawInstruction{
//--- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
//--- check mark ---
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000008},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x05, Jf: 0x00, K: 0x0000000a},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x0000000b},
{Op: unix.BPF_MISC | unix.BPF_TXA, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_JMP | unix.BPF_JA, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
//---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
}},
"tcp": {
table: Conntrack,
filters: []ConnAttr{
{Type: AttrOrigL4Proto, Data: []byte{0x06}}, // TCP
},
rawInstr: []bpf.RawInstruction{
// ---- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
// ---- check proto ----
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x0c, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000002},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x08, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x04, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000006},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
// ---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
}},
"tcp or udp": {
table: Conntrack,
filters: []ConnAttr{
{Type: AttrOrigL4Proto, Data: []byte{0x06}}, // TCP
{Type: AttrOrigL4Proto, Data: []byte{0x11}}, // UDP
},
rawInstr: []bpf.RawInstruction{
// ---- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
// ---- check proto ----
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x0d, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000002},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x09, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x05, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x02, Jf: 0x00, K: 0x00000006},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000011},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
// ---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
},
},
"src 127.0.0.1 or src 127.0.0.2 or src 127.0.0.3": {
table: Conntrack,
filters: []ConnAttr{
{Type: AttrOrigIPv4Src, Data: []byte{0x7F, 0x0, 0x0, 0x1}, Mask: []byte{0xff, 0xff, 0xff, 0xff}},
{Type: AttrOrigIPv4Src, Data: []byte{0x7F, 0x0, 0x0, 0x2}, Mask: []byte{0xff, 0xff, 0xff, 0xff}},
{Type: AttrOrigIPv4Src, Data: []byte{0x7F, 0x0, 0x0, 0x3}, Mask: []byte{0xff, 0xff, 0xff, 0xff}},
},
rawInstr: []bpf.RawInstruction{
// ---- check subsys ---
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
// ---- check src IPv4 ----
{Op: unix.BPF_LD | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000014},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x13, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x0f, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_ALU | unix.BPF_ADD | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_LDX | unix.BPF_IMM, Jt: 0x00, Jf: 0x00, K: 0x00000001},
{Op: unix.BPF_LD | unix.BPF_B | unix.BPF_ABS, Jt: 0x00, Jf: 0x00, K: 0xfffff00c},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x0b, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_MISC | unix.BPF_TAX, Jt: 0x00, Jf: 0x00, K: 0x00000000},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x07, Jf: 0x00, K: 0x7f000001},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x04, Jf: 0x00, K: 0x7f000002},
{Op: unix.BPF_LD | unix.BPF_W | unix.BPF_IND, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{Op: unix.BPF_ALU | unix.BPF_AND | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
{Op: unix.BPF_JMP | unix.BPF_JEQ | unix.BPF_K, Jt: 0x01, Jf: 0x00, K: 0x7f000003},
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0x00000000},
// ---- final verdict ----
{Op: unix.BPF_RET | unix.BPF_K, Jt: 0x00, Jf: 0x00, K: 0xffffffff},
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
rawInstr, err := constructFilter(tc.table, tc.filters)
if !errors.Is(err, tc.err) {
t.Fatal(err)
}
if len(rawInstr) != len(tc.rawInstr) {
t.Fatalf("different length:\n- want:\n%s\n- got:\n%s",
fmtRawInstructions(tc.rawInstr), fmtRawInstructions(rawInstr))
}
var isErr bool
for i, v := range rawInstr {
if v != tc.rawInstr[i] {
t.Errorf("unexpected %d. instruction:\n- want:\n%s\n- got:\n%s",
i, fmtRawInstruction(tc.rawInstr[i]), fmtRawInstruction(rawInstr[i]))
isErr = true
}
}
if isErr {
t.Fatalf("unexpected reply:\n- want:\n%s\n- got:\n%s",
fmtRawInstructions(tc.rawInstr), fmtRawInstructions(rawInstr))
}
})
}
}