forked from florianl/go-conntrack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bpf_test.go
172 lines (167 loc) · 7.83 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
package conntrack
import (
"testing"
"golang.org/x/net/bpf"
)
func TestConstructFilter(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: 0x0e, 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: 0x0a, 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: 0x06, 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: 0x02, 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: 0x17, 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: 0x13, 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: 0x0f, 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: 0x02, 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: 0x0e, 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: 0x0a, 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: 0x06, 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: 0x0050, Jt: 0x00, Jf: 0x00, K: 0x00000004},
{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)
}
}
})
}
}