Skip to content

Commit dd8ff8a

Browse files
authored
Merge pull request #113 from nyren/master
Explicitly enable IPv6 sysctl
2 parents 136399f + 08ec299 commit dd8ff8a

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

pkg/ipam/ipam_linux.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ import (
2121

2222
"github.com/containernetworking/cni/pkg/types/current"
2323
"github.com/containernetworking/plugins/pkg/ip"
24+
"github.com/containernetworking/plugins/pkg/utils/sysctl"
2425

2526
"github.com/vishvananda/netlink"
2627
)
2728

29+
const (
30+
DisableIPv6SysctlTemplate = "net.ipv6.conf.%s.disable_ipv6"
31+
)
32+
2833
// ConfigureIface takes the result of IPAM plugin and
2934
// applies to the ifName interface
3035
func ConfigureIface(ifName string, res *current.Result) error {
@@ -42,6 +47,7 @@ func ConfigureIface(ifName string, res *current.Result) error {
4247
}
4348

4449
var v4gw, v6gw net.IP
50+
var has_enabled_ipv6 bool = false
4551
for _, ipc := range res.IPs {
4652
if ipc.Interface == nil {
4753
continue
@@ -52,6 +58,30 @@ func ConfigureIface(ifName string, res *current.Result) error {
5258
return fmt.Errorf("failed to add IP addr %v to %q: invalid interface index", ipc, ifName)
5359
}
5460

61+
// Make sure sysctl "disable_ipv6" is 0 if we are about to add
62+
// an IPv6 address to the interface
63+
if !has_enabled_ipv6 && ipc.Version == "6" {
64+
// Enabled IPv6 for loopback "lo" and the interface
65+
// being configured
66+
for _, iface := range [2]string{"lo", ifName} {
67+
ipv6SysctlValueName := fmt.Sprintf(DisableIPv6SysctlTemplate, iface)
68+
69+
// Read current sysctl value
70+
value, err := sysctl.Sysctl(ipv6SysctlValueName)
71+
if err != nil || value == "0" {
72+
// FIXME: log warning if unable to read sysctl value
73+
continue
74+
}
75+
76+
// Write sysctl to enable IPv6
77+
_, err = sysctl.Sysctl(ipv6SysctlValueName, "0")
78+
if err != nil {
79+
return fmt.Errorf("failed to enable IPv6 for interface %q (%s=%s): %v", iface, ipv6SysctlValueName, value, err)
80+
}
81+
}
82+
has_enabled_ipv6 = true
83+
}
84+
5585
addr := &netlink.Addr{IPNet: &ipc.Address, Label: ""}
5686
if err = netlink.AddrAdd(link, addr); err != nil {
5787
return fmt.Errorf("failed to add IP addr %v to %q: %v", ipc, ifName, err)

0 commit comments

Comments
 (0)