File: integration_linux_test.go

package info (click to toggle)
golang-github-mdlayher-netlink 1.8.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 420 kB
  • sloc: makefile: 5
file content (429 lines) | stat: -rw-r--r-- 10,630 bytes parent folder | download | duplicates (3)
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
421
422
423
424
425
426
427
428
429
//go:build linux
// +build linux

package integration_test

import (
	"errors"
	"fmt"
	"net"
	"os"
	"os/exec"
	"sync"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
	"github.com/jsimonetti/rtnetlink"
	"github.com/mdlayher/ethtool"
	"github.com/mdlayher/netlink"
	"golang.org/x/net/nettest"
	"golang.org/x/sys/unix"
)

func TestIntegrationConnMulticast(t *testing.T) {
	skipUnprivileged(t)

	c, done := rtnlDial(t, 0)
	defer done()

	// Create an interface to trigger a notification, and remove it at the end
	// of the test.
	const ifName = "nltest0"
	defer shell(t, "ip", "link", "del", ifName)

	ifi := rtnlReceive(t, c, func() {
		shell(t, "ip", "tuntap", "add", ifName, "mode", "tun")
	})

	if diff := cmp.Diff(ifName, ifi); diff != "" {
		t.Fatalf("unexpected interface name (-want +got):\n%s", diff)
	}
}

func TestIntegrationConnNetNSExplicit(t *testing.T) {
	skipUnprivileged(t)

	// Create a network namespace for use within this test.
	const ns = "nltest0"
	shell(t, "ip", "netns", "add", ns)
	defer shell(t, "ip", "netns", "del", ns)

	f, err := os.Open("/var/run/netns/" + ns)
	if err != nil {
		t.Fatalf("failed to open namespace file: %v", err)
	}
	defer f.Close()

	// Create a connection in each the host namespace and the new network
	// namespace. We will use these to validate that a namespace was entered
	// and that an interface creation notification was only visible to the
	// connection within the namespace.
	hostC, hostDone := rtnlDial(t, 0)
	defer hostDone()

	nsC, nsDone := rtnlDial(t, int(f.Fd()))
	defer nsDone()

	var wg sync.WaitGroup
	wg.Add(1)
	defer wg.Wait()

	go func() {
		defer wg.Done()

		_, err := hostC.Receive()
		if err == nil {
			panic("received netlink message in host namespace")
		}

		// Timeout means we were interrupted, so return.
		if nerr, ok := err.(net.Error); ok && nerr.Timeout() {
			return
		}

		panicf("failed to receive in host namespace: %v", err)
	}()

	// Create a temporary interface within the new network namespace.
	const ifName = "nltestns0"
	defer shell(t, "ip", "netns", "exec", ns, "ip", "link", "del", ifName)

	ifi := rtnlReceive(t, nsC, func() {
		// Trigger a notification in the new namespace.
		shell(t, "ip", "netns", "exec", ns, "ip", "tuntap", "add", ifName, "mode", "tun")
	})

	// And finally interrupt the host connection so it can exit its
	// receive goroutine.
	if err := hostC.SetDeadline(time.Unix(1, 0)); err != nil {
		t.Fatalf("failed to interrupt host connection: %v", err)
	}

	if diff := cmp.Diff(ifName, ifi); diff != "" {
		t.Fatalf("unexpected interface name (-want +got):\n%s", diff)
	}
}

func TestIntegrationRTNetlinkStrictCheckExtendedAcknowledge(t *testing.T) {
	c, err := netlink.Dial(unix.NETLINK_ROUTE, nil)
	if err != nil {
		t.Fatalf("failed to open rtnetlink socket: %s", err)
	}
	defer c.Close()

	// Turn on extended acknowledgements and strict checking so rtnetlink
	// reports detailed error information regarding our invalid dump request.
	setStrictCheck(t, c)
	if err := c.SetOption(netlink.ExtendedAcknowledge, true); err != nil {
		t.Fatalf("failed to set extended acknowledge option: %v", err)
	}

	// The kernel will complain that this field isn't valid for a filtered dump
	// request.
	b, err := (&rtnetlink.RouteMessage{SrcLength: 1}).MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request: %v", err)
	}

	_, err = c.Execute(netlink.Message{
		Header: netlink.Header{
			Type:  unix.RTM_GETROUTE,
			Flags: netlink.Request | netlink.Dump,
		},
		Data: b,
	})

	oerr, ok := err.(*netlink.OpError)
	if !ok {
		t.Fatalf("expected *netlink.OpError, but got: %T", err)
	}

	// Assume the message contents will be relatively static but don't hardcode
	// offset just in case things change.

	want := &netlink.OpError{
		Op:      "receive",
		Err:     unix.EINVAL,
		Message: "Invalid values in header for FIB dump request",
	}

	if diff := cmp.Diff(want, oerr); diff != "" {
		t.Fatalf("unexpected *netlink.OpError (-want +got):\n%s", diff)
	}
}

