Skip to content

Commit 2733f7a

Browse files
authored
cni: disable ipv6 RA (#6045)
Signed-off-by: zhangzujian <zhangzujian.7@gmail.com>
1 parent 823edaf commit 2733f7a

File tree

1 file changed

+24
-14
lines changed

1 file changed

+24
-14
lines changed

cmd/cni/sysctl.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,28 +10,38 @@ import (
1010
"github.com/containernetworking/plugins/pkg/utils/sysctl"
1111
)
1212

13+
var ipv6SysctlSettings = []struct {
14+
key string
15+
value string
16+
}{
17+
{"disable_ipv6", "0"},
18+
{"accept_ra", "0"},
19+
}
20+
1321
// For docker version >=17.x the "none" network will disable ipv6 by default.
1422
// We have to enable ipv6 here to add v6 address and gateway.
1523
// See https://github.com/containernetworking/cni/issues/531
1624
func sysctlEnableIPv6(nsPath string) error {
1725
return ns.WithNetNSPath(nsPath, func(_ ns.NetNS) error {
1826
for _, conf := range [...]string{"all", "default"} {
19-
name := fmt.Sprintf("net.ipv6.conf.%s.disable_ipv6", conf)
20-
value, err := sysctl.Sysctl(name)
21-
if err != nil {
22-
if os.IsNotExist(err) {
23-
// The sysctl variable doesn't exist, so we can't set it
24-
continue
25-
}
26-
return fmt.Errorf("failed to get sysctl variable %s: %w", name, err)
27-
}
28-
if value != "0" {
29-
if _, err = sysctl.Sysctl(name, "0"); err != nil {
30-
if os.IsPermission(err) {
31-
// We don't have permission to set the sysctl variable, so we can't set it
27+
for _, settings := range ipv6SysctlSettings {
28+
name := fmt.Sprintf("net.ipv6.conf.%s.%s", conf, settings.key)
29+
value, err := sysctl.Sysctl(name)
30+
if err != nil {
31+
if os.IsNotExist(err) {
32+
// The sysctl variable doesn't exist, so we can't set it
3233
continue
3334
}
34-
return fmt.Errorf("failed to set sysctl variable %s to 0: %w", name, err)
35+
return fmt.Errorf("failed to get sysctl variable %s: %w", name, err)
36+
}
37+
if value != settings.value {
38+
if _, err = sysctl.Sysctl(name, settings.value); err != nil {
39+
if os.IsPermission(err) {
40+
// We don't have permission to set the sysctl variable, so we can't set it
41+
continue
42+
}
43+
return fmt.Errorf("failed to set sysctl variable %s to %s: %w", name, settings.value, err)
44+
}
3545
}
3646
}
3747
}

0 commit comments

Comments
 (0)