forked from mdlayher/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn_linux_integration_test.go
220 lines (185 loc) · 4.42 KB
/
conn_linux_integration_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
//+build integration,linux
package netlink
import (
"fmt"
"os/exec"
"reflect"
"testing"
"time"
"golang.org/x/net/bpf"
)
func TestLinuxNetlinkSetBPF(t *testing.T) {
const familyGeneric = 16
c, err := Dial(familyGeneric, nil)
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer c.Close()
// The sequence number which will be permitted by the BPF filter.
// Using max uint32 helps us avoid dealing with host (netlink) vs
// network (BPF) endianness during this test.
const sequence uint32 = 0xffffffff
prog, err := bpf.Assemble(testBPFProgram(sequence))
if err != nil {
t.Fatalf("failed to assemble BPF program: %v", err)
}
if err := c.SetBPF(prog); err != nil {
t.Fatalf("failed to attach BPF program to socket: %v", err)
}
req := Message{
Header: Header{
Flags: HeaderFlagsRequest | HeaderFlagsAcknowledge,
},
}
sequences := []struct {
seq uint32
ok bool
}{
// OK, bad, OK. Expect two messages to be received.
{seq: sequence, ok: true},
{seq: 10, ok: false},
{seq: sequence, ok: true},
}
for _, s := range sequences {
req.Header.Sequence = s.seq
if _, err := c.Send(req); err != nil {
t.Fatalf("failed to send with sequence %d: %v", s, err)
}
if !s.ok {
continue
}
msgs, err := c.Receive()
if err != nil {
t.Fatalf("failed to receive with sequence %d: %v", s.seq, err)
}
// Make sure the received message has the expected sequence number.
if l := len(msgs); l != 1 {
t.Fatalf("unexpected number of messages: %d", l)
}
if want, got := s.seq, msgs[0].Header.Sequence; want != got {
t.Fatalf("unexpected reply sequence number:\n- want: %v\n- got: %v",
want, got)
}
}
}
func TestLinuxNetlinkSetBPFProgram(t *testing.T) {
vm, err := bpf.NewVM(testBPFProgram(0xffffffff))
if err != nil {
t.Fatalf("failed to create BPF VM: %v", err)
}
msg := []byte{
0x10, 0x00, 0x00, 0x00,
0x01, 0x00,
0x01, 0x00,
// Allowed sequence number.
0xff, 0xff, 0xff, 0xff,
0x01, 0x00, 0x00, 0x00,
}
out, err := vm.Run(msg)
if err != nil {
t.Fatalf("failed to execute OK input: %v", err)
}
if out == 0 {
t.Fatal("BPF filter dropped OK input")
}
msg = []byte{
0x10, 0x00, 0x00, 0x00,
0x01, 0x00,
0x01, 0x00,
// Bad sequence number.
0x00, 0x11, 0x22, 0x33,
0x01, 0x00, 0x00, 0x00,
}
out, err = vm.Run(msg)
if err != nil {
t.Fatalf("failed to execute bad input: %v", err)
}
if out != 0 {
t.Fatal("BPF filter did not drop bad input")
}
}
// testBPFProgram returns a BPF program which only allows frames with the
// input sequence number.
func testBPFProgram(allowSequence uint32) []bpf.Instruction {
return []bpf.Instruction{
bpf.LoadAbsolute{
Off: 8,
Size: 4,
},
bpf.JumpIf{
Cond: bpf.JumpEqual,
Val: allowSequence,
SkipTrue: 1,
},
bpf.RetConstant{
Val: 0,
},
bpf.RetConstant{
Val: 128,
},
}
}
func TestLinuxNetlinkMulticast(t *testing.T) {
cfg := &Config{
Groups: 0x1, // RTMGRP_LINK
}
c, err := Dial(0, cfg) // dials NETLINK_ROUTE
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
in := make(chan []Message)
// routine for receiving any messages
recv := func() {
data, err := c.Receive()
if err != nil {
panic(fmt.Sprintf("error in receive: %s", err))
}
in <- data
}
go recv()
ifName := "test0"
def := sudoIfCreate(t, ifName)
defer def()
timeout := time.After(5 * time.Second)
var data []Message
select {
case data = <-in:
break
case <-timeout:
panic("did not receive any messages after 5 seconds")
}
interf := []byte(ifName)
want := make([]uint8, len(ifName))
copy(want, interf[:])
got := make([]uint8, len(ifName))
copy(got, data[0].Data[20:len(ifName)+20])
if !reflect.DeepEqual(want, got) {
t.Fatalf("received message does not mention ifName %q", ifName)
}
}
func sudoIfCreate(t *testing.T, ifName string) func() {
var err error
cmd := exec.Command("sudo", "ip", "tuntap", "add", ifName, "mode", "tun")
err = cmd.Start()
if err != nil {
t.Fatalf("error creating tuntap device: %s", err)
return func() {}
}
err = cmd.Wait()
if err != nil {
t.Fatalf("error running command to create tuntap device: %s", err)
return func() {}
}
return func() {
var err error
cmd := exec.Command("sudo", "ip", "link", "del", ifName)
err = cmd.Start()
if err != nil {
panic(fmt.Sprintf("error removing tuntap device: %s", err))
}
err = cmd.Wait()
if err != nil {
panic(fmt.Sprintf("error running command to remove tuntap device: %s", err))
}
}
}