func TestIntegrationRTNetlinkRouteManipulation(t *testing.T) {
	skipUnprivileged(t)

	c, err := netlink.Dial(unix.NETLINK_ROUTE, nil)
	if err != nil {
		t.Fatalf("failed to open rtnetlink socket: %s", err)
	}
	defer c.Close()

	// Required for kernel route dump filtering.
	setStrictCheck(t, c)

	lo, err := nettest.LoopbackInterface()
	if err != nil {
		t.Fatalf("failed to get loopback: %v", err)
	}

	// Install synthetic routes in documentation ranges into a non-default table
	// which we will later dump.
	const (
		table   = 100
		ip4Mask = 32
		ip6Mask = 128
	)

	var (
		ip4 = &net.IPNet{
			IP:   net.IPv4(192, 2, 2, 1),
			Mask: net.CIDRMask(ip4Mask, ip4Mask),
		}

		ip6 = &net.IPNet{
			IP:   net.ParseIP("2001:db8::1"),
			Mask: net.CIDRMask(ip6Mask, ip6Mask),
		}

		want = []*net.IPNet{ip4, ip6}
	)

	rtmsgs := []rtnetlink.RouteMessage{
		{
			Family:    unix.AF_INET,
			DstLength: ip4Mask,
			Protocol:  unix.RTPROT_STATIC,
			Scope:     unix.RT_SCOPE_UNIVERSE,
			Type:      unix.RTN_UNICAST,
			Attributes: rtnetlink.RouteAttributes{
				Dst:      ip4.IP,
				OutIface: uint32(lo.Index),
				Table:    table,
			},
		},
		{
			Family:    unix.AF_INET6,
			DstLength: ip6Mask,
			Protocol:  unix.RTPROT_STATIC,
			Scope:     unix.RT_SCOPE_UNIVERSE,
			Type:      unix.RTN_UNICAST,
			Attributes: rtnetlink.RouteAttributes{
				Dst:      ip6.IP,
				OutIface: uint32(lo.Index),
				Table:    table,
			},
		},
	}

	// Verify we can send a batch of updates in one syscall.
	var msgs []netlink.Message
	for _, m := range rtmsgs {
		b, err := m.MarshalBinary()
		if err != nil {
			t.Fatalf("failed to marshal: %v", err)
		}

		msgs = append(msgs, netlink.Message{
			Header: netlink.Header{
				Type:  unix.RTM_NEWROUTE,
				Flags: netlink.Request | netlink.Create | netlink.Replace,
			},
			Data: b,
		})
	}

	if _, err := c.SendMessages(msgs); err != nil {
		t.Fatalf("failed to add routes: %v", err)
	}

	// Only dump routes from the specified table.
	b, err := (&rtnetlink.RouteMessage{
		Attributes: rtnetlink.RouteAttributes{Table: table},
	}).MarshalBinary()
	if err != nil {
		t.Fatalf("failed to marshal request: %v", err)
	}

	routes, err := c.Execute(
		netlink.Message{
			Header: netlink.Header{
				Type:  unix.RTM_GETROUTE,
				Flags: netlink.Request | netlink.Dump,
			},
			Data: b,
		},
	)
	if err != nil {
		t.Fatalf("failed to dump routes: %v", err)
	}

	// Parse the routes back to Go structures.
	got := make([]*net.IPNet, 0, len(routes))
	for _, r := range routes {
		var rtm rtnetlink.RouteMessage
		if err := rtm.UnmarshalBinary(r.Data); err != nil {
			t.Fatalf("failed to unmarshal route: %v", err)
		}

		got = append(got, &net.IPNet{
			IP:   rtm.Attributes.Dst,
			Mask: net.CIDRMask(int(rtm.DstLength), int(rtm.DstLength)),
		})
	}

	// Now clear the routes and verify they're removed before ensuring we got
	// the expected routes.
	for i := range msgs {
		msgs[i].Header.Type = unix.RTM_DELROUTE
		msgs[i].Header.Flags = netlink.Request | netlink.Acknowledge
	}

	if _, err := c.SendMessages(msgs); err != nil {
		t.Fatalf("failed to send: %v", err)
	}
	if _, err := c.Receive(); err != nil {
		t.Fatalf("failed to receive: %v", err)
	}

	if diff := cmp.Diff(want, got); diff != "" {
		t.Fatalf("unexpected routes (-want +got):\n%s", diff)
	}
}

