2021-08-23 09:14:53 -04:00
|
|
|
//go:build linux
|
2021-05-25 19:48:54 -04:00
|
|
|
// +build linux
|
|
|
|
|
2015-02-27 12:11:53 -05:00
|
|
|
package bridge
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-04-24 18:13:44 -04:00
|
|
|
"net"
|
2021-08-24 06:10:50 -04:00
|
|
|
"os"
|
2015-02-27 12:11:53 -05:00
|
|
|
"testing"
|
|
|
|
|
2021-04-05 20:24:47 -04:00
|
|
|
"github.com/docker/docker/libnetwork/testutils"
|
2015-02-27 12:11:53 -05:00
|
|
|
"github.com/vishvananda/netlink"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSetupIPv6(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-02-27 12:11:53 -05:00
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
|
|
|
config, br := setupTestInterface(t, nh)
|
2015-04-15 01:25:42 -04:00
|
|
|
if err := setupBridgeIPv6(config, br); err != nil {
|
2015-02-27 12:11:53 -05:00
|
|
|
t.Fatalf("Failed to setup bridge IPv6: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-08-24 06:10:50 -04:00
|
|
|
procSetting, err := os.ReadFile(fmt.Sprintf("/proc/sys/net/ipv6/conf/%s/disable_ipv6", config.BridgeName))
|
2015-02-27 12:11:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to read disable_ipv6 kernel setting: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-05-27 20:15:56 -04:00
|
|
|
if expected := []byte("0\n"); !bytes.Equal(expected, procSetting) {
|
2015-02-27 12:11:53 -05:00
|
|
|
t.Fatalf("Invalid kernel setting disable_ipv6: expected %q, got %q", string(expected), string(procSetting))
|
|
|
|
}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
addrsv6, err := nh.AddrList(br.Link, netlink.FAMILY_V6)
|
2015-02-27 12:11:53 -05:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to list device IPv6 addresses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
for _, addr := range addrsv6 {
|
2020-10-31 19:21:20 -04:00
|
|
|
if bridgeIPv6.String() == addr.IPNet.String() {
|
2015-02-27 12:11:53 -05:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
2020-10-31 19:21:20 -04:00
|
|
|
t.Fatalf("Bridge device does not have requested IPv6 address %v", bridgeIPv6)
|
2015-02-27 12:11:53 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
func TestSetupGatewayIPv6(t *testing.T) {
|
2015-09-07 13:33:28 -04:00
|
|
|
defer testutils.SetupTestOSContext(t)()
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
_, nw, _ := net.ParseCIDR("2001:db8:ea9:9abc:ffff::/80")
|
|
|
|
gw := net.ParseIP("2001:db8:ea9:9abc:ffff::254")
|
|
|
|
|
2015-05-22 13:56:36 -04:00
|
|
|
config := &networkConfiguration{
|
2015-04-24 18:13:44 -04:00
|
|
|
BridgeName: DefaultBridgeName,
|
2015-10-05 17:53:25 -04:00
|
|
|
AddressIPv6: nw,
|
2015-04-24 18:13:44 -04:00
|
|
|
DefaultGatewayIPv6: gw}
|
|
|
|
|
2016-05-16 14:51:40 -04:00
|
|
|
nh, err := netlink.NewHandle()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
|
|
|
br := &bridgeInterface{nlh: nh}
|
2015-04-24 18:13:44 -04:00
|
|
|
|
|
|
|
if err := setupGatewayIPv6(config, br); err != nil {
|
|
|
|
t.Fatalf("Set Default Gateway failed: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !gw.Equal(br.gatewayIPv6) {
|
|
|
|
t.Fatalf("Set Default Gateway failed. Expected %v, Found %v", gw, br.gatewayIPv6)
|
|
|
|
}
|
|
|
|
}
|