From 4012686925ccf54ce51b281be0e6c62b2c276806 Mon Sep 17 00:00:00 2001 From: Scott Yeager Date: Fri, 20 Jun 2025 20:10:00 -0700 Subject: [PATCH] Fix routing between containers on same host --- main.go | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index b33943b..422e1c9 100644 --- a/main.go +++ b/main.go @@ -150,13 +150,13 @@ func getMyceliumIP(interfaceName string) (net.IP, error) { func generateContainerIP(myceliumPrefix net.IP, containerID string) net.IP { // Generate a unique container IP within the /64 prefix using container ID hash hash := sha256.Sum256([]byte(containerID)) - + containerIP := make(net.IP, len(myceliumPrefix)) copy(containerIP, myceliumPrefix) - + // Use first 8 bytes of hash for the host part (last 64 bits) copy(containerIP[8:], hash[:8]) - + return containerIP } @@ -259,8 +259,28 @@ func configureContainerInterface(containerNS netns.NsHandle, ifName string, cont } if hostLLAddr != nil { - // Add route to Mycelium network via host veth + // First remove any existing route to our /64 + myceliumPrefix := &net.IPNet{ + IP: containerIP.Mask(net.CIDRMask(64, 128)), + Mask: net.CIDRMask(64, 128), + } + existingRoute := &netlink.Route{ + Dst: myceliumPrefix, + } + netlink.RouteDel(existingRoute) + + // Add route to our /64 via host veth route := &netlink.Route{ + Dst: myceliumPrefix, + Gw: hostLLAddr, + LinkIndex: link.Attrs().Index, + } + if err := netlink.RouteAdd(route); err != nil { + return err + } + + // Add route to Mycelium network via host veth + route = &netlink.Route{ Dst: &net.IPNet{ IP: net.ParseIP("400::"), Mask: net.CIDRMask(7, 128),