func TestIntegrationEthtoolExtendedAcknowledge(t *testing.T) {
	t.Parallel()

	// The ethtool package uses extended acknowledgements and should populate
	// all of netlink.OpError's fields when unwrapped.
	c, err := ethtool.New()
	if err != nil {
		if errors.Is(err, os.ErrNotExist) {
			t.Skip("skipping, ethtool genetlink not available on this system")
		}

		t.Fatalf("failed to open ethtool genetlink: %v", err)
	}

	_, err = c.LinkInfo(ethtool.Interface{Name: "notexist0"})
	if err == nil {
		t.Fatal("expected an error, but none occurred")
	}

	var oerr *netlink.OpError
	if !errors.As(err, &oerr) {
		t.Fatalf("expected wrapped *netlink.OpError, but got: %T", err)
	}

	// Assume the message contents will be relatively static but don't hardcode
	// offset just in case things change.
	if oerr.Offset == 0 {
		t.Fatal("no offset specified in *netlink.OpError")
	}
	oerr.Offset = 0

	want := &netlink.OpError{
		Op:      "receive",
		Err:     unix.ENODEV,
		Message: "no device matches name",
	}

	if diff := cmp.Diff(want, oerr); diff != "" {
		t.Fatalf("unexpected *netlink.OpError (-want +got):\n%s", diff)
	}
}

func rtnlDial(t *testing.T, netNS int) (*netlink.Conn, func()) {
	t.Helper()

	timer := time.AfterFunc(10*time.Second, func() {
		panic("test took too long")
	})

	c, err := netlink.Dial(unix.NETLINK_ROUTE, &netlink.Config{
		Groups: unix.RTMGRP_LINK,
		NetNS:  netNS,
	})
	if err != nil {
		t.Fatalf("failed to dial rtnetlink: %v", err)
	}

	return c, func() {
		if err := c.Close(); err != nil {
			t.Fatalf("failed to close rtnetlink connection: %v", err)
		}

		// Stop the timer to prevent a panic if other tests run for a long time.
		timer.Stop()
	}
}

func setStrictCheck(t *testing.T, c *netlink.Conn) {
	if err := c.SetOption(netlink.GetStrictCheck, true); err != nil {
		if errors.Is(err, unix.ENOPROTOOPT) {
			t.Skipf("skipping, netlink strict checking is not supported on this kernel")
		}

		t.Fatalf("failed to set strict check option: %v", err)
	}
}

func rtnlReceive(t *testing.T, c *netlink.Conn, do func()) string {
	t.Helper()

	// Receive messages in goroutine.
	msgC := make(chan rtnetlink.LinkMessage)
	go func() {
		msgs, err := c.Receive()
		if err != nil {
			panicf("failed to receive rtnetlink messages: %v", err)
		}

		var rtmsg rtnetlink.LinkMessage
		if err := rtmsg.UnmarshalBinary(msgs[0].Data); err != nil {
			panicf("failed to unmarshal rtnetlink message: %v", err)
		}

		msgC <- rtmsg
	}()

	// Execute the function which will generate messages, and then wait for
	// a message.
	do()
	m := <-msgC

	return m.Attributes.Name
}

func skipUnprivileged(t *testing.T) {
	const ifName = "nlprobe0"
	shell(t, "ip", "tuntap", "add", ifName, "mode", "tun")
	shell(t, "ip", "link", "del", ifName)
}

func shell(t *testing.T, name string, arg ...string) {
	t.Helper()

	t.Logf("$ %s %v", name, arg)

	cmd := exec.Command(name, arg...)
	if err := cmd.Start(); err != nil {
		t.Fatalf("failed to start command %q: %v", name, err)
	}

	if err := cmd.Wait(); err != nil {
		// Shell operations in these tests require elevated privileges.
		if cmd.ProcessState.ExitCode() == int(unix.EPERM) {
			t.Skipf("skipping, permission denied: %v", err)
		}

		t.Fatalf("failed to wait for command %q: %v", name, err)
	}
}

func panicf(format string, a ...interface{}) {
	panic(fmt.Sprintf(format, a...))
}