Skip to content

Commit ef5295c

Browse files
committed
Don't configure IPv6 addr/gw when IPv6 disabled.
When IPv6 is disabled in a container by, for example, using the --sysctl option - an IPv6 address/gateway is still allocated. Don't attempt to apply that config because doing so enables IPv6 on the interface. Signed-off-by: Rob Murray <[email protected]>
1 parent 4e53936 commit ef5295c

2 files changed

Lines changed: 79 additions & 3 deletions

File tree

integration/networking/bridge_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package networking
33
import (
44
"context"
55
"fmt"
6+
"regexp"
67
"testing"
78
"time"
89

@@ -12,6 +13,7 @@ import (
1213
"github.com/docker/docker/integration/internal/network"
1314
"github.com/docker/docker/testutil"
1415
"github.com/docker/docker/testutil/daemon"
16+
"github.com/google/go-cmp/cmp/cmpopts"
1517
"gotest.tools/v3/assert"
1618
is "gotest.tools/v3/assert/cmp"
1719
"gotest.tools/v3/skip"
@@ -594,3 +596,68 @@ func TestInternalNwConnectivity(t *testing.T) {
594596
assert.Check(t, is.Equal(res.ExitCode, 1))
595597
assert.Check(t, is.Contains(res.Stderr(), "Network is unreachable"))
596598
}
599+
600+
// Check that the container's interface has no IPv6 address when IPv6 is
601+
// disabled in a container via sysctl.
602+
func TestDisableIPv6Addrs(t *testing.T) {
603+
skip.If(t, testEnv.DaemonInfo.OSType == "windows")
604+
605+
ctx := setupTest(t)
606+
d := daemon.New(t)
607+
d.StartWithBusybox(ctx, t)
608+
defer d.Stop(t)
609+
610+
c := d.NewClientT(t)
611+
defer c.Close()
612+
613+
testcases := []struct {
614+
name string
615+
sysctls map[string]string
616+
expIPv6 bool
617+
}{
618+
{
619+
name: "IPv6 enabled",
620+
expIPv6: true,
621+
},
622+
{
623+
name: "IPv6 disabled",
624+
sysctls: map[string]string{"net.ipv6.conf.all.disable_ipv6": "1"},
625+
},
626+
}
627+
628+
const netName = "testnet"
629+
network.CreateNoError(ctx, t, c, netName,
630+
network.WithIPv6(),
631+
network.WithIPAM("fda0:ef3d:6430:abcd::/64", "fda0:ef3d:6430:abcd::1"),
632+
)
633+
defer network.RemoveNoError(ctx, t, c, netName)
634+
635+
inet6RE := regexp.MustCompile(`inet6[ \t]+[0-9a-f:]*`)
636+
637+
for _, tc := range testcases {
638+
t.Run(tc.name, func(t *testing.T) {
639+
ctx := testutil.StartSpan(ctx, t)
640+
641+
opts := []func(config *container.TestContainerConfig){
642+
container.WithCmd("ip", "a"),
643+
container.WithNetworkMode(netName),
644+
}
645+
if len(tc.sysctls) > 0 {
646+
opts = append(opts, container.WithSysctls(tc.sysctls))
647+
}
648+
649+
runRes := container.RunAttach(ctx, t, c, opts...)
650+
defer c.ContainerRemove(ctx, runRes.ContainerID,
651+
containertypes.RemoveOptions{Force: true},
652+
)
653+
654+
stdout := runRes.Stdout.String()
655+
inet6 := inet6RE.FindAllString(stdout, -1)
656+
if tc.expIPv6 {
657+
assert.Check(t, len(inet6) > 0, "Expected IPv6 addresses but found none.")
658+
} else {
659+
assert.Check(t, is.DeepEqual(inet6, []string{}, cmpopts.EquateEmpty()))
660+
}
661+
})
662+
}
663+
}

libnetwork/sandbox_linux.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,12 @@ func (sb *Sandbox) updateGateway(ep *Endpoint) error {
9090
return fmt.Errorf("failed to set gateway while updating gateway: %v", err)
9191
}
9292

93-
if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
94-
return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
93+
// If IPv6 has been disabled in the sandbox a gateway may still have been
94+
// configured, don't attempt to apply it.
95+
if ipv6, ok := sb.ipv6Enabled(); !ok || ipv6 {
96+
if err := osSbox.SetGatewayIPv6(joinInfo.gw6); err != nil {
97+
return fmt.Errorf("failed to set IPv6 gateway while updating gateway: %v", err)
98+
}
9599
}
96100

97101
return nil
@@ -279,7 +283,12 @@ func (sb *Sandbox) populateNetworkResources(ep *Endpoint) error {
279283

280284
ifaceOptions = append(ifaceOptions, osl.WithIPv4Address(i.addr), osl.WithRoutes(i.routes))
281285
if i.addrv6 != nil && i.addrv6.IP.To16() != nil {
282-
ifaceOptions = append(ifaceOptions, osl.WithIPv6Address(i.addrv6))
286+
// If IPv6 has been disabled in the Sandbox, an IPv6 address will still have
287+
// been allocated. Don't apply it, because doing so would enable IPv6 on the
288+
// interface.
289+
if ipv6, ok := sb.ipv6Enabled(); !ok || ipv6 {
290+
ifaceOptions = append(ifaceOptions, osl.WithIPv6Address(i.addrv6))
291+
}
283292
}
284293
if len(i.llAddrs) != 0 {
285294
ifaceOptions = append(ifaceOptions, osl.WithLinkLocalAddresses(i.llAddrs))

0 commit comments

Comments
 (0)