From 84b47e5d0b6f4bf0ed528c69465b4c786f0706d4 Mon Sep 17 00:00:00 2001
From: kaiyou <dev@kaiyou.fr>
Date: Mon, 23 Jan 2023 22:19:11 +0100
Subject: [PATCH] Improve router advertisement configuration

---
 pkg/selfcontain/net.go | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/pkg/selfcontain/net.go b/pkg/selfcontain/net.go
index 1f8ab18..0a6998b 100644
--- a/pkg/selfcontain/net.go
+++ b/pkg/selfcontain/net.go
@@ -14,6 +14,7 @@ import (
 	"github.com/vishvananda/netlink"
 )
 
+const ACCEPT_RA = "net.ipv6.conf.eth0.accept_ra_pinfo"
 const ACCEPT_PINFO = "net.ipv6.conf.eth0.accept_ra_pinfo"
 const ACCEPT_DFTRTR = "net.ipv6.conf.eth0.accept_ra_defrtr"
 
@@ -44,6 +45,10 @@ func (c *Container) SetupNetworking(etc string) error {
 			return fmt.Errorf("could not set the interface up: %w", err)
 		}
 		// Setup addresses and routes
+    err = setupRA(iface)
+    if err != nil {
+      return fmt.Errorf("could not enable RA: %w", err)
+    }
 		err = setupAddress(iface, c.config.IP)
 		if err != nil {
 			return fmt.Errorf("could not set the address: %w", err)
@@ -104,12 +109,20 @@ func (c *Container) setupIPVlan(master string, mtu int) (string, error) {
 	return tmpName, nil
 }
 
+func setupRA(iface netlink.Link) error {
+  // Accept router advertisement, even when forwarding is
+  // enabled, this is further specified by setupAddress
+  // and setupGw
+  _, err := sysctl.Sysctl(ACCEPT_RA, "2")
+  return err
+}
+
 func setupAddress(iface netlink.Link, ip net.IPNet) error {
 	// Accept router advertisement for addresses if required,
 	// otherwise use provided IP
-	accept_ra := "1"
+	accept_pinfo := "1"
 	if len(ip.IP) > 0 {
-		accept_ra = "0"
+		accept_pinfo = "0"
 		addr := &netlink.Addr{
 			IPNet: &ip,
 		}
@@ -118,14 +131,14 @@ func setupAddress(iface netlink.Link, ip net.IPNet) error {
 			return err
 		}
 	}
-	_, err := sysctl.Sysctl(ACCEPT_PINFO, accept_ra)
+	_, err := sysctl.Sysctl(ACCEPT_PINFO, accept_pinfo)
 	return err
 }
 
 func setupGw(iface netlink.Link, gw net.IP) error {
 	// Accept router advertisement for default routes if required,
 	// otherwise use provided gateway
-	accept_ra := "1"
+	accept_defrtr := "1"
 	if len(gw) > 0 {
 		// First add a link-local route to the gateway, so that
 		// out-of-lan default routes are handled properly
@@ -151,7 +164,7 @@ func setupGw(iface netlink.Link, gw net.IP) error {
 			return err
 		}
 	}
-	_, err := sysctl.Sysctl(ACCEPT_DFTRTR, accept_ra)
+	_, err := sysctl.Sysctl(ACCEPT_DFTRTR, accept_defrtr)
 	return err
 }
 
-- 
GitLab