From e55f5694a7f28a142c32cb4262df018418fc5714 Mon Sep 17 00:00:00 2001 From: Scott Yeager Date: Fri, 20 Jun 2025 18:23:37 -0700 Subject: [PATCH] update route handling --- main.go | 57 ++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 34 insertions(+), 23 deletions(-) diff --git a/main.go b/main.go index 61fc0f2..4f8c005 100644 --- a/main.go +++ b/main.go @@ -62,7 +62,7 @@ func cmdAdd(args *skel.CmdArgs) error { // Configure container interface containerIP := generateContainerIP(myceliumIP, args.ContainerID) - if err := configureContainerInterface(containerNS, containerVethName, containerIP, hostVethName); err != nil { + if err := configureContainerInterface(containerNS, containerVethName, containerIP, hostVeth); err != nil { return fmt.Errorf("failed to configure container interface: %v", err) } @@ -182,7 +182,7 @@ func createVethPair(hostName, containerName string) (netlink.Link, netlink.Link, return hostVeth, containerVeth, nil } -func configureContainerInterface(containerNS netns.NsHandle, ifName string, containerIP net.IP, hostVethName string) error { +func configureContainerInterface(containerNS netns.NsHandle, ifName string, containerIP net.IP, hostVeth netlink.Link) error { // Switch to container namespace originalNS, err := netns.Get() if err != nil { @@ -216,27 +216,23 @@ func configureContainerInterface(containerNS netns.NsHandle, ifName string, cont return err } - // Get host veth link-local address for routing - hostVeth, err := netlink.LinkByName(hostVethName) - if err == nil { - hostAddrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6) - if err == nil { - for _, addr := range hostAddrs { - if addr.IP.IsLinkLocalUnicast() { - // Add route to Mycelium network via host veth - route := &netlink.Route{ - Dst: &net.IPNet{ - IP: net.ParseIP("400::"), - Mask: net.CIDRMask(7, 128), - }, - Gw: addr.IP, - LinkIndex: link.Attrs().Index, - } - netlink.RouteAdd(route) - break - } - } - } + // Get host veth link-local address (it should be available now) + hostLinkLocal, err := getHostVethLinkLocal(hostVeth) + if err != nil { + return fmt.Errorf("failed to get host veth link-local address: %v", err) + } + + // Add route to Mycelium network via host veth link-local address + route := &netlink.Route{ + Dst: &net.IPNet{ + IP: net.ParseIP("400::"), + Mask: net.CIDRMask(7, 128), + }, + Gw: hostLinkLocal, + LinkIndex: link.Attrs().Index, + } + if err := netlink.RouteAdd(route); err != nil { + return fmt.Errorf("failed to add route to 400::/7: %v", err) } return nil @@ -254,3 +250,18 @@ func configureHostInterface(hostVeth netlink.Link, containerIP net.IP) error { return netlink.RouteAdd(route) } + +func getHostVethLinkLocal(hostVeth netlink.Link) (net.IP, error) { + addrs, err := netlink.AddrList(hostVeth, netlink.FAMILY_V6) + if err != nil { + return nil, fmt.Errorf("failed to get addresses for host veth: %v", err) + } + + for _, addr := range addrs { + if addr.IP.IsLinkLocalUnicast() { + return addr.IP, nil + } + } + + return nil, fmt.Errorf("no link-local address found on host veth") +}